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


Generated: Mon Jun 24 08:20:01 2019 Cross-referenced by PHPXref 0.7