Make WordPress Core

Changeset 45584


Ignore:
Timestamp:
07/01/2019 08:47:21 PM (5 years ago)
Author:
adamsilverstein
Message:

Taxonomy: add a new 'terms_pre_query' filter to short circuit WP_Term_Query 'get_terms' queries.

Add a new terms_pre_query filter which returns null by default. Return a non-null value to bypass WordPress's default get_terms queries.

Props jarocks, boonebgorges, spacedmonkey.
Fixes #41246.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r43571 r45584  
    673673        $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
    674674
     675        $this->terms = null;
     676
     677        /**
     678         * Filter the terms array before the query takes place.
     679         *
     680         * Return a non-null value to bypass WordPress's default term queries.
     681         *
     682         * @since 5.3.0
     683         *
     684         * @param array|null    $terms Return an array of term data to short-circuit WP's term query,
     685         *                             or null to allow WP queries to run normally.
     686         * @param WP_Term_Query $this  The WP_Term_Query instance, passed by reference.
     687         *
     688         */
     689        $this->terms = apply_filters_ref_array( 'terms_pre_query', array( $this->terms, &$this ) );
     690
     691        if ( null !== $this->terms ) {
     692            return $this->terms;
     693        }
     694
    675695        // $args can be anything. Only use the args defined in defaults to compute the key.
    676696        $key          = md5( serialize( wp_array_slice_assoc( $args, array_keys( $this->query_var_defaults ) ) ) . serialize( $taxonomies ) . $this->request );
  • trunk/tests/phpunit/tests/term/query.php

    r43571 r45584  
    738738        return $term;
    739739    }
     740
     741    /**
     742     * @ticket 41246
     743     */
     744    public function test_terms_pre_query_filter_should_bypass_database_query() {
     745        global $wpdb;
     746
     747        add_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 );
     748
     749        $num_queries = $wpdb->num_queries;
     750
     751        $q       = new WP_Term_Query();
     752        $results = $q->query(
     753            array(
     754                'fields' => 'ids',
     755            )
     756        );
     757
     758        remove_filter( 'terms_pre_query', array( __CLASS__, 'filter_terms_pre_query' ), 10, 2 );
     759
     760        // Make sure no queries were executed.
     761        $this->assertSame( $num_queries, $wpdb->num_queries );
     762
     763        // We manually inserted a non-existing term and overrode the results with it.
     764        $this->assertSame( array( 555 ), $q->terms );
     765    }
     766
     767    public static function filter_terms_pre_query( $terms, $query ) {
     768        return array( 555 );
     769    }
    740770}
Note: See TracChangeset for help on using the changeset viewer.