Make WordPress Core

Changeset 40275


Ignore:
Timestamp:
03/11/2017 02:26:11 AM (8 years ago)
Author:
boonebgorges
Message:

Don't run 'get_terms' filter when querying for terms within get_term_by().

Historically, it has been possible to call get_term_by() within
a 'get_terms' filter callback. Since get_term_by() was refactored
to use get_terms() internally [38677], callbacks of this nature
have resulted in infinite loops.

As a workaround, we introduce a 'suppress_filter' option to get_terms(),
and use it when calling the function from within get_term_by().

Props ocean90.
See #21760.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r40145 r40275  
    850850        'update_term_meta_cache' => false,
    851851        'orderby'                => 'none',
     852        'suppress_filter'        => true,
    852853    );
    853854
     
    10131014 * @since 4.5.0 Changed the function signature so that the `$args` array can be provided as the first parameter.
    10141015 *              Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata.
     1016 * @since 4.8.0 Introduced 'suppress_filter' parameter.
    10151017 *
    10161018 * @internal The `$deprecated` parameter is parsed for backward compatibility only.
     
    10841086 *                                                Default empty.
    10851087 *     @type string       $meta_compare           Comparison operator to test the 'meta_value'. Default empty.
     1088 *     @type bool         $suppress_filter        Whether to suppress the {@see 'get_terms'} filter. Default false.
    10861089 * }
    10871090 * @param array $deprecated Argument array, when using the legacy function parameter format. If present, this
     
    10951098
    10961099    $term_query = new WP_Term_Query();
     1100
     1101    $defaults = array(
     1102        'suppress_filter' => false,
     1103    );
    10971104
    10981105    /*
     
    11091116    if ( $do_legacy_args ) {
    11101117        $taxonomies = (array) $args;
    1111         $args = wp_parse_args( $deprecated );
     1118        $args = wp_parse_args( $deprecated, $defaults );
    11121119        $args['taxonomy'] = $taxonomies;
    11131120    } else {
    1114         $args = wp_parse_args( $args );
     1121        $args = wp_parse_args( $args, $defaults );
    11151122        if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) {
    11161123            $args['taxonomy'] = (array) $args['taxonomy'];
     
    11261133    }
    11271134
     1135    // Don't pass suppress_filter to WP_Term_Query.
     1136    $suppress_filter = $args['suppress_filter'];
     1137    unset( $args['suppress_filter'] );
     1138
    11281139    $terms = $term_query->query( $args );
    11291140
    11301141    // Count queries are not filtered, for legacy reasons.
    11311142    if ( ! is_array( $terms ) ) {
     1143        return $terms;
     1144    }
     1145
     1146    if ( $suppress_filter ) {
    11321147        return $terms;
    11331148    }
  • trunk/tests/phpunit/tests/term/getTermBy.php

    r38677 r40275  
    193193        $this->assertContains( 'LIMIT 1', $wpdb->last_query );
    194194    }
     195
     196    /**
     197     * @ticket 21760
     198     */
     199    public function test_prevent_recursion_by_get_terms_filter() {
     200        $action = new MockAction();
     201
     202        add_filter( 'get_terms', array( $action, 'filter' ) );
     203        get_term_by( 'name', 'burrito', 'post_tag' );
     204        remove_filter( 'get_terms', array( $action, 'filter' ) );
     205
     206        $this->assertEquals( 0, $action->get_call_count() );
     207    }
    195208}
Note: See TracChangeset for help on using the changeset viewer.