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. git-svn-id: https://develop.svn.wordpress.org/trunk@40275 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
5d65051985
commit
824c50f8ae
@ -849,6 +849,7 @@ function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter
|
||||
'taxonomy' => $taxonomy,
|
||||
'update_term_meta_cache' => false,
|
||||
'orderby' => 'none',
|
||||
'suppress_filter' => true,
|
||||
);
|
||||
|
||||
switch ( $field ) {
|
||||
@ -1012,6 +1013,7 @@ function get_term_to_edit( $id, $taxonomy ) {
|
||||
* a list of WP_Term objects.
|
||||
* @since 4.5.0 Changed the function signature so that the `$args` array can be provided as the first parameter.
|
||||
* Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata.
|
||||
* @since 4.8.0 Introduced 'suppress_filter' parameter.
|
||||
*
|
||||
* @internal The `$deprecated` parameter is parsed for backward compatibility only.
|
||||
*
|
||||
@ -1083,6 +1085,7 @@ function get_term_to_edit( $id, $taxonomy ) {
|
||||
* @type string $meta_type Type of object metadata is for (e.g., comment, post, or user).
|
||||
* Default empty.
|
||||
* @type string $meta_compare Comparison operator to test the 'meta_value'. Default empty.
|
||||
* @type bool $suppress_filter Whether to suppress the {@see 'get_terms'} filter. Default false.
|
||||
* }
|
||||
* @param array $deprecated Argument array, when using the legacy function parameter format. If present, this
|
||||
* parameter will be interpreted as `$args`, and the first function parameter will
|
||||
@ -1095,6 +1098,10 @@ function get_terms( $args = array(), $deprecated = '' ) {
|
||||
|
||||
$term_query = new WP_Term_Query();
|
||||
|
||||
$defaults = array(
|
||||
'suppress_filter' => false,
|
||||
);
|
||||
|
||||
/*
|
||||
* Legacy argument format ($taxonomy, $args) takes precedence.
|
||||
*
|
||||
@ -1108,10 +1115,10 @@ function get_terms( $args = array(), $deprecated = '' ) {
|
||||
|
||||
if ( $do_legacy_args ) {
|
||||
$taxonomies = (array) $args;
|
||||
$args = wp_parse_args( $deprecated );
|
||||
$args = wp_parse_args( $deprecated, $defaults );
|
||||
$args['taxonomy'] = $taxonomies;
|
||||
} else {
|
||||
$args = wp_parse_args( $args );
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
if ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) {
|
||||
$args['taxonomy'] = (array) $args['taxonomy'];
|
||||
}
|
||||
@ -1125,6 +1132,10 @@ function get_terms( $args = array(), $deprecated = '' ) {
|
||||
}
|
||||
}
|
||||
|
||||
// Don't pass suppress_filter to WP_Term_Query.
|
||||
$suppress_filter = $args['suppress_filter'];
|
||||
unset( $args['suppress_filter'] );
|
||||
|
||||
$terms = $term_query->query( $args );
|
||||
|
||||
// Count queries are not filtered, for legacy reasons.
|
||||
@ -1132,6 +1143,10 @@ function get_terms( $args = array(), $deprecated = '' ) {
|
||||
return $terms;
|
||||
}
|
||||
|
||||
if ( $suppress_filter ) {
|
||||
return $terms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the found terms.
|
||||
*
|
||||
|
@ -192,4 +192,17 @@ class Tests_Term_GetTermBy extends WP_UnitTestCase {
|
||||
$this->assertSame( $term_id, $found->term_id );
|
||||
$this->assertContains( 'LIMIT 1', $wpdb->last_query );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 21760
|
||||
*/
|
||||
public function test_prevent_recursion_by_get_terms_filter() {
|
||||
$action = new MockAction();
|
||||
|
||||
add_filter( 'get_terms', array( $action, 'filter' ) );
|
||||
get_term_by( 'name', 'burrito', 'post_tag' );
|
||||
remove_filter( 'get_terms', array( $action, 'filter' ) );
|
||||
|
||||
$this->assertEquals( 0, $action->get_call_count() );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user