*/ // TODO: Fixed NoSilencedErrors.Discouraged // @codingStandardsIgnoreStart Generic.PHP.NoSilencedErrors.Discouraged require_once dirname( __FILE__ ) . '/lib/json.php'; class Amazonjs { const VERSION = '0.8'; const AWS_VERSION = '2011-08-01'; const CACHE_LIFETIME = 86400; public $title; public $url; public $option_page_url; public $plugin_rel_file; public $option_page_name; public $option_name; public $setting_sections; public $setting_fields; public $default_settings; public $settings; public $text_domain; public $media_type = 'amazonjs'; public $countries; public $search_indexes; public $display_items = array(); public $simple_template; function __construct() { $path = __FILE__; $dir = dirname( $path ); $slug = basename( $dir ); $this->title = 'AmazonJS'; $this->plugin_rel_file = basename( $dir ) . DIRECTORY_SEPARATOR . basename( $path ); $this->option_page_name = basename( $dir ); $this->option_name = preg_replace( '/[\-\.]/', '_', $this->option_page_name ) . '_settings'; $this->url = plugins_url( '', $path ); $this->option_page_url = admin_url() . 'options-general.php?page=' . $this->option_page_name; $this->text_domain = $slug; load_plugin_textdomain( $this->text_domain, false, dirname( $this->plugin_rel_file ) . '/languages' ); $this->countries = array( 'US' => array( 'label' => __( 'United States', $this->text_domain ), 'domain' => 'Amazon.com', 'baseUri' => 'http://webservices.amazon.com', 'linkTemplate' => '', 'associateTagSuffix' => '-20', ), 'UK' => array( 'label' => __( 'United Kingdom', $this->text_domain ), 'domain' => 'Amazon.co.uk', 'baseUri' => 'http://webservices.amazon.co.uk', 'linkTemplate' => '', 'associateTagSuffix' => '-21', ), 'DE' => array( 'label' => __( 'Deutschland', $this->text_domain ), 'domain' => 'Amazon.de', 'baseUri' => 'http://webservices.amazon.de', 'linkTemplate' => '', 'associateTagSuffix' => '04-21', ), 'FR' => array( 'label' => __( 'France', $this->text_domain ), 'domain' => 'Amazon.fr', 'baseUri' => 'http://webservices.amazon.fr', 'linkTemplate' => '', 'associateTagSuffix' => '09-21', ), 'JP' => array( 'label' => __( 'Japan', $this->text_domain ), 'domain' => 'Amazon.co.jp', 'baseUri' => 'http://webservices.amazon.co.jp', 'linkTemplate' => '', 'associateTagSuffix' => '-22', ), 'CA' => array( 'label' => __( 'Canada', $this->text_domain ), 'domain' => 'Amazon.ca', 'baseUri' => 'http://webservices.amazon.ca', 'linkTemplate' => '', 'associateTagSuffix' => '0c-20', ), 'CN' => array( 'label' => __( 'China', $this->text_domain ), 'domain' => 'Amazon.cn', 'baseUri' => 'http://webservices.amazon.cn', 'linkTemplate' => '', 'associateTagSuffix' => '-23', ), 'IT' => array( 'label' => __( 'Italia', $this->text_domain ), 'domain' => 'Amazon.it', 'baseUri' => 'http://webservices.amazon.it', 'linkTemplate' => '', 'associateTagSuffix' => '-21', ), 'ES' => array( 'label' => __( 'EspaƱa', $this->text_domain ), 'domain' => 'Amazon.es', 'baseUri' => 'http://webservices.amazon.es', 'linkTemplate' => '', 'associateTagSuffix' => '-21', ), ); } function clean() { $this->delete_settings(); } function init() { $this->init_settings(); if ( is_admin() ) { add_action( 'admin_menu', array( $this, 'admin_menu' ) ); add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 2 ); } add_shortcode( 'amazonjs', array( $this, 'shortcode' ) ); if ( ! is_admin() ) { add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_styles' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_scripts' ) ); add_action( 'wp_footer', array( $this, 'wp_enqueue_scripts_for_footer' ), 1 ); } } function admin_init() { add_action( 'media_buttons', array( $this, 'media_buttons' ), 20 ); add_action( 'media_upload_amazonjs', array( $this, 'media_upload_amazonjs' ) ); add_action( 'media_upload_amazonjs_keyword', array( $this, 'media_upload_amazonjs_keyword' ) ); add_action( 'media_upload_amazonjs_id', array( $this, 'media_upload_amazonjs_id' ) ); add_action( 'wp_ajax_amazonjs_search', array( $this, 'ajax_amazonjs_search' ) ); $page = $this->option_page_name; register_setting( $this->option_name, $this->option_name, array( $this, 'validate_settings' ) ); if ( $this->setting_sections ) { foreach ( $this->setting_sections as $key => $section ) { add_settings_section( $page . '_' . $key, $section['label'], array( $this, $section['add'] ), $page ); } } foreach ( $this->setting_fields as $key => $field ) { $label = ('checkbox' == $field['type']) ? '' : $field['label']; add_settings_field( $this->option_name . '_' . $key, $label, array( $this, 'add_settings_field' ), $page, $page . '_' . $field['section'], array( $key, $field ) ); } } function admin_print_styles() { global $wp_version; // use dashicon if ( version_compare( $wp_version, '3.8', '>=' ) ) { wp_enqueue_style( 'amazonjs-options', $this->url . '/css/amazonjs-options.css', array(), self::VERSION ); } } function wp_enqueue_styles() { if ( $this->settings['displayCustomerReview'] ) { wp_enqueue_style( 'thickbox' ); } if ( $this->settings['overrideThemeCss'] ) { wp_enqueue_style( 'amazonjs', $this->url . '/css/amazonjs-force.css', array(), self::VERSION ); } else { wp_enqueue_style( 'amazonjs', $this->url . '/css/amazonjs.css', array(), self::VERSION ); } if ( $this->settings['customCss'] ) { wp_enqueue_style( 'amazonjs-custom', get_stylesheet_directory_uri() . '/amazonjs.css' ); } } function wp_enqueue_scripts() { wp_register_script( 'jquery-tmpl', $this->url . '/components/js/jquery-tmpl/jquery.tmpl.min.js', array( 'jquery' ), '1.0.0pre', true ); $depends = array( 'jquery-tmpl' ); if ( $this->settings['displayCustomerReview'] ) { $depends[] = 'thickbox'; } wp_register_script( 'amazonjs', $this->url . '/js/amazonjs.js', $depends, self::VERSION, true ); if ( $this->settings['customJs'] ) { wp_register_script( 'amazonjs-custom', get_stylesheet_directory_uri() . '/amazonjs.js', array( 'amazonjs' ), self::VERSION, true ); } } function wp_enqueue_scripts_for_footer() { $country_codes = array(); $items = array(); foreach ( $this->display_items as $country_code => $sub_items ) { foreach ( $this->fetch_items( $country_code, $sub_items ) as $asin => $item ) { $items[ $country_code . ':' . $asin ] = $item; } $country_codes[] = $country_code; } if ( count( $items ) == 0 ) { return; } $this->enqueue_amazonjs_scripts( $items, $country_codes ); } function enqueue_amazonjs_scripts( $items = array(), $country_codes = array() ) { $wpurl = get_bloginfo( 'wpurl' ); $region = array(); foreach ( $this->countries as $code => $value ) { if ( in_array( $code, $country_codes ) ) { foreach ( array( 'linkTemplate' ) as $attr ) { $region[ 'Link' . $code ] = $this->tmpl( $value[ $attr ], array( 't' => $this->settings[ 'associateTag' . $code ] ) ); } } } $amazonVars = array( 'thickboxUrl' => $wpurl . '/wp-includes/js/thickbox/', 'regionTemplate' => $region, 'resource' => array( 'BookAuthor' => __( 'Author', $this->text_domain ), 'BookPublicationDate' => __( 'PublicationDate', $this->text_domain ), 'BookPublisher' => __( 'Publisher', $this->text_domain ), 'NumberOfPagesValue' => __( '${NumberOfPages} pages', $this->text_domain ), 'ListPrice' => __( 'List Price', $this->text_domain ), 'Price' => __( 'Price', $this->text_domain ), 'PriceUsage' => __( 'Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [amazon.com or endless.com, as applicable] at the time of purchase will apply to the purchase of this product.', $this->text_domain ), 'PublicationDate' => __( 'Publication Date', $this->text_domain ), 'ReleaseDate' => __( 'Release Date', $this->text_domain ), 'SalesRank' => __( 'SalesRank', $this->text_domain ), 'SalesRankValue' => __( '#${SalesRank}', $this->text_domain ), 'RunningTime' => __( 'Run Time', $this->text_domain ), 'RunningTimeValue' => __( '${RunningTime} minutes', $this->text_domain ), 'CustomerReviewTitle' => __( '${Title} Customer Review', $this->text_domain ), 'SeeCustomerReviews' => __( 'See Customer Reviews', $this->text_domain ), 'PriceUpdatedat' => __( '(at ${UpdatedDate})', $this->text_domain ), ), 'isCustomerReviewEnabled' => ($this->settings['displayCustomerReview']) ? true : false, 'isTrackEventEnabled' => ($this->settings['useTrackEvent']) ? true : false, 'isFadeInEnabled' => ($this->settings['useAnimation']) ? true : false, 'items' => array_values( $items ), ); wp_localize_script( 'amazonjs', 'amazonjsVars', $amazonVars ); wp_enqueue_script( 'amazonjs' ); if ( $this->settings['customJs'] ) { wp_enqueue_script( 'amazonjs-custom' ); } } function init_settings() { // section $this->setting_sections = array( 'api' => array( 'label' => __( 'Product Advertising API settings', $this->text_domain ), 'add' => 'add_api_setting_section', ), 'associate' => array( 'label' => __( 'Amazon Associates settings', $this->text_domain ), 'add' => 'add_associate_setting_section', ), 'appearance' => array( 'label' => __( 'Appearance settings', $this->text_domain ), 'add' => 'add_appearance_setting_section', ), 'analytics' => array( 'label' => __( 'Analytics settings', $this->text_domain ), 'add' => 'add_analytics_setting_section', ), 'customize' => array( 'label' => __( 'Customize', $this->text_domain ), 'add' => 'add_customize_setting_section', ), ); // filed $template_url = get_bloginfo( 'template_url' ); $this->setting_fields = array( 'accessKeyId' => array( 'label' => __( 'Access Key ID', $this->text_domain ), 'type' => 'text', 'size' => 60, 'section' => 'api', ), 'secretAccessKey' => array( 'label' => __( 'Secret Access Key', $this->text_domain ), 'type' => 'text', 'size' => 60, 'section' => 'api', ), 'displayCustomerReview' => array( 'label' => __( 'Display customer review', $this->text_domain ), 'type' => 'checkbox', 'section' => 'appearance', 'description' => __( "AmazonJS will display customer review by using WordPress's Thickbox.", $this->text_domain ), ), 'supportDisabledJavascript' => array( 'label' => __( 'Display official widget when disabled javascript in web browser', $this->text_domain ), 'type' => 'checkbox', 'section' => 'appearance', 'description' => __( 'If set to true, AmazonJS will output html by using <script type="text/javascript">document.write("{$indicator_html}")</script><noscript>{$link_html}</noscript>.', $this->text_domain ), ), 'useAnimation' => array( 'label' => __( 'Use fadeIn animation', $this->text_domain ), 'type' => 'checkbox', 'section' => 'appearance', ), 'overrideThemeCss' => array( 'label' => __( 'Override style of theme', $this->text_domain ), 'type' => 'checkbox', 'section' => 'appearance', 'description' => __( 'If set to true, AmazonJS will override the style of the theme by using !important declaration.', $this->text_domain ), ), 'useTrackEvent' => array( 'label' => __( 'Click Tracking by using Google Analytics', $this->text_domain ), 'type' => 'checkbox', 'section' => 'analytics', 'description' => __( 'If set to true, AmazonJS will call _gaq.push(["_trackEvent", "AmazonJS", "Click", "ASIN TITLE"]) or ga("send", "event", "AmazonJS", "Click", "ASIN TITLE").', $this->text_domain ), ), 'customCss' => array( 'label' => __( 'Use Custom Css', $this->text_domain ), 'type' => 'checkbox', 'section' => 'customize', 'description' => '(' . $template_url . '/amazonjs.css)', ), 'customJs' => array( 'label' => __( 'Use Custom Javascript', $this->text_domain ), 'type' => 'checkbox', 'section' => 'customize', 'description' => '(' . $template_url . '/amazonjs.js)', ), ); foreach ( $this->countries as $key => $value ) { $this->setting_fields[ 'associateTag' . $key ] = array( 'label' => __( $value['domain'], $this->text_domain ), 'type' => 'text', 'size' => 30, 'placeholder' => 'associatetag' . $value['associateTagSuffix'], 'section' => 'associate', ); } $this->default_settings = array(); if ( is_array( $this->setting_fields ) ) { foreach ( $this->setting_fields as $key => $field ) { $this->default_settings[ $key ] = @$field['defaults']; } } //delete_option($this->option_name); $this->settings = wp_parse_args( (array) get_option( $this->option_name ), $this->default_settings ); } function delete_settings() { delete_option( $this->option_name ); } function validate_settings( $settings ) { foreach ( $this->setting_fields as $key => $field ) { if ( 'checkbox' == $field['type'] ) { $settings[ $key ] = ( 'on' == @$settings[ $key ] || '1' == @$settings[ $key ] ); } } foreach ( array( 'accessKeyId', 'secretAccessKey' ) as $key ) { $settings[ $key ] = trim( $settings[ $key ] ); } foreach ( $this->countries as $country_code => $value ) { $key = 'associateTag' . $country_code; $settings[ $key ] = trim( $settings[ $key ] ); } return $settings; } function admin_menu() { if ( function_exists( 'add_options_page' ) ) { $page_hook_suffix = add_options_page( __( $this->title, $this->text_domain ), __( $this->title, $this->text_domain ), 'manage_options', $this->option_page_name, array( $this, 'options_page' ) ); add_action( 'admin_print_styles-' . $page_hook_suffix, array( $this, 'admin_print_styles' ) ); } } function get_amazon_official_link( $asin, $country_code ) { $tmpl = $this->countries[ $country_code ]['linkTemplate']; $item = array( 't' => $this->settings[ 'associateTag' . $country_code ], 'asins' => $asin, 'fc1' => '000000', 'lc1' => '0000FF', 'bc1' => '000000', 'bg1' => 'FFFFFF', 'IS2' => 1, 'lt1' => '_blank', 'f' => 'ifr', 'm' => 'amazon', ); return $this->tmpl( $tmpl, $item ); } function shortcode( $atts, /** @noinspection PhpUnusedParameterInspection */ $content ) { /** * @var string $asin * @var string $tmpl * @var string $locale * @var string $title * @var string $imgsize */ $defaults = array( 'asin' => '', 'tmpl' => '', 'locale' => $this->default_country_code(), 'title' => '', 'imgsize' => '' ); extract( shortcode_atts( $defaults, $atts ) ); if ( empty($asin) ) { return ''; } $country_code = strtoupper( $locale ); $imgsize = strtolower( $imgsize ); if ( is_feed() ) { // use static html for rss reader if ( $ai = $this->get_item( $country_code, $asin ) ) { $aimg = $ai['SmallImage']; if ( array_key_exists( 'MediumImage', $ai ) ) { $aimg = $ai['MediumImage']; } return << {$ai['Title']} {$ai['Title']} EOF; } return $this->get_amazon_official_link( $asin, $country_code ); } if ( ! isset($this->display_items[ $country_code ]) ) { $this->display_items[ $country_code ] = array(); } $item = (array_key_exists( $asin, $this->display_items[ $country_code ] )) ? $this->display_items[ $country_code ][ $asin ] : $this->display_items[ $country_code ][ $asin ] = get_site_transient("amazonjs_{$country_code}_{$asin}"); $url = '#'; if ( is_array( $item ) && array_key_exists( 'DetailPageURL', $item ) ) { $url = $item['DetailPageURL']; } $indicator_html = << EOF; $indicator_html = trim( $indicator_html ); if ( ! $this->settings['supportDisabledJavascript'] ) { return $indicator_html; } $indicator_html = addslashes( $indicator_html ); $link_html = $this->get_amazon_official_link( $asin, $country_code ); return <<document.write("{$indicator_html}") EOF; } /** * Gets default country code by get_locale * @return string */ function default_country_code() { switch ( get_locale() ) { case 'en_CA': return 'CA'; case 'de_DE': return 'DE'; case 'fr_FR': return 'FR'; case 'ja': return 'JP'; case 'en_GB': return 'UK'; case 'zh_CN': return 'CN'; case 'it_IT': return 'IT'; case 'es_ES': return 'ES'; } return 'US'; } function get_item( $country_code, $asin ) { if ( $ai = get_site_transient( "amazonjs_{$country_code}_{$asin}" ) ) { return $ai; } $items = $this->fetch_items( $country_code, array( $asin => false ) ); return @$items[ $asin ]; } /** * @param string $country_code * @param array $items * @return array */ function fetch_items( $country_code, $items ) { $now = time(); $item_ids = array(); foreach ( $items as $asin => $item ) { if ( ! $item && $item['UpdatedAt'] + 86400 < $now ) { $item_ids[] = $asin; } } while ( count( $item_ids ) ) { // fetch via 10 products // ItemLookup ItemId: Must be a valid item ID. For more than one ID, use a comma-separated list of up to ten IDs. $itemid = implode( ',', array_splice( $item_ids, 0, 10 ) ); $results = $this->itemlookup( $country_code, $itemid ); if ( $results && $results['success'] ) { foreach ( $results['items'] as $item ) { $items[ $item['ASIN'] ] = $item; set_site_transient("amazonjs_{$country_code}_{$item['ASIN']}", $item, self::CACHE_LIFETIME); } } } return $items; } function tmpl( $tmpl, $item ) { $s = $tmpl; foreach ( $item as $key => $value ) { $s = str_replace( '${' . $key . '}', $value, $s ); } return $s; } function plugin_row_meta( $links, $file ) { if ( $file == $this->plugin_rel_file ) { array_push( $links, sprintf( '%s', $this->option_page_url, __( 'Settings' ) ) ); array_push( $links, sprintf( '%s', __( 'Old Releases', $this->text_domain ) ) ); } return $links; } function add_api_setting_section() { ?>

text_domain ); ?>

here. Please review the Product Advertising API License Agreement for details.', $this->text_domain ) ?>

Amazon Associates website for details.', $this->text_domain ); ?>

required and verified input parameter in all requests to the Amazon Product Advertising API since 11/1/2011.', $this->text_domain ) ?>

option_name . '_' . $key; $name = $this->option_name . "[{$key}]"; $value = $this->settings[ $key ]; if ( isset($field['html']) ) { echo '' . $field['html'] . ''; } else { switch ( $field['type'] ) { case 'checkbox': ?> value="1" /> $content ) { $input_element_id = $id . '_' . $index; ?> value="" /> ' . $field['description'] . '

'; } } function media_buttons() { global $post_ID, $temp_ID; $iframe_ID = (int) ( 0 == $post_ID ? $temp_ID : $post_ID ); $iframe_src = 'media-upload.php?post_id=' . $iframe_ID . '&type=' . $this->media_type . '&tab=' . $this->media_type . '_keyword'; $label = __( 'Add Amazon Link', $this->text_domain ); ?> <?php echo esc_attr( $label ); ?> wp_enqueue_scripts(); wp_enqueue_style( 'amazonjs-media-upload', $this->url . '/css/media-upload-type-amazonjs.css', array( 'amazonjs' ), self::VERSION ); $this->enqueue_amazonjs_scripts(); } function media_upload_amazonjs() { $this->media_upload_init(); wp_iframe( 'media_upload_type_amazonjs' ); } function media_upload_amazonjs_keyword() { $this->media_upload_init(); wp_iframe( 'media_upload_type_amazonjs_keyword' ); } function media_upload_amazonjs_id() { $this->media_upload_init(); wp_iframe( 'media_upload_type_amazonjs_id' ); } function media_upload_tabs( /** @noinspection PhpUnusedParameterInspection */$tabs ) { return array( $this->media_type . '_keyword' => __( 'Keyword Search', $this->text_domain ), $this->media_type . '_id' => __( 'Search by ASIN/URL', $this->text_domain ), ); } function options_page() { ?>

title ); ?>

options_page_header(); ?>
option_name ); ?> option_page_name ); ?>

text_domain ), $this->title ); ?>

amazon_get( $countryCode, $options ); } // amazon api function itemsearch( $countryCode, $searchIndex, $keywords, $itemPage = 0 ) { $options = array(); if ( $itemPage > 0 ) { $options['ItemPage'] = $itemPage; } $options['Keywords'] = $keywords; $options['Operation'] = 'ItemSearch'; if ( $searchIndex ) { $options['SearchIndex'] = $searchIndex; } return $this->amazon_get( $countryCode, $options ); } /** * parse ASIN from URL * @param string $url * @return bool|string */ static function parse_asin( $url ) { if ( preg_match( '/^https?:\/\/.+\.amazon\.([^\/]+).+\/(dp|gp\/product|ASIN)\/([^\/]+)/', $url, $matches ) ) { return $matches[3]; } return null; } function ajax_amazonjs_search() { $itemId = null; // from http get $itemPage = @$_GET['ItemPage']; $id = @$_GET['ID']; $keywords = @$_GET['Keywords']; $searchIndex = @$_GET['SearchIndex']; $countryCode = @$_GET['CountryCode']; if ( ! empty($id) ) { if ( preg_match( '/^https?:\/\//', $id ) ) { if ( $asin = self::parse_asin( $id ) ) { $itemId = $asin; } else { // url string as query keyword $keywords = $id; } } else { $itemId = $id; } } else if ( ! empty($keywords) ) { if ( preg_match( '/^https?:\/\//', $keywords ) ) { if ( $asin = self::parse_asin( $keywords ) ) { $itemId = $asin; } } } $amazonjs = new Amazonjs(); $amazonjs->init(); if ( isset($itemId) ) { $result = $amazonjs->itemlookup( $countryCode, $itemId ); die(json_encode( $result )); } else { $result = $amazonjs->itemsearch( $countryCode, $searchIndex, $keywords, $itemPage ); die(json_encode( $result )); } } function amazon_get( $countryCode, $options ) { $baseUri = $this->countries[ $countryCode ]['baseUri']; $accessKeyId = @trim( $this->settings['accessKeyId'] ); $secretAccessKey = @trim( $this->settings['secretAccessKey'] ); $associateTag = @$this->settings[ 'associateTag' . $countryCode ]; // validate request if ( empty($countryCode) || (empty($options['ItemId']) && empty($options['Keywords'])) || (empty($accessKeyId) || empty($secretAccessKey)) ) { $message = __( 'Invalid Request Parameters', $this->text_domain ); return compact( 'success', 'message' ); } $options['AWSAccessKeyId'] = $accessKeyId; if ( ! empty($associateTag) ) { $options['AssociateTag'] = @trim( $associateTag ); } $options['ResponseGroup'] = 'ItemAttributes,Small,Images,OfferSummary,SalesRank,Reviews'; $options['Service'] = 'AWSECommerceService'; $options['Timestamp'] = gmdate( 'Y-m-d\TH:i:s\Z' ); $options['Version'] = self::AWS_VERSION; ksort( $options ); $params = array(); foreach ( $options as $k => $v ) { $params[] = $k . '=' . self::urlencode_rfc3986( $v ); } $query = implode( '&', $params ); $urlInfo = parse_url( $baseUri ); $signature = sprintf( "GET\n%s\n/onca/xml\n%s", $urlInfo['host'], $query ); $signature = base64_encode( hash_hmac( 'sha256', $signature, $secretAccessKey, true ) ); unset($params, $urlInfo); $url = sprintf( '%s/onca/xml?%s&Signature=%s', $baseUri, $query, self::urlencode_rfc3986( $signature ) ); $response = wp_remote_request( $url, array( 'timeout' => 30, ) ); if ( is_wp_error( $response ) ) { $error = ''; $errors = $response->get_error_messages(); if ( is_array( $errors ) ) { $error = implode( '
', $errors ); } $message = sprintf( __( 'Network Error: %s', $this->text_domain ), $error ); return compact( 'success', 'message' ); } $body = wp_remote_retrieve_body( $response ); if ( empty($body) ) { $message = sprintf( __( 'Empty Response from %s', $this->text_domain ), $url ); return compact( 'success', 'message' ); } $fetchedAt = time(); $success = false; /* @var $xml stdClass */ $xml = @simplexml_load_string( $body ); if ( WP_DEBUG ) { if ( ! $xml ) { error_log( 'amazonjs: cannot parse xml: ' . $body ); } } if ( $xml ) { if ( 'True' == (string) @$xml->Items->Request->IsValid ) { $success = true; $items = array(); $operation = $options['Operation']; if ( 'ItemSearch' == $operation ) { $os = array(); // OpenSearch $request = $xml->Items->Request->ItemSearchRequest; $resultMap = self::to_array( $xml->Items->SearchResultsMap ); $itemsParPage = 10; $startPage = ($request->ItemPage) ? (int) $request->ItemPage : 1; $os['itemsPerPage'] = $itemsParPage; $os['startIndex'] = ($startPage - 1) * $itemsParPage + 1; $os['Query'] = array( 'searchTerms' => (string) $request->Keywords, 'startPage' => $startPage ); } $os['totalResults'] = (int) $xml->Items->TotalResults; $os['totalPages'] = (int) $xml->Items->TotalPages; foreach ( $xml->Items->Item as $item ) { $r = self::to_array( $item->ItemAttributes ); $r['ASIN'] = trim( (string) $item->ASIN ); $r['DetailPageURL'] = trim( (string) $item->DetailPageURL ); $r['SalesRank'] = (int) $item->SalesRank; if ( $reviews = $item->CustomerReviews ) { $r['IFrameReviewURL'] = (string) $reviews->IFrameURL; } $r['OfferSummary'] = self::to_array( $item->OfferSummary ); $r['SmallImage'] = self::image_element( $item->SmallImage ); $r['MediumImage'] = self::image_element( $item->MediumImage ); $r['LargeImage'] = self::image_element( $item->LargeImage ); $r['CountryCode'] = $countryCode; $r['UpdatedAt'] = $fetchedAt; $items[] = $r; } if ( 'ItemLookup' == $operation ) { if ( 0 == $os['totalResults'] ) { $os['totalResults'] = count( $items ); } if ( 0 == $os['totalPages'] ) { $os['totalPages'] = 1; } } } else { if ( $error = @$xml->Items->Request->Errors->Error ) { $message = __( 'Amazon Product Advertising API Error', $this->text_domain ); $error_code = (string) @$error->Code; $error_message = (string) @$error->Message; } elseif ( $error = @$xml->Error ) { $message = __( 'Amazon Product Advertising API Error', $this->text_domain ); $error_code = (string) @$error->Code; $error_message = (string) @$error->Message; } else { $message = __( 'Cannot Parse Amazon Product Advertising API Response' ); $error_body = (string) $body; } } } else { $message = __( 'Invalid Response', $this->text_domain ); } return compact( 'success', 'operation', 'os', 'items', 'resultMap', 'message', 'error_code' , 'error_message', 'error_body' ); } static function to_array( $element ) { $orgElement = $element; if ( is_object( $element ) && 'SimpleXMLElement' == get_class( $element ) ) { $element = get_object_vars( $element ); } if ( is_array( $element ) ) { $result = array(); if ( count( $element ) <= 0 ) { return trim( strval( $orgElement ) ); } foreach ( $element as $key => $value ) { if ( is_string( $key ) && '@attributes' == $key ) { continue; } $result[ $key ] = self::to_array( $value ); } return $result; } else { return trim( strval( $element ) ); } } static function image_element( $element ) { if ( $element ) { $src = trim( (string) @$element->URL ); $width = (int) @$element->Width; $height = (int) @$element->Height; return compact( 'src', 'width', 'height' ); } return null; } static function urlencode_rfc3986( $string ) { return str_replace( '%7E', '~', rawurlencode( $string ) ); } } function media_upload_type_amazonjs() { include dirname( __FILE__ ) . '/media-upload-type-amazonjs.php'; } function media_upload_type_amazonjs_keyword() { include dirname( __FILE__ ) . '/media-upload-type-amazonjs.php'; } function media_upload_type_amazonjs_id() { include dirname( __FILE__ ) . '/media-upload-type-amazonjs.php'; } function amazonjs_init() { global $amazonjs; $amazonjs = new Amazonjs(); $amazonjs->init(); } function amazonjs_uninstall() { $amazonjs = new Amazonjs(); $amazonjs->clean(); unset($amazonjs); } add_action( 'init', 'amazonjs_init' ); if ( function_exists( 'register_uninstall_hook' ) ) { register_uninstall_hook( __FILE__, 'amazonjs_uninstall' ); }