class-walker-category-checklist.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Taxonomy API: Walker_Category_Checklist class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core walker class to output an unordered list of category checkbox input elements.
  11. *
  12. * @since 2.5.1
  13. *
  14. * @see Walker
  15. * @see wp_category_checklist()
  16. * @see wp_terms_checklist()
  17. */
  18. class Walker_Category_Checklist extends Walker {
  19. public $tree_type = 'category';
  20. public $db_fields = array(
  21. 'parent' => 'parent',
  22. 'id' => 'term_id',
  23. ); //TODO: decouple this
  24. /**
  25. * Starts the list before the elements are added.
  26. *
  27. * @see Walker:start_lvl()
  28. *
  29. * @since 2.5.1
  30. *
  31. * @param string $output Used to append additional content (passed by reference).
  32. * @param int $depth Depth of category. Used for tab indentation.
  33. * @param array $args An array of arguments. @see wp_terms_checklist()
  34. */
  35. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  36. $indent = str_repeat( "\t", $depth );
  37. $output .= "$indent<ul class='children'>\n";
  38. }
  39. /**
  40. * Ends the list of after the elements are added.
  41. *
  42. * @see Walker::end_lvl()
  43. *
  44. * @since 2.5.1
  45. *
  46. * @param string $output Used to append additional content (passed by reference).
  47. * @param int $depth Depth of category. Used for tab indentation.
  48. * @param array $args An array of arguments. @see wp_terms_checklist()
  49. */
  50. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  51. $indent = str_repeat( "\t", $depth );
  52. $output .= "$indent</ul>\n";
  53. }
  54. /**
  55. * Start the element output.
  56. *
  57. * @see Walker::start_el()
  58. *
  59. * @since 2.5.1
  60. *
  61. * @param string $output Used to append additional content (passed by reference).
  62. * @param object $category The current term object.
  63. * @param int $depth Depth of the term in reference to parents. Default 0.
  64. * @param array $args An array of arguments. @see wp_terms_checklist()
  65. * @param int $id ID of the current term.
  66. */
  67. public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
  68. if ( empty( $args['taxonomy'] ) ) {
  69. $taxonomy = 'category';
  70. } else {
  71. $taxonomy = $args['taxonomy'];
  72. }
  73. if ( $taxonomy == 'category' ) {
  74. $name = 'post_category';
  75. } else {
  76. $name = 'tax_input[' . $taxonomy . ']';
  77. }
  78. $args['popular_cats'] = empty( $args['popular_cats'] ) ? array() : $args['popular_cats'];
  79. $class = in_array( $category->term_id, $args['popular_cats'] ) ? ' class="popular-category"' : '';
  80. $args['selected_cats'] = empty( $args['selected_cats'] ) ? array() : $args['selected_cats'];
  81. if ( ! empty( $args['list_only'] ) ) {
  82. $aria_checked = 'false';
  83. $inner_class = 'category';
  84. if ( in_array( $category->term_id, $args['selected_cats'] ) ) {
  85. $inner_class .= ' selected';
  86. $aria_checked = 'true';
  87. }
  88. $output .= "\n" . '<li' . $class . '>' .
  89. '<div class="' . $inner_class . '" data-term-id=' . $category->term_id .
  90. ' tabindex="0" role="checkbox" aria-checked="' . $aria_checked . '">' .
  91. /** This filter is documented in wp-includes/category-template.php */
  92. esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . '</div>';
  93. } else {
  94. $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" .
  95. '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="' . $name . '[]" id="in-' . $taxonomy . '-' . $category->term_id . '"' .
  96. checked( in_array( $category->term_id, $args['selected_cats'] ), true, false ) .
  97. disabled( empty( $args['disabled'] ), false, false ) . ' /> ' .
  98. /** This filter is documented in wp-includes/category-template.php */
  99. esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . '</label>';
  100. }
  101. }
  102. /**
  103. * Ends the element output, if needed.
  104. *
  105. * @see Walker::end_el()
  106. *
  107. * @since 2.5.1
  108. *
  109. * @param string $output Used to append additional content (passed by reference).
  110. * @param object $category The current term object.
  111. * @param int $depth Depth of the term in reference to parents. Default 0.
  112. * @param array $args An array of arguments. @see wp_terms_checklist()
  113. */
  114. public function end_el( &$output, $category, $depth = 0, $args = array() ) {
  115. $output .= "</li>\n";
  116. }
  117. }