wp/wp-includes/category.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * WordPress Category API
     3  * Taxonomy API: Core category-specific functionality
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
       
     6  * @subpackage Taxonomy
     6  */
     7  */
     7 
     8 
     8 /**
     9 /**
     9  * Retrieve list of category objects.
    10  * Retrieve list of category objects.
    10  *
    11  *
    11  * If you change the type to 'link' in the arguments, then the link categories
    12  * If you change the type to 'link' in the arguments, then the link categories
    12  * will be returned instead. Also all categories will be updated to be backwards
    13  * will be returned instead. Also all categories will be updated to be backward
    13  * compatible with pre-2.3 plugins and themes.
    14  * compatible with pre-2.3 plugins and themes.
    14  *
    15  *
    15  * @since 2.1.0
    16  * @since 2.1.0
    16  * @see get_terms() Type of arguments that can be changed.
    17  * @see get_terms() Type of arguments that can be changed.
    17  * @link https://codex.wordpress.org/Function_Reference/get_categories
    18  *
    18  *
    19  * @param string|array $args {
    19  * @param string|array $args Optional. Change the defaults retrieving categories.
    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  * }
    20  * @return array List of categories.
    24  * @return array List of categories.
    21  */
    25  */
    22 function get_categories( $args = '' ) {
    26 function get_categories( $args = '' ) {
    23 	$defaults = array( 'taxonomy' => 'category' );
    27 	$defaults = array( 'taxonomy' => 'category' );
    24 	$args = wp_parse_args( $args, $defaults );
    28 	$args = wp_parse_args( $args, $defaults );
    25 
    29 
    26 	$taxonomy = $args['taxonomy'];
    30 	$taxonomy = $args['taxonomy'];
    27 
    31 
    28 	/**
    32 	/**
    29 	 * Filter the taxonomy used to retrieve terms when calling {@see get_categories()}.
    33 	 * Filters the taxonomy used to retrieve terms when calling get_categories().
    30 	 *
    34 	 *
    31 	 * @since 2.7.0
    35 	 * @since 2.7.0
    32 	 *
    36 	 *
    33 	 * @param string $taxonomy Taxonomy to retrieve terms from.
    37 	 * @param string $taxonomy Taxonomy to retrieve terms from.
    34 	 * @param array  $args     An array of arguments. See {@see get_terms()}.
    38 	 * @param array  $args     An array of arguments. See get_terms().
    35 	 */
    39 	 */
    36 	$taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );
    40 	$taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );
    37 
    41 
    38 	// Back compat
    42 	// Back compat
    39 	if ( isset($args['type']) && 'link' == $args['type'] ) {
    43 	if ( isset($args['type']) && 'link' == $args['type'] ) {
    40 		_deprecated_argument( __FUNCTION__, '3.0', '' );
    44 		_deprecated_argument( __FUNCTION__, '3.0.0',
       
    45 			/* translators: 1: "type => link", 2: "taxonomy => link_category" */
       
    46 			sprintf( __( '%1$s is deprecated. Use %2$s instead.' ),
       
    47 				'<code>type => link</code>',
       
    48 				'<code>taxonomy => link_category</code>'
       
    49 			)
       
    50 		);
    41 		$taxonomy = $args['taxonomy'] = 'link_category';
    51 		$taxonomy = $args['taxonomy'] = 'link_category';
    42 	}
    52 	}
    43 
    53 
    44 	$categories = (array) get_terms( $taxonomy, $args );
    54 	$categories = get_terms( $taxonomy, $args );
    45 
    55 
    46 	foreach ( array_keys( $categories ) as $k )
    56 	if ( is_wp_error( $categories ) ) {
    47 		_make_cat_compat( $categories[$k] );
    57 		$categories = array();
       
    58 	} else {
       
    59 		$categories = (array) $categories;
       
    60 		foreach ( array_keys( $categories ) as $k ) {
       
    61 			_make_cat_compat( $categories[ $k ] );
       
    62 		}
       
    63 	}
    48 
    64 
    49 	return $categories;
    65 	return $categories;
    50 }
    66 }
    51 
    67 
    52 /**
    68 /**
    59  * be retrieved from the database, if it isn't already cached, and pass it back.
    75  * be retrieved from the database, if it isn't already cached, and pass it back.
    60  *
    76  *
    61  * If you look at get_term(), then both types will be passed through several
    77  * If you look at get_term(), then both types will be passed through several
    62  * filters and finally sanitized based on the $filter parameter value.
    78  * filters and finally sanitized based on the $filter parameter value.
    63  *
    79  *
    64  * The category will converted to maintain backwards compatibility.
    80  * The category will converted to maintain backward compatibility.
    65  *
    81  *
    66  * @since 1.5.1
    82  * @since 1.5.1
    67  *
    83  *
    68  * @param int|object $category Category ID or Category row object
    84  * @param int|object $category Category ID or Category row object
    69  * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
    85  * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a
       
    86  *                       WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
    70  * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
    87  * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
    71  * @return object|array|WP_Error|null Category data in type defined by $output parameter. WP_Error if $category is empty, null if it does not exist.
    88  * @return object|array|WP_Error|null Category data in type defined by $output parameter.
       
    89  *                                    WP_Error if $category is empty, null if it does not exist.
    72  */
    90  */
    73 function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
    91 function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
    74 	$category = get_term( $category, 'category', $output, $filter );
    92 	$category = get_term( $category, 'category', $output, $filter );
    75 
    93 
    76 	if ( is_wp_error( $category ) )
    94 	if ( is_wp_error( $category ) )
    94  * for it when using this function.
   112  * for it when using this function.
    95  *
   113  *
    96  * @since 2.1.0
   114  * @since 2.1.0
    97  *
   115  *
    98  * @param string $category_path URL containing category slugs.
   116  * @param string $category_path URL containing category slugs.
    99  * @param bool $full_match Optional. Whether full path should be matched.
   117  * @param bool   $full_match    Optional. Whether full path should be matched.
   100  * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
   118  * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
   101  * @return null|object|array Null on failure. Type is based on $output value.
   119  *                              a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
       
   120  * @return WP_Term|array|WP_Error|null Type is based on $output value.
   102  */
   121  */
   103 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
   122 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
   104 	$category_path = rawurlencode( urldecode( $category_path ) );
   123 	$category_path = rawurlencode( urldecode( $category_path ) );
   105 	$category_path = str_replace( '%2F', '/', $category_path );
   124 	$category_path = str_replace( '%2F', '/', $category_path );
   106 	$category_path = str_replace( '%20', ' ', $category_path );
   125 	$category_path = str_replace( '%20', ' ', $category_path );
   107 	$category_paths = '/' . trim( $category_path, '/' );
   126 	$category_paths = '/' . trim( $category_path, '/' );
   108 	$leaf_path  = sanitize_title( basename( $category_paths ) );
   127 	$leaf_path  = sanitize_title( basename( $category_paths ) );
   109 	$category_paths = explode( '/', $category_paths );
   128 	$category_paths = explode( '/', $category_paths );
   110 	$full_path = '';
   129 	$full_path = '';
   111 	foreach ( (array) $category_paths as $pathdir )
   130 	foreach ( (array) $category_paths as $pathdir ) {
   112 		$full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
   131 		$full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
   113 
   132 	}
   114 	$categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) );
   133 	$categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) );
   115 
   134 
   116 	if ( empty( $categories ) )
   135 	if ( empty( $categories ) ) {
   117 		return null;
   136 		return;
       
   137 	}
   118 
   138 
   119 	foreach ( $categories as $category ) {
   139 	foreach ( $categories as $category ) {
   120 		$path = '/' . $leaf_path;
   140 		$path = '/' . $leaf_path;
   121 		$curcategory = $category;
   141 		$curcategory = $category;
   122 		while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
   142 		while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
   123 			$curcategory = get_term( $curcategory->parent, 'category' );
   143 			$curcategory = get_term( $curcategory->parent, 'category' );
   124 			if ( is_wp_error( $curcategory ) )
   144 			if ( is_wp_error( $curcategory ) ) {
   125 				return $curcategory;
   145 				return $curcategory;
       
   146 			}
   126 			$path = '/' . $curcategory->slug . $path;
   147 			$path = '/' . $curcategory->slug . $path;
   127 		}
   148 		}
   128 
   149 
   129 		if ( $path == $full_path ) {
   150 		if ( $path == $full_path ) {
   130 			$category = get_term( $category->term_id, 'category', $output );
   151 			$category = get_term( $category->term_id, 'category', $output );
   137 	if ( ! $full_match ) {
   158 	if ( ! $full_match ) {
   138 		$category = get_term( reset( $categories )->term_id, 'category', $output );
   159 		$category = get_term( reset( $categories )->term_id, 'category', $output );
   139 		_make_cat_compat( $category );
   160 		_make_cat_compat( $category );
   140 		return $category;
   161 		return $category;
   141 	}
   162 	}
   142 
       
   143 	return null;
       
   144 }
   163 }
   145 
   164 
   146 /**
   165 /**
   147  * Retrieve category object by category slug.
   166  * Retrieve category object by category slug.
   148  *
   167  *
   252 		$return = array();
   271 		$return = array();
   253 		return $return;
   272 		return $return;
   254 	}
   273 	}
   255 
   274 
   256 	/**
   275 	/**
   257 	 * Filter the array of term objects returned for the 'post_tag' taxonomy.
   276 	 * Filters the array of term objects returned for the 'post_tag' taxonomy.
   258 	 *
   277 	 *
   259 	 * @since 2.3.0
   278 	 * @since 2.3.0
   260 	 *
   279 	 *
   261 	 * @param array $tags Array of 'post_tag' term objects.
   280 	 * @param array $tags Array of 'post_tag' term objects.
   262 	 * @param array $args An array of arguments. @see get_terms()
   281 	 * @param array $args An array of arguments. @see get_terms()
   277  * If you look at get_term(), then both types will be passed through several
   296  * If you look at get_term(), then both types will be passed through several
   278  * filters and finally sanitized based on the $filter parameter value.
   297  * filters and finally sanitized based on the $filter parameter value.
   279  *
   298  *
   280  * @since 2.3.0
   299  * @since 2.3.0
   281  *
   300  *
   282  * @param int|object $tag
   301  * @param int|WP_Term|object $tag    A tag ID or object.
   283  * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
   302  * @param string             $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
   284  * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
   303  *                                   a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
   285  * @return object|array|WP_Error|null Tag data in type defined by $output parameter. WP_Error if $tag is empty, null if it does not exist.
   304  * @param string             $filter Optional. Default is raw or no WordPress defined filter will applied.
       
   305  * @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.
   286  */
   306  */
   287 function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
   307 function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
   288 	return get_term( $tag, 'post_tag', $output, $filter );
   308 	return get_term( $tag, 'post_tag', $output, $filter );
   289 }
   309 }
   290 
   310 
   314  *
   334  *
   315  * There is no return value, because everything is updated on the variable you
   335  * There is no return value, because everything is updated on the variable you
   316  * pass to it. This is one of the features with using pass by reference in PHP.
   336  * pass to it. This is one of the features with using pass by reference in PHP.
   317  *
   337  *
   318  * @since 2.3.0
   338  * @since 2.3.0
       
   339  * @since 4.4.0 The `$category` parameter now also accepts a WP_Term object.
   319  * @access private
   340  * @access private
   320  *
   341  *
   321  * @param array|object $category Category Row object or array
   342  * @param array|object|WP_Term $category Category Row object or array
   322  */
   343  */
   323 function _make_cat_compat( &$category ) {
   344 function _make_cat_compat( &$category ) {
   324 	if ( is_object( $category ) ) {
   345 	if ( is_object( $category ) && ! is_wp_error( $category ) ) {
   325 		$category->cat_ID = &$category->term_id;
   346 		$category->cat_ID = $category->term_id;
   326 		$category->category_count = &$category->count;
   347 		$category->category_count = $category->count;
   327 		$category->category_description = &$category->description;
   348 		$category->category_description = $category->description;
   328 		$category->cat_name = &$category->name;
   349 		$category->cat_name = $category->name;
   329 		$category->category_nicename = &$category->slug;
   350 		$category->category_nicename = $category->slug;
   330 		$category->category_parent = &$category->parent;
   351 		$category->category_parent = $category->parent;
   331 	} elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
   352 	} elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
   332 		$category['cat_ID'] = &$category['term_id'];
   353 		$category['cat_ID'] = &$category['term_id'];
   333 		$category['category_count'] = &$category['count'];
   354 		$category['category_count'] = &$category['count'];
   334 		$category['category_description'] = &$category['description'];
   355 		$category['category_description'] = &$category['description'];
   335 		$category['cat_name'] = &$category['name'];
   356 		$category['cat_name'] = &$category['name'];