wp/wp-includes/class-walker-category-dropdown.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * Taxonomy API: Walker_CategoryDropdown class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Template
       
     7  * @since 4.4.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class used to create an HTML dropdown list of Categories.
       
    12  *
       
    13  * @since 2.1.0
       
    14  *
       
    15  * @see Walker
       
    16  */
       
    17 class Walker_CategoryDropdown extends Walker {
       
    18 
       
    19 	/**
       
    20 	 * What the class handles.
       
    21 	 *
       
    22 	 * @since 2.1.0
       
    23 	 * @var string
       
    24 	 *
       
    25 	 * @see Walker::$tree_type
       
    26 	 */
       
    27 	public $tree_type = 'category';
       
    28 
       
    29 	/**
       
    30 	 * Database fields to use.
       
    31 	 *
       
    32 	 * @since 2.1.0
       
    33 	 * @todo Decouple this
       
    34 	 * @var array
       
    35 	 *
       
    36 	 * @see Walker::$db_fields
       
    37 	 */
       
    38 	public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
       
    39 
       
    40 	/**
       
    41 	 * Starts the element output.
       
    42 	 *
       
    43 	 * @since 2.1.0
       
    44 	 *
       
    45 	 * @see Walker::start_el()
       
    46 	 *
       
    47 	 * @param string $output   Used to append additional content (passed by reference).
       
    48 	 * @param object $category Category data object.
       
    49 	 * @param int    $depth    Depth of category. Used for padding.
       
    50 	 * @param array  $args     Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
       
    51 	 *                         See wp_dropdown_categories().
       
    52 	 * @param int    $id       Optional. ID of the current category. Default 0 (unused).
       
    53 	 */
       
    54 	public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
       
    55 		$pad = str_repeat('&nbsp;', $depth * 3);
       
    56 
       
    57 		/** This filter is documented in wp-includes/category-template.php */
       
    58 		$cat_name = apply_filters( 'list_cats', $category->name, $category );
       
    59 
       
    60 		if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
       
    61 			$value_field = $args['value_field'];
       
    62 		} else {
       
    63 			$value_field = 'term_id';
       
    64 		}
       
    65 
       
    66 		$output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\"";
       
    67 
       
    68 		// Type-juggling causes false matches, so we force everything to a string.
       
    69 		if ( (string) $category->{$value_field} === (string) $args['selected'] )
       
    70 			$output .= ' selected="selected"';
       
    71 		$output .= '>';
       
    72 		$output .= $pad.$cat_name;
       
    73 		if ( $args['show_count'] )
       
    74 			$output .= '&nbsp;&nbsp;('. number_format_i18n( $category->count ) .')';
       
    75 		$output .= "</option>\n";
       
    76 	}
       
    77 }