[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Taxonomy API: Core category-specific functionality
   4   *
   5   * @package WordPress
   6   * @subpackage Taxonomy
   7   */
   8  
   9  /**
  10   * Retrieve list of category objects.
  11   *
  12   * If you change the type to 'link' in the arguments, then the link categories
  13   * will be returned instead. Also all categories will be updated to be backward
  14   * compatible with pre-2.3 plugins and themes.
  15   *
  16   * @since 2.1.0
  17   * @see get_terms() Type of arguments that can be changed.
  18   *
  19   * @param string|array $args {
  20   *     Optional. Arguments to retrieve categories. See get_terms() for additional options.
  21   *
  22   *     @type string $taxonomy Taxonomy to retrieve terms for. In this case, default 'category'.
  23   * }
  24   * @return array List of categories.
  25   */
  26  function get_categories( $args = '' ) {
  27      $defaults = array( 'taxonomy' => 'category' );
  28      $args     = wp_parse_args( $args, $defaults );
  29  
  30      /**
  31       * Filters the taxonomy used to retrieve terms when calling get_categories().
  32       *
  33       * @since 2.7.0
  34       *
  35       * @param string $taxonomy Taxonomy to retrieve terms from.
  36       * @param array  $args     An array of arguments. See get_terms().
  37       */
  38      $args['taxonomy'] = apply_filters( 'get_categories_taxonomy', $args['taxonomy'], $args );
  39  
  40      // Back compat
  41      if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
  42          _deprecated_argument(
  43              __FUNCTION__,
  44              '3.0.0',
  45              sprintf(
  46                  /* translators: 1: "type => link", 2: "taxonomy => link_category" */
  47                  __( '%1$s is deprecated. Use %2$s instead.' ),
  48                  '<code>type => link</code>',
  49                  '<code>taxonomy => link_category</code>'
  50              )
  51          );
  52          $args['taxonomy'] = 'link_category';
  53      }
  54  
  55      $categories = get_terms( $args );
  56  
  57      if ( is_wp_error( $categories ) ) {
  58          $categories = array();
  59      } else {
  60          $categories = (array) $categories;
  61          foreach ( array_keys( $categories ) as $k ) {
  62              _make_cat_compat( $categories[ $k ] );
  63          }
  64      }
  65  
  66      return $categories;
  67  }
  68  
  69  /**
  70   * Retrieves category data given a category ID or category object.
  71   *
  72   * If you pass the $category parameter an object, which is assumed to be the
  73   * category row object retrieved the database. It will cache the category data.
  74   *
  75   * If you pass $category an integer of the category ID, then that category will
  76   * be retrieved from the database, if it isn't already cached, and pass it back.
  77   *
  78   * If you look at get_term(), then both types will be passed through several
  79   * filters and finally sanitized based on the $filter parameter value.
  80   *
  81   * The category will converted to maintain backward compatibility.
  82   *
  83   * @since 1.5.1
  84   *
  85   * @param int|object $category Category ID or Category row object
  86   * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a
  87   *                       WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
  88   * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
  89   * @return object|array|WP_Error|null Category data in type defined by $output parameter.
  90   *                                    WP_Error if $category is empty, null if it does not exist.
  91   */
  92  function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
  93      $category = get_term( $category, 'category', $output, $filter );
  94  
  95      if ( is_wp_error( $category ) ) {
  96          return $category;
  97      }
  98  
  99      _make_cat_compat( $category );
 100  
 101      return $category;
 102  }
 103  
 104  /**
 105   * Retrieve category based on URL containing the category slug.
 106   *
 107   * Breaks the $category_path parameter up to get the category slug.
 108   *
 109   * Tries to find the child path and will return it. If it doesn't find a
 110   * match, then it will return the first category matching slug, if $full_match,
 111   * is set to false. If it does not, then it will return null.
 112   *
 113   * It is also possible that it will return a WP_Error object on failure. Check
 114   * for it when using this function.
 115   *
 116   * @since 2.1.0
 117   *
 118   * @param string $category_path URL containing category slugs.
 119   * @param bool   $full_match    Optional. Whether full path should be matched.
 120   * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 121   *                              a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 122   * @return WP_Term|array|WP_Error|null Type is based on $output value.
 123   */
 124  function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
 125      $category_path  = rawurlencode( urldecode( $category_path ) );
 126      $category_path  = str_replace( '%2F', '/', $category_path );
 127      $category_path  = str_replace( '%20', ' ', $category_path );
 128      $category_paths = '/' . trim( $category_path, '/' );
 129      $leaf_path      = sanitize_title( basename( $category_paths ) );
 130      $category_paths = explode( '/', $category_paths );
 131      $full_path      = '';
 132      foreach ( (array) $category_paths as $pathdir ) {
 133          $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
 134      }
 135      $categories = get_terms(
 136          array(
 137              'taxonomy' => 'category',
 138              'get'      => 'all',
 139              'slug'     => $leaf_path,
 140          )
 141      );
 142  
 143      if ( empty( $categories ) ) {
 144          return;
 145      }
 146  
 147      foreach ( $categories as $category ) {
 148          $path        = '/' . $leaf_path;
 149          $curcategory = $category;
 150          while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
 151              $curcategory = get_term( $curcategory->parent, 'category' );
 152              if ( is_wp_error( $curcategory ) ) {
 153                  return $curcategory;
 154              }
 155              $path = '/' . $curcategory->slug . $path;
 156          }
 157  
 158          if ( $path == $full_path ) {
 159              $category = get_term( $category->term_id, 'category', $output );
 160              _make_cat_compat( $category );
 161              return $category;
 162          }
 163      }
 164  
 165      // If full matching is not required, return the first cat that matches the leaf.
 166      if ( ! $full_match ) {
 167          $category = get_term( reset( $categories )->term_id, 'category', $output );
 168          _make_cat_compat( $category );
 169          return $category;
 170      }
 171  }
 172  
 173  /**
 174   * Retrieve category object by category slug.
 175   *
 176   * @since 2.3.0
 177   *
 178   * @param string $slug The category slug.
 179   * @return object Category data object
 180   */
 181  function get_category_by_slug( $slug ) {
 182      $category = get_term_by( 'slug', $slug, 'category' );
 183      if ( $category ) {
 184          _make_cat_compat( $category );
 185      }
 186  
 187      return $category;
 188  }
 189  
 190  /**
 191   * Retrieve the ID of a category from its name.
 192   *
 193   * @since 1.0.0
 194   *
 195   * @param string $cat_name Category name.
 196   * @return int 0, if failure and ID of category on success.
 197   */
 198  function get_cat_ID( $cat_name ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
 199      $cat = get_term_by( 'name', $cat_name, 'category' );
 200      if ( $cat ) {
 201          return $cat->term_id;
 202      }
 203      return 0;
 204  }
 205  
 206  /**
 207   * Retrieve the name of a category from its ID.
 208   *
 209   * @since 1.0.0
 210   *
 211   * @param int $cat_id Category ID
 212   * @return string Category name, or an empty string if category doesn't exist.
 213   */
 214  function get_cat_name( $cat_id ) {
 215      $cat_id   = (int) $cat_id;
 216      $category = get_term( $cat_id, 'category' );
 217      if ( ! $category || is_wp_error( $category ) ) {
 218          return '';
 219      }
 220      return $category->name;
 221  }
 222  
 223  /**
 224   * Check if a category is an ancestor of another category.
 225   *
 226   * You can use either an id or the category object for both parameters. If you
 227   * use an integer the category will be retrieved.
 228   *
 229   * @since 2.1.0
 230   *
 231   * @param int|object $cat1 ID or object to check if this is the parent category.
 232   * @param int|object $cat2 The child category.
 233   * @return bool Whether $cat2 is child of $cat1
 234   */
 235  function cat_is_ancestor_of( $cat1, $cat2 ) {
 236      return term_is_ancestor_of( $cat1, $cat2, 'category' );
 237  }
 238  
 239  /**
 240   * Sanitizes category data based on context.
 241   *
 242   * @since 2.3.0
 243   *
 244   * @param object|array $category Category data
 245   * @param string $context Optional. Default is 'display'.
 246   * @return object|array Same type as $category with sanitized data for safe use.
 247   */
 248  function sanitize_category( $category, $context = 'display' ) {
 249      return sanitize_term( $category, 'category', $context );
 250  }
 251  
 252  /**
 253   * Sanitizes data in single category key field.
 254   *
 255   * @since 2.3.0
 256   *
 257   * @param string $field Category key to sanitize
 258   * @param mixed $value Category value to sanitize
 259   * @param int $cat_id Category ID
 260   * @param string $context What filter to use, 'raw', 'display', etc.
 261   * @return mixed Same type as $value after $value has been sanitized.
 262   */
 263  function sanitize_category_field( $field, $value, $cat_id, $context ) {
 264      return sanitize_term_field( $field, $value, $cat_id, 'category', $context );
 265  }
 266  
 267  /* Tags */
 268  
 269  /**
 270   * Retrieves all post tags.
 271   *
 272   * @since 2.3.0
 273   * @see get_terms() For list of arguments to pass.
 274   *
 275   * @param string|array $args Tag arguments to use when retrieving tags.
 276   * @return WP_Term[]|int $tags Array of 'post_tag' term objects, or a count thereof.
 277   */
 278  function get_tags( $args = '' ) {
 279      $defaults = array( 'taxonomy' => 'post_tag' );
 280      $args     = wp_parse_args( $args, $defaults );
 281  
 282      $tags = get_terms( $args );
 283  
 284      if ( empty( $tags ) ) {
 285          $return = array();
 286          return $return;
 287      }
 288  
 289      /**
 290       * Filters the array of term objects returned for the 'post_tag' taxonomy.
 291       *
 292       * @since 2.3.0
 293       *
 294       * @param WP_Term[]|int $tags Array of 'post_tag' term objects, or a count thereof.
 295       * @param array         $args An array of arguments. @see get_terms()
 296       */
 297      $tags = apply_filters( 'get_tags', $tags, $args );
 298      return $tags;
 299  }
 300  
 301  /**
 302   * Retrieve post tag by tag ID or tag object.
 303   *
 304   * If you pass the $tag parameter an object, which is assumed to be the tag row
 305   * object retrieved the database. It will cache the tag data.
 306   *
 307   * If you pass $tag an integer of the tag ID, then that tag will
 308   * be retrieved from the database, if it isn't already cached, and pass it back.
 309   *
 310   * If you look at get_term(), then both types will be passed through several
 311   * filters and finally sanitized based on the $filter parameter value.
 312   *
 313   * @since 2.3.0
 314   *
 315   * @param int|WP_Term|object $tag    A tag ID or object.
 316   * @param string             $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 317   *                                   a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 318   * @param string             $filter Optional. Default is raw or no WordPress defined filter will applied.
 319   * @return WP_Term|array|WP_Error|null Tag data in type defined by $output parameter. WP_Error if $tag is empty, null if it does not exist.
 320   */
 321  function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
 322      return get_term( $tag, 'post_tag', $output, $filter );
 323  }
 324  
 325  /* Cache */
 326  
 327  /**
 328   * Remove the category cache data based on ID.
 329   *
 330   * @since 2.1.0
 331   *
 332   * @param int $id Category ID
 333   */
 334  function clean_category_cache( $id ) {
 335      clean_term_cache( $id, 'category' );
 336  }
 337  
 338  /**
 339   * Update category structure to old pre 2.3 from new taxonomy structure.
 340   *
 341   * This function was added for the taxonomy support to update the new category
 342   * structure with the old category one. This will maintain compatibility with
 343   * plugins and themes which depend on the old key or property names.
 344   *
 345   * The parameter should only be passed a variable and not create the array or
 346   * object inline to the parameter. The reason for this is that parameter is
 347   * passed by reference and PHP will fail unless it has the variable.
 348   *
 349   * There is no return value, because everything is updated on the variable you
 350   * pass to it. This is one of the features with using pass by reference in PHP.
 351   *
 352   * @since 2.3.0
 353   * @since 4.4.0 The `$category` parameter now also accepts a WP_Term object.
 354   * @access private
 355   *
 356   * @param array|object|WP_Term $category Category Row object or array
 357   */
 358  function _make_cat_compat( &$category ) {
 359      if ( is_object( $category ) && ! is_wp_error( $category ) ) {
 360          $category->cat_ID               = $category->term_id;
 361          $category->category_count       = $category->count;
 362          $category->category_description = $category->description;
 363          $category->cat_name             = $category->name;
 364          $category->category_nicename    = $category->slug;
 365          $category->category_parent      = $category->parent;
 366      } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
 367          $category['cat_ID']               = &$category['term_id'];
 368          $category['category_count']       = &$category['count'];
 369          $category['category_description'] = &$category['description'];
 370          $category['cat_name']             = &$category['name'];
 371          $category['category_nicename']    = &$category['slug'];
 372          $category['category_parent']      = &$category['parent'];
 373      }
 374  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7