&text=] --> inserts link to amazon item [amazon cat=&last=] --> inserts table of random items Layout: amazon_container - Encloses whole wishlist. amazon_prod - Encloses each list item. amazon_img_container - Encloses the item thumbnail (link+img) amazon_pic - Class of the item thumbnail IMG element amazon_text_container - Encloses the item description (Title paragraphs+link + Details paragraphs) amazon_details - Encloses the item details part of the description amazon_price - Spans the item's formatted price. */ require_once('aws_signed_request.php'); require_once('include/displayForm.php'); require_once('include/translate.php'); if (!class_exists('AmazonWishlist_ip2nation')) include_once ( 'include/ip2nation.php'); if (!class_exists('AmazonLinkSearch')) include_once ( 'include/amazonSearch.php'); if (!class_exists('AmazonWishlist_For_WordPress')) { class AmazonWishlist_For_WordPress { /*****************************************************************************************/ /// Settings: /*****************************************************************************************/ var $tags = array(); // String to insert into Posts to indicate where to insert the amazon items var $TagHead = '[amazon'; var $TagTail = ']'; var $option_version= 3; var $plugin_version= '2.0.4'; var $optionName = 'AmazonLinkOptions'; var $user_options = 'amazonlinkoptions'; var $templatesName = 'AmazonLinkTemplates'; var $channels_name = 'AmazonLinkChannels'; var $settings_slug = 'amazon-link-options'; var $multi_id = 0; /*****************************************************************************************/ // Constructor for the Plugin function AmazonWishlist_For_WordPress() { $this->__construct(); } function __construct() { $this->URLRoot = plugins_url("", __FILE__); $this->base_name = plugin_basename( __FILE__ ); $this->plugin_dir = dirname( $this->base_name ); $this->form = new AmazonWishlist_Options; $this->ip2n = new AmazonWishlist_ip2nation; $this->search = new AmazonLinkSearch; register_activation_hook(__FILE__, array($this, 'activate')); // To perform options upgrade add_action('init', array($this, 'init')); // Load i18n and initialise translatable vars add_filter('plugin_row_meta', array($this, 'registerPluginLinks'),10,2); // Add extra links to plugins page add_filter('the_posts', array($this, 'stylesNeeded')); // Run once to determine if styles needed add_filter('the_content', array($this, 'contentFilter'),15); // Process the content add_action('admin_menu', array($this, 'optionsMenu')); // Add options page hooks add_filter('widget_text', array($this, 'widget_filter'), 16 ); // Filter widget text (after the content?) add_action('show_user_profile', array($this, 'show_user_options') ); // Display User Options add_action('edit_user_profile', array($this, 'show_user_options') ); // Display User Options add_action('personal_options_update', array($this, 'update_user_options')); // Update User Options add_action('edit_user_profile_update', array($this, 'update_user_options'));// Update User Options } /*****************************************************************************************/ // Functions for the above hooks // On activation of plugin - used to create default settings function activate() { $Opts = $this->getOptions(); $this->saveOptions($Opts); } function upgrade_settings($Opts) { // Options structure changed so need to update the 'version' option and upgrade as appropriate... if (!isset($Opts['version'])) { $cc_map = array('co.uk' => 'uk', 'com' => 'us', 'fr' => fr, 'de' => 'de', 'ca' => 'ca', 'jp' => 'jp'); // Move from version 1.2 to 1.3 of the plugin (Option Version Null => 1) if (isset($Opts['tld'])) { $cc = isset($cc_map[$Opts['tld']]) ? $cc_map[$Opts['tld']] : 'uk'; $Opts['default_cc'] = $cc; if (isset($Opts['tag'])) $Opts['tag_' . $cc] = $Opts['tag']; } unset($Opts['tld']); unset($Opts['tag']); $Opts['version'] = 1; $this->saveOptions($Opts); } if ($Opts['version'] == 1) { /* Upgrade from 1 to 2: * force Template ids to lower case & update 'wishlist_template'. */ $Templates = $this->getTemplates(); foreach ($Templates as $Name => $value) { $renamed_templates[strtolower($Name)] = $value; } $this->saveTemplates($renamed_templates); $Templates = $renamed_templates; if (isset($Opts['wishlist_template'])) $Opts['wishlist_template'] = strtolower($Opts['wishlist_template']); $Opts['version'] = 2; $this->saveOptions($Opts); } if ($Opts['version'] == 2) { /* Upgrade from 2 to 3: * copy affiliate Ids to new channels section. */ $country_data = $this->get_country_data(); foreach ($country_data as $cc => $data) { $channels['default']['tag_'.$cc] = isset($Opts['tag_'.$cc]) ? $Opts['tag_'.$cc] : ''; } $channels['default']['Name'] = 'Default'; $channels['default']['Description'] = 'Default Affiliate Tags'; $channels['default']['Filter'] = ''; $Opts['version'] = 3; $this->save_channels($channels); $this->saveOptions($Opts); } /* * If first run need to create a default templates */ if(!isset($Templates['wishlist'])) { $default_templates = $this->get_default_templates(); foreach ($default_templates as $templateName => $templateDetails) { if(!isset($Templates[$templateName])) { $Templates[$templateName] = $templateDetails; } } $this->saveTemplates($Templates); } } // On wordpress initialisation - load text domain and register styles & scripts function init() { /* load localisation */ load_plugin_textdomain('amazon-link', $this->plugin_dir . '/i18n', $this->plugin_dir . '/i18n'); // Initialise dependent classes $this->search->init($this); $this->form->init($this); $this->ip2n->init($this); // Register our styles and scripts $script = plugins_url("amazon.js", __FILE__); $admin_script = plugins_url("include/amazon-admin.js", __FILE__); // Allow the user to override our default styles. if (file_exists(dirname (__FILE__).'/user_styles.css')) { $stylesheet = plugins_url("user_styles.css", __FILE__); } else { $stylesheet = plugins_url("Amazon.css", __FILE__); } wp_register_style('amazon-link-style', $stylesheet); wp_register_script('amazon-link-script', $script); wp_register_script('amazon-admin-script', $admin_script); } // If in admin section then register options page and required styles & metaboxes function optionsMenu() { // Add plugin options page, with load hook to bring in meta boxes and scripts and styles $this->opts_page = add_options_page( __('Manage Amazon Link Options', 'amazon-link'), __('Amazon Link', 'amazon-link'), 'manage_options', $this->settings_slug, array($this, 'showOptionsPage')); add_action('load-'.$this->opts_page, array(&$this, 'optionsLoad')); add_action( "admin_print_styles-" . $this->opts_page, array($this,'amazon_admin_styles') ); add_action( "admin_print_scripts-" . $this->opts_page, array($this,'amazon_admin_scripts') ); // Add support for Post/Page edit metabox, this requires our styles and post edit AJAX scripts. add_meta_box('amazonLinkID', 'Add Amazon Link', array($this,'insertForm'), 'post', 'normal'); add_meta_box('amazonLinkID', 'Add Amazon Link', array($this,'insertForm'), 'page', 'normal'); add_action( "admin_print_scripts-post.php", array($this,'edit_scripts') ); add_action( "admin_print_scripts-post-new.php", array($this,'edit_scripts') ); add_action( "admin_print_styles-post-new.php", array($this,'amazon_admin_styles') ); add_action( "admin_print_styles-post.php", array($this,'amazon_admin_styles') ); } // Hooks required to bring up options page with meta boxes: function optionsLoad () { add_filter('screen_layout_columns', array(&$this, 'adminColumns'), 10, 2); wp_enqueue_script('common'); wp_enqueue_script('wp-lists'); wp_enqueue_script('postbox'); add_meta_box( 'alOptions', __( 'Options', 'amazon-link' ), array (&$this, 'showOptions' ), $this->opts_page, 'normal', 'core' ); add_meta_box( 'alChannels', __( 'Amazon Tracking ID Channels', 'amazon-link' ), array (&$this, 'show_channels' ), $this->opts_page, 'normal', 'core' ); add_meta_box( 'alInfo', __( 'About', 'amazon-link' ), array (&$this, 'show_info' ), $this->opts_page, 'side', 'core' ); add_meta_box( 'alTemplateHelp', __( 'Template Help', 'amazon-link' ), array (&$this, 'displayTemplateHelp' ), $this->opts_page, 'side', 'low' ); add_meta_box( 'alTemplates', __( 'Templates', 'amazon-link' ), array (&$this, 'show_templates' ), $this->opts_page, 'advanced', 'core' ); } function adminColumns($columns, $screen) { if ($screen == $this->opts_page) { $columns[$this->opts_page] = 2; } return $columns; } /// We only need the styles and scripts when a Wishlist or the Multinational popup is displayed... function amazon_admin_styles() { wp_enqueue_style('amazon-link-style'); $this->form->enqueue_styles(); } function amazon_admin_scripts() { wp_enqueue_script('amazon-admin-script'); } function amazon_scripts() { wp_enqueue_script('amazon-link-script'); } function edit_scripts() { $script = plugins_url("postedit.js", __FILE__); wp_enqueue_script('wpAmazonLinkAdmin', $script, array('jquery', 'amazon-link-search'), '1.0.0'); } function generate_multi_script() { $Settings = $this->getSettings(); $country_data = $this->get_country_data(); $channels = $this->get_channels(True, True); $TARGET = $Settings['new_window'] ? 'target="_blank"' : ''; ?> base_name) { $links[] = '' . __('Settings','amazon-link') . ''; } return $links; } function stylesNeeded($posts){ // To determine if styles are required is too long winded, always bring them in. wp_enqueue_style('amazon-link-style'); if (empty($posts)) return $posts; $this->stylesNeeded = False; foreach ($posts as $post) { $post->post_content = $this->contentFilter($post->post_content, False, False); if ($this->stylesNeeded) { $this->amazon_scripts(); add_action('wp_print_scripts', array($this, 'generate_multi_script')); break; } } remove_filter('the_posts', array($this, 'stylesNeeded')); return $posts; } /*****************************************************************************************/ /// Options & Templates Handling /*****************************************************************************************/ function get_country_data() { if (!isset($this->country_data)) { /* Move Country Data construction here so we can localise the strings */ // Country specific aspects: // full name of country, // country flag image // market place of amazon site // tld of main amazon site // link to affiliate program site // Default tag if none set up $this->country_data = array('uk' => array('name' => __('United Kingdom', 'amazon-link'), 'lang' => 'en', 'flag' => 'images/flag_uk.gif', 'market' => 'GB', 'm_id' => 2, 'tld' => 'co.uk', 'rcm' => 'rcm-uk.amazon.co.uk', 'site' => 'https://affiliate-program.amazon.co.uk', 'default_tag' => 'livpauls-21'), 'us' => array('name' => __('United States', 'amazon-link'), 'lang' => 'en', 'flag' => 'images/flag_us.gif', 'market' => 'US', 'm_id' => 1, 'tld' => 'com', 'rcm' => 'rcm.amazon.com', 'site' => 'https://affiliate-program.amazon.com', 'default_tag' => 'lipawe-20'), 'de' => array('name' => __('Germany', 'amazon-link'), 'lang' => 'de', 'flag' => 'images/flag_de.gif', 'market' => 'DE', 'm_id' => 3, 'tld' => 'de', 'rcm' => 'rcm-de.amazon.de', 'site' => 'https://partnernet.amazon.de', 'default_tag' => 'lipas03-21'), 'es' => array('name' => __('Spain', 'amazon-link'), 'lang' => 'es', 'flag' => 'images/flag_es.gif', 'market' => 'ES', 'm_id' => 30, 'tld' => 'es', 'rcm' => 'rcm-es.amazon.es', 'site' => 'https://afiliados.amazon.es', 'default_tag' => 'livpauls0b-21'), 'fr' => array('name' => __('France', 'amazon-link'), 'lang' => 'fr', 'flag' => 'images/flag_fr.gif', 'market' => 'FR', 'm_id' => 8, 'tld' => 'fr', 'rcm' => 'rcm-fr.amazon.fr', 'site' => 'https://partenaires.amazon.fr', 'default_tag' => 'lipas-21'), 'jp' => array('name' => __('Japan', 'amazon-link'), 'lang' => 'ja', 'flag' => 'images/flag_jp.gif', 'market' => 'JP', 'm_id' => 9, 'tld' => 'jp', 'rcm' => 'rcm-jp.amazon.co.jp', 'site' => 'https://affiliate.amazon.co.jp', 'default_tag' => 'livpaul21-22'), 'it' => array('name' => __('Italy', 'amazon-link'), 'lang' => 'it', 'flag' => 'images/flag_it.gif', 'market' => 'IT', 'm_id' => 29, 'tld' => 'it', 'rcm' => 'rcm-it.amazon.it', 'site' => 'https://programma-affiliazione.amazon.it', 'default_tag' => 'livpaul-21'), 'cn' => array('name' => __('China', 'amazon-link'), 'lang' => 'zh-CHS', 'flag' => 'images/flag_cn.gif', 'market' => 'CN', 'm_id' => 28, 'rcm' => 'rcm-cn.amazon.cn', 'tld' => 'cn', 'site' => 'https://associates.amazon.cn', 'default_tag' => 'livpaul-23'), 'ca' => array('name' => __('Canada', 'amazon-link'), 'lang' => 'en', 'flag' => 'images/flag_ca.gif', 'market' => 'CA', 'm_id' => 15, 'rcm' => 'rcm-ca.amazon.ca', 'tld' => 'ca', 'site' => 'https://associates.amazon.ca', 'default_tag' => 'lipas-20')); } return $this->country_data; } function get_default_templates() { if (!isset($this->default_templates)) { // Default templates include('include/defaultTemplates.php'); } return $this->default_templates; } function get_option_list() { if (!isset($this->optionList)) { $this->optionList = array( /* Hidden Options - not saved in Settings */ 'nonce' => array ( 'Type' => 'nonce', 'Name' => 'update-AmazonLink-options' ), 'cat' => array ( 'Type' => 'hidden' ), 'last' => array ( 'Type' => 'hidden' ), 'template' => array( 'Type' => 'hidden' ), 'chan' => array( 'Type' => 'hidden' ), /* Options that change how the items are displayed */ 'hd1s' => array ( 'Type' => 'section', 'Value' => __('Display Options', 'amazon-link'), 'Section_Class' => 'al_subhead1'), 'text' => array( 'Name' => __('Link Text', 'amazon-link'), 'Description' => __('Default text to display if none specified', 'amazon-link'), 'Default' => 'www.amazon.co.uk', 'Type' => 'text', 'Size' => '40', 'Class' => 'al_border' ), 'image_class' => array ( 'Name' => __('Image Class', 'amazon-link'), 'Description' => __('Style Sheet Class of image thumbnails', 'amazon-link'), 'Default' => 'wishlist_image', 'Type' => 'text', 'Size' => '40', 'Class' => 'alternate al_border' ), 'wishlist_template' => array ( 'Default' => 'Wishlist', 'Name' => __('Wishlist Template', 'amazon-link') , 'Description' => __('Default template to use for the wishlist * AWS *', 'amazon-link'), 'Type' => 'selection', 'Class' => 'al_border' ), 'wishlist_items' => array ( 'Name' => __('Wishlist Length', 'amazon-link'), 'Description' => __('Maximum number of items to display in a wishlist (Amazon only returns a maximum of 5, for the \'Similar\' type of list) * AWS *', 'amazon-link'), 'Default' => 5, 'Type' => 'text', 'Class' => 'alternate al_border' ), 'wishlist_type' => array ( 'Name' => __('Wishlist Type', 'amazon-link'), 'Description' => __('Default type of wishlist to display, \'Similar\' shows items similar to the ones found, \'Random\' shows a random selection of the ones found * AWS *', 'amazon-link'), 'Default' => 'Similar', 'Options' => array('Similar', 'Random', 'Multi'), 'Type' => 'selection', 'Class' => 'al_border' ), /* Options that change the behaviour of the links */ 'multi_cc' => array('Name' => __('Multinational Link', 'amazon-link'), 'Description' => __('Insert links to all other Amazon sites after primary link.', 'amazon-link'), 'Default' => '1', 'Type' => 'checkbox', 'Class' => 'alternate al_border'), 'localise' => array('Name' => __('Localise Amazon Link', 'amazon-link'), 'Description' => __('Make the link point to the users local Amazon website, (you must have ip2nation installed for this to work).', 'amazon-link'), 'Default' => '1', 'Type' => 'checkbox', 'Class' => 'al_border' ), 'live' => array ( 'Name' => __('Live Data', 'amazon-link'), 'Description' => __('When creating Amazon links, use live data from the Amazon site, otherwise populate the shortcode with static information. * AWS *', 'amazon-link'), 'Default' => '1', 'Type' => 'checkbox', 'Class' => 'al_border' ), 'new_window' => array('Name' => __('New Window Link', 'amazon-link'), 'Description' => __('When link is clicked on open it in a new browser window', 'amazon-link'), 'Default' => '0', 'Type' => 'checkbox', 'Class' => 'alternate' ), 'hd1e' => array ( 'Type' => 'end'), /* Options related to the Amazon backend */ 'hd2s' => array ( 'Type' => 'section', 'Value' => __('Amazon Associate Information','amazon-link'), 'Section_Class' => 'al_subhead1'), 'default_cc' => array( 'Name' => __('Default Country', 'amazon-link'), 'Hint' => __('The Amazon Affiliate Tags should be entered in the \'Channels\' section below', 'amazon-link'),'Description' => __('Which country\'s Amazon site to use by default', 'amazon-link'), 'Default' => 'uk', 'Type' => 'selection', 'Class' => 'al_border' ), 'aws_help' => array( 'Name' => __('AWS Note', 'amazon-link'), 'Value' => __('AWS Access Keys', 'amazon-link'), 'Description' => __('The AWS Keys are required for some of the features of the plugin to work (The ones marked with AWS above), visit Amazon Web Services to sign up to get your own keys.', 'amazon-link'), 'Title_Class' => 'al_subheading', 'Id' => 'aws_notes', 'Default' => '', 'Type' => 'title', 'Class' => 'alternate al_border' ), 'pub_key' => array( 'Name' => __('AWS Public Key', 'amazon-link'), 'Description' => __('Access Key ID provided by your AWS Account, found under Security Credentials/Access Keys of your AWS account', 'amazon-link'), 'Default' => '', 'Type' => 'text', 'Size' => '40', 'Class' => 'alternate al_border' ), 'priv_key' => array( 'Name' => __('AWS Private key', 'amazon-link'), 'Description' => __('Secret Access Key ID provided by your AWS Account.', 'amazon-link'), 'Default' => '', 'Type' => 'text', 'Size' => '40', 'Class' => 'al_border' ), 'debug' => array( 'Name' => __('Debug Output', 'amazon-link'), 'Description' => __('Adds hidden debug output to the page source to aid debugging. Do not enable on live sites.', 'amazon-link'), 'Default' => '0', 'Type' => 'checkbox', 'Size' => '40', 'Class' => 'al_border' ), 'hd2e' => array ( 'Type' => 'end'), 'button' => array( 'Type' => 'buttons', 'Buttons' => array( __('Update Options', 'amazon-link' ) => array( 'Class' => 'button-primary', 'Action' => 'AmazonLinkAction')))); $country_data = $this->get_country_data(); // Populate Country related options foreach ($country_data as $cc => $data) { $this->optionList['default_cc']['Options'][$cc]['Name'] = $data['name']; } if (isset($this->search->keywords)) { // Populate the hidden Template Keywords foreach ($this->search->keywords as $keyword => $details) { if (!isset($this->optionList[$keyword])) $this->optionList[$keyword] = array( 'Type' => 'hidden' ); } } } return $this->optionList; } function get_user_option_list() { $option_list = array( 'title' => array ( 'Type' => 'subhead', 'Value' => __('Amazon Link Affiliate IDs', 'amazon-link'), 'Description' => __('Valid affiliate IDs from all Amazon locales can be obtained from the relevant Amazon sites: ', 'amazon-link'), 'Class' => 'al_pad al_border'), ); $country_data = $this->get_country_data(); // Populate Country related options foreach ($country_data as $cc => $data) { $option_list ['tag_' . $cc] = array('Type' => 'text', 'Default' => '', 'Name' => ' ' . $data['name'], 'Hint' => sprintf(__('Enter your affiliate tag for %1$s.', 'amazon-link'), $data['name'] )); $option_list ['title']['Description'] .= ''. $data['name']. ', '; } return $option_list; } function getOptions() { if (!isset($this->Opts)) { $this->Opts = get_option($this->optionName, array()); if (!isset($this->Opts['version']) || ($this->Opts['version'] < $this->option_version)) { $this->upgrade_settings($this->Opts); $this->Opts = get_option($this->optionName, array()); } } return $this->Opts; } function saveOptions($Opts) { $optionList = $this->get_option_list(); if (!is_array($Opts)) { return; } // Ensure hidden items are not stored in the database foreach ( $optionList as $optName => $optDetails ) { if ($optDetails['Type'] == 'hidden') unset($Opts[$optName]); } update_option($this->optionName, $Opts); $this->Opts = $Opts; } function deleteOptions() { delete_option($this->optionName); } function getTemplates() { if (!isset($this->Templates)) { $this->Templates = get_option($this->templatesName, array()); ksort($this->Templates); } return $this->Templates; } function saveTemplates($Templates) { if (!is_array($Templates)) { return; } update_option($this->templatesName, $Templates); $this->Templates = $Templates; } function get_channels($override = False, $user_channels = False) { if (!$override || !isset($this->channels)) { $channels = get_option($this->channels_name, array()); if ($user_channels) { $users = get_users(); foreach ($users as $user => $data) { $user_options = $this->get_user_options($data->ID); if (is_array($user_options)) { $channels['al_user_' . $data->ID] = $user_options; $channels['al_user_' . $data->ID]['user_channel'] = 1; } } } if (!$override) return $channels; $country_data = $this->get_country_data(); $this->channels = array(); foreach ( $channels as $channel_id => $channel_data) { $this->channels[$channel_id] = $channel_data; $this->channels[$channel_id]['ID'] = $channel_id; foreach ( $country_data as $cc => $data) { if ($channel_data['tag_'. $cc] == '') { $this->channels[$channel_id]['tag_'. $cc] = ($channels['default']['tag_' .$cc] != '') ? $channels['default']['tag_' .$cc] : $data['default_tag']; } } } } return $this->channels; } function save_channels($channels) { if (!is_array($channels)) { return; } update_option($this->channels_name, $channels); $this->channels = $channels; } /* * Parse the arguments passed in. */ function parseArgs($arguments) { $optionList = $this->get_option_list(); $args = array(); parse_str(html_entity_decode($arguments), $args); $Opts = $this->getOptions(); unset($this->Settings); /* * Check for each setting, local overides saved option, otherwise fallback to default. */ foreach ($optionList as $key => $details) { if (isset($args[$key])) { $this->Settings[$key] = trim(stripslashes($args[$key]),"\x22\x27"); // Local setting } else if (isset($Opts[$key])) { $this->Settings[$key] = $Opts[$key]; // Global setting if (isset($details['OverrideBlank']) && ($Opts[$key] == '')) { $this->Settings[$key] = $details['OverrideBlank']; // Use default if Global Setting is blank } } else if (isset ($details['Default'])) { $this->Settings[$key] = $details['Default']; // Use default } else if (isset ($details['OverrideBlank'])) { $this->Settings[$key] = $details['OverrideBlank']; // Use default } } $this->Settings['asin'] = isset($this->Settings['asin']) ? explode (',', $this->Settings['asin']) : array(); } /* * Normally Settings are populated from parsing user arguments, however some * external calls do not cause argument parsing (e.g. amazon_query). So this * ensures we have the defaults. */ function getSettings() { if (!isset($this->Settings)) { $this->Settings = $this->getOptions(); } $optionList = $this->get_option_list(); foreach ($optionList as $key => $details) { if ((!isset($this->Settings[$key]) || ($this->Settings[$key] == '')) && isset($details['OverrideBlank'])) { $this->Settings[$key] = $details['OverrideBlank']; // Use default } } return $this->Settings; } function get_user_options($ID) { $options = get_the_author_meta( $this->user_options, $ID ); return $options; } function save_user_options($ID, $options ) { update_usermeta( $ID, $this->user_options, $options ); } function valid_keys() { $Settings = $this->getSettings(); if ( (strlen($Settings['pub_key']) > 10) && (strlen($Settings['priv_key']) > 10)) return True; return False; } /*****************************************************************************************/ /// Affiliate Tracking ID Channels /*****************************************************************************************/ /* * Check the channels in order until we get a match * * Filter rules: * cat = [category slug|category id] * parent_cat = [category slug| category id] * author = [author name|author id] * tag = [tag name|tag id] * type = [page|post|other = widget|template, etc] * parent = [page/post id] * random = 1-99 * empty filter = always use. */ function get_channel() { // Need $GLOBALS & Channels $settings = $this->getSettings(); $channels = $this->get_channels(True, True); if (isset($settings['in_post']) && $settings['in_post']) { $post = $GLOBALS['post']; } else { $post = ''; } // If manually set always respect. if (isset($settings['chan']) && isset($channels[strtolower($settings['chan'])])) { return $channels[strtolower($settings['chan'])]; } // If post or page, check $this->channel_ids[ID] to see if already processed // For each channel (excluding default) // For each filter check for match // Switch on rule type [cat|parent_cat|author|tag|type] // cat // if !isset($cats) grab array(cat_id => cat_slug) // check cat in array or array_keys // parent_cat = recursive list of all category parents // if !isset($cats) grab array(cat_id => cat_slug) // if !isset($parent_cats) grab array(cat_id => cat_slug) // check cat in either cats/parent_cats array or array_keys // author // if !isset($author) grab array(author_id => author_name) // check author in array or array_keys // tag // if !isset($tags) grab array(tag_id => tag_name) // check tag in array or array_keys // type // if !isset($type) post_type / other // check type == type // If any rule fails drop to next channel // If all rules pass use this channel, save in $this->channel_ids[ID] // If no specific channel detected then check for author specific IDs via get_the_author_meta if (isset($post->post_author) && isset($channels['al_user_'.$post->post_author])) { return $channels['al_user_'.$post->post_author]; } // No match found return default channel. return $channels['default']; } /*****************************************************************************************/ /// Localise Link Facility /*****************************************************************************************/ function get_country() { // Pretty arbitrary mapping of domains to Amazon sites, default to 'com' - the 'international' site. $country_map = array('uk' => array('uk', 'ie', 'gi', 'gl', 'nl', 'vg', 'cy', 'gb', 'dk'), 'fr' => array('fr', 'be', 'bj', 'bf', 'bi', 'cm', 'cf', 'td', 'km', 'cg', 'dj', 'ga', 'gp', 'gf', 'gr', 'pf', 'tf', 'ht', 'ci', 'lu', 'mg', 'ml', 'mq', 'yt', 'mc', 'nc', 'ne', 're', 'sn', 'sc', 'tg', 'vu', 'wf'), 'de' => array('de', 'at', 'ch', 'no', 'dn', 'li', 'sk'), 'es' => array('es'), 'it' => array('it'), 'cn' => array('cn'), 'ca' => array('ca', 'pm'), 'jp' => array('jp') ); $country = False; if ($this->Settings['localise']) { if (!isset($this->local_country)) { $cc = $this->ip2n->get_cc(); $country = 'us'; foreach ($country_map as $key => $countries) { if (in_array($cc, $countries)) { $country = $key; continue; } } $this->local_country = $country; } return $this->local_country; } return $this->Settings['default_cc']; } function widget_filter($content) { return $this->contentFilter($content, TRUE, TRUE, FALSE); } /*****************************************************************************************/ /// Searches through the_content for our 'Tag' and replaces it with the lists or links /* * Performs 3 functions: * 1. Searches through the posts, and if multinational link required or a wishlist then bring in the styles. * 2. Process the content and replace the shortcode with amazon links and wishlists * 3. Search through the content and record any Amazon ASIN numbers ready to generate a wishlist. */ /*****************************************************************************************/ function contentFilter($content, $doRecs=TRUE, $doLinks=TRUE, $in_post=TRUE) { $newContent=''; $index=0; $found = 0; while ($found !== FALSE) { $found = strpos($content, $this->TagHead, $index); if ($found === FALSE) { // Add the remaining content to the output $newContent = $newContent . substr($content, $index); break; } else { $output = ''; // Need to parse any arguments $tagEnd = strpos($content, $this->TagTail, $found); $arguments = substr($content, $found + strlen($this->TagHead), ($tagEnd-$found-strlen($this->TagHead))); $this->parseArgs($arguments); if (isset($this->Settings['cat'])) { if ($doRecs) { $this->Settings['in_post'] = $in_post; if ($this->Settings['debug']) { $output .= ''; } $output .= $this->showRecommendations($this->Settings['cat'], isset($this->Settings['last']) ? $this->Settings['last'] : 30); } elseif (!$this->stylesNeeded) { $output .= '' . substr($content, $found, ($tagEnd - $found) + strlen($this->TagTail)); $this->stylesNeeded = True; } else { $output .= substr($content, $found, ($tagEnd - $found) + strlen($this->TagTail)); } } else if ($doLinks) { // Generate Amazon Link $this->tags = array_merge($this->Settings['asin'], $this->tags); $this->Settings['in_post'] = $in_post; if ($this->Settings['debug']) { $output .= ''; } $output .= $this->make_links($this->Settings['asin'], $this->Settings['text']); } else { $this->tags = array_merge($this->Settings['asin'], $this->tags); if ($this->Settings['multi_cc'] && !$this->stylesNeeded) { $this->stylesNeeded = True; $output .= '' . substr($content, $found, ($tagEnd - $found) + strlen($this->TagTail)); } else { $output .= substr($content, $found, ($tagEnd - $found) + strlen($this->TagTail)); } } $newContent = $newContent . substr($content, $index, ($found-$index)); $newContent = $newContent . $output; $index = $tagEnd + strlen($this->TagTail); } } return $newContent; } /*****************************************************************************************/ /// Display Content, Widgets and Pages /*****************************************************************************************/ // Top level function to display options function showOptionsPage() { global $screen_layout_columns; ?>

opts_page, 'normal',0); ?>
opts_page, 'advanced',0); ?>
opts_page, 'side',0); ?>

Hover the mouse pointer over the keywords for more information.

', 'amazon-link'); foreach ($this->search->keywords as $keyword => $details) { $text = $text . '

%' . strtoupper($keyword) . '%

'; } echo $text; } /*****************************************************************************************/ // Page/Post Edit Screen Widget function insertForm($post) { include('include/insertForm.php'); } /*****************************************************************************************/ /// Do Amazon Link Constructs /*****************************************************************************************/ function showRecommendations ($categories='1', $last='30') { return include('include/showRecommendations.php'); } function get_local_info() { $channel = $this->get_channel(); $top_cc = $this->get_country(); $country_data = $this->get_country_data(); $info = array( 'cc' => $top_cc, 'rcm' => $country_data[$top_cc]['rcm'], 'mplace_id' => $country_data[$top_cc]['m_id'], 'mplace' => $country_data[$top_cc]['market'], 'tld' => $country_data[$top_cc]['tld'], 'tag' => $channel['tag_' . $top_cc], 'channel' => $channel['ID']); return $info; } function getURL($asin) { $li = $this->get_local_info(); $text='http://www.amazon.' . $li['tld'] . '/gp/product/'. $asin. '?ie=UTF8&tag=' . $li['tag'] .'&linkCode=as2&camp=1634&creative=6738&creativeASIN='. $asin; return $text; } function itemLookup($asin, $Settings = NULL ) { if ($Settings === NULL) $Settings = $this->getSettings(); else $this->Settings = $Settings; // Create query to retrieve the an item $request = array('Operation' => 'ItemLookup', 'ResponseGroup' => 'Offers,Small,Reviews,Images,SalesRank', 'ItemId' => $asin, 'IdType' => 'ASIN', 'MerchantId' => 'Amazon'); $pxml = $this->doQuery($request, $Settings); if (($pxml === False) || !isset($pxml['Items']['Item'])) { $item = array('found' => 0, 'ASIN' => $asin); } else { $item =array_merge($pxml['Items']['Item'], array('found' => 1)); } $item['Settings'] = $Settings; return $item; } function doQuery($request, $Settings = NULL) { if ($Settings === NULL) $Settings = $this->getSettings(); else $this->Settings = $Settings; $li = $this->get_local_info(); $tld = $li['tld']; if (!isset($request['AssociateTag'])) $request['AssociateTag'] = $li['tag']; return aws_signed_request($tld, $request, $Settings['pub_key'], $Settings['priv_key']); } function make_links($asins, $link_text, $Settings = NULL) { global $post; if ($Settings === NULL) $Settings = $this->getSettings(); else $this->Settings = $Settings; $output = ''; /* * If a template is specified and exists then populate it */ if (isset($Settings['template'])) { $template = strtolower($Settings['template']); $Templates = $this->getTemplates(); if (isset($Templates[$template])) { $details = array(); unset($Settings['asin']); $Settings['template_content'] = $Templates[$template]['Content']; if (preg_match('/%ASINS%/i', $Settings['template_content'])) { $details[] = array('ASINS' => implode(',', $asins), 'Settings' => $Settings); } else { foreach ($asins as $asin) { if (strlen($asin) > 8) { if ((($Settings['live']) || (count($asins) > 1)) && $this->valid_keys()) { $lookup = $this->itemLookup($asin, $Settings); if (!$lookup['found'] && $Settings['localise']) { /* Not found - try the default locale */ $Settings['localise'] = 0; $lookup = $this->itemLookup($asin, $Settings); $Settings['localise'] = 1; } $details[] = $lookup; } else { $details[] = array('ASIN' => $asin, 'found' => 1, 'Settings' => $Settings ); } } } } if (!empty($details)) { $items = array_shift($this->search->parse_results($details)); foreach ($items as $item => $details) { $output .= $details['template']; } } return $output; } } foreach ($asins as $asin) { /* * This code required to maintain backward compatibility */ $object = stripslashes($link_text); // Do we need to display or link to an image ? if (!empty($this->Settings['image']) || !empty($this->Settings['thumb'])) { $media_ids = $this->search->find_attachments($asin); if (!is_wp_error($media_ids)) { $media_id = $media_ids[0]->ID; } if (!empty($this->Settings['thumb'])) { if (isset($media_id)) { $thumb = wp_get_attachment_thumb_url($media_id); } elseif (strlen($this->Settings['thumb']) > 4) { $thumb = $this->Settings['thumb']; } } if (!empty($this->Settings['image'])) { if (isset($media_id)) { $image = wp_get_attachment_url($media_id); } elseif (strlen($this->Settings['image']) > 4) { $image = $this->Settings['image']; } } } // If both thumb and image are specified then just insert the image if (isset($thumb) && isset($image)) { $object = ''. $link_text .''; return $object; } if (isset($image)) $object = ''. $link_text .''; if (isset($thumb)) $object = ''. $link_text .''; /* * Generate a localised/multinational link, wrapped around '$object' */ $TARGET = $this->Settings['new_window'] ? 'target="_blank"' : ''; $URL = $this->getURL($asin); if ($this->Settings['multi_cc']) { $local_info = $this->get_local_info(); $text=''; $text .= $object. ''; $this->multi_id++; } else { $text='' . $object . ''; } $output .= $text; } return $output; } ///////////////////////////////////////////////////////////////////// } // End Class $awlfw = new AmazonWishlist_For_WordPress(); } // End if exists function amazon_get_link($args) { global $awlfw; $awlfw->parseArgs($args); // Get the default settings foreach ($awlfw->Settings['asin'] as $asin) { return $awlfw->getURL($asin); // Return a URL } } function amazon_scripts() { global $awlfw; $this->generate_multi_script(); } function amazon_make_links($args) { global $awlfw; $awlfw->parseArgs($args); // Get the default settings return $awlfw->make_links($awlfw->Settings['asin'], $awlfw->Settings['text']); // Return html } function amazon_query($request) { global $awlfw; return $awlfw->doQuery($request); // Return response } function amazon_recommends($categories='1', $last='30') { global $awlfw; return $awlfw->showRecommendations ($categories, $last); } ?>