WordPress.org

Make WordPress Core

Ticket #11904: is_tax.11904.2.diff

File is_tax.11904.2.diff, 2.1 KB (added by scribu, 8 years ago)

better taxonomy validation

  • wp-includes/query.php

     
    221221}
    222222
    223223/**
    224  * Whether the current page query has the given taxonomy slug or contains taxonomy.
     224 * Whether the current query is for the given taxonomy and/or term.
    225225 *
     226 * If no taxonomy argument is set, returns true if any taxonomy is queried.
     227 * If the taxonomy argument is passed but no term argument, returns true
     228 *    if the taxonomy or taxonomies in the argument are being queried.
     229 * If both taxonomy and term arguments are passed, returns true
     230 *    if the current query is for a term contained in the terms argument
     231 *    which has a taxonomy contained in the taxonomy argument.
     232 *
    226233 * @since 2.5.0
    227234 * @uses $wp_query
    228235 *
    229  * @param string|array $slug Optional. Slug or slugs to check in current query.
     236 * @param string|array $taxonomy Optional. Slug or slugs to check in current query.
     237 * @param int|array|string $term. Optional. The term's ID, array of term IDs, or term slug.
    230238 * @return bool
    231239 */
    232 function is_tax( $slug = '' ) {
    233         global $wp_query;
     240function is_tax( $taxonomy = '', $term = '' ) {
     241        global $wp_query, $wp_taxonomies;
    234242
    235         if ( !$wp_query->is_tax )
     243        $queried_object = $wp_query->get_queried_object();
     244        $tax_array = array_intersect(array_keys($wp_taxonomies), (array) $taxonomy);
     245        $term_array = (array) $term;
     246
     247        if ( ! ( $wp_query->is_tax || $wp_query->is_category || $wp_query->is_tag ) )
    236248                return false;
    237249
    238         if ( empty($slug) )
     250        if ( empty( $taxonomy ) )
    239251                return true;
    240252
    241         return in_array( get_query_var('taxonomy'), (array) $slug );
     253        if (
     254                count( $tax_array ) &&
     255                (
     256                        isset($queried_object->taxonomy) &&
     257                        in_array($queried_object->taxonomy, $tax_array) &&
     258                        (
     259                                empty( $term ) ||
     260                                (
     261                                        isset($queried_object->term_id) &&
     262                                        count(array_intersect(
     263                                                array($queried_object->term_id, $queried_object->name, $queried_object->slug),
     264                                                $term_array
     265                                        ))
     266                                )
     267                        )
     268                )
     269        ) {
     270                return true;
     271        }
     272
     273        return false;
    242274}
    243275
    244276/**