*/ /* Plugin Name: Media Libraries: Amazon Provider Version: 1.3.2 Plugin URI: http://impleri.net/development/media_libraries/ Description: Provides access to Amazon for adding products to Media Libraries. Author: Christopher Roussel Author URI: http://impleri.net */ /** * AmazonECS wrapper * * acts as a simple middle layer between AML and AmazonECS. * @static */ class ml_amazon { /** * @var Amazon domains that can be searched */ public static $domains = array( 'com' => 'United States', 'co.uk' => 'United Kingdom', 'de' => 'Germany', 'co.jp' => 'Japan', 'fr' => 'France', 'ca' => 'Canada', 'it' => 'Italy', 'cn' => 'China', 'es' => 'Spain', ); /** * @var accepted Amazon categories */ public static $categories = array( 'b' => 'Books', 'v' => 'DVD', 'm' => 'Music', 'g' => 'VideoGames', ); /** * @var image sizes */ public static $image_sizes = array( 'sm' => '._SL75_', 'med' => '._SL110_', 'lg' => '._SL160_', 'or' => '', ); /** * @var image size texts */ public static $img_size_text = array( 'sm' => 'Small', 'med' => 'Medium', 'lg' => 'Large', 'or' => 'Original', ); /** * @var url prefix for Amazon images */ private static $image_base = 'http://ecx.images-amazon.com/images/I/'; /** * @var template for html error messages */ private static $error = '
' . __('The following settings determine what Media Libraries will retrieve from Amazon.', 'media-libraries') . '
'; } /** * Amazon AWS key field */ function ml_amz_key_field() { ml_text_field('ml_amz_key', sprintf(__('Required to add products from Amazon. It is free to sign up. Register here.', 'media-libraries'), 'https://aws-portal.amazon.com/gp/aws/developer/registration/index.html') ); } /** * Amazon AWS secret field */ function ml_amz_secret_field() { ml_text_field('ml_amz_secret', sprintf(__('Required to add products from Amazon. Found at the same site as above. Register here.', 'media-libraries'), 'https://aws-portal.amazon.com/gp/aws/developer/registration/index.html') ); } /** * Amazon associate id field */ function ml_amz_assoc_field() { ml_text_field('ml_amz_assoc', sprintf(__('Required to add products from Amazon. If your visitors purchase products, you will get commission on their purchases. Register here.', 'media-libraries'), 'http://associates.amazon.com/') ); } /** * Amazon domain field */ function ml_amz_domain_field() { ml_select_field('ml_amz_domain', ml_amazon::$domains, __('Country-specific Amazon site to use for searching and product links', 'media-libraries')); echo '' . __('NB: If you have country-specific books in your catalogue and then change your domain setting, some old links might stop working.', 'media-libraries') . '
'; } /** * Amazon image size field * @todo remove NB when image auto-size working */ function ml_amz_image_field() { ml_select_field('ml_amz_image', ml_amazon::$img_size_text, __('NB: This change will only be applied to products you add from this point onwards.')); } /** * default amazon options */ function ml_amazon_defaults ($options) { $options['ml_amz_key'] = ''; $options['ml_amz_secret'] = ''; $options['ml_amz_assoc'] = ''; $options['ml_amz_domain'] = 'com'; $options['ml_amz_image'] = 'med'; return $options; } /** * validates posted options for amazon * * @param array $_POST data passed from WP via ml_options_validate() * @return array validated options */ function ml_amazon_validate ($valid, $ml_post) { $options = get_option('ml_options'); // Amazon fields $valid['ml_amz_key'] = ($ml_post['ml_amz_key']) ? sanitize_text_field($ml_post['ml_amz_key']) : null; $valid['ml_amz_secret'] = ($ml_post['ml_amz_secret']) ? sanitize_text_field($ml_post['ml_amz_secret']) : null; $valid['ml_amz_assoc'] = ($ml_post['ml_amz_assoc']) ? sanitize_text_field($ml_post['ml_amz_assoc']) : null; $valid['ml_amz_domain'] = ($ml_post['ml_amz_domain']) ? sanitize_text_field($ml_post['ml_amz_domain']) : null; $valid['ml_amz_image'] = in_array($ml_post['ml_amz_image'], array('sm', 'med', 'lg')) ? $ml_post['ml_amz_image'] : null; // Throw an error if no AWS info if (empty($valid['ml_amz_key'])) { add_settings_error('ml_options', 'media-libraries', __('Amazon WDSL requires an Amazon AWS key!', 'media-libraries')); } if (empty($valid['ml_amz_secret'])) { add_settings_error('ml_options', 'media-libraries', __('Amazon WDSL requires the Amazon AWS secret attached to an AWS key!', 'media-libraries')); } if (empty($valid['ml_amz_assoc'])) { add_settings_error('ml_options', 'media-libraries', __('Amazon WDSL requires an Amazon Associates ID!', 'media-libraries')); } return $valid; } /** * checks if required options (aws keys) need to be set * * @return bool true if necessary options are valid */ function ml_amazon_check ($pass) { if ($pass == true) { $aws_key = ml_get_option('ml_amz_key'); $aws_secret = ml_get_option('ml_amz_secret'); $aws_associate = ml_get_option('ml_amz_assoc'); $pass = (!(empty($aws_key) || empty($aws_secret) || empty($aws_associate))); } return $pass; } /** * register amazon options with WP settings api */ function ml_amazon_init_options() { add_settings_section('ml_options_amazon', __('Amazon Settings', 'media-libraries'), 'ml_options_amazon', 'ml_options'); // Amazon field definitions add_settings_field('ml_amz_key', __('Amazon Web Services Access Key ID', 'media-libraries'), 'ml_amz_key_field', 'ml_options', 'ml_options_amazon'); add_settings_field('ml_amz_secret', __('Amazon Web Services Secret Access Key', 'media-libraries'), 'ml_amz_secret_field', 'ml_options', 'ml_options_amazon'); add_settings_field('ml_amz_assoc', __('Your Amazon Associates ID', 'media-libraries'), 'ml_amz_assoc_field', 'ml_options', 'ml_options_amazon'); add_settings_field('ml_amz_domain', __('Amazon domain to use', 'media-libraries'), 'ml_amz_domain_field', 'ml_options', 'ml_options_amazon'); add_settings_field('ml_amz_image', __('Image size to use', 'media-libraries'), 'ml_amz_image_field', 'ml_options', 'ml_options_display'); } function ml_amazon_init() { add_filter('ml-check-init', 'ml_amazon_check'); add_filter('ml-default-options', 'ml_amazon_defaults'); add_filter('ml-options-validate', 'ml_amazon_validate', 10, 2); add_filter('ml-do-search', array('ml_amazon', 'search)', 10, 3)); } add_action('ml-options-init', 'ml_amazon_init_options'); add_action('init', 'ml_amazon_init');