Make WordPress Core

Changeset 34376


Ignore:
Timestamp:
09/22/2015 04:24:53 AM (9 years ago)
Author:
wonderboymusic
Message:

Widgets: Make the categories widget work with custom taxonomies.

Props fonglh, wonderboymusic, DrewAPicture, kucrut.
Fixes #21165.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy-functions.php

    r34359 r34376  
    460460 * - name - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is Tags/Categories
    461461 * - singular_name - name for one object of this taxonomy. Default is Tag/Category
     462 * - select_name - prompt to select a taxonomy when using a dropdown list in the Categories widget. Default is 'Select Category'.
    462463 * - search_items - Default is Search Tags/Search Categories
    463464 * - popular_items - This string isn't used on hierarchical taxonomies. Default is Popular Tags
     
    498499        'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
    499500        'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
     501        'select_name' => array( null, __( 'Select Category' ) ),
    500502        'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
    501503        'popular_items' => array( __( 'Popular Tags' ), null ),
  • trunk/src/wp-includes/widgets/class-wp-widget-categories.php

    r33954 r34376  
    2323        static $first_dropdown = true;
    2424
     25        $current_taxonomy = $this->_get_current_taxonomy( $instance );
     26
    2527        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    2628        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base );
     
    3537        }
    3638
     39        $tax = get_taxonomy( $current_taxonomy );
     40
    3741        $cat_args = array(
    3842            'orderby'      => 'name',
    3943            'show_count'   => $c,
    40             'hierarchical' => $h
     44            'hierarchical' => $h,
     45            'taxonomy'     => $current_taxonomy,
    4146        );
    4247
     
    4752            echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
    4853
    49             $cat_args['show_option_none'] = __( 'Select Category' );
     54            $cat_args['show_option_none'] = $tax->labels->select_name;
    5055            $cat_args['id'] = $dropdown_id;
    5156
     
    110115        $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
    111116        $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
     117        $instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
    112118
    113119        return $instance;
     
    124130        $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
    125131        $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
     132        $current_taxonomy = $this->_get_current_taxonomy( $instance );
    126133?>
    127134        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
    128135        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
     136
     137        <p>
     138            <label for="<?php echo esc_attr( $this->get_field_id( 'taxonomy' ) ); ?>"><?php _e( 'Taxonomy:' ) ?></label>
     139            <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'taxonomy' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'taxonomy' ) ); ?>">
     140            <?php foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) : ?>
     141                <?php
     142                if ( ! $taxonomy->hierarchical || empty( $taxonomy->labels->name ) ) :
     143                    continue;
     144                endif;
     145                ?>
     146                <option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php selected( $taxonomy->name, $current_taxonomy ) ?>><?php echo esc_html( $taxonomy->labels->name ); ?></option>
     147            <?php endforeach; ?>
     148            </select>
     149        </p>
    129150
    130151        <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 ); ?> />
     
    139160    }
    140161
     162    /**
     163     * Retrieves the taxonomy for the current widget instance.
     164     *
     165     * @since 4.4.0
     166     * @access public
     167     *
     168     * @param array $instance Current settings.
     169     * @return string Name of the current taxonomy if set, otherwise 'category'.
     170     */
     171    public function _get_current_taxonomy( $instance ) {
     172        if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) {
     173            return $instance['taxonomy'];
     174        }
     175        return 'category';
     176    }
    141177}
Note: See TracChangeset for help on using the changeset viewer.