*/
// 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 = 'affiliate-amazon-block(aab)';
$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' => __( "affiliate-amazon-block(aab) 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, affiliate-amazon-block(aab) will output html by using 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 ) ?><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, affiliate-amazon-block(aab) 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, affiliate-amazon-block(aab) will call _gaq.push(["_trackEvent", "affiliate-amazon-block(aab)", "Click", "ASIN TITLE"]) or ga("send", "event", "affiliate-amazon-block(aab)", "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']}
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 = <<
text_domain ), $this->title ); ?>