/**
  * 
  * @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;
 }
 public function __construct()
 {
     if (self::$_bLoaded) {
         return;
     }
     self::$_bLoaded = true;
     $this->_oTemplateOption = AmazonAutoLinks_TemplateOption::getInstance();
     $this->_loadFunctionsOfActiveTemplates();
     $this->_loadStylesOfActiveTemplates();
     $this->_loadSettingsOfActiveTemplates();
 }
 public function replyToDoAfterTab($oFactory)
 {
     $_oOption = AmazonAutoLinks_Option::getInstance();
     if (!$_oOption->isDebug()) {
         return;
     }
     echo "<h3>" . __('Debug', 'amazon-auto-links') . "</h3>";
     echo "<h4>" . __('Raw Template Option Values', 'amazon-auto-links') . "</h4>";
     echo $oFactory->oDebug->get(get_option(AmazonAutoLinks_Registry::$aOptionKeys['template'], array()));
     echo "<h4>" . __('Data of Active Templates', 'amazon-auto-links') . "</h4>";
     echo $oFactory->oDebug->get(AmazonAutoLinks_TemplateOption::getInstance()->get());
 }
 /**
  * Creates a search unit type
  * 
  * @since       2.0.2
  * @return      integer     A newly created post id.
  */
 protected function _createSearchUnit($aInput)
 {
     $_oOption = AmazonAutoLinks_Option::getInstance();
     $_oTemplateOption = AmazonAutoLinks_TemplateOption::getInstance();
     $_oUtil = new AmazonAutoLinks_PluginUtility();
     $_bDoAutoInsert = $aInput['auto_insert'];
     // Sanitize the unit options
     $_sClassName = "AmazonAutoLinks_UnitOption_{$aInput['unit_type']}";
     $_oUnitOptions = new $_sClassName(null, $aInput);
     $aInput = $_oUnitOptions->get();
     // will format the options.
     $aInput['template_id'] = $_oTemplateOption->getDefaultTemplateIDByUnitType($aInput['unit_type']);
     // Create a post.
     $_iNewPostID = $_oUtil->insertPost($aInput, AmazonAutoLinks_Registry::$aPostTypes['unit']);
     // Create an auto insert
     if ($_bDoAutoInsert) {
         $this->_createAutoInsert($_iNewPostID);
     }
     return $_iNewPostID;
 }
 /**
  * Returns an array holding the template directories.
  * 
  * @since       3
  * @return      array       Contains list of template directory paths.
  */
 private function _getTemplateDirs()
 {
     if (!empty(self::$_aTemplateDirs)) {
         return self::$_aTemplateDirs;
     }
     foreach ($this->_getTemplateContainerDirs() as $__sTemplateDirPath) {
         if (!@file_exists($__sTemplateDirPath)) {
             continue;
         }
         $__aFoundDirs = glob($__sTemplateDirPath . DIRECTORY_SEPARATOR . "*", GLOB_ONLYDIR);
         if (is_array($__aFoundDirs)) {
             // glob can return false
             self::$_aTemplateDirs = array_merge($__aFoundDirs, self::$_aTemplateDirs);
         }
     }
     self::$_aTemplateDirs = array_unique(self::$_aTemplateDirs);
     self::$_aTemplateDirs = (array) apply_filters('aal_filter_template_directories', self::$_aTemplateDirs);
     self::$_aTemplateDirs = array_filter(self::$_aTemplateDirs);
     // drops elements of empty values.
     self::$_aTemplateDirs = array_unique(self::$_aTemplateDirs);
     return self::$_aTemplateDirs;
 }
 /**
  * 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;
 }
 /**
  * Processes bulk actions of the table form.
  * 
  * @remark      This method uses redirect so it must be called before the header gets sent.
  */
 public function process_bulk_action()
 {
     if (!isset($_REQUEST['template'])) {
         return;
     }
     $_oTemplateOption = AmazonAutoLinks_TemplateOption::getInstance();
     switch (strtolower($this->current_action())) {
         case 'activate':
             foreach ((array) $_REQUEST['template'] as $sID) {
                 if (!$sID) {
                     continue;
                 }
                 $this->aData[$sID]['is_active'] = true;
             }
             break;
         case 'deactivate':
             foreach ((array) $_REQUEST['template'] as $sID) {
                 if (!$sID) {
                     continue;
                 }
                 $this->aData[$sID]['is_active'] = false;
             }
             break;
         default:
             return;
             // do nothing.
     }
     /**
      * For the first time of loading, although the default templates are activated,
      * no item is actually set to the option.
      * In this situation, even deactivating an item does not mean anything 
      * because no active template is stored. Just the default active templates will get listed in the next page.
      * So set the options here so that the active tempaltes will be saved.
      */
     $_oTemplateOption->aOptions = $this->aData;
     // Save the options.
     $_oTemplateOption->save();
     // Reload the page.
     exit(wp_redirect(add_query_arg(array('post_type' => AmazonAutoLinks_Registry::$aPostTypes['unit'], 'page' => AmazonAutoLinks_Registry::$aAdminPages['template'], 'tab' => 'table'), admin_url($GLOBALS['pagenow']))));
 }
 public function __construct()
 {
     $this->oOption = AmazonAutoLinks_Option::getInstance();
     $this->oTemplateOption = AmazonAutoLinks_TemplateOption::getInstance();
 }
 /**
  * @callback        filter      cell_{post type slug}_{column key}
  * @return          string
  */
 public function cell_amazon_auto_links_template($sCell, $iPostID)
 {
     return AmazonAutoLinks_TemplateOption::getInstance()->getTemplateNameByID(get_post_meta($iPostID, 'template_id', true));
 }
 /**
  * 
  * @remark      Each unit has to define its own default template.
  * @since       3
  * @return      string
  */
 public function getDefaultTemplatePath()
 {
     $_oTemplateOption = AmazonAutoLinks_TemplateOption::getInstance();
     $_aTemplate = $_oTemplateOption->getTemplateArrayByDirPath(AmazonAutoLinks_Registry::$sDirPath . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'category', false);
     return $_aTemplate['dir_path'] . '/template.php';
 }