Created
November 21, 2014 16:13
-
-
Save joeydsmith/f431de5545bb8fc1759f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Luminate API for Convio by SPARK | |
Plugin URL: http://sparkexperience.com | |
Description: Luminate Extend API for Convio by SPARK | |
Version: 0.1 | |
Author: Spark Experience | |
Author URI: http://sparkexperience.com | |
*/ | |
if(!class_exists('sparkLuminate')){ | |
class sparkLuminate { | |
public function __construct(){ | |
// Activation & Deactivation | |
register_activation_hook( __FILE__, array(&$this, 'activate') ); | |
register_deactivation_hook( __FILE__, array(&$this, 'deactivate') ); | |
// Create Admin Menus | |
add_action( 'admin_menu', array(&$this, 'menus') ); | |
// Crude shortcode to display convio API response | |
add_shortcode( 'convio', array(&$this, 'convio_utility') ); | |
add_shortcode( 'form', array(&$this, 'convio_display') ); | |
// Register Post Types | |
add_action('init', array(&$this, 'register_donationForms')); | |
// Register Meta Boxes | |
add_action('add_meta_boxes', array(&$this, 'createMetaBoxes')); | |
// Admin CSS & JS | |
add_action('admin_enqueue_scripts', array(&$this, 'adminAssets')); | |
// Save CPT Data | |
add_action('save_post', array(&$this, 'save_cptData')); | |
} | |
public function activate(){ | |
if (!current_user_can('activate_plugins')) return; | |
// Register CPTs | |
$this->register_donationForms(); | |
// Flush the rewrite rules to add the specials permalinks | |
flush_rewrite_rules(); | |
} | |
public function deactivate() { | |
//get the global post types | |
global $wp_rewrite; | |
//check to see if the post type is removed so we can properly remove the slugs - thanks wordpress (again) | |
if(isset($wp_rewrite->extra_permastructs[self::POST_TYPE])) unset($wp_rewrite->extra_permastructs[self::POST_TYPE]); | |
//check to see if the taxonomy is removed so we can properly remove the slugs | |
if(isset($wp_rewrite->extra_permastructs[self::TAXONOMY])) unset($wp_rewrite->extra_permastructs[self::TAXONOMY]); | |
//flush the rewrite rules to remove the specials permalinks | |
$wp_rewrite->flush_rules(); | |
} | |
// Main Menu | |
public function menus() { | |
add_options_page( 'Settings', 'Convio / Luminate', 'manage_options', 'convio-general-settings', array(&$this, 'settings') ); | |
add_action( 'admin_init', array(&$this, 'register_settings')); | |
} | |
// Register the available settings | |
public function register_settings() { | |
register_setting( 'convio-general-settings', 'convio_api_url' ); | |
register_setting( 'convio-general-settings', 'convio_api_key' ); | |
register_setting( 'convio-general-settings', 'convio_api_ver' ); | |
register_setting( 'convio-general-settings', 'convio_api_auth_user' ); | |
register_setting( 'convio-general-settings', 'convio_api_auth_pw' ); | |
} | |
public function settings() { | |
if ( !current_user_can( 'manage_options' ) ) { | |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); | |
} | |
// Include the settings page | |
include ('inc/admin/settings.php'); | |
} | |
public function fetchUrl($url){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 20); | |
curl_setopt($ch, CURLOPT_VERBOSE, 0); | |
// You may need to add the line below | |
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); | |
$feedData = curl_exec($ch); | |
curl_close($ch); | |
return $feedData; | |
} | |
public function fetchUrl_POST($url, $fields){ | |
//open connection | |
$ch = curl_init(); | |
foreach($fields as $key=>$value) { | |
$fields_string .= $key.'='.$value.'&'; | |
} | |
rtrim($fields_string, '&'); | |
//set the url, number of POST vars, POST data | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_VERBOSE, 0); | |
curl_setopt($ch, CURLOPT_POST, count($fields)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); | |
//execute post | |
$feedData = curl_exec($ch); | |
curl_close($ch); | |
return $feedData; | |
} | |
public function getConvioAuth() { | |
$url = esc_attr( get_option('convio_api_url') ) . 'SRConsAPI'; | |
$atts = array( | |
'method' => 'getSingleSignOnToken', | |
'api_key' => esc_attr( get_option('convio_api_key') ), | |
'login_name' => esc_attr( get_option('convio_api_auth_user') ), | |
'login_password' => esc_attr( get_option('convio_api_auth_pw') ), | |
'v' => '1.0', | |
'response_format' => 'json' | |
); | |
$response = $this->fetchUrl_POST( $url, $atts ); | |
if ( $response != '' ) { | |
$response = json_decode($response); | |
return $response->getSingleSignOnTokenResponse->token; | |
} | |
} | |
public function convio_is_logged_in() { | |
$url = esc_attr( get_option('convio_api_url') ) . 'CRConsAPI?method=loginTest&api_key=' . esc_attr( get_option('convio_api_key') ) . '&v=1.0&response_format=json'; | |
$response = $this->fetchUrl_POST( $url ); | |
print_r($response); | |
if ( $response != '' ) { | |
$response = json_decode($response); | |
print_r($response); | |
} | |
} | |
public function convio_utility($atts) { | |
extract(shortcode_atts(array( | |
'api' => '', | |
'api_ver' => esc_attr( get_option('convio_api_ver') ), | |
'api_url' => esc_attr( get_option('convio_api_url') ), | |
'api_method' => '', | |
'api_key' => esc_attr( get_option('convio_api_key') ), | |
'api_format' => 'json', | |
'form_id' => '', | |
'debug' => 'false', | |
'spark_method' => '', | |
), $atts)); | |
ob_start(); | |
// Build File Path | |
$method_path = 'inc/api/' . $api_ver . '/' . $api . '/' . $spark_method . $api_method . '.php'; | |
// Check if API Method is available | |
if ( file_exists( plugin_dir_path( __FILE__ ) . $method_path ) ) { | |
// If available, incldue | |
include $method_path; | |
// Print Out for Testing | |
if ( $debug == 'true' && $response ) { | |
// Print $response | |
echo '<pre>'; | |
echo $url . '<hr/>'; | |
echo $method_path . '<hr/>'; | |
print_r( $response ); | |
echo '</pre>'; | |
} | |
} else { | |
// If not available, output error. | |
echo '[Error: API Method does not exist]'; | |
} | |
return ob_get_clean(); | |
} | |
public function convio_display( $atts ) { | |
extract(shortcode_atts(array( | |
'id' => '', | |
'type' => '', | |
'tpl' => 'inc/frontend/display.inc.php' | |
), $atts)); | |
ob_start(); | |
include $tpl; | |
return ob_get_clean(); | |
} | |
// Admin Assets | |
public function adminAssets(){ | |
if ( get_post_type() == 'convio_donation' ) { | |
wp_enqueue_script( 'spark_luminate-admin_js', plugin_dir_url(__FILE__).'inc/js/admin.js', array('jquery')); | |
wp_register_style( 'spark_luminate-admin_css', plugin_dir_url(__FILE__).'inc/css/admin.css', false, '1.0' ); | |
wp_enqueue_style( 'spark_luminate-admin_css' ); | |
} | |
} | |
// Meta Boxes | |
public function createMetaBoxes(){ | |
add_meta_box('donation_meta_b', 'Form ID', array(&$this,'metaBox_donate_b'), 'convio_donation', 'side'); | |
add_meta_box('donation_meta', 'Build Form', array(&$this,'metaBox_donate'), 'convio_donation', 'normal'); | |
} | |
// Register Donation Forms | |
public function register_donationForms(){ | |
$labels = array( | |
'name' => _x( 'Donation Forms', 'post type general name' ), | |
'singular_name' => _x( 'Donation Form', 'post type singular name' ), | |
'add_new' => _x( 'Add New Donation Form', 'book' ), | |
'add_new_item' => __( 'Add New Donation Form' ), | |
'edit_item' => __( 'Edit Donation Form' ), | |
'new_item' => __( 'New Donation Form' ), | |
'all_items' => __( 'All Donation Forms' ), | |
'view_item' => __( 'View Donation Form Page' ), | |
'search_items' => __( 'Search Donation Form' ), | |
'not_found' => __( 'No Donation Form found' ), | |
'not_found_in_trash' => __( 'No Donation Form found in the Trash' ), | |
'parent_item_colon' => '', | |
'menu_name' => 'Donation Forms' | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => 'Manage Convio Donation Forms', | |
'public' => true, | |
'menu_position' => 5, | |
'hierarchical' => true, | |
'supports' => array( 'title', 'thumbnail', 'excerpt', 'revisions' ), | |
'has_archive' => false, | |
'menu_icon' => 'dashicons-list-view', | |
'rewrite' => array( 'slug' => 'donation' ) | |
); | |
register_post_type( 'convio_donation', $args ); | |
} | |
// Donation Form Meta | |
public function metaBox_donate(){ | |
global $post; | |
if ( get_post_meta( $post->ID, 'donate_form_id', true ) != '' ) { | |
include('inc/admin/meta_box/convio_donate.php'); | |
} else { | |
echo '<p>Please enter a form ID and save.</p>'; | |
} | |
} | |
// Donation Form Meta | |
public function metaBox_donate_b(){ | |
include('inc/admin/meta_box/convio_donate_b.php'); | |
} | |
// Global Save Function for all CPTs | |
public function save_cptData(){ | |
global $post; | |
// Disable AutoSave | |
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post->ID; | |
// Save Donation Forms Data | |
if(get_post_type() == 'convio_donation') { | |
update_post_meta($post->ID, 'donate_form', $_POST['donate_form']); | |
update_post_meta($post->ID, 'donate_form_id', $_POST['donate_form_id']); | |
} | |
} | |
} | |
//initialize the plugin | |
$sparkLuminate = new sparkLuminate(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment