'). */ if (!class_exists('AmazonLinkSearch')) { class AmazonLinkSearch { var $data = array(); function AmazonLinkSearch() { $this->__construct(); } function __construct() { $this->URLRoot = plugins_url("", __FILE__); $this->base_name = plugin_basename( __FILE__ ); $this->plugin_dir = dirname( $this->base_name ); } /* * Must be called by the client in its init function. */ function init($parent) { $script = plugins_url("amazon-link-search.js", __FILE__); wp_register_script('amazon-link-search', $script, array('jquery'), '1.0.0'); add_action('wp_ajax_amazon-link-search', array($this, 'performSearch')); // Handle ajax search requests add_action('wp_ajax_amazon-link-get-image', array($this, 'grabImage')); // Handle ajax image download add_action('wp_ajax_amazon-link-remove-image', array($this, 'removeImage')); // Handle ajax image removal $this->alink = $parent; $this->keywords = array( 'link_open' => array( 'Description' => __('Create an Amazon link to a product with user defined content, of the form %LINK_OPEN%My Content%LINK_CLOSE%', 'amazon-link'), 'link' => '1'), 'rlink_open' => array( 'Description' => __('Create an Amazon link to product reviews with user defined content, of the form %RLINK_OPEN%My Content%LINK_CLOSE%', 'amazon-link'), 'link' => '1'), 'slink_open' => array( 'Description' => __('Create an Amazon link to a search page with user defined content, of the form %SLINK_OPEN%My Content%LINK_CLOSE%', 'amazon-link'), 'link' => '1'), 'link_close' => array( 'Description' => __('Must follow a LINK_OPEN (translates to "").', 'amazon-link')), 'asin' => array( 'Description' => __('Item\'s unique ASIN', 'amazon-link'), 'live' => '1'), 'asins' => array( 'Description' => __('Comma seperated list of ASINs', 'amazon-link')), 'product' => array( 'Description' => __('Item\'s Product Group', 'amazon-link'), 'live' => '1'), 'title' => array( 'Description' => __('Item\'s Title', 'amazon-link'), 'live' => '1'), 'text' => array( 'Description' => __('User Defined Text string', 'amazon-link'), 'user' => '1'), 'text1' => array( 'Description' => __('User Defined Text string', 'amazon-link'), 'user' => '1'), 'text2' => array( 'Description' => __('User Defined Text string', 'amazon-link'), 'user' => '1'), 'text3' => array( 'Description' => __('User Defined Text string', 'amazon-link'), 'user' => '1'), 'text4' => array( 'Description' => __('User Defined Text string', 'amazon-link'), 'user' => '1'), 'artist' => array( 'Description' => __('Item\'s Author, Artist or Creator', 'amazon-link'), 'live' => '1'), 'manufacturer' => array( 'Description' => __('Item\'s Manufacturer', 'amazon-link'), 'live' => '1'), 'thumb' => array( 'Description' => __('URL to Thumbnail Image', 'amazon-link'), 'live' => '1', 'image' => '1'), 'image' => array( 'Description' => __('URL to Full size Image', 'amazon-link'), 'live' => '1', 'image' => '1'), 'image_class' => array( 'Description' => __('Class of Image as defined in settings', 'amazon-link')), 'url' => array( 'Description' => __('The URL returned from the Item Search (not localised!)', 'amazon-link'), 'live' => '1'), 'rank' => array( 'Description' => __('Amazon Rank', 'amazon-link'), 'live' => '1'), 'rating' => array( 'Description' => __('Numeric User Rating - (No longer Available)', 'amazon-link'), 'live' => '1'), 'price' => array( 'Description' => __('Price of Item', 'amazon-link'), 'live' => '1'), 'tag' => array( 'Description' => __('Localised Amazon Associate Tag', 'amazon-link')), 'cc' => array( 'Description' => __('Localised Country Code (us, uk, etc.)', 'amazon-link')), 'flag' => array( 'Description' => __('Localised Country Flag Image URL', 'amazon-link')), 'mplace' => array( 'Description' => __('Localised Amazon Marketplace Code (US, GB, etc.)', 'amazon-link')), 'mplace_id' => array( 'Description' => __('Localised Numeric Amazon Marketplace Code (2=uk, 8=fr, etc.)', 'amazon-link')), 'tld' => array( 'Description' => __('Localised Top Level Domain (.com, .co.uk, etc.)', 'amazon-link')), 'rcm' => array( 'Description' => __('Localised RCM site host domain (rcm.amazon.com, rcm-uk.amazon.co.uk, etc.)', 'amazon-link')), 'downloaded' => array( 'Description' => __('1 if Images are in the local Wordpress media library', 'amazon-link')), 'found' => array( 'Description' => __('1 if product was found doing a live data request (also 1 if live not enabled).', 'amazon-link')) ); } /*****************************************************************************************/ /// AJAX Call Handlers /*****************************************************************************************/ function performSearch($Opts='') { if (!is_array($Opts)) $Opts = $_POST; $Settings = array_merge($this->alink->getSettings(), $Opts); $Settings['multi_cc'] = '0'; $Settings['localise'] = 0; if ( empty($Opts['s_title']) && empty($Opts['s_author']) ) { $Items = $this->alink->itemLookup($Opts['asin'], $Settings); } else { $Settings['found'] = 1; $Items = $this->do_search($Opts); } $results['success'] = 0; if (isset($Items['Error'])) { $results['message'] = 'Error: ' . $Items['Error']; } else if (is_array($Items) && (count($Items) >0)) { foreach($Items as $Item) { $Item['found'] = 1; $item = $this->parse_xml($Item, $Settings['default_cc']); $item = array_merge($Settings,$item); $results['items'][]['template'] = $this->parse_template($item); } $results['success'] = 1; } print json_encode($results); exit(); } function removeImage() { $Opts = $_POST; /* Do we have this image? */ $media_ids = $this->find_attachments( $Opts['asin'] ); if (is_wp_error($media_ids)) { $results = array('in_library' => false, 'asin' => $Opts['asin'], 'error' => __('No matching image found', 'amazon-link')); } else { $results = array('in_library' => false, 'asin' => $Opts['asin'], 'error' => __('Images deleted','amazon-link')); /* Only remove images attached to this post */ foreach ($media_ids as $id => $media_id) { if ($media_id->post_parent == $Opts['post']) { /* Remove attachment */ wp_delete_attachment($media_id->ID); } else { $results['in_library'] = true; $results['id'] = $media_id->ID; } } } print json_encode($results); exit(); } function grabImage() { $Opts = $_POST; /* Do not upload if we already have this image */ $media_ids = $this->find_attachments( $Opts['asin'] ); if (!is_wp_error($media_ids)) { $results = array('in_library' => true, 'asin' => $Opts['asin'], 'id' => $media_ids[0]->ID); } else { /* Attempt to download the image */ $result = $this->grab_image($Opts['asin'], $Opts['post']); if (is_wp_error($result)) { $results = array('in_library' => false, 'asin' => $Opts['asin'], 'error' => $result->get_error_code()); } else { $results = array('in_library' => true, 'asin' => $Opts['asin'], 'id' => $result); } } print json_encode($results); exit(); } /*****************************************************************************************/ /// Helper Functions /*****************************************************************************************/ function get_aws_info() { $search_index_by_locale = array( 'ca' => array('All', 'Blended', 'Books', 'Classical', 'DVD', 'Electronics', 'ForeignBooks', 'Kitchen', 'Music', 'Software', 'SoftwareVideoGames', 'VHS', 'Video', 'VideoGames'), 'us' => array('All', 'Apparel', 'Appliances', 'ArtsAndCrafts', 'Automotive', 'Baby', 'Beauty', 'Blended', 'Books', 'Classical', 'DigitalMusic', 'Grocery', 'MP3Downloads', 'DVD', 'Electronics', 'HealthPersonalCare', 'HomeGarden', 'Industrial', 'Jewelry', 'KindleStore', 'Kitchen', 'Magazines', 'Merchants', 'Miscellaneous', 'MobileApps', 'Music', 'MusicalInstruments', 'MusicTracks', 'OfficeProducts', 'OutdoorLiving', 'PCHardware', 'PetSupplies', 'Photo', 'Shoes', 'Software', 'SportingGoods', 'Tools', 'Toys', 'UnboxVideo', 'VHS', 'Video', 'VideoGames', 'Watches', 'Wireless', 'WirelessAccessories'), 'cn' => array('All', 'Apparel', 'Appliances', 'Automotive', 'Baby', 'Beauty', 'Books', 'Electronics', 'Grocery', 'HealthPersonalCare', 'Home', 'HomeImprovement', 'Jewelry', 'Misc', 'Music', 'OfficeProducts', 'Photo', 'Shoes', 'Software', 'SportingGoods', 'Toys', 'Video', 'VideoGames', 'Watches'), 'de' => array('All', 'Apparel', 'Automotive', 'Baby', 'Blended', 'Beauty', 'Books', 'Classical', 'DVD', 'Electronics', 'ForeignBooks', 'Grocery', 'HealthPersonalCare', 'HomeGarden', 'Jewelry', 'KindleStore', 'Kitchen', 'Lighting', 'Magazines', 'MP3Downloads', 'Music', 'MusicalInstruments', 'MusicTracks', 'OfficeProducts', 'OutdoorLiving', 'Outlet', 'PCHardware', 'Photo', 'Software', 'SoftwareVideoGames', 'SportingGoods', 'Tools', 'Toys', 'VHS', 'Video', 'VideoGames', 'Watches'), 'es' => array('All', 'Books', 'DVD', 'Electronics', 'ForeignBooks', 'Kitchen', 'Music', 'Software', 'Toys', 'VideoGames', 'Watches'), 'fr' => array('All', 'Apparel', 'Baby', 'Beauty', 'Blended', 'Books', 'Classical', 'DVD', 'Electronics', 'ForeignBooks', 'HealthPersonalCare', 'Jewelry', 'Kitchen', 'Lighting', 'MP3Downloads', 'Music', 'MusicalInstruments', 'MusicTracks', 'OfficeProducts', 'Outlet', 'Shoes', 'Software', 'SoftwareVideoGames', 'VHS', 'Video', 'VideoGames', 'Watches'), 'it' => array('All', 'Books', 'DVD', 'Electronics', 'ForeignBooksSearchIndex:Garden', 'Kitchen', 'Music', 'Shoes', 'Software', 'Toys', 'VideoGames', 'Watches'), 'jp' => array('All', 'Apparel', 'Automotive', 'Baby', 'Beauty', 'Blended', 'Books', 'Classical', 'DVD', 'Electronics', 'ForeignBooks', 'Grocery', 'HealthPersonalCare', 'Hobbies', 'HomeImprovement', 'Jewelry', 'Kitchen', 'MP3Downloads', 'Music', 'MusicalInstruments', 'MusicTracks', 'OfficeProducts', 'Shoes', 'Software', 'SportingGoods', 'Toys', 'VHS', 'Video', 'VideoGames', 'Watches'), 'uk' => array('All', 'Apparel', 'Automotive', 'Baby', 'Beauty', 'Blended', 'Books', 'Classical', 'DVD', 'Electronics', 'Grocery', 'HealthPersonalCare', 'HomeGarden', 'Jewelry', 'Kitchen', 'Lighting', 'MP3Downloads', 'Music', 'MusicalInstruments', 'MusicTracks', 'OfficeProducts', 'OutdoorLiving', 'Outlet', 'Shoes', 'Software', 'SoftwareVideoGames', 'Toys', 'VHS', 'Video', 'VideoGames', 'Watches'), 'us' => array('All', 'Apparel', 'Appliances', 'ArtsAndCrafts', 'Automotive', 'Baby', 'Beauty', 'Blended', 'Books', 'Classical', 'DigitalMusic', 'Grocery', 'MP3Downloads', 'DVD', 'Electronics', 'HealthPersonalCare', 'HomeGarden', 'Industrial', 'Jewelry', 'KindleStore', 'Kitchen', 'Magazines', 'Merchants', 'Miscellaneous', 'MobileApps', 'Music', 'MusicalInstruments', 'MusicTracks', 'OfficeProducts', 'OutdoorLiving', 'PCHardware', 'PetSupplies', 'Photo', 'Shoes', 'Software', 'SportingGoods', 'Tools', 'Toys', 'UnboxVideo', 'VHS', 'Video', 'VideoGames', 'Watches', 'Wireless', 'WirelessAccessories')); return array('SearchIndexByLocale' => $search_index_by_locale); } function do_search($Opts) { $Settings = array_merge($this->alink->getSettings(), $Opts); $Settings['multi_cc'] = '0'; $Settings['found'] = 1; $Settings['localise'] = 0; // Not working: Baby, MusicalInstruments $Creator = array( 'Author' => array( 'Books', 'ForeignBooks', 'MobileApps', 'MP3Downloads'), 'Actor' => array( 'DigitalMusic' ), 'Artist' => array('Music'), 'Director' => array('DVD', 'UnboxVideo', 'VHS', 'Video'), 'Publisher' => array('Magazines'), 'Brand' => array('Apparel', 'ArtsAndCrafts', 'Baby', 'Beauty', 'Grocery', 'Lighting', 'OfficeProducts', 'Miscellaneous', 'PetSupplies', 'Shoes', 'MusicalInstruments', 'VideoGames'), 'Manufacturer' => array('Appliances', 'Automotive', 'Electronics', 'Garden', 'HealthPersonalCare', 'Hobbies', 'Home', 'HomeGarden', 'HomeImprovement', 'Industrial', 'Kitchen', 'OutdoorLiving', 'Photo', 'Software', 'SoftwareVideoGames'), 'Composer' => array('Classical')); $Keywords = array('Blended', 'All', 'MusicTracks', 'Outlet'); $Sort = array('salesrank' => array('Books', 'Classical', 'DVD', 'Electronics', 'HealthPersonalCare', 'HomeGarden', 'HomeImprovement', 'Kitchen', 'MarketPlace', 'Music', 'OutdoorLiving', 'PCHardware', 'Software', 'SoftwareVideoGames', 'Toys', 'VHS', 'Video', 'VideoGames'), 'relevancerank' => array('Apparel', 'Automotive', 'Baby', 'Beauty', 'Grocery', 'Jewelry', 'KindleStore', 'MP3Downloads', 'MusicalInstruments', 'OfficeProducts', 'Shoes', 'Watches'), 'xsrelevancerank' => array('Shoes')); // Create query to retrieve the first 10 matching items $request = array("Operation" => "ItemSearch", "ResponseGroup" => "Offers,ItemAttributes,Small,Reviews,Images,SalesRank", "SearchIndex"=>$Opts['s_index'], "ItemPage"=>$Opts['s_page']); foreach ($Sort as $Term => $Indices) { if (in_array($Opts['s_index'], $Indices)) { $request['Sort'] = $Term; continue; } } foreach ($Creator as $Term => $Indices) { if (in_array($Opts['s_index'], $Indices)) { $request[$Term] = $Opts['s_author']; continue; } } if (in_array($Opts['s_index'], $Keywords)) { $request['Keywords'] = $Opts['s_title']; } else { $request['Title'] = $Opts['s_title']; } $pxml = $this->alink->doQuery($request, $Settings); if (($pxml === False) || !isset($pxml['Items']['Item'])) { $Items = array(); if (isset($pxml['Error'])) { $Items['Error'] = $pxml['Error']['Message']; } else { $Items['Error'] = 'Query returned no results.'; } } else { $Items=$pxml['Items']['Item']; } /* Test Code to check availibility at all sites... // if( !class_exists( 'WP_Http' ) ) // include_once( ABSPATH . WPINC. '/class-http.php' ); foreach($Items as $item => $item_info) { $map = '
'.$result[0].''; if (!is_wp_error($result) && ($result['response']['code'] == 200)) { $map .= '
DATA: "; print_r($data); echo ""; } $data = $this->parse_xml($item_data, $country, $data); } else { $data[$keyword][$country] = 'Undefined'; $data['found'][$country] = 1; } } else if (($keyword == 'found') && !isset($data[$keyword][$country])) { $data[$keyword][$country] = 1; } else { $data = $this->regionalise($local_info, $country, $data); if (!isset($data[$keyword][$country])) $data[$keyword][$country] = 'NL'; } } $phrase = $data[$keyword][$country]; if ($item['multi_cc']) unset ($data['link_open'][$country]); // Only use once if ($escaped) $phrase = addslashes($phrase); //urlencode $output .= substr($input, $index, ($key_start-$index)) . $phrase; $index = $key_end; } $input = $output . substr($input, $index); } $this->alink->Settings['default_cc'] = $item['default_cc']; $this->alink->Settings['multi_cc'] = $item['multi_cc']; $this->alink->Settings['localise'] = $item['localise']; return $input; } /**/ function find_attachments ($asin, $number = '-1') { // Do we already have a local image ? $args = array( 'post_type' => 'attachment', 'numberposts' => $number, 'post_status' => 'all', 'no_filters' => true, 'meta_query' => array(array('key' => 'amazon-link-ASIN', 'value' => $asin))); $query = new WP_Query( $args ); $media_ids = $query->posts; if ($media_ids) { return $media_ids; } else { return new WP_Error(__('No images found','amazon-link')); } } function grab_image ($ASIN, $post_id = 0) { $ASIN = strtoupper($ASIN); $result = array_shift($this->alink->itemLookup($ASIN)); $data = $this->parse_xml($result, '0'); if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ) return new WP_Error($uploads['error']); $filename = $ASIN. '.JPG'; $filename = '/' . wp_unique_filename( $uploads['path'], basename($filename)); $filename_full = $uploads['path'] . $filename; if( !class_exists( 'WP_Http' ) ) include_once( ABSPATH . WPINC. '/class-http.php' ); $request = new WP_Http; $result = $request->request( $data['image']['0']); if ($result instanceof WP_Error ) return new WP_Error(__('Could not retrieve remote image file','amazon-link')); // Save file to media library $content = $result['body']; $size = file_put_contents ($filename_full, $content); if (is_readable($filename_full)) { // Grabbed Image successfully now add it to the media library $wp_filetype = wp_check_filetype(basename($filename_full), null ); $attachment = array( 'guid' => $filename, 'post_mime_type' => $wp_filetype['type'], 'post_title' => $data['artist']['0'] . ' - ' . $data['title']['0'], // Title 'post_excerpt' => $data['title']['0'], // Caption 'post_content' => '', // Description 'post_status' => 'inherit'); $attach_id = wp_insert_attachment( $attachment, $filename_full, $post_id); // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work update_post_meta($attach_id , 'amazon-link-ASIN', $ASIN); require_once(ABSPATH . "wp-admin" . '/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $filename_full ); //echo "
"; print_r($attach_data); echo ""; wp_update_attachment_metadata( $attach_id, $attach_data ); } else { return new WP_Error(__('Could not read downloaded image','amazon-link')); } return $attach_id; } function remove_parents ($array) { if (is_array($array)) { return $this->remove_parents($array[0]); } else { return $array; } } } } ?>