Make WordPress Core


Ignore:
Timestamp:
03/11/2022 11:05:02 AM (3 years ago)
Author:
spacedmonkey
Message:

Taxonomy: Use get_terms instead of a database lookup in term_exists().

Replace raw SQL queries to the terms table, with a call to the get_terms function. Using get_terms means that term_exists is now cached. For developers using term_exists where cache invalidation is disabled, such as importing, a workaround was added to ensure that queries are uncached.

Props Spacedmonkey, boonebgorges, flixos90, peterwilsoncc.
Fixes #36949.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/termExists.php

    r52389 r52921  
    272272    }
    273273
     274    /**
     275     * @ticket 36949
     276     * @covers ::term_exists()
     277     */
     278    public function test_term_lookup_by_id_and_update() {
     279        register_taxonomy( 'wptests_tax', 'post' );
     280
     281        $slug = __FUNCTION__;
     282        $t    = self::factory()->term->create(
     283            array(
     284                'slug'     => $slug,
     285                'taxonomy' => 'wptests_tax',
     286            )
     287        );
     288        $this->assertEquals( $t, term_exists( $t ) );
     289        $this->assertTrue( wp_delete_term( $t, 'wptests_tax' ) );
     290        $this->assertNull( term_exists( $t ) );
     291
     292        // Clean up.
     293        _unregister_taxonomy( 'wptests_tax' );
     294    }
     295
     296    /**
     297     * @ticket 36949
     298     * @covers ::term_exists()
     299     */
     300    public function test_term_lookup_by_slug_and_update() {
     301        register_taxonomy( 'wptests_tax', 'post' );
     302
     303        $slug = __FUNCTION__;
     304        $t    = self::factory()->term->create(
     305            array(
     306                'slug'     => $slug,
     307                'taxonomy' => 'wptests_tax',
     308            )
     309        );
     310        $this->assertEquals( $t, term_exists( $slug ) );
     311        $this->assertTrue( wp_delete_term( $t, 'wptests_tax' ) );
     312        $this->assertNull( term_exists( $slug ) );
     313
     314        // Clean up.
     315        _unregister_taxonomy( 'wptests_tax' );
     316    }
     317
     318    /**
     319     * @ticket 36949
     320     * @covers ::term_exists()
     321     */
     322    public function test_term_exists_caching() {
     323        global $wpdb;
     324        register_taxonomy( 'wptests_tax', 'post' );
     325
     326        $slug = __FUNCTION__;
     327        $t    = self::factory()->term->create(
     328            array(
     329                'slug'     => $slug,
     330                'taxonomy' => 'wptests_tax',
     331            )
     332        );
     333        $this->assertEquals( $t, term_exists( $slug ) );
     334        $num_queries = $wpdb->num_queries;
     335        $this->assertEquals( $t, term_exists( $slug ) );
     336        $this->assertSame( $num_queries, $wpdb->num_queries );
     337
     338        $this->assertTrue( wp_delete_term( $t, 'wptests_tax' ) );
     339        $num_queries = $wpdb->num_queries;
     340        $this->assertNull( term_exists( $slug ) );
     341        $this->assertSame( $num_queries + 2, $wpdb->num_queries );
     342
     343        // Clean up.
     344        _unregister_taxonomy( 'wptests_tax' );
     345    }
     346
     347    /**
     348     * @ticket 36949
     349     * @covers ::term_exists()
     350     */
     351    public function test_term_exists_caching_suspend_cache_invalidation() {
     352        global $wpdb;
     353        register_taxonomy( 'wptests_tax', 'post' );
     354
     355        wp_suspend_cache_invalidation( true );
     356        $slug = __FUNCTION__;
     357        $t    = self::factory()->term->create(
     358            array(
     359                'slug'     => $slug,
     360                'taxonomy' => 'wptests_tax',
     361            )
     362        );
     363
     364        $this->assertEquals( $t, term_exists( $slug ) );
     365        $num_queries = $wpdb->num_queries;
     366        $this->assertEquals( $t, term_exists( $slug ) );
     367        $this->assertSame( $num_queries + 1, $wpdb->num_queries );
     368        wp_suspend_cache_invalidation( false );
     369
     370        // Clean up.
     371        _unregister_taxonomy( 'wptests_tax' );
     372    }
     373
     374    /**
     375     * @ticket 36949
     376     * @covers ::term_exists()
     377     */
     378    public function test_term_exists_caching_by_int_suspend_cache_invalidation() {
     379        register_taxonomy( 'wptests_tax', 'post' );
     380
     381        $slug = __FUNCTION__;
     382        $t    = self::factory()->term->create(
     383            array(
     384                'slug'     => $slug,
     385                'taxonomy' => 'wptests_tax',
     386            )
     387        );
     388
     389        // Warm cache in get_term() via term_exists().
     390        term_exists( $t );
     391        wp_suspend_cache_invalidation( true );
     392        wp_delete_term( $t, 'wptests_tax' );
     393        $this->assertNull( term_exists( $t ) );
     394
     395        // Reneable cache invalidation.
     396        wp_suspend_cache_invalidation( false );
     397        _unregister_taxonomy( 'wptests_tax' );
     398    }
     399
    274400    public function test_term_exists_unknown() {
    275401        $this->assertNull( term_exists( rand_str() ) );
Note: See TracChangeset for help on using the changeset viewer.