SHARE
TWEET

WP Display Widgets plugin: Custom Post Type Archive support

a guest May 7th, 2013 375 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Display widgets
  4. Plugin URI: http://strategy11.com/display-widgets/
  5. Description: Adds checkboxes to each widget to show or hide on site pages.
  6. Author: Strategy11
  7. Author URI: http://strategy11.com
  8. Version: 1.24
  9. */
  10.  
  11. load_plugin_textdomain( 'display-widgets', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
  12.  
  13. add_filter('widget_display_callback', 'show_dw_widget');
  14. add_action('in_widget_form', 'dw_show_hide_widget_options', 10, 3);
  15. add_filter('widget_update_callback', 'dw_update_widget_options', 10, 3);
  16.  
  17. function show_dw_widget($instance){
  18.     global $wp_query;
  19.     $post_id = $wp_query->get_queried_object_id();
  20.     $post_id = dw_get_lang_id($post_id, 'page');
  21.    
  22.     if(defined('ICL_LANGUAGE_CODE'))
  23.         $show = isset($instance['lang-'. ICL_LANGUAGE_CODE]) ? ($instance['lang-'. ICL_LANGUAGE_CODE]) : false;
  24.  
  25.     if (is_home()){
  26.         $show = isset($instance['page-home']) ? ($instance['page-home']) : false;
  27.     }else if (is_front_page()){
  28.         $show = isset($instance['page-front']) ? ($instance['page-front']) : false;
  29.     }else if (is_category()){
  30.         $show = isset($instance['cat-'. get_query_var('cat')]) ? ($instance['cat-'. get_query_var('cat')]) : false;
  31.     }else if(is_tax()){
  32.         $term = get_queried_object();
  33.         $show = isset($instance['tax-'. $term->taxonomy]) ? ($instance['tax-'. $term->taxonomy]) : false;
  34.         unset($term);
  35.     }else if (is_archive()){
  36.         $show = isset($instance['page-archive']) ? ($instance['page-archive']) : false;
  37.         if (!$show) {
  38.             if(function_exists('get_post_type')){
  39.                 $type = get_post_type();
  40.                 $show = isset($instance['type-'. $type.'-archive']) ? ($instance['type-'. $type.'-archive']) : false;
  41.             }
  42.         }
  43.  
  44.     }else if (is_single()){
  45.         if(function_exists('get_post_type')){
  46.             $type = get_post_type();
  47.             if($type != 'page' and $type != 'post')
  48.                 $show = isset($instance['type-'. $type]) ? ($instance['type-'. $type]) : false;
  49.         }
  50.        
  51.         if(!isset($show))
  52.             $show = isset($instance['page-single']) ? ($instance['page-single']) : false;
  53.            
  54.         if (!$show){
  55.             $cats = get_the_category();
  56.             foreach($cats as $cat){
  57.                 if ($show) continue;
  58.                 $cat_id = dw_get_lang_id($cat->cat_ID, 'category');
  59.                 if (isset($instance['cat-'. $cat_id]))
  60.                     $show = $instance['cat-'. $cat_id];
  61.             }
  62.         }
  63.     }else if (is_404()){
  64.         $show = isset($instance['page-404']) ? ($instance['page-404']) : false;
  65.     }else if (is_search()){
  66.         $show = isset($instance['page-search']) ? ($instance['page-search']) : false;
  67.     }else if($post_id){
  68.         $show = isset($instance['page-'. $post_id]) ? ($instance['page-'. $post_id]) : false;
  69.     }
  70.        
  71.     if ($post_id and !$show and isset($instance['other_ids']) and !empty($instance['other_ids'])){
  72.         $other_ids = explode(',', $instance['other_ids']);
  73.         foreach($other_ids as $other_id){
  74.             if($post_id == (int)$other_id)
  75.                 $show = true;
  76.         }
  77.     }
  78.    
  79.     if(!isset($show))
  80.         $show = false;
  81.        
  82.     $instance['dw_include'] = isset($instance['dw_include']) ? $instance['dw_include'] : (isset($instance['include']) ? $instance['include'] : 0);
  83.     $instance['dw_logout'] = isset($instance['dw_logout']) ? $instance['dw_logout'] : (isset($instance['logout']) ? $instance['logout'] : 0);
  84.     $instance['dw_login'] = isset($instance['dw_login']) ? $instance['dw_login'] : (isset($instance['login']) ? $instance['login'] : 0);
  85.  
  86.     if (($instance['dw_include'] and $show == false) or ($instance['dw_include'] == 0 and $show)){
  87.         return false;
  88.     }else{
  89.         global $user_ID;
  90.         if( (isset($instance['dw_logout']) and $instance['dw_logout'] and $user_ID) or
  91.             (isset($instance['dw_login']) and $instance['dw_login'] and !$user_ID))
  92.             return false;
  93.            
  94.     }
  95.  
  96.         return $instance;
  97. }
  98.  
  99. function dw_show_hide_widget_options($widget, $return, $instance){
  100.     dw_register_globals();
  101.    
  102.     global $dw_pages, $dw_cats, $dw_taxes, $dw_cposts, $dw_checked, $dw_loaded, $dw_langs;
  103.  
  104.     $wp_page_types = array(
  105.         'front' => __('Front', 'display-widgets'),
  106.         'home' => __('Blog', 'display-widgets'),
  107.         'archive' => __('Archives', 'display-widgets'),
  108.         'single' => __('Single Post', 'display-widgets'),
  109.         '404' => '404', 'search' => __('Search', 'display-widgets')
  110.     );
  111.            
  112.     $instance['dw_include'] = isset($instance['dw_include']) ? $instance['dw_include'] : (isset($instance['include']) ? $instance['include'] : 0);
  113.     $instance['dw_logout'] = isset($instance['dw_logout']) ? $instance['dw_logout'] : (isset($instance['logout']) ? $instance['logout'] : 0);
  114.     $instance['dw_login'] = isset($instance['dw_login']) ? $instance['dw_login'] : (isset($instance['login']) ? $instance['login'] : 0);
  115.     $instance['other_ids'] = isset($instance['other_ids']) ? $instance['other_ids'] : '';
  116. ?>  
  117.      <p>
  118.         <label for="<?php echo $widget->get_field_id('dw_include'); ?>"><?php _e('Show/Hide Widget', 'display-widgets') ?></label>
  119.         <select name="<?php echo $widget->get_field_name('dw_include'); ?>" id="<?php echo $widget->get_field_id('dw_include'); ?>" class="widefat">
  120.             <option value="0"><?php _e('Hide on checked', 'display-widgets') ?></option>
  121.             <option value="1" <?php echo selected( $instance['dw_include'], 1 ) ?>><?php _e('Show on checked', 'display-widgets') ?></option>
  122.         </select>
  123.     </p>    
  124.  
  125. <div style="height:150px; overflow:auto; border:1px solid #dfdfdf;">
  126.     <p><input class="checkbox" type="checkbox" <?php checked($instance['dw_logout'], true) ?> id="<?php echo $widget->get_field_id('dw_logout'); ?>" name="<?php echo $widget->get_field_name('dw_logout'); ?>" value="1" />
  127.     <label for="<?php echo $widget->get_field_id('dw_logout'); ?>"><?php _e('Show only for Logged-out users', 'display-widgets') ?></label></p>
  128.     <p><input class="checkbox" type="checkbox" <?php checked($instance['dw_login'], true) ?> id="<?php echo $widget->get_field_id('dw_login'); ?>" name="<?php echo $widget->get_field_name('dw_login'); ?>" value="1" />
  129.     <label for="<?php echo $widget->get_field_id('dw_login'); ?>"><?php _e('Show only for Logged-in users', 'display-widgets') ?></label></p>
  130.    
  131.     <h4 onclick="dw_toggle(jQuery(this))" style="cursor:pointer;"><?php _e('Miscellaneous', 'display-widgets') ?> +/-</h4>
  132.     <div class="dw_collapse">
  133.     <?php foreach ($wp_page_types as $key => $label){
  134.         $instance['page-'. $key] = isset($instance['page-'. $key]) ? $instance['page-'. $key] : false;
  135.     ?>
  136.         <p><input class="checkbox" type="checkbox" <?php checked($instance['page-'. $key], true) ?> id="<?php echo $widget->get_field_id('page-'. $key); ?>" name="<?php echo $widget->get_field_name('page-'. $key); ?>" />
  137.         <label for="<?php echo $widget->get_field_id('page-'. $key); ?>"><?php echo $label .' '. __('Page', 'display-widgets') ?></label></p>
  138.     <?php } ?>
  139.     </div>
  140.    
  141.     <h4 onclick="dw_toggle(jQuery(this))" style="cursor:pointer;"><?php _e('Pages', 'display-widgets') ?> +/-</h4>
  142.     <div class="dw_collapse">
  143.     <?php foreach ($dw_pages as $page){
  144.         $instance['page-'. $page->ID] = isset($instance['page-'. $page->ID]) ? $instance['page-'. $page->ID] : false;  
  145.     ?>
  146.         <p><input class="checkbox" type="checkbox" <?php checked($instance['page-'. $page->ID], true) ?> id="<?php echo $widget->get_field_id('page-'. $page->ID); ?>" name="<?php echo $widget->get_field_name('page-'. $page->ID); ?>" />
  147.         <label for="<?php echo $widget->get_field_id('page-'. $page->ID); ?>"><?php echo $page->post_title ?></label></p>
  148.     <?php       }  ?>
  149.     </div>
  150.    
  151.     <?php if(isset($dw_cposts) and !empty($dw_cposts)){ ?>
  152.     <h4 onclick="dw_toggle(jQuery(this))" style="cursor:pointer;"><?php _e('Custom Post Types', 'display-widgets') ?> +/-</h4>
  153.     <div class="dw_collapse">
  154.     <?php foreach ($dw_cposts as $post_key => $custom_post){
  155.         $instance['type-'. $post_key] = isset($instance['type-'. $post_key]) ? $instance['type-'. $post_key] : false;
  156.     ?>
  157.         <p><input class="checkbox" type="checkbox" <?php checked($instance['type-'. $post_key], true) ?> id="<?php echo $widget->get_field_id('type-'. $post_key); ?>" name="<?php echo $widget->get_field_name('type-'. $post_key); ?>" />
  158.         <label for="<?php echo $widget->get_field_id('type-'. $post_key); ?>"><?php echo stripslashes($custom_post->labels->name) ?></label></p>
  159.     <?php } ?>
  160.     </div>
  161.     <?php } ?>
  162.  
  163.     <?php if(isset($dw_cposts) and !empty($dw_cposts)){ ?>
  164.     <h4 onclick="dw_toggle(jQuery(this))" style="cursor:pointer;"><?php _e('Custom Post Type Archives', 'display-widgets') ?> +/-</h4>
  165.     <div class="dw_collapse">
  166.     <?php foreach ($dw_cposts as $post_key => $custom_post){
  167.         $instance['type-'. $post_key.'-archive'] = isset($instance['type-'. $post_key.'-archive']) ? $instance['type-'. $post_key.'-archive'] : false;
  168.     ?>
  169.         <p><input class="checkbox" type="checkbox" <?php checked($instance['type-'. $post_key.'-archive'], true) ?> id="<?php echo $widget->get_field_id('type-'. $post_key.'-archive'); ?>" name="<?php echo $widget->get_field_name('type-'. $post_key.'-archive'); ?>" />
  170.         <label for="<?php echo $widget->get_field_id('type-'. $post_key.'-archive'); ?>"><?php echo stripslashes($custom_post->labels->name) ?> Archive</label></p>
  171.     <?php } ?>
  172.     </div>
  173.     <?php } ?>
  174.    
  175.     <h4 onclick="dw_toggle(jQuery(this))" style="cursor:pointer;"><?php _e('Categories', 'display-widgets') ?> +/-</h4>
  176.     <div class="dw_collapse">
  177.     <?php foreach ($dw_cats as $cat){
  178.         $instance['cat-'. $cat->cat_ID] = isset($instance['cat-'. $cat->cat_ID]) ? $instance['cat-'. $cat->cat_ID] : false;  
  179.     ?>
  180.         <p><input class="checkbox" type="checkbox" <?php checked($instance['cat-'. $cat->cat_ID], true) ?> id="<?php echo $widget->get_field_id('cat-'. $cat->cat_ID); ?>" name="<?php echo $widget->get_field_name('cat-'. $cat->cat_ID); ?>" />
  181.         <label for="<?php echo $widget->get_field_id('cat-'. $cat->cat_ID); ?>"><?php echo $cat->cat_name ?></label></p>
  182.     <?php
  183.         unset($cat);
  184.         }
  185.     ?>
  186.     </div>
  187.    
  188.     <?php if(!empty($dw_taxes)){ ?>
  189.     <h4 onclick="dw_toggle(jQuery(this))" style="cursor:pointer;"><?php _e('Taxonomies', 'display-widgets') ?> +/-</h4>
  190.     <div class="dw_collapse">
  191.     <?php foreach ($dw_taxes as $tax){
  192.         $instance['tax-'. $tax] = isset($instance['tax-'. $tax]) ? $instance['tax-'. $tax] : false;  
  193.     ?>
  194.         <p><input class="checkbox" type="checkbox" <?php checked($instance['tax-'. $tax], true) ?> id="<?php echo $widget->get_field_id('tax-'. $tax); ?>" name="<?php echo $widget->get_field_name('tax-'. $tax); ?>" />
  195.         <label for="<?php echo $widget->get_field_id('tax-'. $tax); ?>"><?php echo str_replace(array('_','-'), ' ', ucfirst($tax)) ?></label></p>
  196.     <?php
  197.         unset($tax);
  198.         }
  199.     ?>
  200.     </div>
  201.     <?php } ?>
  202.    
  203.     <?php if(isset($dw_langs) and !empty($dw_langs)){ ?>
  204.     <h4 onclick="dw_toggle(jQuery(this))" style="cursor:pointer;"><?php _e('Languages', 'display-widgets') ?> +/-</h4>
  205.     <div class="dw_collapse">
  206.     <?php foreach($dw_langs as $lang){
  207.                 $key = $lang['language_code'];
  208.         $instance['lang-'. $key] = isset($instance['lang-'. $key]) ? $instance['lang-'. $key] : false;
  209.     ?>
  210.         <p><input class="checkbox" type="checkbox" <?php checked($instance['lang-'. $key], true) ?> id="<?php echo $widget->get_field_id('lang-'. $key); ?>" name="<?php echo $widget->get_field_name('lang-'. $key); ?>" />
  211.         <label for="<?php echo $widget->get_field_id('lang-'. $key); ?>"><?php echo $lang['native_name'] ?></label></p>
  212.        
  213.     <?php
  214.         unset($lang);
  215.         unset($key);
  216.         }
  217.     ?>
  218.     </div>
  219.     <?php } ?>
  220.    
  221.     <p><label for="<?php echo $widget->get_field_id('other_ids'); ?>"><?php _e('Comma Separated list of IDs of posts not listed above', 'display-widgets') ?>:</label>
  222.     <input type="text" value="<?php echo $instance['other_ids'] ?>" name="<?php echo $widget->get_field_name('other_ids'); ?>" id="<?php echo $widget->get_field_id('other_ids'); ?>" />
  223.     </p>
  224.     </div>
  225. <?php if(!$dw_loaded){ ?>
  226. <script type="text/javascript">function dw_toggle($this){$this.next('.dw_collapse').toggle();}</script>
  227. <?php
  228.         $dw_loaded = true;
  229.     }
  230. }
  231.  
  232. function dw_update_widget_options($instance, $new_instance, $old_instance){
  233.     dw_register_globals();
  234.    
  235.     global $dw_pages, $dw_cats, $dw_taxes, $dw_cposts, $dw_checked, $dw_langs;
  236.    
  237.     if($dw_pages){
  238.         foreach ($dw_pages as $page){
  239.             if(isset($new_instance['page-'. $page->ID]))
  240.                 $instance['page-'. $page->ID] = 1;
  241.             else if(isset($instance['page-'. $page->ID]))
  242.                 unset($instance['page-'. $page->ID]);
  243.             unset($page);
  244.         }
  245.     }
  246.    
  247.     foreach ($dw_cats as $cat){
  248.         if(isset($new_instance['cat-'. $cat->cat_ID]))
  249.             $instance['cat-'. $cat->cat_ID] = 1;
  250.         else if(isset($instance['cat-'. $cat->cat_ID]))
  251.             unset($instance['cat-'. $cat->cat_ID]);
  252.         unset($cat);
  253.     }
  254.    
  255.     if($dw_cposts){
  256.         foreach ($dw_cposts as $post_key => $custom_post){
  257.             if(isset($new_instance['type-'. $post_key]))
  258.                 $instance['type-'. $post_key] = 1;
  259.             else if(isset($instance['type-'. $post_key]))
  260.                 unset($instance['type-'. $post_key]);
  261.  
  262.             if(isset($new_instance['type-'. $post_key.'-archive']))
  263.                 $instance['type-'. $post_key.'-archive'] = 1;
  264.             else if(isset($instance['type-'. $post_key.'-archive']))
  265.                 unset($instance['type-'. $post_key.'-archive']);
  266.  
  267.             unset($custom_post);
  268.         }
  269.     }
  270.    
  271.     if($dw_taxes){
  272.         foreach ($dw_taxes as $tax){
  273.             if(isset($new_instance['tax-'. $tax]))
  274.                 $instance['tax-'. $tax] = 1;
  275.             else if(isset($instance['tax-'. $tax]))
  276.                 unset($instance['tax-'. $tax]);
  277.             unset($tax);
  278.         }
  279.     }
  280.    
  281.     if($dw_langs){
  282.         foreach($dw_langs as $lang){
  283.             if(isset($new_instance['lang-'. $lang['language_code'] ]))
  284.                 $instance['lang-'. $lang['language_code']] = 1;
  285.             else if(isset($instance['lang-'. $lang['language_code']]))
  286.                 unset($instance['lang-'. $lang['language_code']]);
  287.             unset($lang);
  288.         }    
  289.     }
  290.          
  291.     $instance['dw_include'] = $new_instance['dw_include'] ? 1 : 0;
  292.     if(isset($new_instance['dw_logout']))
  293.         $instance['dw_logout'] =  $new_instance['dw_logout'];
  294.     if(isset($new_instance['dw_login']))
  295.         $instance['dw_login'] = $new_instance['dw_login'];
  296.     $instance['other_ids'] = $new_instance['other_ids'] ? $new_instance['other_ids'] : '';
  297.    
  298.     foreach(array('front', 'home', 'archive', 'single', '404', 'search') as $page){
  299.         if(isset($new_instance['page-'. $page]))
  300.             $instance['page-'. $page] = 1;
  301.         else if(isset($instance['page-'. $page]))
  302.             unset($instance['page-'. $page]);
  303.     }
  304.  
  305.     return $instance;
  306. }
  307.  
  308. function dw_register_globals(){
  309.     global $dw_pages, $dw_cats, $dw_taxes, $dw_cposts, $dw_checked, $dw_langs;
  310.    
  311.     if(!$dw_checked){
  312.         if(!$dw_pages)
  313.             $dw_pages = get_posts( array(
  314.                 'post_type' => 'page', 'post_status' => 'publish',
  315.                 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC'
  316.             ));
  317.        
  318.         if(!$dw_cats)
  319.             $dw_cats = get_categories(array('hide_empty' => false));    
  320.            
  321.         if(!$dw_cposts and function_exists('get_post_types')){
  322.             $dw_cposts = get_post_types(array(), 'object');
  323.             foreach(array('revision','post','page','attachment','nav_menu_item') as $unset)
  324.                 unset($dw_cposts[$unset]);
  325.                
  326.             $dw_taxes = array();
  327.            
  328.             foreach($dw_cposts as $c => $type){
  329.                 $post_taxes = get_object_taxonomies($c);
  330.                 foreach($post_taxes as $post_tax)
  331.                     $dw_taxes[] = $post_tax;
  332.             }
  333.         }
  334.        
  335.         if(!$dw_langs and function_exists('icl_get_languages'))
  336.             $dw_langs = icl_get_languages('skip_missing=0&orderby=code');
  337.  
  338.         $dw_checked = true;
  339.     }
  340.  
  341. }
  342.  
  343. /* WPML support */
  344. function dw_get_lang_id($id, $type='page'){
  345.     global $dw_wpml_support;
  346.    
  347.     if(!$dw_wpml_support)
  348.         $dw_wpml_support = (function_exists('icl_object_id')) ? 'true' : 'false';
  349.    
  350.     if($dw_wpml_support == 'true')
  351.         $id = icl_object_id($id, $type, true);
  352.    
  353.     return $id;
  354. }
  355.  
  356. //TODO: Add text field that accepts full urls that will be checked under 'else'
RAW Paste Data
Want to get better at PHP?
Learn to code PHP in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top