Make WordPress Core


Ignore:
Timestamp:
09/16/2022 10:55:58 AM (21 months ago)
Author:
spacedmonkey
Message:

Posts, Post Types: Improve performance of the get_user_data_from_wp_global_styles method.

Improve the logic found in get_user_data_from_wp_global_styles method. Replace call to wp_get_recent_posts with the more standard, WP_Query for consistancy. Use transient over standard cache, to improve performance on sites without persistent object caching. Improve handling of cases where wp_insert_post returns a WP_Error.

Props spacedmonkey, adamsilverstein, mukesh27, peterwilsoncc, andregal.
Fixes #55392.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php

    r54184 r54186  
    354354    }
    355355
     356    /**
     357     * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
     358     */
    356359    function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() {
     360        $theme = wp_get_theme();
     361        WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
    357362        add_filter( 'query', array( $this, 'filter_db_query' ) );
    358363        $query_count = count( $this->queries );
    359364        for ( $i = 0; $i < 3; $i++ ) {
    360             WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() );
    361             WP_Theme_JSON_Resolver::clean_cached_data();
    362         }
    363         $query_count = count( $this->queries ) - $query_count;
    364         $this->assertEquals( 1, $query_count, 'Only one SQL query should be peformed for multiple invocations of WP_Theme_JSON_Resolver::get_global_styles_from_post()' );
    365 
    366         $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() );
    367         $this->assertEmpty( $user_cpt );
    368 
    369         $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true );
    370         $this->assertNotEmpty( $user_cpt );
    371 
    372         $query_count = count( $this->queries );
    373         for ( $i = 0; $i < 3; $i++ ) {
    374             WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() );
     365            WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
    375366            WP_Theme_JSON_Resolver::clean_cached_data();
    376367        }
    377368        $query_count = count( $this->queries ) - $query_count;
    378369        $this->assertEquals( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' );
     370
     371        $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     372        $this->assertEmpty( $user_cpt );
     373
     374        $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
     375        $this->assertNotEmpty( $user_cpt );
     376
     377        $query_count = count( $this->queries );
     378        for ( $i = 0; $i < 3; $i ++ ) {
     379            $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     380            WP_Theme_JSON_Resolver::clean_cached_data();
     381            $this->assertSameSets( $user_cpt, $new_user_cpt );
     382        }
     383        $query_count = count( $this->queries ) - $query_count;
     384        $this->assertEquals( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' );
    379385        remove_filter( 'query', array( $this, 'filter_db_query' ) );
    380386    }
     387
     388    /**
     389     * @ticket 55392
     390     * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
     391     */
     392    function test_get_user_data_from_wp_global_styles_does_exist() {
     393        $theme = wp_get_theme();
     394        $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
     395        $this->assertIsArray( $post1 );
     396        $this->assertArrayHasKey( 'ID', $post1 );
     397        wp_delete_post( $post1['ID'], true );
     398        $post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
     399        $this->assertIsArray( $post2 );
     400        $this->assertArrayHasKey( 'ID', $post2 );
     401    }
     402
     403    /**
     404     * @ticket 55392
     405     * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
     406     */
     407    function test_get_user_data_from_wp_global_styles_create_post() {
     408        $theme = wp_get_theme( 'testing' );
     409        $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     410        $this->assertIsArray( $post1 );
     411        $this->assertSameSets( array(), $post1 );
     412        $post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
     413        $this->assertIsArray( $post2 );
     414        $this->assertSameSets( array(), $post2 );
     415        $post3 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
     416        $this->assertIsArray( $post3 );
     417        $this->assertArrayHasKey( 'ID', $post3 );
     418    }
     419
     420    /**
     421     * @ticket 55392
     422     * @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
     423     */
     424    function test_get_user_data_from_wp_global_styles_filter_state() {
     425        $theme = wp_get_theme( 'foo' );
     426        $post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true, array( 'publish' ) );
     427        $this->assertIsArray( $post1 );
     428        $this->assertArrayHasKey( 'ID', $post1 );
     429        $post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, false, array( 'draft' ) );
     430        $this->assertIsArray( $post2 );
     431        $this->assertSameSets( array(), $post2 );
     432    }
    381433}
Note: See TracChangeset for help on using the changeset viewer.