&text=] --> inserts link to amazon item
[amazon cat= ' . $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'),
'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;
?>
%' . 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, '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 = '