/**
  * Performs an Amazon Product API request.
  * 
  * @since            2.0.2
  */
 protected function getRequest($iCount)
 {
     $_oAPI = new AmazonAutoLinks_ProductAdvertisingAPI($this->oUnitOption->get('country'), $this->oOption->get('authentication_keys', 'access_key'), $this->oOption->get('authentication_keys', 'access_key_secret'), $this->oUnitOption->get('associate_id'));
     // Perform the search for the first page regardless the specified count (number of items).
     // Keys with an empty value will be filtered out when performing the request.
     return $_oAPI->request($this->getAPIParameterArray($this->oUnitOption->get('Operation')), $this->oUnitOption->get('country'), $this->oUnitOption->get('cache_duration'));
 }
 /**
  * Performs an Amazon Product API request.
  * 
  * @since            2.0.2
  */
 protected function getRequest($iCount)
 {
     $_oAPI = new AmazonAutoLinks_ProductAdvertisingAPI($this->arrArgs['country'], $this->oOption->getAccessPublicKey(), $this->oOption->getAccessPrivateKey(), $this->arrArgs['associate_id']);
     // Perform the search for the first page regardless the specified count (number of items).
     // Keys with an empty value will be filtered out when performing the request.
     return $_oAPI->request($this->getAPIParameterArray($this->arrArgs['Operation']), '', $this->arrArgs['cache_duration']);
 }
 /**
  * Performs paged API requests.
  * 
  * This enables to retrieve more than 10 items. However, for it, it performs multiple requests, thus, it will be slow.
  * 
  * @since            2.0.1
  */
 protected function getRequest($iCount)
 {
     $_oAPI = new AmazonAutoLinks_ProductAdvertisingAPI($this->arrArgs['country'], $this->oOption->getAccessPublicKey(), $this->oOption->getAccessPrivateKey(), $this->arrArgs['associate_id']);
     // First, perform the search for the first page regardless the specified count (number of items).
     // Keys with an empty value will be filtered out when performing the request.
     $_aResponse = $_oAPI->request($this->getAPIParameterArray($this->arrArgs['Operation']), '', $this->arrArgs['cache_duration']);
     if ($iCount <= 10) {
         return $_aResponse;
     }
     // Check necessary key is set
     if (!isset($_aResponse['Items']['Item']) || !is_array($_aResponse['Items']['Item'])) {
         return $_aResponse;
     }
     // Calculate the required number of pages.
     $_iPage = $this->_getTotalPageNumber($iCount, $_aResponse, $this->arrArgs['SearchIndex']);
     $_aResponseTrunk = $_aResponse;
     // First perform fetching data in the background if caches are not available. Parse backwards
     $_fScheduled = null;
     for ($_i = $_iPage; $_i >= 2; $_i--) {
         $_fResult = $_oAPI->scheduleInBackground($this->getAPIParameterArray($this->arrArgs['Operation'], $_i));
         $_fScheduled = $_fScheduled ? $_fScheduled : $_fResult;
     }
     if ($_fScheduled) {
         // there are items scheduled to fetch in the background, do it right now.
         AmazonAutoLinks_Shadow::gaze();
     }
     // Start from the second page since the first page has been already done.
     for ($_i = 2; $_i <= $_iPage; $_i++) {
         $_aResponse = $_oAPI->request($this->getAPIParameterArray($this->arrArgs['Operation'], $_i), '', $this->arrArgs['cache_duration']);
         if (isset($_aResponse['Items']['Item']) && is_array($_aResponse['Items']['Item'])) {
             $_aResponseTrunk['Items']['Item'] = $this->_addItems($_aResponseTrunk['Items']['Item'], $_aResponse['Items']['Item']);
         }
     }
     return $_aResponseTrunk;
 }
 public function _replyToRenewAPITransients($arrRequestInfo)
 {
     $strLocale = $arrRequestInfo['locale'];
     $arrParams = $arrRequestInfo['parameters'];
     $oAmazonAPI = new AmazonAutoLinks_ProductAdvertisingAPI($strLocale, $GLOBALS['oAmazonAutoLinks_Option']->getAccessPublicKey(), $GLOBALS['oAmazonAutoLinks_Option']->getAccessPrivateKey());
     $oAmazonAPI->request($arrParams, $strLocale, null);
     // passing null will fetch the data right away and sets the cache.
 }
 /**
  * 
  * @callback        action        aal_action_api_transient_renewal
  */
 public function doAction()
 {
     $_oOption = AmazonAutoLinks_Option::getInstance();
     if (!$_oOption->isAPIConnected()) {
         return;
     }
     $_aParams = func_get_args() + array(null);
     $aRequestInfo = $_aParams[0];
     $_sLocale = $aRequestInfo['locale'];
     $_aParameters = $aRequestInfo['parameters'];
     $_oAmazonAPI = new AmazonAutoLinks_ProductAdvertisingAPI($_sLocale, $_oOption->get(array('authentication_keys', 'access_key'), ''), $_oOption->get(array('authentication_keys', 'access_key_secret'), ''));
     $_oAmazonAPI->request($_aParameters, $_sLocale, null);
 }
 /**
  * Extracts product data from the response data fetched from amazon server with the Product Advertising API.
  * @return      array
  * @see         http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html
  */
 private function _getProductData($sASIN, $sLocale, $sAssociateID)
 {
     $_oOption = AmazonAutoLinks_Option::getInstance();
     if (!$_oOption->isAPIConnected()) {
         return array();
     }
     $_sPublicKey = $_oOption->get(array('authentication_keys', 'access_key'), '');
     $_sPrivateKey = $_oOption->get(array('authentication_keys', 'access_key_secret'), '');
     if (empty($_sPublicKey) || empty($_sPrivateKey)) {
         return array();
     }
     // Construct API arguments
     $_aAPIArguments = array('Operation' => 'ItemLookup', 'Condition' => 'All', 'IdType' => 'ASIN', 'IncludeReviewsSummary' => "True", 'ItemId' => $sASIN, 'ResponseGroup' => 'Large');
     $_oAmazonAPI = new AmazonAutoLinks_ProductAdvertisingAPI($sLocale, $_sPublicKey, $_sPrivateKey, $sAssociateID);
     $_aRawData = $_oAmazonAPI->request($_aAPIArguments, $sLocale, 60);
     return $this->getElement($_aRawData, array('Items', 'Item'), array());
 }
 public function validation_aal_settings_authentication($arrInput, $arrOldInput)
 {
     // validation_ + page slug + tab slug
     $fVerified = true;
     $arrErrors = array();
     // Access Key must be 20 characters
     $arrInput['aal_settings']['authentication_keys']['access_key'] = trim($arrInput['aal_settings']['authentication_keys']['access_key']);
     $strPublicKey = $arrInput['aal_settings']['authentication_keys']['access_key'];
     if (strlen($strPublicKey) != 20) {
         $arrErrors['authentication_keys']['access_key'] = __('The Access Key ID must consist of 20 characters.', 'amazon-auto-links') . ' ';
         $fVerified = false;
     }
     // Access Secret Key must be 40 characters.
     $arrInput['aal_settings']['authentication_keys']['access_key_secret'] = trim($arrInput['aal_settings']['authentication_keys']['access_key_secret']);
     $strPrivateKey = $arrInput['aal_settings']['authentication_keys']['access_key_secret'];
     if (strlen($strPrivateKey) != 40) {
         $arrErrors['authentication_keys']['access_key_secret'] = __('The Secret Access Key must consist of 40 characters.', 'amazon-auto-links') . ' ';
         $fVerified = false;
     }
     // An invalid value is found.
     if (!$fVerified) {
         // Set the error array for the input fields.
         $this->setFieldErrors($arrErrors);
         $this->setSettingNotice(__('There was an error in your input.', 'amazon-auto-links'));
         return $arrOldInput;
     }
     // Test authentication - browse the Books node in amazon.com.
     $oAmazonAPI = new AmazonAutoLinks_ProductAdvertisingAPI('com', $strPublicKey, $strPrivateKey);
     if (!$oAmazonAPI->test()) {
         $arrErrors['authentication_keys']['access_key'] = __('Sent Value', 'amazon-auto-links') . ': ' . $strPublicKey;
         $arrErrors['authentication_keys']['access_key_secret'] = __('Sent Value', 'amazon-auto-links') . ': ' . $strPrivateKey;
         $this->setFieldErrors($arrErrors);
         $this->setSettingNotice(__('Failed authentication.', 'amazon-auto-links'));
         $arrOldInput;
     }
     return $arrInput;
 }
 protected function getNodeListByCategory($arrPreviousPageInput)
 {
     // Determine the locale.
     if (!empty($arrPreviousPageInput)) {
         $strLocale = $arrPreviousPageInput['country'];
     } else {
         if ($GLOBALS['strAmazonAutoLinks_UnitType'] == 'search') {
             $strLocale = get_post_meta($_GET['post'], 'country', true);
         } else {
             return array('error' => 'ERROR');
         }
     }
     // Prepare API object
     $strPublicKey = $GLOBALS['oAmazonAutoLinks_Option']->getAccessPublicKey();
     $strPrivateKey = $GLOBALS['oAmazonAutoLinks_Option']->getAccessPrivateKey();
     $oAmazonAPI = new AmazonAutoLinks_ProductAdvertisingAPI($strLocale, $strPublicKey, $strPrivateKey);
     // Now fetch the category node.
     $arrBrowseNodes = array();
     $arrNodeLabels = array(0 => __('All', 'amazon-auto-links'));
     foreach (AmazonAutoLinks_Properties::getRootNoeds($strLocale) as $arrNodeIDs) {
         $arrResult = $oAmazonAPI->request(array("Operation" => "BrowseNodeLookup", "BrowseNodeId" => implode(',', $arrNodeIDs)), $strLocale, 30);
         if (!isset($arrResult['BrowseNodes']['BrowseNode'])) {
             continue;
         }
         $arrBrowseNodes = array_merge($arrResult['BrowseNodes']['BrowseNode'], $arrBrowseNodes);
     }
     foreach ($arrBrowseNodes as $arrNode) {
         if (isset($arrNode['Ancestors'])) {
             $arrNode = $arrNode['Ancestors']['BrowseNode'];
         }
         $arrNodeLabels[$arrNode['BrowseNodeId']] = $arrNode['Name'];
     }
     // AmazonAutoLinks_Debug::logArray( $arrNodeLabels );
     return $arrNodeLabels;
 }
 /**
  * 
  * @since       3
  * @return      boolean
  */
 private function _isConnected($sPublicKey = '', $sPrivateKey = '')
 {
     $_oOption = AmazonAutoLinks_Option::getInstance();
     $_oAmazonAPI = new AmazonAutoLinks_ProductAdvertisingAPI('US', $sPublicKey ? $sPublicKey : $_oOption->get(array('authentication_keys', 'access_key'), ''), $sPrivateKey ? $sPrivateKey : $_oOption->get(array('authentication_keys', 'access_key_secret'), ''));
     return (bool) $_oAmazonAPI->test();
 }
 public function validation_aal_add_search_unit_initial_search_settings($aInput, $aOldInput)
 {
     // validation_{page slug}_{tab slug}
     $fVerified = true;
     $arrErrors = array();
     $arrSearchOptions = $aInput['aal_add_search_unit']['search'];
     // Check the limitation.
     if ($this->oOption->isUnitLimitReached()) {
         $this->setFieldErrors(array('error'));
         // must set an field error array which does not yield empty so that it won't be redirected.
         $this->setSettingNotice(sprintf(__('Please upgrade to <A href="%1$s">Pro</a> to add more units! Make sure to empty the <a href="%2$s">trash box</a> to delete the units completely!', 'amazon-auto-links'), 'http://en.michaeluno.jp/amazon-auto-links-pro/', admin_url('edit.php?post_status=trash&post_type=' . AmazonAutoLinks_Commons::PostTypeSlug)));
         return $aOldInput;
     }
     // If the Access Key fields are present, it means the user has not set them yet in the Settings page.
     // In this case, just check if they are valid and if so, save them in the settings' option array. Otherwise, return an error.
     if (isset($arrSearchOptions['search_access_key'], $arrSearchOptions['search_access_key_secret'])) {
         $strPublicKey = $arrSearchOptions['search_access_key'];
         if (strlen($strPublicKey) != 20) {
             $arrErrors['search']['search_access_key'] = __('The Access Key ID must consist of 20 characters.', 'amazon-auto-links') . ': ' . $strPublicKey . ' ';
             $fVerified = false;
         }
         $strPrivateKey = $arrSearchOptions['search_access_key_secret'];
         if (strlen($strPrivateKey) != 40) {
             $arrErrors['search']['search_access_key_secret'] = __('The Secret Access Key must consist of 40 characters.', 'amazon-auto-links') . ': ' . $strPrivateKey . ' ';
             $fVerified = false;
         }
         // An invalid value is found.
         if (!$fVerified) {
             // Set the error array for the input fields.
             $this->setFieldErrors($arrErrors);
             $this->setSettingNotice(__('There was an error in your input.', 'amazon-auto-links'));
             return $aOldInput;
         }
         // Test authentication - browse the Books node in amazon.com.
         $oAmazonAPI = new AmazonAutoLinks_ProductAdvertisingAPI('com', $strPublicKey, $strPrivateKey);
         if (!$oAmazonAPI->test()) {
             $arrErrors['search']['search_access_key'] = __('Sent Value', 'amazon-auto-links') . ': ' . $strPublicKey;
             $arrErrors['search']['search_access_key_secret'] = __('Sent Value', 'amazon-auto-links') . ': ' . $strPrivateKey;
             $this->setFieldErrors($arrErrors);
             $this->setSettingNotice(__('Failed authentication.', 'amazon-auto-links'));
             $aOldInput;
         }
         // It is authenticated, so set the keys in the Settings option array.
         // Since the validation_ callbacks internally merge with the framework's property option array,
         // modify the property array, NOT the option object that plugin creates.
         $this->oProps->arrOptions['aal_settings']['authentication_keys']['access_key'] = $strPublicKey;
         $this->oProps->arrOptions['aal_settings']['authentication_keys']['access_key_secret'] = $strPrivateKey;
     }
     if (empty($arrSearchOptions['search_associate_id'])) {
         $arrErrors['search']['search_associate_id'] = __('The associate ID cannot be empty.', 'amazon-auto-links');
         $fVerified = false;
     }
     // An invalid value is found.
     if (!$fVerified) {
         // Set the error array for the input fields.
         $this->setFieldErrors($arrErrors);
         $this->setSettingNotice(__('There was an error in your input.', 'amazon-auto-links'));
         return $aOldInput;
     }
     // Drop the sections.
     $_aNewFields = array();
     foreach ($aInput['aal_add_search_unit'] as $strSection => $arrFields) {
         $_aNewFields = $_aNewFields + $arrFields;
     }
     // Remove the search_ prefix in the keys.
     $_aSanitizedFields = array();
     foreach ($_aNewFields as $strKey => $vValue) {
         $_aSanitizedFields[preg_replace('/^search_/', '', $strKey)] = $vValue;
     }
     // Set the unit type based on the chosen one.
     // Redirect to the appropriate page by the search type.
     switch ($_aSanitizedFields['Operation']) {
         case 'ItemSearch':
             $_aSanitizedFields['unit_type'] = 'search';
             $sTabSlug = 'search_products';
             break;
         case 'ItemLookup':
             $_aSanitizedFields['unit_type'] = 'item_lookup';
             $sTabSlug = 'item_lookup';
             break;
         case 'SimilarityLookup':
             $_aSanitizedFields['unit_type'] = 'similarity_lookup';
             $sTabSlug = 'similarity_lookup';
             break;
     }
     // Save the transient
     $arrTempUnitOptions = (array) AmazonAutoLinks_WPUtilities::getTransient('AAL_CreateUnit_' . $_aSanitizedFields['transient_id']);
     $aSavingUnitOptions = AmazonAutoLinks_Utilities::uniteArrays($_aSanitizedFields, $arrTempUnitOptions);
     AmazonAutoLinks_WPUtilities::setTransient('AAL_CreateUnit_' . $_aSanitizedFields['transient_id'], $aSavingUnitOptions, 60 * 10 * 6 * 24);
     // Go to the next page.
     die(wp_redirect(add_query_arg(array('tab' => $sTabSlug, 'transient_id' => $_aSanitizedFields['transient_id']) + $_GET, $_aSanitizedFields['bounce_url'])));
 }