Nessuna descrizione

class-walker-category-checklist.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @since 5.9.0 Renamed `$category` to `$data_object` and `$id` to `$current_object_id`
  61. * to match parent class for PHP 8 named parameter support.
  62. *
  63. * @param string $output Used to append additional content (passed by reference).
  64. * @param WP_Term $data_object The current term object.
  65. * @param int $depth Depth of the term in reference to parents. Default 0.
  66. * @param array $args An array of arguments. @see wp_terms_checklist()
  67. * @param int $current_object_id Optional. ID of the current term. Default 0.
  68. */
  69. public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
  70. // Restores the more descriptive, specific name for use within this method.
  71. $category = $data_object;
  72. if ( empty( $args['taxonomy'] ) ) {
  73. $taxonomy = 'category';
  74. } else {
  75. $taxonomy = $args['taxonomy'];
  76. }
  77. if ( 'category' === $taxonomy ) {
  78. $name = 'post_category';
  79. } else {
  80. $name = 'tax_input[' . $taxonomy . ']';
  81. }
  82. $args['popular_cats'] = ! empty( $args['popular_cats'] ) ? array_map( 'intval', $args['popular_cats'] ) : array();
  83. $class = in_array( $category->term_id, $args['popular_cats'], true ) ? ' class="popular-category"' : '';
  84. $args['selected_cats'] = ! empty( $args['selected_cats'] ) ? array_map( 'intval', $args['selected_cats'] ) : array();
  85. if ( ! empty( $args['list_only'] ) ) {
  86. $aria_checked = 'false';
  87. $inner_class = 'category';
  88. if ( in_array( $category->term_id, $args['selected_cats'], true ) ) {
  89. $inner_class .= ' selected';
  90. $aria_checked = 'true';
  91. }
  92. $output .= "\n" . '<li' . $class . '>' .
  93. '<div class="' . $inner_class . '" data-term-id=' . $category->term_id .
  94. ' tabindex="0" role="checkbox" aria-checked="' . $aria_checked . '">' .
  95. /** This filter is documented in wp-includes/category-template.php */
  96. esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . '</div>';
  97. } else {
  98. $is_selected = in_array( $category->term_id, $args['selected_cats'], true );
  99. $is_disabled = ! empty( $args['disabled'] );
  100. $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" .
  101. '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="' . $name . '[]" id="in-' . $taxonomy . '-' . $category->term_id . '"' .
  102. checked( $is_selected, true, false ) .
  103. disabled( $is_disabled, true, false ) . ' /> ' .
  104. /** This filter is documented in wp-includes/category-template.php */
  105. esc_html( apply_filters( 'the_category', $category->name, '', '' ) ) . '</label>';
  106. }
  107. }
  108. /**
  109. * Ends the element output, if needed.
  110. *
  111. * @see Walker::end_el()
  112. *
  113. * @since 2.5.1
  114. * @since 5.9.0 Renamed `$category` to `$data_object` to match parent class for PHP 8 named parameter support.
  115. *
  116. * @param string $output Used to append additional content (passed by reference).
  117. * @param WP_Term $data_object The current term object.
  118. * @param int $depth Depth of the term in reference to parents. Default 0.
  119. * @param array $args An array of arguments. @see wp_terms_checklist()
  120. */
  121. public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
  122. $output .= "</li>\n";
  123. }
  124. }