wp/wp-includes/class-walker-category.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    33 	 * @var array
    33 	 * @var array
    34 	 *
    34 	 *
    35 	 * @see Walker::$db_fields
    35 	 * @see Walker::$db_fields
    36 	 * @todo Decouple this
    36 	 * @todo Decouple this
    37 	 */
    37 	 */
    38 	public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
    38 	public $db_fields = array(
       
    39 		'parent' => 'parent',
       
    40 		'id'     => 'term_id',
       
    41 	);
    39 
    42 
    40 	/**
    43 	/**
    41 	 * Starts the list before the elements are added.
    44 	 * Starts the list before the elements are added.
    42 	 *
    45 	 *
    43 	 * @since 2.1.0
    46 	 * @since 2.1.0
    48 	 * @param int    $depth  Optional. Depth of category. Used for tab indentation. Default 0.
    51 	 * @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
    52 	 * @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.
    53 	 *                       value is 'list'. See wp_list_categories(). Default empty array.
    51 	 */
    54 	 */
    52 	public function start_lvl( &$output, $depth = 0, $args = array() ) {
    55 	public function start_lvl( &$output, $depth = 0, $args = array() ) {
    53 		if ( 'list' != $args['style'] )
    56 		if ( 'list' != $args['style'] ) {
    54 			return;
    57 			return;
    55 
    58 		}
    56 		$indent = str_repeat("\t", $depth);
    59 
       
    60 		$indent  = str_repeat( "\t", $depth );
    57 		$output .= "$indent<ul class='children'>\n";
    61 		$output .= "$indent<ul class='children'>\n";
    58 	}
    62 	}
    59 
    63 
    60 	/**
    64 	/**
    61 	 * Ends the list of after the elements are added.
    65 	 * Ends the list of after the elements are added.
    68 	 * @param int    $depth  Optional. Depth of category. Used for tab indentation. Default 0.
    72 	 * @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
    73 	 * @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.
    74 	 *                       value is 'list'. See wp_list_categories(). Default empty array.
    71 	 */
    75 	 */
    72 	public function end_lvl( &$output, $depth = 0, $args = array() ) {
    76 	public function end_lvl( &$output, $depth = 0, $args = array() ) {
    73 		if ( 'list' != $args['style'] )
    77 		if ( 'list' != $args['style'] ) {
    74 			return;
    78 			return;
    75 
    79 		}
    76 		$indent = str_repeat("\t", $depth);
    80 
       
    81 		$indent  = str_repeat( "\t", $depth );
    77 		$output .= "$indent</ul>\n";
    82 		$output .= "$indent</ul>\n";
    78 	}
    83 	}
    79 
    84 
    80 	/**
    85 	/**
    81 	 * Starts the element output.
    86 	 * Starts the element output.
    97 			esc_attr( $category->name ),
   102 			esc_attr( $category->name ),
    98 			$category
   103 			$category
    99 		);
   104 		);
   100 
   105 
   101 		// Don't generate an element if the category name is empty.
   106 		// Don't generate an element if the category name is empty.
   102 		if ( ! $cat_name ) {
   107 		if ( '' === $cat_name ) {
   103 			return;
   108 			return;
   104 		}
   109 		}
   105 
   110 
   106 		$link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
   111 		$atts         = array();
       
   112 		$atts['href'] = get_term_link( $category );
       
   113 
   107 		if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
   114 		if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
   108 			/**
   115 			/**
   109 			 * Filters the category description for display.
   116 			 * Filters the category description for display.
   110 			 *
   117 			 *
   111 			 * @since 1.2.0
   118 			 * @since 1.2.0
   112 			 *
   119 			 *
   113 			 * @param string $description Category description.
   120 			 * @param string $description Category description.
   114 			 * @param object $category    Category object.
   121 			 * @param object $category    Category object.
   115 			 */
   122 			 */
   116 			$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
   123 			$atts['title'] = strip_tags( apply_filters( 'category_description', $category->description, $category ) );
   117 		}
   124 		}
   118 
   125 
   119 		$link .= '>';
   126 		/**
   120 		$link .= $cat_name . '</a>';
   127 		 * Filters the HTML attributes applied to a category list item's anchor element.
       
   128 		 *
       
   129 		 * @since 5.2.0
       
   130 		 *
       
   131 		 * @param array   $atts {
       
   132 		 *     The HTML attributes applied to the list item's `<a>` element, empty strings are ignored.
       
   133 		 *
       
   134 		 *     @type string $href  The href attribute.
       
   135 		 *     @type string $title The title attribute.
       
   136 		 * }
       
   137 		 * @param WP_Term $category Term data object.
       
   138 		 * @param int     $depth    Depth of category, used for padding.
       
   139 		 * @param array   $args     An array of arguments.
       
   140 		 * @param int     $id       ID of the current category.
       
   141 		 */
       
   142 		$atts = apply_filters( 'category_list_link_attributes', $atts, $category, $depth, $args, $id );
       
   143 
       
   144 		$attributes = '';
       
   145 		foreach ( $atts as $attr => $value ) {
       
   146 			if ( ! empty( $value ) ) {
       
   147 				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
       
   148 				$attributes .= ' ' . $attr . '="' . $value . '"';
       
   149 			}
       
   150 		}
       
   151 
       
   152 		$link = sprintf(
       
   153 			'<a%s>%s</a>',
       
   154 			$attributes,
       
   155 			$cat_name
       
   156 		);
   121 
   157 
   122 		if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
   158 		if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
   123 			$link .= ' ';
   159 			$link .= ' ';
   124 
   160 
   125 			if ( empty( $args['feed_image'] ) ) {
   161 			if ( empty( $args['feed_image'] ) ) {
   127 			}
   163 			}
   128 
   164 
   129 			$link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
   165 			$link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
   130 
   166 
   131 			if ( empty( $args['feed'] ) ) {
   167 			if ( empty( $args['feed'] ) ) {
   132 				$alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
   168 				/* translators: %s: category name */
       
   169 				$alt = ' alt="' . sprintf( __( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
   133 			} else {
   170 			} else {
   134 				$alt = ' alt="' . $args['feed'] . '"';
   171 				$alt   = ' alt="' . $args['feed'] . '"';
   135 				$name = $args['feed'];
   172 				$name  = $args['feed'];
   136 				$link .= empty( $args['title'] ) ? '' : $args['title'];
   173 				$link .= empty( $args['title'] ) ? '' : $args['title'];
   137 			}
   174 			}
   138 
   175 
   139 			$link .= '>';
   176 			$link .= '>';
   140 
   177 
   141 			if ( empty( $args['feed_image'] ) ) {
   178 			if ( empty( $args['feed_image'] ) ) {
   142 				$link .= $name;
   179 				$link .= $name;
   143 			} else {
   180 			} else {
   144 				$link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
   181 				$link .= "<img src='" . esc_url( $args['feed_image'] ) . "'$alt" . ' />';
   145 			}
   182 			}
   146 			$link .= '</a>';
   183 			$link .= '</a>';
   147 
   184 
   148 			if ( empty( $args['feed_image'] ) ) {
   185 			if ( empty( $args['feed_image'] ) ) {
   149 				$link .= ')';
   186 				$link .= ')';
   152 
   189 
   153 		if ( ! empty( $args['show_count'] ) ) {
   190 		if ( ! empty( $args['show_count'] ) ) {
   154 			$link .= ' (' . number_format_i18n( $category->count ) . ')';
   191 			$link .= ' (' . number_format_i18n( $category->count ) . ')';
   155 		}
   192 		}
   156 		if ( 'list' == $args['style'] ) {
   193 		if ( 'list' == $args['style'] ) {
   157 			$output .= "\t<li";
   194 			$output     .= "\t<li";
   158 			$css_classes = array(
   195 			$css_classes = array(
   159 				'cat-item',
   196 				'cat-item',
   160 				'cat-item-' . $category->term_id,
   197 				'cat-item-' . $category->term_id,
   161 			);
   198 			);
   162 
   199 
   163 			if ( ! empty( $args['current_category'] ) ) {
   200 			if ( ! empty( $args['current_category'] ) ) {
   164 				// 'current_category' can be an array, so we use `get_terms()`.
   201 				// 'current_category' can be an array, so we use `get_terms()`.
   165 				$_current_terms = get_terms( $category->taxonomy, array(
   202 				$_current_terms = get_terms(
   166 					'include' => $args['current_category'],
   203 					$category->taxonomy,
   167 					'hide_empty' => false,
   204 					array(
   168 				) );
   205 						'include'    => $args['current_category'],
       
   206 						'hide_empty' => false,
       
   207 					)
       
   208 				);
   169 
   209 
   170 				foreach ( $_current_terms as $_current_term ) {
   210 				foreach ( $_current_terms as $_current_term ) {
   171 					if ( $category->term_id == $_current_term->term_id ) {
   211 					if ( $category->term_id == $_current_term->term_id ) {
   172 						$css_classes[] = 'current-cat';
   212 						$css_classes[] = 'current-cat';
   173 					} elseif ( $category->term_id == $_current_term->parent ) {
   213 					} elseif ( $category->term_id == $_current_term->parent ) {
   174 						$css_classes[] = 'current-cat-parent';
   214 						$css_classes[] = 'current-cat-parent';
   175 					}
   215 					}
   176 					while ( $_current_term->parent ) {
   216 					while ( $_current_term->parent ) {
   177 						if ( $category->term_id == $_current_term->parent ) {
   217 						if ( $category->term_id == $_current_term->parent ) {
   178 							$css_classes[] =  'current-cat-ancestor';
   218 							$css_classes[] = 'current-cat-ancestor';
   179 							break;
   219 							break;
   180 						}
   220 						}
   181 						$_current_term = get_term( $_current_term->parent, $category->taxonomy );
   221 						$_current_term = get_term( $_current_term->parent, $category->taxonomy );
   182 					}
   222 					}
   183 				}
   223 				}
   194 			 * @param object $category    Category data object.
   234 			 * @param object $category    Category data object.
   195 			 * @param int    $depth       Depth of page, used for padding.
   235 			 * @param int    $depth       Depth of page, used for padding.
   196 			 * @param array  $args        An array of wp_list_categories() arguments.
   236 			 * @param array  $args        An array of wp_list_categories() arguments.
   197 			 */
   237 			 */
   198 			$css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
   238 			$css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
   199 
   239 			$css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
   200 			$output .=  ' class="' . $css_classes . '"';
   240 
       
   241 			$output .= $css_classes;
   201 			$output .= ">$link\n";
   242 			$output .= ">$link\n";
   202 		} elseif ( isset( $args['separator'] ) ) {
   243 		} elseif ( isset( $args['separator'] ) ) {
   203 			$output .= "\t$link" . $args['separator'] . "\n";
   244 			$output .= "\t$link" . $args['separator'] . "\n";
   204 		} else {
   245 		} else {
   205 			$output .= "\t$link<br />\n";
   246 			$output .= "\t$link<br />\n";
   218 	 * @param int    $depth  Optional. Depth of category. Not used.
   259 	 * @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
   260 	 * @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.
   261 	 *                       to output. See wp_list_categories(). Default empty array.
   221 	 */
   262 	 */
   222 	public function end_el( &$output, $page, $depth = 0, $args = array() ) {
   263 	public function end_el( &$output, $page, $depth = 0, $args = array() ) {
   223 		if ( 'list' != $args['style'] )
   264 		if ( 'list' != $args['style'] ) {
   224 			return;
   265 			return;
       
   266 		}
   225 
   267 
   226 		$output .= "</li>\n";
   268 		$output .= "</li>\n";
   227 	}
   269 	}
   228 
   270 
   229 }
   271 }