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