/**
  * 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)));
 }
 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'))));
 }
 protected function importUnit($arrV1Unit, $strDefaultTemplateID)
 {
     $arrV2UnitOptions = array('unit_type' => 'category', 'template_id' => $strDefaultTemplateID, 'unit_title' => $arrV1Unit['unitlabel'], 'count' => $arrV1Unit['numitems'], 'column' => $arrV1Unit['numberofcolumns'], 'country' => $arrV1Unit['country'], 'associate_id' => $arrV1Unit['associateid'], 'image_size' => $arrV1Unit['imagesize'], 'sort' => $arrV1Unit['sortorder'], 'keep_raw_title' => $arrV1Unit['keep_raw_title']['keeprawtitle'], 'feed_type' => array('bestsellers' => $arrV1Unit['adtypes']['bestsellers']['check'], 'new-releases' => $arrV1Unit['adtypes']['hotnewreleases']['check'], 'movers-and-shakers' => $arrV1Unit['adtypes']['moverandshakers']['check'], 'top-rated' => $arrV1Unit['adtypes']['toprated']['check'], 'most-wished-for' => $arrV1Unit['adtypes']['mostwishedfor']['check'], 'most-gifted' => $arrV1Unit['adtypes']['giftideas']['check']), 'ref_nosim' => $arrV1Unit['nosim'], 'title_length' => $arrV1Unit['titlelength'], 'link_style' => $arrV1Unit['linkstyle'], 'credit_link' => $arrV1Unit['credit'], 'categories' => $this->formatCategories($arrV1Unit['categories']), 'categories_exclude' => $this->formatCategories($arrV1Unit['blacklist_categories']));
     // Insert the post
     $intPostID = AmazonAutoLinks_Option::insertPost($arrV2UnitOptions, AmazonAutoLinks_Commons::PostTypeSlug, array(), array('unit_title'));
     if (!$intPostID) {
         return false;
     }
     // Insert the term
     $vTerm = wp_insert_term($arrV1Unit['unitlabel'], AmazonAutoLinks_Commons::TagSlug);
     // if ( ! is_wp_error( $vTerm ) && isset( $vTerm['term_id'], $vTerm['term_taxonomy_id'] ) )        // $vTarm = array('term_id'=>12,'term_taxonomy_id'=>34)) or WP_Error
     // The term already exists, in that case, try retreiving the term id from the term name.
     if (is_wp_error($vTerm)) {
         $vTerm = get_term_by('name', $arrV1Unit['unitlabel'], AmazonAutoLinks_Commons::TagSlug, 'ARRAY_A');
     }
     if (isset($vTerm['term_id'])) {
         wp_set_object_terms($intPostID, isset($vTerm['slug']) ? $vTerm['slug'] : $vTerm['term_id'], AmazonAutoLinks_Commons::TagSlug);
     }
     // Add an auto-insert definition ( custom post type)
     if (array_filter($arrV1Unit['insert'])) {
         // if at least one item is checked,
         AmazonAutoLinks_Option::insertPost($this->composeAutoInsertOptions($intPostID, $arrV1Unit['insert'], $arrV1Unit['disableonhome'], trim($arrV1Unit['poststobedisabled'])), AmazonAutoLinks_Commons::PostTypeSlugAutoInsert);
     }
     return true;
 }
 /**
  * Creates a post of amazon_auto_links custom post type with unit option meta fields.
  * 
  */
 protected function postUnitByCategory($arrUnitOptions)
 {
     // Create a custom post if it's a new unit.
     if (!isset($_GET['post']) || !$_GET['post']) {
         $intPostID = wp_insert_post(array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => $GLOBALS['user_ID'], 'post_title' => $arrUnitOptions['unit_title'], 'post_status' => 'publish', 'post_type' => AmazonAutoLinks_Commons::PostTypeSlug));
     }
     // Add meta fields.
     $intPostID = $arrUnitOptions['mode'] == 1 ? $intPostID : $_GET['post'];
     if ($arrUnitOptions['mode'] != 1) {
         // if not New
         unset($arrUnitOptions['auto_insert']);
     }
     unset($arrUnitOptions['unit_title']);
     foreach ($arrUnitOptions as $strFieldID => $vValue) {
         update_post_meta($intPostID, $strFieldID, $vValue);
     }
     // Create an auto insert - the 'auto_insert' key will be removed when creating a post.s
     if (isset($arrUnitOptions['auto_insert']) && $arrUnitOptions['auto_insert'] && $arrUnitOptions['mode'] == 1) {
         $arrAutoInsertOptions = array('unit_ids' => array($intPostID)) + AmazonAutoLinks_Form_AutoInsert::$arrStructure_AutoInsertOptions;
         AmazonAutoLinks_Option::insertPost($arrAutoInsertOptions, AmazonAutoLinks_Commons::PostTypeSlugAutoInsert);
     }
 }
 /**
  * Creates a search unit type
  * 
  * @since            2.0.2
  */
 protected function _createSearchUnit($aInput)
 {
     // Drop the sections.
     $_aNewFields = array();
     foreach ($aInput['aal_add_search_unit'] as $_sSection => $_aFields) {
         $_aNewFields = $_aNewFields + $_aFields;
     }
     // Remove the search_ prefix in the keys.
     $_aSanitizedFields = array();
     foreach ($_aNewFields as $_sKey => $_vValue) {
         $_aSanitizedFields[preg_replace('/^search\\d_/', '', $_sKey)] = $_vValue;
     }
     $_aSanitizedFields = $this->oOption->sanitizeUnitOpitons($_aSanitizedFields);
     // Create a post.
     $_fDoAutoInsert = $_aSanitizedFields['auto_insert'];
     unset($_aSanitizedFields['auto_insert']);
     $_iNewPostID = AmazonAutoLinks_Option::insertPost($_aSanitizedFields);
     // Create an auto insert
     if ($_fDoAutoInsert) {
         $_aAutoInsertOptions = array('unit_ids' => array($_iNewPostID)) + AmazonAutoLinks_Form_AutoInsert::$arrStructure_AutoInsertOptions;
         AmazonAutoLinks_Option::insertPost($_aAutoInsertOptions, AmazonAutoLinks_Commons::PostTypeSlugAutoInsert);
     }
     die(wp_redirect(add_query_arg(array('post_type' => AmazonAutoLinks_Commons::PostTypeSlug, 'action' => 'edit', 'post' => $_iNewPostID), admin_url('post.php'))));
 }