/**
  * THIS IS THE ACTUAL LOGIC OF THE PLUGIN
  *
  * Here we find out if some sidebars should be replaced, and if it is
  * replaced we determine which custom sidebar to use.
  *
  * @param   array $options Plugin options with the replacement rules.
  * @return  array List of the replaced sidebars.
  */
 public function determine_replacements($options)
 {
     global $post, $sidebar_category;
     $sidebars = self::get_options('modifiable');
     $replacements_todo = sizeof($sidebars);
     $replacements = array();
     $expl = CustomSidebarsExplain::do_explain();
     foreach ($sidebars as $sb) {
         $replacements[$sb] = false;
     }
     // 1 |== Single posts/pages --------------------------------------------
     if (is_single()) {
         $post_type = get_post_type();
         $expl && do_action('cs_explain', 'Type 1: Single ' . ucfirst($post_type));
         if (!self::supported_post_type($post_type)) {
             $expl && do_action('cs_explain', 'Invalid post type, use default sidebars.');
             return $options;
         }
         // 1.1 Check if replacements are defined in the post metadata.
         $reps = self::get_post_meta($this->original_post_id);
         foreach ($sidebars as $sb_id) {
             if (is_array($reps) && !empty($reps[$sb_id])) {
                 $replacements[$sb_id] = array($reps[$sb_id], 'particular', -1);
                 $replacements_todo -= 1;
             }
         }
         // 1.2 Try to use the parents metadata.
         if ($post->post_parent != 0 && $replacements_todo > 0) {
             $reps = self::get_post_meta($post->post_parent);
             foreach ($sidebars as $sb_id) {
                 if ($replacements[$sb_id]) {
                     continue;
                 }
                 if (is_array($reps) && !empty($reps[$sb_id])) {
                     $replacements[$sb_id] = array($reps[$sb_id], 'particular', -1);
                     $replacements_todo -= 1;
                 }
             }
         }
         // 1.3 If no metadata set then use the category settings.
         if ($replacements_todo > 0) {
             $categories = self::get_sorted_categories();
             $ind = sizeof($categories) - 1;
             while ($replacements_todo > 0 && $ind >= 0) {
                 $cat_id = $categories[$ind]->cat_ID;
                 foreach ($sidebars as $sb_id) {
                     if ($replacements[$sb_id]) {
                         continue;
                     }
                     if (!empty($options['category_single'][$cat_id][$sb_id])) {
                         $replacements[$sb_id] = array($options['category_single'][$cat_id][$sb_id], 'category_single', $sidebar_category);
                         $replacements_todo -= 1;
                     }
                 }
                 $ind -= 1;
             }
         }
         // 1.4 Look for post-type level replacements.
         if ($replacements_todo > 0) {
             foreach ($sidebars as $sb_id) {
                 if ($replacements[$sb_id]) {
                     continue;
                 }
                 if (isset($options['post_type_single'][$post_type]) && !empty($options['post_type_single'][$post_type][$sb_id])) {
                     $replacements[$sb_id] = array($options['post_type_single'][$post_type][$sb_id], 'post_type_single', $post_type);
                     $replacements_todo -= 1;
                 }
             }
         }
     } else {
         // 2 |== Category archive ----------------------------------------------
         if (is_category()) {
             $expl && do_action('cs_explain', 'Type 2: Category Archive');
             // 2.1 Start at current category and travel up all parents
             $category_object = get_queried_object();
             $current_category = $category_object->term_id;
             while ($current_category != 0 && $replacements_todo > 0) {
                 foreach ($sidebars as $sb_id) {
                     if ($replacements[$sb_id]) {
                         continue;
                     }
                     if (!empty($options['category_archive'][$current_category][$sb_id])) {
                         $replacements[$sb_id] = array($options['category_archive'][$current_category][$sb_id], 'category_archive', $current_category);
                         $replacements_todo -= 1;
                     }
                 }
                 $current_category = $category_object->category_parent;
                 if ($current_category != 0) {
                     $category_object = get_category($current_category);
                 }
             }
         } else {
             // 3 |== Search --------------------------------------------------------
             // Must be before the post-type archive section; otherwise a search with
             // no results is recognized as post-type archive...
             if (is_search()) {
                 $expl && do_action('cs_explain', 'Type 3: Search Results');
                 foreach ($sidebars as $sb_id) {
                     if (!empty($options['search'][$sb_id])) {
                         $replacements[$sb_id] = array($options['search'][$sb_id], 'search', -1);
                     }
                 }
             } else {
                 // 4 |== Post-Tpe Archive ----------------------------------------------
                 // `get_post_type() != 'post'` .. post-archive = post-index (see 7)
                 if (!is_category() && !is_singular() && get_post_type() != 'post') {
                     $post_type = get_post_type();
                     $expl && do_action('cs_explain', 'Type 4: ' . ucfirst($post_type) . ' Archive');
                     if (!self::supported_post_type($post_type)) {
                         $expl && do_action('cs_explain', 'Invalid post type, use default sidebars.');
                         return $options;
                     }
                     foreach ($sidebars as $sb_id) {
                         if (isset($options['post_type_archive'][$post_type]) && !empty($options['post_type_archive'][$post_type][$sb_id])) {
                             $replacements[$sb_id] = array($options['post_type_archive'][$post_type][$sb_id], 'post_type_archive', $post_type);
                             $replacements_todo -= 1;
                         }
                     }
                 } else {
                     // 5 |== Page ----------------------------------------------------------
                     // `! is_front_page()` .. in case the site uses static front page.
                     if (is_page() && !is_front_page()) {
                         $post_type = get_post_type();
                         $expl && do_action('cs_explain', 'Type 5: ' . ucfirst($post_type));
                         if (!self::supported_post_type($post_type)) {
                             $expl && do_action('cs_explain', 'Invalid post type, use default sidebars.');
                             return $options;
                         }
                         // 5.1 Check if replacements are defined in the post metadata.
                         $reps = self::get_post_meta($this->original_post_id);
                         foreach ($sidebars as $sb_id) {
                             if (is_array($reps) && !empty($reps[$sb_id])) {
                                 $replacements[$sb_id] = array($reps[$sb_id], 'particular', -1);
                                 $replacements_todo -= 1;
                             }
                         }
                         // 5.2 Try to use the parents metadata.
                         if ($post->post_parent != 0 && $replacements_todo > 0) {
                             $reps = self::get_post_meta($post->post_parent);
                             foreach ($sidebars as $sb_id) {
                                 if ($replacements[$sb_id]) {
                                     continue;
                                 }
                                 if (is_array($reps) && !empty($reps[$sb_id])) {
                                     $replacements[$sb_id] = array($reps[$sb_id], 'particular', -1);
                                     $replacements_todo -= 1;
                                 }
                             }
                         }
                         // 5.3 Look for post-type level replacements.
                         if ($replacements_todo > 0) {
                             foreach ($sidebars as $sb_id) {
                                 if ($replacements[$sb_id]) {
                                     continue;
                                 }
                                 if (isset($options['post_type_single'][$post_type]) && !empty($options['post_type_single'][$post_type][$sb_id])) {
                                     $replacements[$sb_id] = array($options['post_type_single'][$post_type][$sb_id], 'post_type_single', $post_type);
                                     $replacements_todo -= 1;
                                 }
                             }
                         }
                     } else {
                         // 6 |== Front Page ----------------------------------------------------
                         if (is_front_page()) {
                             /*
                              * The front-page of the site. Either
                              * - the post-index (default) or
                              * - a static front-page.
                              */
                             $expl && do_action('cs_explain', 'Type 6: Front Page');
                             if (!is_home()) {
                                 // A static front-page. Maybe we need the post-meta data...
                                 $reps_post = self::get_post_meta($this->original_post_id);
                                 $reps_parent = self::get_post_meta($post->post_parent);
                             }
                             foreach ($sidebars as $sb_id) {
                                 // First check if there is a 'Front Page' replacement.
                                 if (!empty($options['blog'][$sb_id])) {
                                     $replacements[$sb_id] = array($options['blog'][$sb_id], 'blog', -1);
                                 } else {
                                     if (!is_home()) {
                                         // There is no 'Front Page' reaplcement and this is a static
                                         // front page, so check if the page has a replacement.
                                         // 6.1 Check if replacements are defined in the post metadata.
                                         if (is_array($reps_post) && !empty($reps_post[$sb_id])) {
                                             $replacements[$sb_id] = array($reps_post[$sb_id], 'particular', -1);
                                             $replacements_todo -= 1;
                                         }
                                         // 6.2 Try to use the parents metadata.
                                         if ($post->post_parent != 0 && $replacements_todo > 0) {
                                             if ($replacements[$sb_id]) {
                                                 continue;
                                             }
                                             if (is_array($reps_parent) && !empty($reps_parent[$sb_id])) {
                                                 $replacements[$sb_id] = array($reps_parent[$sb_id], 'particular', -1);
                                                 $replacements_todo -= 1;
                                             }
                                         }
                                     }
                                 }
                             }
                         } else {
                             // 7 |== Post Index ----------------------------------------------------
                             if (is_home()) {
                                 /*
                                  * The post-index of the site. Either
                                  * - the front-page (default)
                                  * - when a static front page is used the post-index page.
                                  *
                                  * Note: When the default front-page is used the condition 6
                                  * "is_front_page" above is used and this node is never executed.
                                  */
                                 $expl && do_action('cs_explain', 'Type 7: Post Index');
                                 foreach ($sidebars as $sb_id) {
                                     if (!empty($options['post_type_archive']['post'][$sb_id])) {
                                         $replacements[$sb_id] = array($options['post_type_archive']['post'][$sb_id], 'postindex', -1);
                                     }
                                 }
                             } else {
                                 // 8 |== Tag archive ---------------------------------------------------
                                 if (is_tag()) {
                                     $expl && do_action('cs_explain', 'Type 8: Tag Archive');
                                     foreach ($sidebars as $sb_id) {
                                         if (!empty($options['tags'][$sb_id])) {
                                             $replacements[$sb_id] = array($options['tags'][$sb_id], 'tags', -1);
                                         }
                                     }
                                 } else {
                                     // 9 |== Author archive ------------------------------------------------
                                     if (is_author()) {
                                         $author_object = get_queried_object();
                                         $current_author = $author_object->ID;
                                         $expl && do_action('cs_explain', 'Type 9: Author Archive');
                                         // 9.2 Then check if there is an "Any authors" sidebar
                                         if ($replacements_todo > 0) {
                                             foreach ($sidebars as $sb_id) {
                                                 if ($replacements[$sb_id]) {
                                                     continue;
                                                 }
                                                 if (!empty($options['authors'][$sb_id])) {
                                                     $replacements[$sb_id] = array($options['authors'][$sb_id], 'authors', -1);
                                                 }
                                             }
                                         }
                                     } else {
                                         // 10 |== Date archive -------------------------------------------------
                                         if (is_date()) {
                                             $expl && do_action('cs_explain', 'Type 10: Date Archive');
                                             foreach ($sidebars as $sb_id) {
                                                 if (!empty($options['date'][$sb_id])) {
                                                     $replacements[$sb_id] = array($options['date'][$sb_id], 'date', -1);
                                                 }
                                             }
                                         } else {
                                             // 11 |== 404 not found ------------------------------------------------
                                             if (is_404()) {
                                                 $expl && do_action('cs_explain', 'Type 11: 404 not found');
                                                 foreach ($sidebars as $sb_id) {
                                                     if (!empty($options['404'][$sb_id])) {
                                                         $replacements[$sb_id] = array($options['404'][$sb_id], '404', -1);
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     /**
      * Filter the replaced sidebars before they are processed by the plugin.
      *
      * @since  2.0
      *
      * @param  array $replacements List of the final/replaced sidebars.
      */
     $replacements = apply_filters('cs_replace_sidebars', $replacements);
     return $replacements;
 }
 public function maybe_display_widget($instance)
 {
     global $post, $wp_query;
     static $Type_list = null;
     static $Tax_list = null;
     $show_widget = true;
     $condition_true = true;
     $action = 'show';
     $explain = '';
     // This is used to explain why a widget is not displayed.
     $expl = CustomSidebarsExplain::do_explain();
     if (empty($instance['csb_visibility']) || empty($instance['csb_visibility']['conditions'])) {
         return $show_widget;
     }
     $cond = $instance['csb_visibility']['conditions'];
     $action = 'hide' != $instance['csb_visibility']['action'] ? 'show' : 'hide';
     if ($instance['csb_visibility']['always']) {
         $expl && do_action('cs_explain', '<span style="color:#090">Always</span> <b>' . $action . '</b>');
         return 'hide' == $action ? false : true;
     }
     if (null === $Type_list) {
         $Tax_list = get_taxonomies(array('public' => true), 'objects');
         $Type_list = get_post_types(array('public' => true), 'objects');
     }
     // Filter for DATE-RANGE.
     if ($condition_true && !empty($cond['date'])) {
         // not implemented yet...
     }
     // Filter for GUEST STATUS.
     if ($condition_true && !empty($cond['guest']) && is_array($cond['guest'])) {
         $expl && ($explain .= '<br />GUEST [');
         if (is_user_logged_in()) {
             if ('member' != $cond['guest'][0]) {
                 $expl && ($explain .= 'user is logged in');
                 $condition_true = false;
             }
         } else {
             if ('guest' != $cond['guest'][0]) {
                 $expl && ($explain .= 'user not logged in');
                 $condition_true = false;
             }
         }
         $expl && ($explain .= '] ');
     }
     // Filter for USER ROLES.
     if ($condition_true && !empty($cond['roles']) && is_array($cond['roles'])) {
         $expl && ($explain .= '<br />ROLE [');
         if (!is_user_logged_in()) {
             $expl && ($explain .= 'user not logged in');
             $condition_true = false;
         } else {
             global $current_user;
             $has_role = false;
             foreach ($current_user->roles as $user_role) {
                 if (in_array($user_role, $cond['roles'])) {
                     $expl && ($explain .= 'ok:' . $user_role);
                     $has_role = true;
                     break;
                 }
             }
             if (!$has_role) {
                 $expl && ($explain .= 'invalid role');
                 $condition_true = false;
             }
         }
         $expl && ($explain .= '] ');
     }
     // Filter for MEMBERSHIP Level.
     if ($condition_true && !empty($cond['membership'])) {
         $expl && ($explain .= '<br />MEMBERSHIP [');
         if (class_exists('Membership_Factory')) {
             $has_level = false;
             $wpuser = get_userdata(get_current_user_id());
             $is_admin = $wpuser && ($wpuser->has_cap('membershipadmin') || $wpuser->has_cap('manage_options') || is_super_admin());
             if ($is_admin) {
                 $expl && ($explain .= 'is admin');
                 $has_level = true;
             } else {
                 $factory = new Membership_Factory();
                 $user = $factory->get_member(get_current_user_id());
                 $levels = $user->get_level_ids();
                 if (!is_array($levels)) {
                     $levels = array($levels);
                 }
                 foreach ($cond['membership'] as $need_level_id) {
                     if (empty($need_level_id)) {
                         continue;
                     }
                     foreach ($levels as $the_level) {
                         if ($the_level->level_id == $need_level_id) {
                             $expl && ($explain .= 'ok');
                             $has_level = true;
                             break;
                         }
                     }
                     if ($has_level) {
                         break;
                     }
                 }
             }
             if (!$has_level) {
                 $expl && ($explain .= 'invalid user level');
                 $condition_true = false;
             }
         }
         $expl && ($explain .= '] ');
     }
     // Filter for MEMBERSHIP2 Level.
     if ($condition_true && !empty($cond['membership2'])) {
         $expl && ($explain .= '<br />MEMBERSHIP2 [');
         if (apply_filters('ms_active')) {
             $is_member = false;
             $member = MS_Plugin::$api->get_current_member();
             if ($member->is_admin_user()) {
                 $expl && ($explain .= 'is admin');
                 $is_member = true;
             } else {
                 foreach ($cond['membership2'] as $membership_id) {
                     if ($member->has_membership($membership_id)) {
                         $is_member = true;
                         break;
                     }
                 }
             }
             if (!$is_member) {
                 $expl && ($explain .= 'is no member');
                 $condition_true = false;
             }
         }
         $expl && ($explain .= '] ');
     }
     // Filter for PRO-SITE Level.
     if ($condition_true && !empty($cond['prosite'])) {
         $expl && ($explain .= '<br />PROSITE [');
         // not implemented yet...
         $expl && ($explain .= '] ');
     }
     // Filter for SPECIAL PAGES.
     if ($condition_true && !empty($cond['pagetypes']) && is_array($cond['pagetypes'])) {
         $expl && ($explain .= '<br />PAGETYPE [');
         $is_type = false;
         foreach ($cond['pagetypes'] as $type) {
             if ($is_type) {
                 break;
             }
             switch ($type) {
                 case 'e404':
                     $is_type = $is_type || is_404();
                     break;
                 case 'single':
                     $is_type = $is_type || is_singular();
                     break;
                 case 'search':
                     $is_type = $is_type || is_search();
                     break;
                 case 'archive':
                     $is_type = $is_type || is_archive();
                     break;
                 case 'preview':
                     $is_type = $is_type || is_preview();
                     break;
                 case 'day':
                     $is_type = $is_type || is_day();
                     break;
                 case 'month':
                     $is_type = $is_type || is_month();
                     break;
                 case 'year':
                     $is_type = $is_type || is_year();
                     break;
                 case 'frontpage':
                     if (current_theme_supports('infinite-scroll')) {
                         $is_type = $is_type || is_front_page();
                     } else {
                         $is_type = $is_type || is_front_page() && !is_paged();
                     }
                     break;
                 case 'posts':
                 case 'home':
                     $is_type = $is_type || is_home();
                     break;
             }
             $expl && ($explain .= $type . ':' . ($is_type ? 'ok' : 'invalid'));
         }
         if (!$is_type) {
             $condition_true = false;
         }
         $expl && ($explain .= '] ');
     }
     // Filter for POST-TYPE.
     if ($condition_true && !empty($cond['posttypes'])) {
         $posttype = get_post_type();
         $expl && ($explain .= '<br />POSTTYPE-' . strtoupper($posttype) . ' [');
         if (!in_array($posttype, $cond['posttypes'])) {
             $expl && ($explain .= 'invalid posttype');
             $condition_true = false;
         } else {
             // Filter for SPECIFIC POSTS.
             if (!empty($cond['pt-' . $posttype])) {
                 if (!in_array(get_the_ID(), $cond['pt-' . $posttype])) {
                     $expl && ($explain .= 'invalid post_id');
                     $condition_true = false;
                 }
             }
         }
         if ($condition_true) {
             $expl && ($explain .= 'ok');
         }
         $expl && ($explain .= '] ');
     }
     if ($condition_true) {
         // TAXONOMY condition.
         $tax_query = @$wp_query->tax_query->queries;
         if (is_array($tax_query)) {
             $tax_type = @$tax_query[0]['taxonomy'];
             $tax_terms = @$tax_query[0]['terms'];
         } else {
             $tax_type = false;
             $tax_terms = false;
         }
         foreach ($Tax_list as $tax_item) {
             if (!$condition_true) {
                 break;
             }
             $tax_key = 'tax-' . $tax_item->name;
             if (isset($cond[$tax_key]) && !empty($cond[$tax_key])) {
                 $expl && ($explain .= '<br />TAX-' . strtoupper($tax_item->name) . ' [');
                 $has_term = false;
                 if ($tax_type && $tax_type == $tax_item->name) {
                     // Check if we did filter for the specific taxonomy.
                     foreach ($tax_terms as $slug) {
                         $term_data = get_term_by('slug', $slug, $tax_type);
                         if (in_array($term_data->term_id, $cond[$tax_key])) {
                             $expl && ($explain .= 'ok:' . $term_data->term_id);
                             $has_term = true;
                         }
                     }
                 } else {
                     // Check if current post has the specific taxonomy.
                     foreach ($cond[$tax_key] as $term) {
                         if (has_term($term, $tax_item->name)) {
                             $expl && ($explain .= 'ok:' . $term);
                             $has_term = true;
                             break;
                         }
                     }
                 }
                 if (!$has_term) {
                     $expl && ($explain .= 'no match');
                     $condition_true = false;
                 }
                 $expl && ($explain .= '] ');
             }
         }
     }
     if ('show' == $action && !$condition_true || 'hide' == $action && $condition_true) {
         $show_widget = false;
     }
     $expl && do_action('cs_explain', ($condition_true ? '<span style="color:#090">Do</span>' : '<span style="color:#900">Dont</span>') . ' <b>' . $action . '</b> - ' . $explain);
     return $show_widget;
 }