Make WordPress Core

Ticket #41246: 41246.4.diff

File 41246.4.diff, 2.4 KB (added by adamsilverstein, 6 years ago)
  • src/wp-includes/class-wp-term-query.php

    diff --git src/wp-includes/class-wp-term-query.php src/wp-includes/class-wp-term-query.php
    index 4b949c6e16..b2224ed0da 100644
    class WP_Term_Query { 
    672672
    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 data 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 );
    677697                $last_changed = wp_cache_get_last_changed( 'terms' );
  • tests/phpunit/tests/term/query.php

    diff --git tests/phpunit/tests/term/query.php tests/phpunit/tests/term/query.php
    index f40a9b55e7..4daac1c35f 100644
    class Tests_Term_Query extends WP_UnitTestCase { 
    737737
    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}