wp/wp-includes/category.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    23  * }
    23  * }
    24  * @return array List of categories.
    24  * @return array List of categories.
    25  */
    25  */
    26 function get_categories( $args = '' ) {
    26 function get_categories( $args = '' ) {
    27 	$defaults = array( 'taxonomy' => 'category' );
    27 	$defaults = array( 'taxonomy' => 'category' );
    28 	$args = wp_parse_args( $args, $defaults );
    28 	$args     = wp_parse_args( $args, $defaults );
    29 
    29 
    30 	$taxonomy = $args['taxonomy'];
    30 	$taxonomy = $args['taxonomy'];
    31 
    31 
    32 	/**
    32 	/**
    33 	 * Filters the taxonomy used to retrieve terms when calling get_categories().
    33 	 * Filters the taxonomy used to retrieve terms when calling get_categories().
    38 	 * @param array  $args     An array of arguments. See get_terms().
    38 	 * @param array  $args     An array of arguments. See get_terms().
    39 	 */
    39 	 */
    40 	$taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );
    40 	$taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );
    41 
    41 
    42 	// Back compat
    42 	// Back compat
    43 	if ( isset($args['type']) && 'link' == $args['type'] ) {
    43 	if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
    44 		_deprecated_argument( __FUNCTION__, '3.0.0',
    44 		_deprecated_argument(
       
    45 			__FUNCTION__,
       
    46 			'3.0.0',
    45 			/* translators: 1: "type => link", 2: "taxonomy => link_category" */
    47 			/* translators: 1: "type => link", 2: "taxonomy => link_category" */
    46 			sprintf( __( '%1$s is deprecated. Use %2$s instead.' ),
    48 			sprintf(
       
    49 				__( '%1$s is deprecated. Use %2$s instead.' ),
    47 				'<code>type => link</code>',
    50 				'<code>type => link</code>',
    48 				'<code>taxonomy => link_category</code>'
    51 				'<code>taxonomy => link_category</code>'
    49 			)
    52 			)
    50 		);
    53 		);
    51 		$taxonomy = $args['taxonomy'] = 'link_category';
    54 		$taxonomy = $args['taxonomy'] = 'link_category';
    89  *                                    WP_Error if $category is empty, null if it does not exist.
    92  *                                    WP_Error if $category is empty, null if it does not exist.
    90  */
    93  */
    91 function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
    94 function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
    92 	$category = get_term( $category, 'category', $output, $filter );
    95 	$category = get_term( $category, 'category', $output, $filter );
    93 
    96 
    94 	if ( is_wp_error( $category ) )
    97 	if ( is_wp_error( $category ) ) {
    95 		return $category;
    98 		return $category;
       
    99 	}
    96 
   100 
    97 	_make_cat_compat( $category );
   101 	_make_cat_compat( $category );
    98 
   102 
    99 	return $category;
   103 	return $category;
   100 }
   104 }
   118  * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
   122  * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
   119  *                              a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
   123  *                              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.
   124  * @return WP_Term|array|WP_Error|null Type is based on $output value.
   121  */
   125  */
   122 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
   126 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
   123 	$category_path = rawurlencode( urldecode( $category_path ) );
   127 	$category_path  = rawurlencode( urldecode( $category_path ) );
   124 	$category_path = str_replace( '%2F', '/', $category_path );
   128 	$category_path  = str_replace( '%2F', '/', $category_path );
   125 	$category_path = str_replace( '%20', ' ', $category_path );
   129 	$category_path  = str_replace( '%20', ' ', $category_path );
   126 	$category_paths = '/' . trim( $category_path, '/' );
   130 	$category_paths = '/' . trim( $category_path, '/' );
   127 	$leaf_path  = sanitize_title( basename( $category_paths ) );
   131 	$leaf_path      = sanitize_title( basename( $category_paths ) );
   128 	$category_paths = explode( '/', $category_paths );
   132 	$category_paths = explode( '/', $category_paths );
   129 	$full_path = '';
   133 	$full_path      = '';
   130 	foreach ( (array) $category_paths as $pathdir ) {
   134 	foreach ( (array) $category_paths as $pathdir ) {
   131 		$full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
   135 		$full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
   132 	}
   136 	}
   133 	$categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) );
   137 	$categories = get_terms(
       
   138 		'category',
       
   139 		array(
       
   140 			'get'  => 'all',
       
   141 			'slug' => $leaf_path,
       
   142 		)
       
   143 	);
   134 
   144 
   135 	if ( empty( $categories ) ) {
   145 	if ( empty( $categories ) ) {
   136 		return;
   146 		return;
   137 	}
   147 	}
   138 
   148 
   139 	foreach ( $categories as $category ) {
   149 	foreach ( $categories as $category ) {
   140 		$path = '/' . $leaf_path;
   150 		$path        = '/' . $leaf_path;
   141 		$curcategory = $category;
   151 		$curcategory = $category;
   142 		while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
   152 		while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
   143 			$curcategory = get_term( $curcategory->parent, 'category' );
   153 			$curcategory = get_term( $curcategory->parent, 'category' );
   144 			if ( is_wp_error( $curcategory ) ) {
   154 			if ( is_wp_error( $curcategory ) ) {
   145 				return $curcategory;
   155 				return $curcategory;
   168  * @since 2.3.0
   178  * @since 2.3.0
   169  *
   179  *
   170  * @param string $slug The category slug.
   180  * @param string $slug The category slug.
   171  * @return object Category data object
   181  * @return object Category data object
   172  */
   182  */
   173 function get_category_by_slug( $slug  ) {
   183 function get_category_by_slug( $slug ) {
   174 	$category = get_term_by( 'slug', $slug, 'category' );
   184 	$category = get_term_by( 'slug', $slug, 'category' );
   175 	if ( $category )
   185 	if ( $category ) {
   176 		_make_cat_compat( $category );
   186 		_make_cat_compat( $category );
       
   187 	}
   177 
   188 
   178 	return $category;
   189 	return $category;
   179 }
   190 }
   180 
   191 
   181 /**
   192 /**
   186  * @param string $cat_name Category name.
   197  * @param string $cat_name Category name.
   187  * @return int 0, if failure and ID of category on success.
   198  * @return int 0, if failure and ID of category on success.
   188  */
   199  */
   189 function get_cat_ID( $cat_name ) {
   200 function get_cat_ID( $cat_name ) {
   190 	$cat = get_term_by( 'name', $cat_name, 'category' );
   201 	$cat = get_term_by( 'name', $cat_name, 'category' );
   191 	if ( $cat )
   202 	if ( $cat ) {
   192 		return $cat->term_id;
   203 		return $cat->term_id;
       
   204 	}
   193 	return 0;
   205 	return 0;
   194 }
   206 }
   195 
   207 
   196 /**
   208 /**
   197  * Retrieve the name of a category from its ID.
   209  * Retrieve the name of a category from its ID.
   200  *
   212  *
   201  * @param int $cat_id Category ID
   213  * @param int $cat_id Category ID
   202  * @return string Category name, or an empty string if category doesn't exist.
   214  * @return string Category name, or an empty string if category doesn't exist.
   203  */
   215  */
   204 function get_cat_name( $cat_id ) {
   216 function get_cat_name( $cat_id ) {
   205 	$cat_id = (int) $cat_id;
   217 	$cat_id   = (int) $cat_id;
   206 	$category = get_term( $cat_id, 'category' );
   218 	$category = get_term( $cat_id, 'category' );
   207 	if ( ! $category || is_wp_error( $category ) )
   219 	if ( ! $category || is_wp_error( $category ) ) {
   208 		return '';
   220 		return '';
       
   221 	}
   209 	return $category->name;
   222 	return $category->name;
   210 }
   223 }
   211 
   224 
   212 /**
   225 /**
   213  * Check if a category is an ancestor of another category.
   226  * Check if a category is an ancestor of another category.
   260  *
   273  *
   261  * @since 2.3.0
   274  * @since 2.3.0
   262  * @see get_terms() For list of arguments to pass.
   275  * @see get_terms() For list of arguments to pass.
   263  *
   276  *
   264  * @param string|array $args Tag arguments to use when retrieving tags.
   277  * @param string|array $args Tag arguments to use when retrieving tags.
   265  * @return array List of tags.
   278  * @return WP_Term[]|int $tags Array of 'post_tag' term objects, or a count thereof.
   266  */
   279  */
   267 function get_tags( $args = '' ) {
   280 function get_tags( $args = '' ) {
   268 	$tags = get_terms( 'post_tag', $args );
   281 	$tags = get_terms( 'post_tag', $args );
   269 
   282 
   270 	if ( empty( $tags ) ) {
   283 	if ( empty( $tags ) ) {
   275 	/**
   288 	/**
   276 	 * Filters the array of term objects returned for the 'post_tag' taxonomy.
   289 	 * Filters the array of term objects returned for the 'post_tag' taxonomy.
   277 	 *
   290 	 *
   278 	 * @since 2.3.0
   291 	 * @since 2.3.0
   279 	 *
   292 	 *
   280 	 * @param array $tags Array of 'post_tag' term objects.
   293 	 * @param WP_Term[]|int $tags Array of 'post_tag' term objects, or a count thereof.
   281 	 * @param array $args An array of arguments. @see get_terms()
   294 	 * @param array         $args An array of arguments. @see get_terms()
   282 	 */
   295 	 */
   283 	$tags = apply_filters( 'get_tags', $tags, $args );
   296 	$tags = apply_filters( 'get_tags', $tags, $args );
   284 	return $tags;
   297 	return $tags;
   285 }
   298 }
   286 
   299 
   341  *
   354  *
   342  * @param array|object|WP_Term $category Category Row object or array
   355  * @param array|object|WP_Term $category Category Row object or array
   343  */
   356  */
   344 function _make_cat_compat( &$category ) {
   357 function _make_cat_compat( &$category ) {
   345 	if ( is_object( $category ) && ! is_wp_error( $category ) ) {
   358 	if ( is_object( $category ) && ! is_wp_error( $category ) ) {
   346 		$category->cat_ID = $category->term_id;
   359 		$category->cat_ID               = $category->term_id;
   347 		$category->category_count = $category->count;
   360 		$category->category_count       = $category->count;
   348 		$category->category_description = $category->description;
   361 		$category->category_description = $category->description;
   349 		$category->cat_name = $category->name;
   362 		$category->cat_name             = $category->name;
   350 		$category->category_nicename = $category->slug;
   363 		$category->category_nicename    = $category->slug;
   351 		$category->category_parent = $category->parent;
   364 		$category->category_parent      = $category->parent;
   352 	} elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
   365 	} elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
   353 		$category['cat_ID'] = &$category['term_id'];
   366 		$category['cat_ID']               = &$category['term_id'];
   354 		$category['category_count'] = &$category['count'];
   367 		$category['category_count']       = &$category['count'];
   355 		$category['category_description'] = &$category['description'];
   368 		$category['category_description'] = &$category['description'];
   356 		$category['cat_name'] = &$category['name'];
   369 		$category['cat_name']             = &$category['name'];
   357 		$category['category_nicename'] = &$category['slug'];
   370 		$category['category_nicename']    = &$category['slug'];
   358 		$category['category_parent'] = &$category['parent'];
   371 		$category['category_parent']      = &$category['parent'];
   359 	}
   372 	}
   360 }
   373 }