Advertisement
BackuPs-nl

strikingr-sidebarfilters

May 6th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.54 KB | None | 0 0
  1. <?php
  2. class sidebarGenerator {
  3.     var $sidebar_names = array();
  4.     var $footer_sidebar_count = 0;
  5.     var $footer_sidebar_names = array();
  6.    
  7.     public function __construct(){
  8.         $this->sidebar_names = array(
  9.             'home'=>__('Homepage Widget Area','theme_admin'),
  10.             'page'=>__('Page Widget Area','theme_admin'),
  11.             'blog'=>__('Blog Widget Area','theme_admin'),
  12.             'portfolio' =>__('Portfolio Widget Area','theme_admin'),
  13.         );
  14.         $this->footer_sidebar_names = array(
  15.             'footer-first' => __('First Footer Widget Area','theme_admin'),
  16.             'footer-second' =>__('Second Footer Widget Area','theme_admin'),
  17.             'footer-third' =>__('Third Footer Widget Area','theme_admin'),
  18.             'footer-fourth' =>__('Fourth Footer Widget Area','theme_admin'),
  19.             'footer-fifth' =>__('Fifth Footer Widget Area','theme_admin'),
  20.             'footer-sixth' =>__('Sixth Footer Widget Area','theme_admin'),
  21.         );
  22.     }
  23.  
  24.     function custom_sidebar_available($sidebar) {
  25.         global $wp_registered_sidebars;
  26.  
  27.         $sidebar = sanitize_title($sidebar);
  28.         foreach ( (array) $wp_registered_sidebars as $key => $value ) {
  29.             if ( sanitize_title($value['name']) == $sidebar ) {
  30.                 return true;
  31.             }
  32.         }
  33.  
  34.         return false;
  35.     }
  36.  
  37.     function register_sidebar(){
  38.         foreach ($this->sidebar_names as $slug => $name){
  39.             register_sidebar(array(
  40.                 'name' => $name,
  41.                 'id' => 'sidebar-'.$slug,
  42.                 'description' => $name,
  43.                 'before_widget' =>  apply_filters('theme_sidebar_before_widget','<section id="%1$s" class="widget %2$s">'),
  44.                 'after_widget' => apply_filters('theme_sidebar_after_widget','</section>'),
  45.                 'before_title' => apply_filters('theme_sidebar_before_title','<h3 class="widgettitle">'),
  46.                 'after_title' => apply_filters('theme_sidebar_after_title','</h3>'),
  47.             ));
  48.         }
  49.        
  50.         //register footer sidebars
  51.         foreach ($this->footer_sidebar_names as $slug => $name){
  52.             register_sidebar(array(
  53.                 'name' =>  $name,
  54.                 'id' => 'sidebar-'.$slug,
  55.                 'description' => $name,
  56.                 'before_widget' => apply_filters('theme_sidebar_footer_before_widget','<section id="%1$s" class="widget %2$s">'),
  57.                 'after_widget' => apply_filters('theme_sidebar_footer_after_widget','</section>'),
  58.                 'before_title' => apply_filters('theme_sidebar_footer_before_title','<h3 class="widgettitle">'),
  59.                 'after_title' => apply_filters('theme_sidebar_footer_after_title','</h3>'),
  60.             ));
  61.         }
  62.        
  63.         $top_area_type = theme_get_option_from_db('general','top_area_type');
  64.         if($top_area_type == 'widget'){
  65.             register_sidebar(array(
  66.                 'name' =>  __('Header Widget Area','theme_admin'),
  67.                 'id' => 'sidebar-top-area',
  68.                 'description' => __('Header Widget Area','theme_admin'),
  69.                 'before_widget' => apply_filters('theme_sidebar_header_before_widget','<section id="%1$s" class="widget %2$s">'),
  70.                 'after_widget' => apply_filters('theme_sidebar_header_before_widget','</section>'),
  71.                 'before_title' => apply_filters('theme_sidebar_header_before_title',''),
  72.                 'after_title' => apply_filters('theme_sidebar_header_after_title',''),
  73.             ));
  74.         }
  75.         $footer_right_area_type = theme_get_option_from_db('footer','footer_right_area_type');
  76.         if($footer_right_area_type == 'widget'){
  77.             register_sidebar(array(
  78.                 'name' =>  __('Sub Footer Widget Area','theme_admin'),
  79.                 'id' => 'sidebar-footer-right-area',
  80.                 'description' => __('Sub Footer Widget Area','theme_admin'),
  81.                 'before_widget' => apply_filters('theme_sidebar_footer_right_area_before_widget','<section id="%1$s" class="widget %2$s">'),
  82.                 'after_widget' => apply_filters('theme_sidebar_footer_right_area_after_widget','</section>'),
  83.                 'before_title' => apply_filters('theme_sidebar_footer_right_area_before_title',''),
  84.                 'after_title' => apply_filters('theme_sidebar_footer_right_area_after_title',''),
  85.             ));
  86.         }
  87.        
  88.         //register custom sidebars
  89.         $custom_sidebars = theme_get_option_from_db('sidebar','sidebars');
  90.         if(!empty($custom_sidebars)){
  91.             $custom_sidebar_names = explode(',',$custom_sidebars);
  92.             foreach ($custom_sidebar_names as $name){
  93.                 $slug = strtolower(preg_replace('/[^a-zA-Z0-9-]/', '-', $name));
  94.                 register_sidebar(array(
  95.                     'name' =>  $name,
  96.                     'id' => 'sidebar-custom-'.$slug,
  97.                     'description' => $name,
  98.                     'before_widget' => apply_filters('theme_sidebar_custom_before_widget','<section id="%1$s" class="widget %2$s">'),
  99.                     'after_widget' => apply_filters('theme_sidebar_custom_after_widget','</section>'),
  100.                     'before_title' => apply_filters('theme_sidebar_custom_before_title','<h3 class="widgettitle">'),
  101.                     'after_title' => apply_filters('theme_sidebar_custom_after_title','</h3>'),
  102.                 ));
  103.             }
  104.         }
  105.     }
  106.  
  107.     function set_sidebar($sidebar, $custom) {
  108.         if($custom && $this->custom_sidebar_available($custom)){
  109.             return $custom;
  110.         }
  111.  
  112.         return $sidebar;
  113.     }
  114.    
  115.     function get_sidebar($post_id){
  116.         $sidebar = '';
  117.         if(is_page()){
  118.             $sidebar = $this->sidebar_names['page'];
  119.         }
  120.         if(is_front_page() || $post_id == theme_get_option('homepage','home_page') ){
  121.             $home_page_id = theme_get_option('homepage','home_page');
  122.             $post_id = wpml_get_object_id($home_page_id,'page');
  123.             $sidebar = $this->sidebar_names['home'];
  124.         }
  125.         if(is_blog()){
  126.             $sidebar = $this->sidebar_names['blog'];
  127.         }
  128.         if(is_singular('post')){
  129.             $sidebar = $this->sidebar_names['blog'];
  130.         }elseif(is_singular('portfolio')){
  131.             $sidebar = $this->sidebar_names['portfolio'];
  132.         }
  133.         if(is_archive()){
  134.             $custom = theme_get_option('sidebar','archive');
  135.             if(!empty($custom)){
  136.                 $sidebar = $custom;
  137.             }else{
  138.                 $sidebar = $this->sidebar_names['blog'];
  139.             }
  140.         }
  141.  
  142.         if(is_search()){
  143.             $custom = theme_get_option('advanced','search_sidebar');
  144.             if(!empty($custom)){
  145.                 $sidebar = $custom;
  146.             }else{
  147.                 $sidebar = $this->sidebar_names['blog'];
  148.             }
  149.         }
  150.  
  151.         if(is_category()){
  152.             $custom = theme_get_option('sidebar','category');
  153.             $sidebar = $this->set_sidebar($sidebar, $custom);
  154.         }
  155.  
  156.         if(is_tag()){
  157.             $custom = theme_get_option('sidebar','tag');
  158.             $sidebar = $this->set_sidebar($sidebar, $custom);
  159.         }
  160.  
  161.         if(is_author()){
  162.             $custom = theme_get_option('sidebar','author');
  163.             $sidebar = $this->set_sidebar($sidebar, $custom);
  164.         }
  165.  
  166.         if(is_date()){
  167.             $custom = theme_get_option('sidebar','date');
  168.             $sidebar = $this->set_sidebar($sidebar, $custom);
  169.         }
  170.  
  171.         if(is_404()){
  172.             $custom = theme_get_option('advanced','404_sidebar');
  173.             $sidebar = $this->set_sidebar($sidebar, $custom);
  174.         }
  175.        
  176.         if(is_singular()){
  177.             $post_type_name = get_query_var( 'post_type' );
  178.             $custom = theme_get_option('sidebar','post_type_'.$post_type_name.'_sidebar');
  179.             $sidebar = $this->set_sidebar($sidebar, $custom);
  180.         }
  181.  
  182.         if(function_exists('is_post_type_archive')){
  183.             if(is_post_type_archive()){
  184.                 $custom = theme_get_option('sidebar','post_type_archive_'.get_query_var( 'post_type' ).'_sidebar');
  185.  
  186.                 $sidebar = $this->set_sidebar($sidebar, $custom);
  187.             }
  188.         }
  189.  
  190.         if(is_tax()){
  191.             $custom = theme_get_option('sidebar','taxonomy_'.get_query_var( 'taxonomy' ).'_sidebar');
  192.  
  193.             $sidebar = $this->set_sidebar($sidebar, $custom);
  194.         }
  195.         if(theme_get_option('advanced', 'woocommerce')){
  196.             if(is_singular( array('product') )){
  197.                 $custom = theme_get_option('advanced','woocommerce_product_sidebar');
  198.                 $sidebar = $this->set_sidebar($sidebar, $custom);
  199.             }
  200.             if((function_exists('is_post_type_archive') && is_post_type_archive('product')) || (function_exists('is_shop') && is_shop()) ){
  201.                 $custom = theme_get_option('advanced','woocommerce_shop_sidebar');
  202.                 $sidebar = $this->set_sidebar($sidebar, $custom);
  203.             }
  204.             if(is_tax( 'product_cat' )){
  205.                 $custom = theme_get_option('advanced','woocommerce_cat_sidebar');
  206.                 $sidebar = $this->set_sidebar($sidebar, $custom);
  207.             }
  208.             if(is_tax( 'product_tag' )){
  209.                 $custom = theme_get_option('advanced','woocommerce_tag_sidebar');
  210.                 $sidebar = $this->set_sidebar($sidebar, $custom);
  211.             }
  212.         }
  213.         if ( class_exists( 'Tribe__Events__Main' ) ) {
  214.             if ( tribe_is_list_view() || tribe_is_showing_all() || tribe_is_month() || tribe_is_day()) {
  215.                 $sidebar = $this->sidebar_names['page'];
  216.             }
  217.         }
  218.         if(!empty($post_id)){
  219.             $custom = get_post_meta($post_id, '_sidebar', true);
  220.             $sidebar = $this->set_sidebar($sidebar, $custom);
  221.         }
  222.         if(isset($sidebar)){
  223.             dynamic_sidebar($sidebar);
  224.         }
  225.     }
  226.    
  227.     function get_footer_sidebar(){
  228.         $keys = array_keys($this->footer_sidebar_names);
  229.         dynamic_sidebar($this->footer_sidebar_names[$keys[$this->footer_sidebar_count]]);
  230.         $this->footer_sidebar_count++;
  231.     }
  232. }
  233.  
  234. function sidebar_generator($function){
  235.     global $_sidebarGenerator;
  236.     $args = array_slice( func_get_args(), 1 );
  237.     return call_user_func_array(array( &$_sidebarGenerator, $function ), $args );
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement