class-walker-category-dropdown.php 2.4 KB

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