wp/wp-includes/class-walker-category.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * Taxonomy API: Walker_Category class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Template
       
     7  * @since 4.4.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class used to create an HTML list of categories.
       
    12  *
       
    13  * @since 2.1.0
       
    14  *
       
    15  * @see Walker
       
    16  */
       
    17 class Walker_Category extends Walker {
       
    18 
       
    19 	/**
       
    20 	 * What the class handles.
       
    21 	 *
       
    22 	 * @since 2.1.0
       
    23 	 * @var string
       
    24 	 *
       
    25 	 * @see Walker::$tree_type
       
    26 	 */
       
    27 	public $tree_type = 'category';
       
    28 
       
    29 	/**
       
    30 	 * Database fields to use.
       
    31 	 *
       
    32 	 * @since 2.1.0
       
    33 	 * @var array
       
    34 	 *
       
    35 	 * @see Walker::$db_fields
       
    36 	 * @todo Decouple this
       
    37 	 */
       
    38 	public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
       
    39 
       
    40 	/**
       
    41 	 * Starts the list before the elements are added.
       
    42 	 *
       
    43 	 * @since 2.1.0
       
    44 	 *
       
    45 	 * @see Walker::start_lvl()
       
    46 	 *
       
    47 	 * @param string $output Used to append additional content. Passed by reference.
       
    48 	 * @param int    $depth  Optional. Depth of category. Used for tab indentation. Default 0.
       
    49 	 * @param array  $args   Optional. An array of arguments. Will only append content if style argument
       
    50 	 *                       value is 'list'. See wp_list_categories(). Default empty array.
       
    51 	 */
       
    52 	public function start_lvl( &$output, $depth = 0, $args = array() ) {
       
    53 		if ( 'list' != $args['style'] )
       
    54 			return;
       
    55 
       
    56 		$indent = str_repeat("\t", $depth);
       
    57 		$output .= "$indent<ul class='children'>\n";
       
    58 	}
       
    59 
       
    60 	/**
       
    61 	 * Ends the list of after the elements are added.
       
    62 	 *
       
    63 	 * @since 2.1.0
       
    64 	 *
       
    65 	 * @see Walker::end_lvl()
       
    66 	 *
       
    67 	 * @param string $output Used to append additional content. Passed by reference.
       
    68 	 * @param int    $depth  Optional. Depth of category. Used for tab indentation. Default 0.
       
    69 	 * @param array  $args   Optional. An array of arguments. Will only append content if style argument
       
    70 	 *                       value is 'list'. See wp_list_categories(). Default empty array.
       
    71 	 */
       
    72 	public function end_lvl( &$output, $depth = 0, $args = array() ) {
       
    73 		if ( 'list' != $args['style'] )
       
    74 			return;
       
    75 
       
    76 		$indent = str_repeat("\t", $depth);
       
    77 		$output .= "$indent</ul>\n";
       
    78 	}
       
    79 
       
    80 	/**
       
    81 	 * Starts the element output.
       
    82 	 *
       
    83 	 * @since 2.1.0
       
    84 	 *
       
    85 	 * @see Walker::start_el()
       
    86 	 *
       
    87 	 * @param string $output   Used to append additional content (passed by reference).
       
    88 	 * @param object $category Category data object.
       
    89 	 * @param int    $depth    Optional. Depth of category in reference to parents. Default 0.
       
    90 	 * @param array  $args     Optional. An array of arguments. See wp_list_categories(). Default empty array.
       
    91 	 * @param int    $id       Optional. ID of the current category. Default 0.
       
    92 	 */
       
    93 	public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
       
    94 		/** This filter is documented in wp-includes/category-template.php */
       
    95 		$cat_name = apply_filters(
       
    96 			'list_cats',
       
    97 			esc_attr( $category->name ),
       
    98 			$category
       
    99 		);
       
   100 
       
   101 		// Don't generate an element if the category name is empty.
       
   102 		if ( ! $cat_name ) {
       
   103 			return;
       
   104 		}
       
   105 
       
   106 		$link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
       
   107 		if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
       
   108 			/**
       
   109 			 * Filters the category description for display.
       
   110 			 *
       
   111 			 * @since 1.2.0
       
   112 			 *
       
   113 			 * @param string $description Category description.
       
   114 			 * @param object $category    Category object.
       
   115 			 */
       
   116 			$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
       
   117 		}
       
   118 
       
   119 		$link .= '>';
       
   120 		$link .= $cat_name . '</a>';
       
   121 
       
   122 		if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
       
   123 			$link .= ' ';
       
   124 
       
   125 			if ( empty( $args['feed_image'] ) ) {
       
   126 				$link .= '(';
       
   127 			}
       
   128 
       
   129 			$link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
       
   130 
       
   131 			if ( empty( $args['feed'] ) ) {
       
   132 				$alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
       
   133 			} else {
       
   134 				$alt = ' alt="' . $args['feed'] . '"';
       
   135 				$name = $args['feed'];
       
   136 				$link .= empty( $args['title'] ) ? '' : $args['title'];
       
   137 			}
       
   138 
       
   139 			$link .= '>';
       
   140 
       
   141 			if ( empty( $args['feed_image'] ) ) {
       
   142 				$link .= $name;
       
   143 			} else {
       
   144 				$link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
       
   145 			}
       
   146 			$link .= '</a>';
       
   147 
       
   148 			if ( empty( $args['feed_image'] ) ) {
       
   149 				$link .= ')';
       
   150 			}
       
   151 		}
       
   152 
       
   153 		if ( ! empty( $args['show_count'] ) ) {
       
   154 			$link .= ' (' . number_format_i18n( $category->count ) . ')';
       
   155 		}
       
   156 		if ( 'list' == $args['style'] ) {
       
   157 			$output .= "\t<li";
       
   158 			$css_classes = array(
       
   159 				'cat-item',
       
   160 				'cat-item-' . $category->term_id,
       
   161 			);
       
   162 
       
   163 			if ( ! empty( $args['current_category'] ) ) {
       
   164 				// 'current_category' can be an array, so we use `get_terms()`.
       
   165 				$_current_terms = get_terms( $category->taxonomy, array(
       
   166 					'include' => $args['current_category'],
       
   167 					'hide_empty' => false,
       
   168 				) );
       
   169 
       
   170 				foreach ( $_current_terms as $_current_term ) {
       
   171 					if ( $category->term_id == $_current_term->term_id ) {
       
   172 						$css_classes[] = 'current-cat';
       
   173 					} elseif ( $category->term_id == $_current_term->parent ) {
       
   174 						$css_classes[] = 'current-cat-parent';
       
   175 					}
       
   176 					while ( $_current_term->parent ) {
       
   177 						if ( $category->term_id == $_current_term->parent ) {
       
   178 							$css_classes[] =  'current-cat-ancestor';
       
   179 							break;
       
   180 						}
       
   181 						$_current_term = get_term( $_current_term->parent, $category->taxonomy );
       
   182 					}
       
   183 				}
       
   184 			}
       
   185 
       
   186 			/**
       
   187 			 * Filters the list of CSS classes to include with each category in the list.
       
   188 			 *
       
   189 			 * @since 4.2.0
       
   190 			 *
       
   191 			 * @see wp_list_categories()
       
   192 			 *
       
   193 			 * @param array  $css_classes An array of CSS classes to be applied to each list item.
       
   194 			 * @param object $category    Category data object.
       
   195 			 * @param int    $depth       Depth of page, used for padding.
       
   196 			 * @param array  $args        An array of wp_list_categories() arguments.
       
   197 			 */
       
   198 			$css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
       
   199 
       
   200 			$output .=  ' class="' . $css_classes . '"';
       
   201 			$output .= ">$link\n";
       
   202 		} elseif ( isset( $args['separator'] ) ) {
       
   203 			$output .= "\t$link" . $args['separator'] . "\n";
       
   204 		} else {
       
   205 			$output .= "\t$link<br />\n";
       
   206 		}
       
   207 	}
       
   208 
       
   209 	/**
       
   210 	 * Ends the element output, if needed.
       
   211 	 *
       
   212 	 * @since 2.1.0
       
   213 	 *
       
   214 	 * @see Walker::end_el()
       
   215 	 *
       
   216 	 * @param string $output Used to append additional content (passed by reference).
       
   217 	 * @param object $page   Not used.
       
   218 	 * @param int    $depth  Optional. Depth of category. Not used.
       
   219 	 * @param array  $args   Optional. An array of arguments. Only uses 'list' for whether should append
       
   220 	 *                       to output. See wp_list_categories(). Default empty array.
       
   221 	 */
       
   222 	public function end_el( &$output, $page, $depth = 0, $args = array() ) {
       
   223 		if ( 'list' != $args['style'] )
       
   224 			return;
       
   225 
       
   226 		$output .= "</li>\n";
       
   227 	}
       
   228 
       
   229 }