WordPress.org

Make WordPress Core


Ignore:
Timestamp:
12/09/2008 11:31:11 PM (12 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.