class-walker-category-dropdown.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 array
  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. *
  44. * @see Walker::start_el()
  45. *
  46. * @param string $output Used to append additional content (passed by reference).
  47. * @param object $category Category data object.
  48. * @param int $depth Depth of category. Used for padding.
  49. * @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
  50. * See wp_dropdown_categories().
  51. * @param int $id Optional. ID of the current category. Default 0 (unused).
  52. */
  53. public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
  54. $pad = str_repeat( '&nbsp;', $depth * 3 );
  55. /** This filter is documented in wp-includes/category-template.php */
  56. $cat_name = apply_filters( 'list_cats', $category->name, $category );
  57. if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
  58. $value_field = $args['value_field'];
  59. } else {
  60. $value_field = 'term_id';
  61. }
  62. $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . '"';
  63. // Type-juggling causes false matches, so we force everything to a string.
  64. if ( (string) $category->{$value_field} === (string) $args['selected'] ) {
  65. $output .= ' selected="selected"';
  66. }
  67. $output .= '>';
  68. $output .= $pad . $cat_name;
  69. if ( $args['show_count'] ) {
  70. $output .= '&nbsp;&nbsp;(' . number_format_i18n( $category->count ) . ')';
  71. }
  72. $output .= "</option>\n";
  73. }
  74. }