WordPress.org

Make WordPress Core

Ticket #21165: 21165.2.diff

File 21165.2.diff, 5.2 KB (added by fonglh, 6 years ago)

Second draft. Fixed issues mentioned by scribu.

  • wp-includes/default-widgets.php

     
    440440class WP_Widget_Categories extends WP_Widget {
    441441
    442442        function __construct() {
    443                 $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
     443                $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories or custom taxonomies" ) );
    444444                parent::__construct('categories', __('Categories'), $widget_ops);
    445445        }
    446446
    447447        function widget( $args, $instance ) {
    448448                extract( $args );
    449 
     449                $current_taxonomy = $this->_get_current_taxonomy( $instance );
    450450                $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base);
    451451                $c = ! empty( $instance['count'] ) ? '1' : '0';
    452452                $h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
     
    456456                if ( $title )
    457457                        echo $before_title . $title . $after_title;
    458458
    459                 $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
     459                $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => $current_taxonomy);
    460460
    461461                if ( $d ) {
    462                         $cat_args['show_option_none'] = __('Select Category');
     462                        $cat_args['show_option_none'] = get_taxonomy( $current_taxonomy )->labels->select_name;
    463463                        wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
    464464?>
    465465
     
    496496                $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
    497497                $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
    498498                $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
     499                $instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
    499500
    500501                return $instance;
    501502        }
     
    507508                $count = isset($instance['count']) ? (bool) $instance['count'] :false;
    508509                $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
    509510                $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
     511                $current_taxonomy = $this->_get_current_taxonomy( $instance );
    510512?>
    511513                <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
    512514                <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>
    513515
     516                <p><label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy:' ) ?></label>
     517                <select class="widefat" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>">
     518                <?php foreach ( get_taxonomies() as $taxonomy ) :
     519                                        $tax = get_taxonomy( $taxonomy );
     520                                        if ( ! $tax->hierarchical || empty( $tax->labels->name ) )
     521                                                continue;
     522                ?>
     523                <option value="<?php echo esc_attr( $taxonomy ) ?>" <?php selected( $taxonomy, $current_taxonomy ) ?>><?php echo $tax->labels->name; ?></option>
     524        <?php endforeach; ?>
     525        </select></p>
     526
    514527                <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 ); ?> />
    515528                <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
    516529
     
    522535<?php
    523536        }
    524537
     538        function _get_current_taxonomy($instance) {
     539                if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) )
     540                        return $instance['taxonomy'];
     541
     542                return 'category';
     543        }
     544
     545
    525546}
    526547
    527548/**
  • wp-includes/taxonomy.php

     
    4343
    4444        register_taxonomy( 'category', 'post', array(
    4545                'hierarchical' => true,
     46                'labels' => array(
     47                        'select_name' => __( 'Select Category' )
     48                ),
    4649                'query_var' => 'category_name',
    4750                'rewrite' => $rewrite['category'],
    4851                'public' => true,
     
    385388 * Accepted keys of the label array in the taxonomy object:
    386389 * - name - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is Tags/Categories
    387390 * - singular_name - name for one object of this taxonomy. Default is Tag/Category
     391 * - select_name - prompt to select a taxonomy when using a dropdown list in the Categories widget. Default is 'Select Taxonomy'.
    388392 * - search_items - Default is Search Tags/Search Categories
    389393 * - popular_items - This string isn't used on hierarchical taxonomies. Default is Popular Tags
    390394 * - all_items - Default is All Tags/All Categories
     
    413417        $nohier_vs_hier_defaults = array(
    414418                'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
    415419                'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
     420                'select_name' => array( null, __( 'Select Taxonomy' ) ),
    416421                'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
    417422                'popular_items' => array( __( 'Popular Tags' ), null ),
    418423                'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),