/**
  * Sanitizes the unit options of the item_lookup unit type.
  * 
  * @since            2.0.2
  */
 protected function sanitizeUnitOptions_ItemLookUp(array &$aUnitOptions)
 {
     // if the ISDN is spceified, the search index must be set to Books.
     if (isset($aUnitOptions['IdType'], $aUnitOptions['SearchIndex']) && $aUnitOptions['IdType'] == 'ISBN') {
         $aUnitOptions['SearchIndex'] = 'Books';
     }
     $aUnitOptions['ItemId'] = trim(AmazonAutoLinks_Utilities::trimDelimitedElements($aUnitOptions['ItemId'], ','));
 }
 /**
  * The define Auto Insert page.
  */
 public function validation_aal_define_auto_insert($arrInput, $arrOldInput)
 {
     // Drop the sections.
     $arrNewFields = array();
     foreach ($arrInput['aal_define_auto_insert'] as $strSection => $arrFields) {
         $arrNewFields = $arrNewFields + $arrFields;
     }
     // Remove the search_ prefix in the keys.
     $arrSanitizedFields = array();
     foreach ($arrNewFields as $strKey => $vValue) {
         $arrSanitizedFields[preg_replace('/^autoinsert_/', '', $strKey)] = $vValue;
     }
     $fVerified = true;
     $arrErrors = array();
     // Check necessary settings.
     if (!array_filter($arrSanitizedFields['built_in_areas'] + $arrSanitizedFields['static_areas']) && !$arrSanitizedFields['filter_hooks'] && !$arrSanitizedFields['action_hooks']) {
         $arrErrors['autoinsert_area']['autoinsert_built_in_areas'] = __('At least one area must be set.', 'amazon-auto-links');
         $arrErrors['autoinsert_static_insertion']['autoinsert_static_areas'] = __('At least one area must be set.', 'amazon-auto-links');
         $arrErrors['autoinsert_area']['autoinsert_filter_hooks'] = __('At least one area must be set.', 'amazon-auto-links');
         $arrErrors['autoinsert_area']['autoinsert_action_hooks'] = __('At least one area must be set.', 'amazon-auto-links');
         $fVerified = false;
     }
     if (!isset($arrSanitizedFields['unit_ids'])) {
         // if no item is selected, the select input with the multiple attribute does not send the key.
         $arrErrors['autoinsert_area']['autoinsert_unit_ids'] = __('A unit must be selected.', '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;
     }
     $arrSanitizedFields['filter_hooks'] = AmazonAutoLinks_Utilities::trimDelimitedElements($arrSanitizedFields['filter_hooks'], ',');
     $arrSanitizedFields['action_hooks'] = AmazonAutoLinks_Utilities::trimDelimitedElements($arrSanitizedFields['action_hooks'], ',');
     $arrSanitizedFields['enable_post_ids'] = AmazonAutoLinks_Utilities::trimDelimitedElements($arrSanitizedFields['enable_post_ids'], ',');
     $arrSanitizedFields['diable_post_ids'] = AmazonAutoLinks_Utilities::trimDelimitedElements($arrSanitizedFields['diable_post_ids'], ',');
     // AmazonAutoLinks_Debug::logArray( $arrSanitizedFields );
     // Edit - Update the post.
     $fIsEdit = isset($_POST['mode'], $_POST['post']) && $_POST['post'] && $_POST['mode'] == 'edit';
     if ($fIsEdit) {
         AmazonAutoLinks_Option::updatePostMeta($_POST['post'], $arrSanitizedFields);
     } else {
         // New - Create a post.
         $intNewPostID = AmazonAutoLinks_Option::insertPost($arrSanitizedFields, AmazonAutoLinks_Commons::PostTypeSlugAutoInsert);
     }
     // e.g. http://.../wp-admin/edit.php?post_type=aal_auto_insert
     die(wp_redirect($fIsEdit ? admin_url('edit.php?post_type=' . AmazonAutoLinks_Commons::PostTypeSlug . '&page=aal_define_auto_insert&mode=edit&post=' . $_POST['post']) : admin_url('edit.php?post_type=' . AmazonAutoLinks_Commons::PostTypeSlugAutoInsert)));
 }
 /**
  * 
  * 'Operation' => 'ItemSearch',    // ItemSearch, ItemLookup, SimilarityLookup
  * @since   2.0.2
  * @see     http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html
  */
 protected function getAPIParameterArray($sOperation = 'ItemSearch', $iItemPage = null)
 {
     $_bIsIndexAllOrBlended = 'All' === $this->arrArgs['SearchIndex'] || 'Blended' === $this->arrArgs['SearchIndex'];
     $_aParams = array('Keywords' => AmazonAutoLinks_Utilities::trimDelimitedElements($this->arrArgs['Keywords'], ',', false), 'Title' => $_bIsIndexAllOrBlended ? null : AmazonAutoLinks_Utilities::trimDelimitedElements($this->arrArgs['Title'], ',', false), 'Operation' => $this->arrArgs['Operation'], 'SearchIndex' => $this->arrArgs['SearchIndex'], $this->arrArgs['search_by'] => $this->arrArgs['additional_attribute'] ? $this->arrArgs['additional_attribute'] : null, 'Sort' => $_bIsIndexAllOrBlended ? null : $this->arrArgs['Sort'], 'ResponseGroup' => "Large", 'BrowseNode' => !$_bIsIndexAllOrBlended && isset($this->arrArgs['BrowseNode']) && $this->arrArgs['BrowseNode'] ? $this->arrArgs['BrowseNode'] : null, 'Availability' => isset($this->arrArgs['Availability']) && $this->arrArgs['Availability'] ? 'Available' : null, 'Condition' => $_bIsIndexAllOrBlended ? null : $this->arrArgs['Condition'], 'IncludeReviewsSummary' => "True", 'MaximumPrice' => !$_bIsIndexAllOrBlended && $this->arrArgs['MaximumPrice'] ? $this->arrArgs['MaximumPrice'] : null, 'MinimumPrice' => !$_bIsIndexAllOrBlended && $this->arrArgs['MinimumPrice'] ? $this->arrArgs['MinimumPrice'] : null, 'MinPercentageOff' => $this->arrArgs['MinPercentageOff'] ? $this->arrArgs['MinPercentageOff'] : null, 'MerchantId' => 'Amazon' === $this->arrArgs['MerchantId'] ? $this->arrArgs['MerchantId'] : null, 'MarketplaceDomain' => 'Marketplace' === $this->arrArgs['SearchIndex'] ? AmazonAutoLinks_Properties::getMarketplaceDomainByLocale($this->arrArgs['country']) : null);
     $_aParams = $iItemPage ? $_aParams + array('ItemPage' => $iItemPage) : $_aParams;
     return $_aParams;
 }
 public function validation_aal_add_tag_unit($arrInput, $arrOldInput)
 {
     // validation + _ + page slug + tab slug
     $fVerified = true;
     $arrErrors = array();
     // Check the limitation.
     if ($this->oOption->isUnitLimitReached()) {
         $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 $arrOldInput;
     }
     // Customer ID must be 13 characters
     if ($arrInput['aal_add_tag_unit']['tag']['tag_customer_id'] && strlen($arrInput['aal_add_tag_unit']['tag']['tag_customer_id']) != 13) {
         $arrErrors['tag']['tag_customer_id'] = __('The customer ID must consist of 13 characters.', 'amazon-auto-links') . ' ';
         $arrInput['aal_add_tag_unit']['tag']['tag_customer_id'] = '';
         $fVerified = false;
     }
     if (empty($arrInput['aal_add_tag_unit']['tag']['tag_tags']) && empty($arrInput['aal_add_tag_unit']['tag']['tag_customer_id'])) {
         $arrErrors['tag']['tag_tags'] = __('Either tags or customer ID has to be entered.', 'amazon-auto-links');
         $strMessage = __('Either tags or customer ID has to be entered.', 'amazon-auto-links');
         $arrErrors['tag']['tag_customer_id'] = isset($arrErrors['tag']['tag_customer_id']) ? $arrErrors['tag']['tag_customer_id'] . $strMessage : $strMessage;
         $fVerified = false;
     }
     if (empty($arrInput['aal_add_tag_unit']['tag']['tag_associate_id'])) {
         $arrErrors['tag']['tag_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 $arrOldInput;
     }
     // Drop the sections.
     $arrNewFields = array();
     foreach ($arrInput['aal_add_tag_unit'] as $strSection => $arrFields) {
         $arrNewFields = $arrNewFields + $arrFields;
     }
     $arrSanitizedFields = array();
     // Remove the tag_ prefix in the keys.
     foreach ($arrNewFields as $strKey => $vValue) {
         $arrSanitizedFields[preg_replace('/^tag_/', '', $strKey)] = $vValue;
     }
     // Sanitize the tag input
     $arrSanitizedFields['tags'] = trim(AmazonAutoLinks_Utilities::trimDelimitedElements($arrSanitizedFields['tags'], ','));
     $arrSanitizedFields = $this->oOption->sanitizeUnitOpitons($arrSanitizedFields);
     // If nothing is checked for the feed type, enable the bestseller item.
     if (!array_filter($arrSanitizedFields['feed_type'])) {
         $arrSanitizedFields['feed_type']['new'] = true;
     }
     // AmazonAutoLinks_Debug::logArray( '--Before Escaping KSES Filter--' );
     // AmazonAutoLinks_Debug::logArray( $arrSanitizedFields['item_format'] );
     // AmazonAutoLinks_Debug::logArray( $arrSanitizedFields['image_format'] );
     // AmazonAutoLinks_Debug::logArray( $arrSanitizedFields['title_format'] );
     // Apply allowed HTML tags for the KSES filter.
     add_filter('safe_style_css', array($this, 'allowInlineStyleMaxWidth'));
     $arrAllowedHTMLTags = AmazonAutoLinks_Utilities::convertStringToArray($this->oOption->arrOptions['aal_settings']['form_options']['allowed_html_tags'], ',');
     $arrSanitizedFields['item_format'] = AmazonAutoLinks_WPUtilities::escapeKSESFilter($arrSanitizedFields['item_format'], $arrAllowedHTMLTags);
     $arrSanitizedFields['image_format'] = AmazonAutoLinks_WPUtilities::escapeKSESFilter($arrSanitizedFields['image_format'], $arrAllowedHTMLTags);
     $arrSanitizedFields['title_format'] = AmazonAutoLinks_WPUtilities::escapeKSESFilter($arrSanitizedFields['title_format'], $arrAllowedHTMLTags);
     remove_filter('safe_style_css', array($this, 'allowInlineStyleMaxWidth'));
     // AmazonAutoLinks_Debug::logArray( '--After Escaping KSES Filter--' );
     // AmazonAutoLinks_Debug::logArray( $arrAllowedHTMLTags );
     // AmazonAutoLinks_Debug::logArray( $arrSanitizedFields['item_format'] );
     // AmazonAutoLinks_Debug::logArray( $arrSanitizedFields['image_format'] );
     // AmazonAutoLinks_Debug::logArray( $arrSanitizedFields['title_format'] );
     // Create a post.
     $fDoAutoInsert = $arrSanitizedFields['auto_insert'];
     unset($arrSanitizedFields['auto_insert']);
     $intNewPostID = AmazonAutoLinks_Option::insertPost($arrSanitizedFields);
     // Create an auto insert
     if ($fDoAutoInsert) {
         $arrAutoInsertOptions = array('unit_ids' => array($intNewPostID)) + AmazonAutoLinks_Form_AutoInsert::$arrStructure_AutoInsertOptions;
         AmazonAutoLinks_Option::insertPost($arrAutoInsertOptions, AmazonAutoLinks_Commons::PostTypeSlugAutoInsert);
     }
     die(wp_redirect(add_query_arg(array('post_type' => AmazonAutoLinks_Commons::PostTypeSlug, 'action' => 'edit', 'post' => $intNewPostID), admin_url('post.php'))));
 }
 /**
  * 
  *
    [aal_settings] => Array
     (
         [product_filters] => Array
             (
                 [white_list] => Array
                     (
                         [asin] => 
                         [title] => 
                         [description] => 
                     )
 
                 [black_list] => Array
                     (
                         [asin] => 
                         [title] => 
                         [description] => 
                     )
 
             )
 
         [support] => Array
             (
                 [rate] => 10
                 [review] => 0
             )
 
         [query] => Array
             (
                 [cloak] => productlink
                 [submit_general] => Save Changes
             )
 
     )     
 */
 public function validation_aal_settings_general($arrInput, $arrOldInput)
 {
     // Sanitize text inputs
     foreach ($arrInput['aal_settings']['product_filters']['black_list'] as &$str1) {
         $str1 = trim(AmazonAutoLinks_Utilities::trimDelimitedElements($str1, ','));
     }
     foreach ($arrInput['aal_settings']['product_filters']['white_list'] as &$str2) {
         $str2 = trim(AmazonAutoLinks_Utilities::trimDelimitedElements($str2, ','));
     }
     // Sanitize the query key.
     $arrInput['aal_settings']['query']['cloak'] = AmazonAutoLinks_Utilities::sanitizeCharsForURLQueryKey($arrInput['aal_settings']['query']['cloak']);
     // Sanitize the custom preview slug.
     $_sCustomPreviewPostTypeSlug = AmazonAutoLinks_Utilities::getTrancatedString($arrInput['aal_settings']['unit_preview']['preview_post_type_slug'], 20, '');
     $_sCustomPreviewPostTypeSlug = AmazonAutoLinks_Utilities::sanitizeCharsForURLQueryKey($_sCustomPreviewPostTypeSlug);
     $arrInput['aal_settings']['unit_preview']['preview_post_type_slug'] = $_sCustomPreviewPostTypeSlug;
     return $arrInput;
 }