'Auction Affiliate', 'plugin_version' => '2.2', 'shortcode' => 'auction-affiliate', 'plugin_prefix' => 'aa', 'param_lister_prefix' => 'aa', 'param_banner_prefix' => 'aa_b', 'param_store_empty_string' => array( 'aa_aColourP', 'aa_aColourS', 'aa_aColourB' ) ); //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; $bad = array( $plugin_settings['param_banner_prefix'] . $prefix_seperator, $plugin_settings['param_lister_prefix'] . $prefix_seperator ); $good = array('', ''); return str_replace($bad, $good, $name); } /** * Add parameter prefix */ function aa_prefix($name, $tool_key = 'lister', $prefix_seperator = '_') { global $plugin_settings; return $plugin_settings['param_' . $tool_key . '_prefix'] . $prefix_seperator . $name; } /** * Get names of inputs */ function aa_get_input_names() { 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; } /** * Wrapper for get_post_meta */ function aa_get_post_meta_single($post_id, $field_name) { $post_meta_array = get_post_meta($post_id, $field_name); //We have a value if(sizeof($post_meta_array) > 0) { //Can be anything - including an string $set_value = $post_meta_array[0]; //No post meta } else { $set_value = false; } return $set_value; } /** * 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['param_lister_prefix'] . '_' . $param_value; break; case 'id': $param_value = $plugin_settings['param_lister_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; //Build Stylesheet URL $stylesheet_url = $plugin_settings['stylesheet_url']; //Output stylesheet link echo '' . "\n"; } add_action('wp_head', 'aa_load_stylesheet'); /** * Output version # */ function aa_output_version() { global $plugin_settings; echo '' . "\n"; } add_action('wp_head','aa_output_version'); /** * Shortcode */ function aa_output_html($shortcode_params = false, $widget_params = false){ global $post, $plugin_settings, $AA; //Get tool key if($shortcode_params && array_key_exists('tool', $shortcode_params) && in_array($shortcode_params['tool'], array('lister', 'banner'))) { //Passes in shortcode $tool_key = $shortcode_params['tool']; } elseif($widget_params && array_key_exists('tool', $widget_params) && in_array($widget_params['tool'], array('lister', 'banner'))) { //Passes in shortcode $tool_key = $widget_params['tool']; } else { //Default $tool_key = 'lister'; } $AA->set_tool_key($tool_key); //If shortcode data if(is_array($shortcode_params)) { //Validate and re-camel case shortcode attributes $input_names = aa_get_input_names(); $allowable_shortcode_params = array(); foreach($shortcode_params as $attr_key => $attr_value) { //If allowable if(array_key_exists($attr_key, $input_names)) { $allowable_shortcode_params[$input_names[$attr_key]] = $attr_value; } } //If widget data } elseif(is_array($widget_params)) { $widget_params_unprefixed = array(); foreach($widget_params as $attr_key => $attr_value) { $attr_key = aa_unprefix($attr_key); $widget_params_unprefixed[$attr_key] = $attr_value; } } //Build request URL from user options foreach($plugin_settings['custom_field_inputs'] as $field) { $prefixed_field_name = aa_prefix($field['name'], $tool_key); //Don't add client options to URL, also check this param is valid for the tool if(($field['name'][0] == 'e' || $field['name'][0] == 'a') && $field[$tool_key . '_param'] == true) { //WIDGET //There are widget params and this one is a non-empty string value if(isset($widget_params_unprefixed) && array_key_exists($field['name'], $widget_params_unprefixed) && $widget_params_unprefixed[$field['name']] !== '') { $request_parameters[$field['name']] = $widget_params_unprefixed[$field['name']]; //SHORTCODE //There are shortcode params and this one is allowed } elseif(isset($allowable_shortcode_params) && array_key_exists($field['name'], $allowable_shortcode_params)) { //Add to URL $request_parameters[$field['name']] = $allowable_shortcode_params[$field['name']]; //CUSTOM FIELD //The value for this param is not an empty string and we aren't dealing with a wiget } elseif(($field_value = get_post_meta($post->ID, $prefixed_field_name, true)) !== '' && ! isset($widget_params_unprefixed)) { //Add to URL $request_parameters[$field['name']] = $field_value; } } } //Client type $request_parameters['aClientType'] = 'WP'; $AA->set_request_parameters($request_parameters, $tool_key); $AA->build_request(); $request = $AA->get_request(); $cache_id = $plugin_settings['plugin_prefix'] . '_' . md5($request); //Do we have a copy in the cache? //FWIW, incase you're curious, eliminating or lowering //the transient duration won't affect how often the feed //is updated. It will just cause the same data to be //re-downloaded more often. So now you know! 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_output_html'); /** * ======================================================== * ================== 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 $post, $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 will appear:
[' . $plugin_settings['shortcode'] . ' tool="lister"]
Multiple shortcodes can be added, see the plugin help section for more information:
Plugin cache cleared.
' . "\n"; echo 'This will delete the entire cache for the plugin!
'; echo 'Clear cache!'; */ 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"; } /** * Text to accompany affiliate defaults section */ function aa_defaults_affiliate_text() { echo '' . "\n"; } /** * Output eBay site setting */ function aa_default_eSite_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 custom ID setting */ function aa_default_eCustomID_setting() { global $plugin_settings; $options = get_option('aa_options'); echo '' . "\n"; echo '?' . "\n"; } /** * Text to accompany affiliate defaults section */ function aa_defaults_lister_text() { 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_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_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; } } /** * ======================================================== * ==================== WIDGETS =========================== * ======================================================== */ function aa_display_widget_title($instance) { if(array_key_exists('aa_widget_title', $instance) && $instance['aa_widget_title']) { return '