'Auction Affiliate', 'custom_field_prefix' => 'aa', 'shortcode' => 'auction-affiliate' ); //Load PHP client require_once 'auctionaffiliate.class.php'; $AA = new AuctionAffiliate; //Get settings from PHP Client aa_get_plugin_settings($AA); /** * ======================================================== * =============== UTILITY FUNCTIONS ===================== * ======================================================== */ /** * Remove parameter prefix */ function aa_unprefix($name, $prefix_seperator = '_') { global $plugin_settings; return str_replace($plugin_settings['custom_field_prefix'] . $prefix_seperator, '', $name); } /** * Add parameter prefix */ function aa_prefix($name, $prefix_seperator = '_') { global $plugin_settings; return $plugin_settings['custom_field_prefix'] . $prefix_seperator . $name; } /** * Get names of inputs */ function aa_get_input_names($strip_prefix = false) { global $plugin_settings; $input_names = array(); foreach($plugin_settings['custom_field_inputs'] as $input) { $input_name = aa_unprefix($input['name']); $input_names[strtolower($input_name)] = $input_name; } return $input_names; } /** * Get some settings from PHP client */ function aa_get_plugin_settings($AA) { global $plugin_settings; //Get settings $settings = $AA->get_settings(); //Set some plugin settings $plugin_settings['html_output_id_prefix'] = $settings['html_output_id_prefix']; $plugin_settings['html_output_prefix'] = $settings['html_output_prefix']; $plugin_settings['stylesheet_url'] = $settings['stylesheet_url']; $plugin_settings['request_parameter_groups'] = $settings['request_parameter_groups']; //Custom field definitions $definitions = $settings['request_parameter_definitions']; foreach($definitions as $def_key => &$def_value) { foreach($def_value as $param_key => &$param_value) { switch($param_key) { case 'name': $param_value = $plugin_settings['custom_field_prefix'] . '_' . $param_value; break; case 'id': $param_value = $plugin_settings['custom_field_prefix'] . '-' . $param_value; break; } //Do we have a default? if($default = aa_get_option('aa_default_' . $def_key)) { $def_value['default'] = $default; } } $plugin_settings['custom_field_inputs'][$def_key] = $def_value; } } /** * ======================================================== * =================== FRONT END ========================== * ======================================================== */ /** * Load the stylesheet in the head element */ function aa_load_stylesheet() { global $plugin_settings; //Get theme // $theme = get_post_meta($post->ID, 'aa_aTheme', true); //Build Stylesheet URL $stylesheet_url = $plugin_settings['stylesheet_url']; //Output stylesheet link echo '' . "\n"; } add_action('wp_head', 'aa_load_stylesheet'); /** * Shortcode */ function aa_shortcode($shortcode_attrs){ global $post, $plugin_settings, $AA; //If shortcode data if(is_array($shortcode_attrs)) { //Validate and re-camel case shortcode attributes $input_names = aa_get_input_names(); $allowable_shortcode_attrs = array(); foreach($shortcode_attrs as $attr_key => $attr_value) { //If allowable if(array_key_exists($attr_key, $input_names)) { $allowable_shortcode_attrs[$input_names[$attr_key]] = $attr_value; } } } //Build request URL from user options foreach($plugin_settings['custom_field_inputs'] as $field) { $unprefixed_field_name = aa_unprefix($field['name']); //Don't add client options to URL if($unprefixed_field_name[0] == 'e' || $unprefixed_field_name[0] == 'a') { //If it is in SHORTCODE attributes use that if(isset($allowable_shortcode_attrs) && array_key_exists($unprefixed_field_name, $allowable_shortcode_attrs)) { //Add to URL $request_parameters[$unprefixed_field_name] = $allowable_shortcode_attrs[$unprefixed_field_name]; //Get CUSTOM FIELD value } elseif($field_value = get_post_meta($post->ID, $field['name'], true)) { //Add to URL $request_parameters[$unprefixed_field_name] = $field_value; } } } //Client type $request_parameters['aClientType'] = 'WP'; //Add account key $options = get_option('aa_options', array()); if(array_key_exists('aa_aKey', $options)) { $request_parameters['aKey'] = $options['aa_aKey']; } $AA->set_request_parameters($request_parameters); $AA->build_request(); $request = $AA->get_request(); $cache_id = 'aa_' . md5($request); //Do we have a copy in the cache? if(false === ($output_html = get_transient($cache_id))) { //Run the request $AA->do_request(); //Build HTML $AA->build_html_output(); //Add HTML to cache $output_html = $AA->get_html(); set_transient($cache_id, $output_html, HOUR_IN_SECONDS); } return $output_html; } add_shortcode($plugin_settings['shortcode'], 'aa_shortcode'); /** * ======================================================== * ================== ADMIN ONLY ========================== * ======================================================== */ function aa_admin_init() { //Permissions if(current_user_can('manage_options')) { //Add custom fields add_action( 'admin_head-post.php', 'aa_create_custom_fields_box' ); add_action( 'admin_head-post-new.php', 'aa_create_custom_fields_box' ); //Save custom fields add_action('save_post', 'aa_save_custom_fields', 10, 2); //Add CSS wp_register_style('aa_css', plugins_url('auctionaffiliate.css', __FILE__)); wp_enqueue_style('aa_css'); //Add JS wp_register_script('aa_js', plugins_url('auctionaffiliate.js', __FILE__), array('jquery')); wp_enqueue_script('aa_js'); //Thickbox add_thickbox(); } } add_action('admin_init', 'aa_admin_init'); /** * ======================================================== * ================= CUSTOM FIELDS ======================== * ======================================================== */ /** * Create the custom fields box */ function aa_create_custom_fields_box() { global $plugin_settings; foreach(array('post', 'page') as $post_type) { add_meta_box('aa-custom-fields', $plugin_settings['plugin_name'], 'aa_create_custom_field_form', $post_type, 'normal', 'high'); } } /** * Create the custom field form */ function aa_create_custom_field_form() { global $plugin_settings; echo '
Use these options to specify which eBay items to display within your page/post. Add the following shortcode within your content editor to specify where the items wil appear:
[' . $plugin_settings['shortcode'] . ']
Some text about app settings...
' . "\n"; } /** * Output account key setting */ function aa_aKey_setting() { $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Text to accompany default settings section */ function aa_defaults_text() { echo 'The settings below enable you to specify default preferences, saving you from entering them each time when adding ' . $plugin_settings['plugin_name'] . ' to your pages or posts. Defaults can easily be overridden on a page-by-page basis.
' . "\n"; echo 'Hover over the question marks for an explanation of each setting, or click the Plugin Help link for further information.
' . "\n"; } /** * Output eBay site setting */ function aa_default_eSite_setting() { global $plugin_settings; $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Output default theme setting */ function aa_default_aTheme_setting() { global $plugin_settings; $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Output default campaign setting */ function aa_default_eCampID_setting() { global $plugin_settings; $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Output default campaign setting */ function aa_default_eCustomID_setting() { global $plugin_settings; $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Output default campaign setting */ function aa_default_aColourP_setting() { global $plugin_settings; $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Output default campaign setting */ function aa_default_aColourS_setting() { global $plugin_settings; $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Output default campaign setting */ function aa_default_aColourB_setting() { global $plugin_settings; $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Validate our options */ function aa_options_validate($input) { $output['aa_aKey'] = trim($input['aa_aKey']); $output['aa_default_eSite'] = trim($input['aa_default_eSite']); $output['aa_default_aTheme'] = trim($input['aa_default_aTheme']); $output['aa_default_eCampID'] = trim($input['aa_default_eCampID']); $output['aa_default_eCustomID'] = trim($input['aa_default_eCustomID']); $output['aa_default_aColourP'] = trim($input['aa_default_aColourP']); $output['aa_default_aColourS'] = trim($input['aa_default_aColourS']); $output['aa_default_aColourB'] = trim($input['aa_default_aColourB']); return $output; } /** * Get plugin options */ function aa_get_option($option_key) { $options = get_option('aa_options'); if(is_array($options) && array_key_exists($option_key, $options)) { return $options[$option_key]; } else { return false; } }