/**
  * 
  * @callback        filter      validation + _ + page slug
  */
 public function validate($aInput, $aOldInput, $oFactory, $aSubmitInfo)
 {
     $_bVerified = true;
     $_aErrors = array();
     $_oOption = AmazonAutoLinks_Option::getInstance();
     $_oTemplateOption = AmazonAutoLinks_TemplateOption::getInstance();
     $_oUtil = new AmazonAutoLinks_PluginUtility();
     // Check the limitation.
     if ($_oOption->isUnitLimitReached()) {
         $oFactory->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_Registry::$aPostTypes['unit'])));
         return $aOldInput;
     }
     // If specified and the customer ID is not 13 characters,
     $aInput['customer_id'] = trim($aInput['customer_id']);
     if ($aInput['customer_id'] && strlen($aInput['customer_id']) != 13) {
         $_aErrors['customer_id'] = __('The customer ID must consist of 13 characters.', 'amazon-auto-links') . ' ';
         $_bVerified = false;
     }
     if (empty($aInput['tags']) && empty($aInput['customer_id'])) {
         $_aErrors['tags'] = __('Either tags or customer ID has to be entered.', 'amazon-auto-links');
         $_sMessage = __('Either tags or customer ID has to be entered.', 'amazon-auto-links');
         $_aErrors['customer_id'] = isset($_aErrors['customer_id']) ? $_aErrors['customer_id'] . $_sMessage : $_sMessage;
         $_bVerified = false;
     }
     if (empty($aInput['associate_id'])) {
         $_aErrors['associate_id'] = __('The associate ID cannot be empty.', 'amazon-auto-links');
         $_bVerified = false;
     }
     // An invalid value is found.
     if (!$_bVerified) {
         // Set the error array for the input fields.
         $oFactory->setFieldErrors($_aErrors);
         $oFactory->setSettingNotice(__('There was an error in your input.', 'amazon-auto-links'));
         return $aInput;
     }
     // Sanitize the tag input
     $aInput['tags'] = trim($_oUtil->trimDelimitedElements($aInput['tags'], ','));
     $_bDoAutoInsert = $aInput['auto_insert'];
     // Format the unit options to sanitize the data.
     $_oUnitOptions = new AmazonAutoLinks_UnitOption_tag(null, $aInput);
     $aInput = $_oUnitOptions->get();
     $aInput['template_id'] = $_oTemplateOption->getDefaultTemplateIDByUnitType('tag');
     // Create a unit post
     $_iNewPostID = $_oUtil->insertPost($aInput, AmazonAutoLinks_Registry::$aPostTypes['unit']);
     // Create an auto insert
     if ($_bDoAutoInsert) {
         $_oUtil->createAutoInsert($_iNewPostID);
     }
     // Clean the temporary form options data.
     $_oUtil->deleteTransient($GLOBALS['aal_transient_id']);
     // Schedule pre-fetching the unit feed in the background
     // so that by the time the user opens the unit page, the cache will be ready.
     AmazonAutoLinks_Event_Scheduler::prefetch($_iNewPostID);
     // Store the inputs for the next time.
     update_option(AmazonAutoLinks_Registry::$aOptionKeys['last_input'], $aInput, false);
     // Go to the post editing page and exit. This way the framework won't create a new form transient row.
     $_oUtil->goToPostDefinitionPage($_iNewPostID, AmazonAutoLinks_Registry::$aPostTypes['unit']);
     // This won't be reached.
     return $aInput;
 }
 /**
  * Creates a post of amazon_auto_links custom post type with unit option meta fields.
  * 
  * @return      integer     the post(unit) id.
  */
 private function _postUnitByCategory($aUnitOptions, $aOptions)
 {
     $_iPostID = 0;
     // Create a custom post if it's a new unit.
     if (!isset($_GET['post']) || !$_GET['post']) {
         $_iPostID = wp_insert_post(array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => $GLOBALS['user_ID'], 'post_title' => $aOptions['unit_title'], 'post_status' => 'publish', 'post_type' => AmazonAutoLinks_Registry::$aPostTypes['unit']));
     }
     // Add meta fields.
     $_iPostID = 1 == $aOptions['mode'] ? $_iPostID : $_GET['post'];
     // Remove unnecessary items.
     // The unit title was converted to post_title above.
     unset($aUnitOptions['unit_title'], $aUnitOptions['is_preview']);
     $_oOption = AmazonAutoLinks_Option::getInstance();
     $_oTemplateOption = AmazonAutoLinks_TemplateOption::getInstance();
     $aUnitOptions['template_id'] = $_oTemplateOption->getDefaultTemplateIDByUnitType('category');
     AmazonAutoLinks_WPUtility::updatePostMeta($_iPostID, $aUnitOptions);
     // Create an auto insert - the 'auto_insert' key will be removed when creating a post.s
     if (isset($aOptions['auto_insert']) && $aOptions['auto_insert'] && 1 == $aOptions['mode']) {
         AmazonAutoLinks_PluginUtility::createAutoInsert($_iPostID);
     }
     return $_iPostID;
 }