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