/**
  * Validates submitted form data.
  */
 public function validate($aInput, $aOriginal, $oFactory)
 {
     // Formats the options
     $_oUnitOption = new AmazonAutoLinks_UnitOption_tag(null, $aInput);
     $_aFormatted = $_oUnitOption->get();
     // Drop unsent keys.
     foreach ($_aFormatted as $_sKey => $_mValue) {
         if (!array_key_exists($_sKey, $aInput)) {
             unset($_aFormatted[$_sKey]);
         }
     }
     // Schedule pre-fetch for the unit if the options have been changed.
     if ($aInput !== $aOriginal) {
         AmazonAutoLinks_Event_Scheduler::prefetch(AmazonAutoLinks_PluginUtility::getCurrentPostID());
     }
     return $_aFormatted + $aInput;
 }
 /**
  * 
  * @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;
 }