/**
  * validate ajax params
  */
 private function validate_preset($preset)
 {
     $prefix = $this->config->parse_prefix($preset['slug']);
     $sub_type = $this->config->parse_sub_type($preset['slug']);
     $type = $this->config->get_type_by_prefix($prefix);
     if (!$sub_type or !$type) {
         throw new Exception(__('Error: Type or sub_type error', 'fw'));
     }
     $preset['type'] = $type;
     $preset['sub_type'] = $sub_type;
     if ($this->config->is_enabled_select_option($type, $sub_type) === false) {
         throw new Exception(__(sprintf('Error this option (%s) is disabled', $type . '-' . $sub_type), 'fw'));
     }
     if (!isset($preset['position']) or !$this->config->has_position($preset['position'])) {
         throw new Exception(__("Error: Position doesn't exists. Please check config file.", 'fw'));
     }
     $position_allowed_colors = $this->config->get_allowed_color_by_position($preset['position']);
     //removing invalid data from $preset['sidebars']
     $exceptionMsg = __('Error: Sidebars not set', 'fw');
     if (isset($preset['sidebars']) and is_array($preset['sidebars'])) {
         $exception = new _FW_Extension_Sidebars_MissingSidebar_Exception($exceptionMsg);
         array_walk($preset['sidebars'], array($this, 'clear_preset_sidebars'), $position_allowed_colors);
         foreach ($preset['sidebars'] as $color => $sidebar_id) {
             if (empty($sidebar_id)) {
                 //if added invalid sidebarId, add 'color' to exception
                 unset($preset['sidebars'][$color]);
                 $exception->add_color($color);
             }
         }
         if ($exception->has_colors()) {
             throw $exception;
         }
     } else {
         if (!empty($position_allowed_colors)) {
             //if not set sidebars, but it is in position throw exception
             throw new Exception($exceptionMsg);
         } else {
             $preset['sidebars'] = array();
         }
     }
     //remove duplicates in array
     if (is_array($preset['ids'])) {
         $preset['ids'] = array_unique($preset['ids']);
     }
     return $preset;
 }
 private function _fw_check_conditional_tags($priority)
 {
     $conditional_tags = $this->config->get_conditional_tags($priority);
     foreach ($conditional_tags as $key => $cond_tag) {
         $function = null;
         if (isset($cond_tag['conditional_tag'])) {
             $function = isset($cond_tag['conditional_tag']['callback']) ? $cond_tag['conditional_tag']['callback'] : '';
             if (is_callable($function)) {
                 $params = array();
                 if (isset($cond_tag['conditional_tag']['params']) and is_array($cond_tag['conditional_tag']['params'])) {
                     $params = $cond_tag['conditional_tag']['params'];
                 }
                 if (call_user_func_array($function, $params)) {
                     $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::CONDITIONAL_TAGS_PREFIX);
                     $data['sub_type'] = $key;
                     $result = $this->get_preset_sidebars($data);
                     if ($result) {
                         return $result;
                     }
                 }
             }
         } else {
             $function = $key;
             if (is_callable($function)) {
                 if (call_user_func($function)) {
                     $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::CONDITIONAL_TAGS_PREFIX);
                     $data['sub_type'] = $key;
                     $result = $this->get_preset_sidebars($data);
                     if ($result) {
                         return $result;
                     }
                 }
             }
         }
     }
 }
 /**
  * validate ajax params
  */
 private function validate_preset($preset)
 {
     $prefix = $this->config->parse_prefix($preset['slug']);
     $sub_type = $this->config->parse_sub_type($preset['slug']);
     $type = $this->config->get_type_by_prefix($prefix);
     if (!$sub_type or !$type) {
         throw new Exception(__('Error: Type or sub_type error', 'fw'));
     }
     $preset['type'] = $type;
     $preset['sub_type'] = $sub_type;
     if ($this->config->is_enabled_select_option($type, $sub_type) === false) {
         throw new Exception(__(sprintf('Error this option (%s) is disabled', $type . '-' . $sub_type), 'fw'));
     }
     if (fw()->extensions->get('sidebars')->is_missing_config()) {
         $this->_fw_check_allowed_replace_positions($preset);
     } else {
         $this->_fw_check_allowed_colors($preset);
     }
     //remove duplicates in array
     if (is_array($preset['ids'])) {
         $preset['ids'] = array_unique($preset['ids']);
     }
     return $preset;
 }
 /**
  * Generate current page requirements and return array with avaible sidebars for current page
  */
 public function get_current_page_preset()
 {
     //check if current_page_preset doesn't get before
     if ($this->current_page_preset !== null) {
         return $this->current_page_preset;
     }
     if (is_singular()) {
         $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::POST_TYPES_PREFIX);
         $data['sub_type'] = get_post_type();
         $data['id'] = get_the_id();
         $result = $this->get_preset_sidebars($data);
         if ($result) {
             return $result;
         }
     }
     if (is_category()) {
         $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::TAXONOMIES_PREFIX);
         $data['sub_type'] = 'category';
         $data['id'] = get_query_var('cat');
         $result = $this->get_preset_sidebars($data);
         if ($result) {
             return $result;
         }
     }
     if (is_tag()) {
         $term_obj = get_term_by('slug', get_query_var('tag'), 'post_tag');
         $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::TAXONOMIES_PREFIX);
         $data['sub_type'] = $term_obj->taxonomy;
         $data['id'] = $term_obj->term_id;
         $result = $this->get_preset_sidebars($data);
         if ($result) {
             return $result;
         }
     }
     if (is_tax()) {
         $term_obj = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
         $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::TAXONOMIES_PREFIX);
         $data['sub_type'] = $term_obj->taxonomy;
         $data['id'] = $term_obj->term_id;
         $result = $this->get_preset_sidebars($data);
         if ($result) {
             return $result;
         }
     }
     $conditional_tags = $this->config->get_conditional_tags();
     foreach ($conditional_tags as $key => $cond_tag) {
         $function = null;
         if (isset($cond_tag['conditional_tag'])) {
             $function = isset($cond_tag['conditional_tag']['callback']) ? $cond_tag['conditional_tag']['callback'] : '';
             if (is_callable($function)) {
                 $params = array();
                 if (isset($cond_tag['conditional_tag']['params']) and is_array($cond_tag['conditional_tag']['params'])) {
                     $params = $cond_tag['conditional_tag']['params'];
                 }
                 if (call_user_func_array($function, $params)) {
                     $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::CONDITIONAL_TAGS_PREFIX);
                     $data['sub_type'] = $key;
                     $result = $this->get_preset_sidebars($data);
                     if ($result) {
                         return $result;
                     }
                 }
             }
         } else {
             $function = $key;
             if (is_callable($function)) {
                 if ($function()) {
                     $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::CONDITIONAL_TAGS_PREFIX);
                     $data['sub_type'] = $key;
                     $result = $this->get_preset_sidebars($data);
                     if ($result) {
                         return $result;
                     }
                 }
             }
         }
     }
     $data['type'] = $this->config->get_type_by_prefix(_FW_Extension_Sidebars_Config::DEFAULT_PREFIX);
     $data['sub_type'] = _FW_Extension_Sidebars_Config::DEFAULT_SUB_TYPE;
     $result = $this->get_preset_sidebars($data);
     //return preset default for all pages
     return $result;
 }