Convert in_category() and has_tags() to use the same is_object_in_term(), which accepts one object, one taxonomy, and 0, 1 or multiple term term_ids/names/slugs.

In those functions, compare ints only to term_ids. Strings are compared to term_ids, names, slugs.

Document all three better, and note how they've changed with WP versions. Prop mdawaffe

git-svn-id: http://svn.automattic.com/wordpress/trunk@10159 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-12-09 23:31:11 +00:00
parent bb643f3ecb
commit d81c59d985
2 changed files with 100 additions and 55 deletions

View File

@ -265,46 +265,44 @@ function get_the_category_list( $separator = '', $parents='', $post_id = false )
return apply_filters( 'the_category', $thelist, $separator, $parents );
}
/**
* Checks whether the current post is within a particular category.
* Check if the current post in within any of the given categories.
*
* This function checks to see if the post is within the supplied category. The
* category can be specified by number or name and will be checked as a name
* first to allow for categories with numeric names. Note: Prior to v2.5 of
* WordPress category names were not supported.
* The given categories are checked against the post's categories' term_ids, names and slugs.
* Categories given as integers will only be checked against the post's categories' term_ids.
*
* Prior to v2.5 of WordPress, category names were not supported.
* Prior to v2.7, category slugs were not supported.
* Prior to v2.7, only one category could be compared: in_category( $single_category ).
* Prior to v2.7, this function could only be used in the WordPress Loop.
* As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
*
* @since 1.2.0
*
* @param int|string $category Category ID or category name.
* @return bool True, if the post is in the supplied category.
*/
function in_category( $category ) {
global $post;
* @uses is_object_in_term()
*
* @param int|string|array $category. Category ID, name or slug, or array of said.
* @param int|post object Optional. Post to check instead of the current post. @since 2.7.0
* @return bool True if the current post is in any of the given categories.
*/
function in_category( $category, $_post = null ) {
if ( empty( $category ) )
return false;
// If category is not an int, check to see if it's a name
if ( ! is_int( $category ) ) {
$cat_ID = get_cat_ID( $category );
if ( $cat_ID )
$category = $cat_ID;
if ( $_post ) {
$_post = get_post( $_post );
} else {
$_post =& $GLOBALS['post'];
}
$categories = get_object_term_cache( $post->ID, 'category' );
if ( false !== $categories ) {
if ( array_key_exists( $category, $categories ) )
return true;
else
return false;
}
$categories = wp_get_object_terms( $post->ID, 'category', 'fields=ids' );
if ( is_array($categories) && in_array($category, $categories) )
return true;
else
if ( !$_post )
return false;
$r = is_object_in_term( $_post->ID, 'category', $category );
if ( is_wp_error( $r ) )
return false;
return $r;
}
/**
@ -900,41 +898,38 @@ function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
}
/**
* Check if the current post has the given tag.
* Check if the current post has any of given tags.
*
* This function is only for use within the WordPress Loop.
* The given tags are checked against the post's tags' term_ids, names and slugs.
* Tags given as integers will only be checked against the post's tags' term_ids.
* If no tags are given, determines if post has any tags.
*
* Prior to v2.7 of WordPress, tags given as integers would also be checked against the post's tags' names and slugs (in addition to term_ids)
* Prior to v2.7, this function could only be used in the WordPress Loop.
* As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
*
* @since 2.6.0
*
* @uses wp_get_object_terms() Gets the tags.
* @uses is_object_in_term()
*
* @param string|int|array $tag Optional. The tag name/id/slug or array of them to check for.
* @return bool True if the current post has the given tag, or any tag, if no tag specified.
* @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
* @param int|post object Optional. Post to check instead of the current post. @since 2.7.0
* @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
*/
function has_tag( $tag = '' ) {
global $post;
$taxonomy = 'post_tag';
if ( !in_the_loop() ) return false; // in-the-loop function
$post_id = (int) $post->ID;
$terms = get_object_term_cache( $post_id, $taxonomy );
if ( empty( $terms ) )
$terms = wp_get_object_terms( $post_id, $taxonomy );
if ( empty( $terms ) ) return false;
if ( empty( $tag ) ) return ( !empty( $terms ) );
$tag = (array) $tag;
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $tag ) ) return true;
if ( in_array( $term->name, $tag ) ) return true;
if ( in_array( $term->slug, $tag ) ) return true;
function has_tag( $tag = '', $_post = null ) {
if ( $_post ) {
$_post = get_post( $_post );
} else {
$_post =& $GLOBALS['post'];
}
return false;
if ( !$_post )
return false;
$r = is_object_in_term( $_post->ID, 'post_tag', $tag );
if ( is_wp_error( $r ) )
return false;
return $r;
}
?>

View File

@ -2224,4 +2224,54 @@ function get_post_taxonomies($post = 0) {
return get_object_taxonomies($post);
}
/**
* Determine if the given object is associated with any of the given terms.
*
* The given terms are checked against the object's terms' term_ids, names and slugs.
* Terms given as integers will only be checked against the object's terms' term_ids.
* If no terms are given, determines if object is associated with any terms in the given taxonomy.
*
* @since 2.7.0
* @uses get_object_term_cache()
* @uses wp_get_object_terms()
*
* @param int $object_id. ID of the object (post ID, link ID, ...)
* @param string $taxonomy. Single taxonomy name
* @param int|string|array $terms Optional. Term term_id, name, slug or array of said
* @return bool|WP_Error. WP_Error on input error.
*/
function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
if ( !$object_id = (int) $object_id )
return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );
$object_terms = get_object_term_cache( $object_id, $taxonomy );
if ( empty( $object_terms ) )
$object_terms = wp_get_object_terms( $object_id, $taxonomy );
if ( is_wp_error( $object_terms ) )
return $object_terms;
if ( empty( $object_terms ) )
return false;
if ( empty( $terms ) )
return ( !empty( $object_terms ) );
$terms = (array) $terms;
if ( $ints = array_filter( $terms, 'is_int' ) )
$strs = array_diff( $terms, $ints );
else
$strs =& $terms;
foreach ( $object_terms as $object_term ) {
if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id
if ( $strs ) {
if ( in_array( $object_term->term_id, $strs ) ) return true;
if ( in_array( $object_term->name, $strs ) ) return true;
if ( in_array( $object_term->slug, $strs ) ) return true;
}
}
return false;
}
?>