You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

126 regels
4.1 KiB

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