Make WordPress Core

Changes between Version 1 and Version 2 of Ticket #17276, comment 10


Ignore:
Timestamp:
05/01/2011 08:34:58 PM (13 years ago)
Author:
scribu
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #17276, comment 10

    v1 v2  
    1111-- please don't be distracted by my rants within the comments...
    1212I was just going off on half baked ideas...
    13 
    14 
    15 {{{
    16 <?php
    17 /*
    18  *
    19  * @file: post-type-categories-widget.php
    20  *
    21  *
    22 
    23 
    24 Plugin Name: Post Type Categories Widget Test
    25 Plugin URI: http://mywebwizards.com/wordpress-tests/plugins/post-type-categories-widget/
    26 Description: This is just a plugin for testing - it symbolizes the hope and enthusiasm of an entire custom post type generation summed up in one widget supplied by WordPress and altered by squeeky: When activated you will see a new widget named <cite>Post Type Categories</cite> in the "Appearence" > "Widgets" page.
    27 Author: squeeky
    28 Version: 0.1
    29 Author URI: http://mywebwizards.com/
    30 
    31 
    32 
    33         ______________________________________________
    34         **********************************************
    35         Categories widget
    36         for relavant Taxonomies
    37         of ALL relavant Post Types                                      */
    38 
    39 class Post_Type_Categories_Widget extends WP_Widget {
    40 
    41         function Post_Type_Categories_Widget() {
    42                 $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories / and optiionally post type categories" ) );
    43                 $this->WP_Widget('post_type_categories', __('* Post Type Categories'), $widget_ops);
    44         }
    45 
    46         function widget( $args, $instance ) {
    47                 extract( $args );
    48 
    49                 $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Post Type Categories' ) : $instance['title'], $instance, $this->id_base);
    50                 $pt_cats = $instance['ptcats']; // ****** $pt_cats $instance ******
    51                 $c = $instance['count'] ? '1' : '0';
    52                 $h = $instance['hierarchical'] ? '1' : '0';
    53                 $d = $instance['dropdown'] ? '1' : '0';
    54 
    55                 echo $before_widget;
    56                 if ( $title )
    57                         echo $before_title . $title . $after_title;
    58 
    59                 // In case past taxonomy was deactivated
    60                 if ( taxonomy_exists($pt_cats) )
    61                         $pt_cat_tax = $pt_cats;
    62                 else
    63                         $pt_cat_tax = 'category';
    64 
    65                 $cat_args = array('taxonomy' => $pt_cat_tax, 'orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
    66 
    67                 if ( $d ) {
    68                         $cat_args['show_option_none'] = __('Select Category');
    69                         wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
    70 ?>
    71 
    72 <script type='text/javascript'>
    73 /* <![CDATA[ */
    74         var dropdown = document.getElementById("cat");
    75         function onCatChange() {
    76                 if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
    77                         location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
    78                 }
    79         }
    80         dropdown.onchange = onCatChange;
    81 /* ]]> */
    82 </script>
    83 
    84 <?php
    85                 } else {
    86 ?>
    87                 <ul>
    88 <?php
    89                 $cat_args['title_li'] = '';
    90                 wp_list_categories(apply_filters('widget_categories_args', $cat_args));
    91 ?>
    92                 </ul>
    93 <?php
    94                 }
    95 
    96                 echo $after_widget;
    97         }
    98 
    99         function update( $new_instance, $old_instance ) {
    100                 $instance = $old_instance;
    101                 $instance['title'] = strip_tags($new_instance['title']);
    102                 $instance['ptcats'] = $new_instance['ptcats']; // ****** $pt_cats $instance ******
    103                 $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
    104                 $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
    105                 $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
    106 
    107                 return $instance;
    108         }
    109 
    110         function form( $instance ) {
    111 
    112                 //Defaults
    113                 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
    114                 $title = esc_attr( $instance['title'] );
    115                 $pt_cats = esc_attr( $instance['ptcats'] ); // ****** $pt_cats $instance ******
    116                 $count = isset($instance['count']) ? (bool) $instance['count'] :false;
    117                 $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
    118                 $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
    119 
    120 ?>
    121                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
    122                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
    123 
    124                 <?php
    125 
    126                 // set arguments for custom post types
    127                 $args=array(
    128                         'public' => true,
    129                         '_builtin' => false,
    130                 );
    131 
    132 
    133 
    134                 /*      ______________________________________________
    135                         **********************************************
    136                         Probe custom post types and related taxonomies
    137 
    138                  * This function arrangement, or something in this direction,
    139                  * should really be deeper within the WordPress core,
    140                  * ( perhaps in post.php and/or taxonomy.php / depending )
    141                  * where it could be reached by "WordPress features" or even
    142                  * plugins and themes that could be utilized by custom post
    143                  * types and/or custom taxonomies.
    144                  *
    145                  * The idea behind this function arrangement should be
    146                  * expanded upon to determine other aspects of custom
    147                  * post types and custom taxonomies that may be easily
    148                  * intergrated into other existing WordPress, plugin,
    149                  * or theme features where such would make sense -
    150                  * like a sitemap template for example - ETC., ETC.
    151                  *
    152                  * Note on function's current state :
    153                  * I probably could have gone straight to the taxonomies,
    154                  * but I wanted to illustrate how a broad gathering of
    155                  * custom post types could be narrowed down to a specific
    156                  * feature - to hint at the notion of how other
    157                  * custom post type aspects and custom taxonomy aspects
    158                  * could be focused upon - and "pulled" where desired.
    159                  * This relates to my comment about how 'This function
    160                  * should be expanded upon' - perhaps in the format of
    161                  * a SWITCH where feature probes could be targeted more
    162                  * directly / more efficiantly...
    163                  *
    164                  *
    165                  * Here is some more BRAIN-STORMING (please excuse)...
    166                  *
    167                  * I guess in a way I am proposing a kind of custom post
    168                  * type / taxonomy API - or more accurately, a kind of
    169                  * custom post type / taxonomy SDK - making all easier
    170                  * for coders who in turn can then make it easier for
    171                  * end-users - I mean think about it: wouldn't it be
    172                  * cool if you could simple pop up something like:
    173                  *
    174                  * <?php cpt_info('list') ?>
    175                  *
    176                  * and/or maybe a member function
    177                  *
    178                  * <?php this->get_cpt_info('taxonomy') ?>
    179                  *
    180                  * with alternatively 'ctax_info' and 'get_ctax_info'
    181                  * where you could do things like:
    182                  *
    183                  * <?php if(my_type->get_ctax_info('hierarchical')) : ?>
    184                  *
    185                  * sort of like 'bloginfo' and 'get_bloginfo' instead of
    186                  * sorting through all those functions for this or that.
    187                  * OR maybe a custon_post_tax CLASS to reach into......
    188                  * (???????????????????????????????????????????????????)
    189                  *
    190                  * OPPS --- SO SORRY - I'm getting really carried away.
    191                  *
    192                  * --- Back to the functions at hand....
    193                  */
    194 
    195                 // IF custom post types exists
    196                 if( get_post_types( $args ) ) {
    197 
    198                         // array to collect custom post-type hierarchical taxonomies
    199                         // - in other words > the "custom post-type categories"
    200                         $custom_post_type_cats = (array) $custom_post_type_cats;
    201 
    202                         // 'foreach' custom post-type
    203                         foreach( get_post_types($args) as $cptype ) {
    204 
    205                                 // run through 'foreach' custom post-type's taxonomy
    206                                 // in case custom post-type has more than one taxonomy
    207                                 foreach( get_object_taxonomies($cptype) as $cptype_tax ) {
    208 
    209                                         // collect the taxonomies that are hierarchical
    210                                         if( is_taxonomy_hierarchical($cptype_tax) )
    211                                                 $custom_post_type_cats[] = $cptype_tax;
    212 
    213                                 }
    214 
    215                         }
    216 
    217                 }
    218 
    219                 // did we collect any "custom post types categories"
    220                 if( $custom_post_type_cats ) {
    221 
    222                         $current_pt_cats = $instance['ptcats']; // ****** $pt_cats $instance ******
    223 
    224 ?>
    225 
    226                 <p><label for="<?php echo $this->get_field_id('ptcats'); ?>"><?php _e('Select taxonomy:') ?></label>
    227                 <select class="widefat" id="<?php echo $this->get_field_id('ptcats'); ?>" name="<?php echo $this->get_field_name('ptcats'); ?>">
    228                         <option value="category" <?php selected('category', $current_pt_cats) ?>><?php _e('categories ( default / for post )') ?></option>
    229 <?php foreach ( $custom_post_type_cats as $pt_cats ) : ?>
    230                         <option value="<?php echo esc_attr($pt_cats) ?>" <?php selected($pt_cats, $current_pt_cats) ?>><?php echo $pt_cats; ?></option>
    231 <?php endforeach; ?>
    232                 </select></p>
    233 
    234 <?php
    235 
    236                 } else {
    237 
    238                         // set to our default:
    239                         $pt_cats = 'category';
    240 
    241                 }
    242 
    243  ?>
    244 
    245                 <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
    246                 <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
    247 
    248                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
    249                 <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
    250 
    251                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
    252                 <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
    253 <?php
    254 
    255         }
    256 
    257 }
    258 
    259 
    260 /**
    261  * Register widgets - Then call 'widgets_init' action
    262  * after the widget has been registered.
    263  *
    264  * @since 3.2.0 [ HOPEFULLY ]
    265  */
    266 function post_type_widgets_init() {
    267         if ( !is_blog_installed() )
    268                 return;
    269 
    270         register_widget('Post_Type_Categories_Widget');
    271 
    272         do_action('widgets_init');
    273 }
    274 
    275 add_action('init', 'post_type_widgets_init', 1);
    276 
    277 ?>
    278 
    279 }}}