[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-walker-category.php (source)

   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(
  39          'parent' => 'parent',
  40          'id'     => 'term_id',
  41      );
  42  
  43      /**
  44       * Starts the list before the elements are added.
  45       *
  46       * @since 2.1.0
  47       *
  48       * @see Walker::start_lvl()
  49       *
  50       * @param string $output Used to append additional content. Passed by reference.
  51       * @param int    $depth  Optional. Depth of category. Used for tab indentation. Default 0.
  52       * @param array  $args   Optional. An array of arguments. Will only append content if style argument
  53       *                       value is 'list'. See wp_list_categories(). Default empty array.
  54       */
  55  	public function start_lvl( &$output, $depth = 0, $args = array() ) {
  56          if ( 'list' != $args['style'] ) {
  57              return;
  58          }
  59  
  60          $indent  = str_repeat( "\t", $depth );
  61          $output .= "$indent<ul class='children'>\n";
  62      }
  63  
  64      /**
  65       * Ends the list of after the elements are added.
  66       *
  67       * @since 2.1.0
  68       *
  69       * @see Walker::end_lvl()
  70       *
  71       * @param string $output Used to append additional content. Passed by reference.
  72       * @param int    $depth  Optional. Depth of category. Used for tab indentation. Default 0.
  73       * @param array  $args   Optional. An array of arguments. Will only append content if style argument
  74       *                       value is 'list'. See wp_list_categories(). Default empty array.
  75       */
  76  	public function end_lvl( &$output, $depth = 0, $args = array() ) {
  77          if ( 'list' != $args['style'] ) {
  78              return;
  79          }
  80  
  81          $indent  = str_repeat( "\t", $depth );
  82          $output .= "$indent</ul>\n";
  83      }
  84  
  85      /**
  86       * Starts the element output.
  87       *
  88       * @since 2.1.0
  89       *
  90       * @see Walker::start_el()
  91       *
  92       * @param string $output   Used to append additional content (passed by reference).
  93       * @param object $category Category data object.
  94       * @param int    $depth    Optional. Depth of category in reference to parents. Default 0.
  95       * @param array  $args     Optional. An array of arguments. See wp_list_categories(). Default empty array.
  96       * @param int    $id       Optional. ID of the current category. Default 0.
  97       */
  98  	public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
  99          /** This filter is documented in wp-includes/category-template.php */
 100          $cat_name = apply_filters(
 101              'list_cats',
 102              esc_attr( $category->name ),
 103              $category
 104          );
 105  
 106          // Don't generate an element if the category name is empty.
 107          if ( '' === $cat_name ) {
 108              return;
 109          }
 110  
 111          $atts         = array();
 112          $atts['href'] = get_term_link( $category );
 113  
 114          if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
 115              /**
 116               * Filters the category description for display.
 117               *
 118               * @since 1.2.0
 119               *
 120               * @param string $description Category description.
 121               * @param object $category    Category object.
 122               */
 123              $atts['title'] = strip_tags( apply_filters( 'category_description', $category->description, $category ) );
 124          }
 125  
 126          /**
 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          );
 157  
 158          if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
 159              $link .= ' ';
 160  
 161              if ( empty( $args['feed_image'] ) ) {
 162                  $link .= '(';
 163              }
 164  
 165              $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
 166  
 167              if ( empty( $args['feed'] ) ) {
 168                  /* translators: %s: category name */
 169                  $alt = ' alt="' . sprintf( __( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
 170              } else {
 171                  $alt   = ' alt="' . $args['feed'] . '"';
 172                  $name  = $args['feed'];
 173                  $link .= empty( $args['title'] ) ? '' : $args['title'];
 174              }
 175  
 176              $link .= '>';
 177  
 178              if ( empty( $args['feed_image'] ) ) {
 179                  $link .= $name;
 180              } else {
 181                  $link .= "<img src='" . esc_url( $args['feed_image'] ) . "'$alt" . ' />';
 182              }
 183              $link .= '</a>';
 184  
 185              if ( empty( $args['feed_image'] ) ) {
 186                  $link .= ')';
 187              }
 188          }
 189  
 190          if ( ! empty( $args['show_count'] ) ) {
 191              $link .= ' (' . number_format_i18n( $category->count ) . ')';
 192          }
 193          if ( 'list' == $args['style'] ) {
 194              $output     .= "\t<li";
 195              $css_classes = array(
 196                  'cat-item',
 197                  'cat-item-' . $category->term_id,
 198              );
 199  
 200              if ( ! empty( $args['current_category'] ) ) {
 201                  // 'current_category' can be an array, so we use `get_terms()`.
 202                  $_current_terms = get_terms(
 203                      $category->taxonomy,
 204                      array(
 205                          'include'    => $args['current_category'],
 206                          'hide_empty' => false,
 207                      )
 208                  );
 209  
 210                  foreach ( $_current_terms as $_current_term ) {
 211                      if ( $category->term_id == $_current_term->term_id ) {
 212                          $css_classes[] = 'current-cat';
 213                      } elseif ( $category->term_id == $_current_term->parent ) {
 214                          $css_classes[] = 'current-cat-parent';
 215                      }
 216                      while ( $_current_term->parent ) {
 217                          if ( $category->term_id == $_current_term->parent ) {
 218                              $css_classes[] = 'current-cat-ancestor';
 219                              break;
 220                          }
 221                          $_current_term = get_term( $_current_term->parent, $category->taxonomy );
 222                      }
 223                  }
 224              }
 225  
 226              /**
 227               * Filters the list of CSS classes to include with each category in the list.
 228               *
 229               * @since 4.2.0
 230               *
 231               * @see wp_list_categories()
 232               *
 233               * @param array  $css_classes An array of CSS classes to be applied to each list item.
 234               * @param object $category    Category data object.
 235               * @param int    $depth       Depth of page, used for padding.
 236               * @param array  $args        An array of wp_list_categories() arguments.
 237               */
 238              $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
 239              $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
 240  
 241              $output .= $css_classes;
 242              $output .= ">$link\n";
 243          } elseif ( isset( $args['separator'] ) ) {
 244              $output .= "\t$link" . $args['separator'] . "\n";
 245          } else {
 246              $output .= "\t$link<br />\n";
 247          }
 248      }
 249  
 250      /**
 251       * Ends the element output, if needed.
 252       *
 253       * @since 2.1.0
 254       *
 255       * @see Walker::end_el()
 256       *
 257       * @param string $output Used to append additional content (passed by reference).
 258       * @param object $page   Not used.
 259       * @param int    $depth  Optional. Depth of category. Not used.
 260       * @param array  $args   Optional. An array of arguments. Only uses 'list' for whether should append
 261       *                       to output. See wp_list_categories(). Default empty array.
 262       */
 263  	public function end_el( &$output, $page, $depth = 0, $args = array() ) {
 264          if ( 'list' != $args['style'] ) {
 265              return;
 266          }
 267  
 268          $output .= "</li>\n";
 269      }
 270  
 271  }


Generated: Sun Jul 21 01:00:03 2019 Cross-referenced by PHPXref 0.7.1