WordPress.org

Make WordPress Core

Changeset 10159


Ignore:
Timestamp:
12/09/2008 11:31:11 PM (11 years ago)
Author:
ryan
Message:

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

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category-template.php

    r10150 r10159  
    266266}
    267267
    268 /**
    269  * Checks whether the current post is within a particular category.
    270  *
    271  * This function checks to see if the post is within the supplied category. The
    272  * category can be specified by number or name and will be checked as a name
    273  * first to allow for categories with numeric names. Note: Prior to v2.5 of
    274  * WordPress category names were not supported.
     268
     269/**
     270 * Check if the current post in within any of the given categories.
     271 *
     272 * The given categories are checked against the post's categories' term_ids, names and slugs.
     273 * Categories given as integers will only be checked against the post's categories' term_ids.
     274 *
     275 * Prior to v2.5 of WordPress, category names were not supported.
     276 * Prior to v2.7, category slugs were not supported.
     277 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
     278 * Prior to v2.7, this function could only be used in the WordPress Loop.
     279 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
    275280 *
    276281 * @since 1.2.0
    277282 *
    278  * @param int|string $category Category ID or category name.
    279  * @return bool True, if the post is in the supplied category.
    280 */
    281 function in_category( $category ) {
    282     global $post;
    283 
     283 * @uses is_object_in_term()
     284 *
     285 * @param int|string|array $category. Category ID, name or slug, or array of said.
     286 * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
     287 * @return bool True if the current post is in any of the given categories.
     288 */
     289function in_category( $category, $_post = null ) {
    284290    if ( empty( $category ) )
    285291        return false;
    286292
    287     // If category is not an int, check to see if it's a name
    288     if ( ! is_int( $category ) ) {
    289         $cat_ID = get_cat_ID( $category );
    290         if ( $cat_ID )
    291             $category = $cat_ID;
    292     }
    293 
    294     $categories = get_object_term_cache( $post->ID, 'category' );
    295     if ( false !== $categories ) {
    296         if ( array_key_exists( $category, $categories ) )
    297             return true;
    298         else
    299             return false;
    300     }
    301 
    302     $categories = wp_get_object_terms( $post->ID, 'category', 'fields=ids' );
    303     if ( is_array($categories) && in_array($category, $categories) )
    304         return true;
    305     else
     293    if ( $_post ) {
     294        $_post = get_post( $_post );
     295    } else {
     296        $_post =& $GLOBALS['post'];
     297    }
     298
     299    if ( !$_post )
    306300        return false;
    307301
     302    $r = is_object_in_term( $_post->ID, 'category', $category );
     303    if ( is_wp_error( $r ) )
     304        return false;
     305    return $r;
    308306}
    309307
     
    901899
    902900/**
    903  * Check if the current post has the given tag.
    904  *
    905  * This function is only for use within the WordPress Loop.
     901 * Check if the current post has any of given tags.
     902 *
     903 * The given tags are checked against the post's tags' term_ids, names and slugs.
     904 * Tags given as integers will only be checked against the post's tags' term_ids.
     905 * If no tags are given, determines if post has any tags.
     906 *
     907 * 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)
     908 * Prior to v2.7, this function could only be used in the WordPress Loop.
     909 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
    906910 *
    907911 * @since 2.6.0
    908912 *
    909  * @uses wp_get_object_terms() Gets the tags.
    910  *
    911  * @param string|int|array $tag Optional. The tag name/id/slug or array of them to check for.
    912  * @return bool True if the current post has the given tag, or any tag, if no tag specified.
    913  */
    914 function has_tag( $tag = '' ) {
    915     global $post;
    916     $taxonomy = 'post_tag';
    917 
    918     if ( !in_the_loop() ) return false; // in-the-loop function
    919 
    920     $post_id = (int) $post->ID;
    921 
    922     $terms = get_object_term_cache( $post_id, $taxonomy );
    923     if ( empty( $terms ) )
    924          $terms = wp_get_object_terms( $post_id, $taxonomy );
    925     if ( empty( $terms ) ) return false;
    926 
    927     if ( empty( $tag ) ) return ( !empty( $terms ) );
    928 
    929     $tag = (array) $tag;
    930 
    931     foreach ( $terms as $term ) {
    932         if ( in_array( $term->term_id, $tag ) ) return true;
    933         if ( in_array( $term->name, $tag ) ) return true;
    934         if ( in_array( $term->slug, $tag ) ) return true;
    935     }
    936 
    937     return false;
     913 * @uses is_object_in_term()
     914 *
     915 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
     916 * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
     917 * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
     918 */
     919function has_tag( $tag = '', $_post = null ) {
     920    if ( $_post ) {
     921        $_post = get_post( $_post );
     922    } else {
     923        $_post =& $GLOBALS['post'];
     924    }
     925
     926    if ( !$_post )
     927        return false;
     928
     929    $r = is_object_in_term( $_post->ID, 'post_tag', $tag );
     930    if ( is_wp_error( $r ) )
     931        return false;
     932    return $r;
    938933}
    939934
  • trunk/wp-includes/taxonomy.php

    r10150 r10159  
    22252225}
    22262226
     2227/**
     2228 * Determine if the given object is associated with any of the given terms.
     2229 *
     2230 * The given terms are checked against the object's terms' term_ids, names and slugs.
     2231 * Terms given as integers will only be checked against the object's terms' term_ids.
     2232 * If no terms are given, determines if object is associated with any terms in the given taxonomy.
     2233 *
     2234 * @since 2.7.0
     2235 * @uses get_object_term_cache()
     2236 * @uses wp_get_object_terms()
     2237 *
     2238 * @param int $object_id.  ID of the object (post ID, link ID, ...)
     2239 * @param string $taxonomy.  Single taxonomy name
     2240 * @param int|string|array $terms Optional.  Term term_id, name, slug or array of said
     2241 * @return bool|WP_Error. WP_Error on input error.
     2242 */
     2243function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
     2244    if ( !$object_id = (int) $object_id )
     2245        return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );
     2246
     2247    $object_terms = get_object_term_cache( $object_id, $taxonomy );
     2248    if ( empty( $object_terms ) )
     2249         $object_terms = wp_get_object_terms( $object_id, $taxonomy );
     2250
     2251    if ( is_wp_error( $object_terms ) )
     2252        return $object_terms;
     2253    if ( empty( $object_terms ) )
     2254        return false;
     2255    if ( empty( $terms ) )
     2256        return ( !empty( $object_terms ) );
     2257
     2258    $terms = (array) $terms;
     2259
     2260    if ( $ints = array_filter( $terms, 'is_int' ) )
     2261        $strs = array_diff( $terms, $ints );
     2262    else
     2263        $strs =& $terms;
     2264
     2265    foreach ( $object_terms as $object_term ) {
     2266        if ( $ints && in_array( $object_term->term_id, $ints ) ) return true; // If int, check against term_id
     2267        if ( $strs ) {
     2268            if ( in_array( $object_term->term_id, $strs ) ) return true;
     2269            if ( in_array( $object_term->name, $strs ) )    return true;
     2270            if ( in_array( $object_term->slug, $strs ) )    return true;
     2271        }
     2272    }
     2273
     2274    return false;
     2275}
     2276
    22272277?>
Note: See TracChangeset for help on using the changeset viewer.