&text=] --> inserts link to amazon item
[amazon cat=';
$this->optionList['default_cc']['Options'][$cc]['Input'] = 'tag_' . $cc;
$this->optionList['tag_' . $cc] = array('Type' => 'option', 'OverrideBlank' => $data['default_tag'], 'Name' => $data['name'] . __(' Affiliate Tag', 'amazon-link'),
'Description' => sprintf(__('Enter your affiliate tag for %1$s.', 'amazon-link'), ''.$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 getOptions() {
if (null === $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 (null === $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;
}
/*
* 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'] = explode (',', $this->Settings['asin']);
}
/*
* 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 (null === $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 valid_keys() {
$Settings = $this->getSettings();
if ( (strlen($Settings['pub_key']) > 10) && (strlen($Settings['priv_key']) > 10))
return True;
return False;
}
/*****************************************************************************************/
/// 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', 'es'),
'de' => array('de', 'at', 'ch', 'no', 'dn', 'li', 'sk'),
'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'];
}
/*****************************************************************************************/
/// 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) {
$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) {
if ($this->Settings['debug']) {
$output .= '';
}
$output .= $this->showRecommendations($this->Settings['cat'],$this->Settings['last']);
} 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);
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() { $top_cc = $this->get_country(); $country_data = $this->get_country_data(); $info = array( 'cc' => $top_cc, 'mplace' => $country_data[$top_cc]['market'], 'tld' => $country_data[$top_cc]['tld'], 'tag' => $this->Settings['tag_' . $top_cc]); 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 getURLs($asin) { $country_data = $this->get_country_data(); $li = $this->get_local_info(); $text[$li['cc']]='http://www.amazon.' . $li['tld'] . '/gp/product/'. $asin. '?ie=UTF8&tag=' . $li['tag'] .'&linkCode=as2&camp=1634&creative=6738&creativeASIN='. $asin; foreach ($country_data as $cc => $data) { if ($cc != $li['cc']) { $tld = $data['tld']; $tag = $this->Settings['tag_'.$cc]; $text[$cc]='http://www.amazon.' . $tld . '/gp/product/'. $asin. '?ie=UTF8&tag=' . $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; $country_data = $this->get_country_data(); $li = $this->get_local_info(); $cc = $li['cc']; $tld = $country_data[$cc]['tld']; if (!isset($request['AssociateTag'])) $request['AssociateTag'] = $Settings['tag_' . $cc]; 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 ($this->Settings['image'] || $this->Settings['thumb']) { $media_ids = $this->search->find_attachments($asin); if (!is_wp_error($media_ids)) { $media_id = $media_ids[0]->ID; } if ($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 ($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 = '