WordPress.org

Make WordPress Core

Changeset 10809


Ignore:
Timestamp:
03/18/2009 01:50:20 AM (9 years ago)
Author:
ryan
Message:

Move categories widget to WP_Widget. see #8441

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/default-widgets.php

    r10808 r10809  
    8484}
    8585
    86 
    8786/**
    8887 * Links widget class
     
    363362
    364363/**
    365  * Display categories widget.
    366  *
    367  * Allows multiple category widgets.
    368  *
    369  * @since 2.2.0
    370  *
    371  * @param array $args Widget arguments.
    372  * @param int $number Widget number.
    373  */
    374 function wp_widget_categories($args, $widget_args = 1) {
    375     extract($args, EXTR_SKIP);
    376     if ( is_numeric($widget_args) )
    377         $widget_args = array( 'number' => $widget_args );
    378     $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    379     extract($widget_args, EXTR_SKIP);
    380 
    381     $options = get_option('widget_categories');
    382     if ( !isset($options[$number]) )
    383         return;
    384 
    385     $c = $options[$number]['count'] ? '1' : '0';
    386     $h = $options[$number]['hierarchical'] ? '1' : '0';
    387     $d = $options[$number]['dropdown'] ? '1' : '0';
    388 
    389     $title = empty($options[$number]['title']) ? __('Categories') : apply_filters('widget_title', $options[$number]['title']);
    390 
    391     echo $before_widget;
    392     echo $before_title . $title . $after_title;
    393 
    394     $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
    395 
    396     if ( $d ) {
    397         $cat_args['show_option_none'] = __('Select Category');
    398         wp_dropdown_categories($cat_args);
     364 * Categories widget class
     365 *
     366 * @since 2.8.0
     367 */
     368class WP_Widget_Categories extends WP_Widget {
     369
     370    function WP_Widget_Categories() {
     371        $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
     372        $this->WP_Widget('categories', __('Categories'), $widget_ops);
     373    }
     374
     375    function widget( $args, $instance ) {
     376        extract( $args );
     377
     378        $title = empty( $instance['title'] ) ? __( 'Categories' ) : apply_filters('widget_title', $instance['title']);
     379        $c = $instance['count'] ? '1' : '0';
     380        $h = $instance['hierarchical'] ? '1' : '0';
     381        $d = $instance['dropdown'] ? '1' : '0';
     382
     383        echo $before_widget;
     384        echo $before_title . $title . $after_title;
     385
     386        $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
     387
     388        if ( $d ) {
     389            $cat_args['show_option_none'] = __('Select Category');
     390            wp_dropdown_categories($cat_args);
    399391?>
    400392
     
    412404
    413405<?php
    414     } else {
     406        } else {
    415407?>
    416408        <ul>
     
    421413        </ul>
    422414<?php
    423     }
    424 
    425     echo $after_widget;
    426 }
    427 
    428 /**
    429  * Display and process categories widget options form.
    430  *
    431  * @since 2.2.0
    432  *
    433  * @param int $widget_args Widget number.
    434  */
    435 function wp_widget_categories_control( $widget_args ) {
    436     global $wp_registered_widgets;
    437     static $updated = false;
    438 
    439     if ( is_numeric($widget_args) )
    440         $widget_args = array( 'number' => $widget_args );
    441     $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    442     extract($widget_args, EXTR_SKIP);
    443 
    444     $options = get_option('widget_categories');
    445 
    446     if ( !is_array( $options ) )
    447         $options = array();
    448 
    449     if ( !$updated && !empty($_POST['sidebar']) ) {
    450         $sidebar = (string) $_POST['sidebar'];
    451 
    452         $sidebars_widgets = wp_get_sidebars_widgets();
    453         if ( isset($sidebars_widgets[$sidebar]) )
    454             $this_sidebar =& $sidebars_widgets[$sidebar];
    455         else
    456             $this_sidebar = array();
    457 
    458         foreach ( (array) $this_sidebar as $_widget_id ) {
    459             if ( 'wp_widget_categories' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
    460                 $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
    461                 if ( !in_array( "categories-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
    462                     unset($options[$widget_number]);
    463             }
    464         }
    465 
    466         foreach ( (array) $_POST['widget-categories'] as $widget_number => $widget_cat ) {
    467             if ( !isset($widget_cat['title']) && isset($options[$widget_number]) ) // user clicked cancel
    468                 continue;
    469             $title = trim(strip_tags(stripslashes($widget_cat['title'])));
    470             $count = isset($widget_cat['count']);
    471             $hierarchical = isset($widget_cat['hierarchical']);
    472             $dropdown = isset($widget_cat['dropdown']);
    473             $options[$widget_number] = compact( 'title', 'count', 'hierarchical', 'dropdown' );
    474         }
    475 
    476         update_option('widget_categories', $options);
    477         $updated = true;
    478     }
    479 
    480     if ( -1 == $number ) {
    481         $title = '';
    482         $count = false;
    483         $hierarchical = false;
    484         $dropdown = false;
    485         $number = '%i%';
    486     } else {
    487         $title = attribute_escape( $options[$number]['title'] );
    488         $count = (bool) $options[$number]['count'];
    489         $hierarchical = (bool) $options[$number]['hierarchical'];
    490         $dropdown = (bool) $options[$number]['dropdown'];
    491     }
     415        }
     416
     417        echo $after_widget;     
     418    }
     419   
     420    function update( $new_instance, $old_instance ) {
     421        $instance = $old_instance;
     422        $instance['title'] = strip_tags($new_instance['title']);
     423        $instance['count'] = $new_instance['count'] ? 1 : 0;
     424        $instance['hierarchical'] = $new_instance['hierarchical'] ? 1 : 0;
     425        $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
     426
     427        return $instance;
     428    }
     429   
     430    function form( $instance ) {
     431        //Defaults
     432        $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
     433        $title = attribute_escape( $instance['title'] );
     434        $count = (bool) $instance['count'];
     435        $hierarchical = (bool) $instance['hierarchical'];
     436        $dropdown = (bool) $instance['dropdown'];
    492437?>
    493438            <p>
    494                 <label for="categories-title-<?php echo $number; ?>">
     439                <label for="<?php echo $this->get_field_id('title'); ?>">
    495440                    <?php _e( 'Title:' ); ?>
    496                     <input class="widefat" id="categories-title-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" />
     441                    <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; ?>" />
    497442                </label>
    498443            </p>
    499444
    500445            <p>
    501                 <label for="categories-dropdown-<?php echo $number; ?>">
    502                     <input type="checkbox" class="checkbox" id="categories-dropdown-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][dropdown]"<?php checked( $dropdown ); ?> />
     446                <label for="<?php echo $this->get_field_id('dropdown'); ?>">
     447                    <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
    503448                    <?php _e( 'Show as dropdown' ); ?>
    504449                </label>
    505450                <br />
    506                 <label for="categories-count-<?php echo $number; ?>">
    507                     <input type="checkbox" class="checkbox" id="categories-count-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][count]"<?php checked( $count ); ?> />
     451                <label for="<?php echo $this->get_field_id('count'); ?>">
     452                    <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
    508453                    <?php _e( 'Show post counts' ); ?>
    509454                </label>
    510455                <br />
    511                 <label for="categories-hierarchical-<?php echo $number; ?>">
    512                     <input type="checkbox" class="checkbox" id="categories-hierarchical-<?php echo $number; ?>" name="widget-categories[<?php echo $number; ?>][hierarchical]"<?php checked( $hierarchical ); ?> />
     456                <label for="<?php echo $this->get_field_id('hierarchical'); ?>">
     457                    <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
    513458                    <?php _e( 'Show hierarchy' ); ?>
    514459                </label>
    515460            </p>
    516 
    517             <input type="hidden" name="widget-categories[<?php echo $number; ?>][submit]" value="1" />
    518 <?php
    519 }
    520 
    521 /**
    522  * Register categories widget on startup.
    523  *
    524  * @since 2.3.0
    525  */
    526 function wp_widget_categories_register() {
    527     if ( !$options = get_option( 'widget_categories' ) )
    528         $options = array();
    529 
    530     if ( isset($options['title']) )
    531         $options = wp_widget_categories_upgrade();
    532 
    533     $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
    534 
    535     $name = __( 'Categories' );
    536 
    537     $id = false;
    538     foreach ( (array) array_keys($options) as $o ) {
    539         // Old widgets can have null values for some reason
    540         if ( !isset($options[$o]['title']) )
    541             continue;
    542         $id = "categories-$o";
    543         wp_register_sidebar_widget( $id, $name, 'wp_widget_categories', $widget_ops, array( 'number' => $o ) );
    544         wp_register_widget_control( $id, $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => $o ) );
    545     }
    546 
    547     // If there are none, we register the widget's existance with a generic template
    548     if ( !$id ) {
    549         wp_register_sidebar_widget( 'categories-1', $name, 'wp_widget_categories', $widget_ops, array( 'number' => -1 ) );
    550         wp_register_widget_control( 'categories-1', $name, 'wp_widget_categories_control', array( 'id_base' => 'categories' ), array( 'number' => -1 ) );
    551     }
    552 }
    553 
    554 /**
    555  * Upgrade previous category widget to current version.
    556  *
    557  * @since 2.3.0
    558  *
    559  * @return array
    560  */
    561 function wp_widget_categories_upgrade() {
    562     $options = get_option( 'widget_categories' );
    563 
    564     if ( !isset( $options['title'] ) )
    565         return $options;
    566 
    567     $newoptions = array( 1 => $options );
    568 
    569     update_option( 'widget_categories', $newoptions );
    570 
    571     $sidebars_widgets = get_option( 'sidebars_widgets' );
    572     if ( is_array( $sidebars_widgets ) ) {
    573         foreach ( $sidebars_widgets as $sidebar => $widgets ) {
    574             if ( is_array( $widgets ) ) {
    575                 foreach ( $widgets as $widget )
    576                     $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget;
    577             } else {
    578                 $new_widgets[$sidebar] = $widgets;
    579             }
    580         }
    581         if ( $new_widgets != $sidebars_widgets )
    582             update_option( 'sidebars_widgets', $new_widgets );
    583     }
    584 
    585     return $newoptions;
     461<?php
     462    }
     463
    586464}
    587465
     
    12341112    register_widget('WP_Widget_Text');
    12351113
     1114    register_widget('WP_Widget_Categories');
     1115
    12361116    $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
    12371117    wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);
     
    12421122    wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' );
    12431123
    1244     wp_widget_categories_register();
    12451124    wp_widget_rss_register();
    12461125    wp_widget_recent_comments_register();
Note: See TracChangeset for help on using the changeset viewer.