mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@54186 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -353,29 +353,81 @@ class Tests_Theme_wpThemeJsonResolver extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
|
||||
*/
|
||||
function test_get_user_data_from_wp_global_styles_does_not_use_uncached_queries() {
|
||||
$theme = wp_get_theme();
|
||||
WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
|
||||
add_filter( 'query', array( $this, 'filter_db_query' ) );
|
||||
$query_count = count( $this->queries );
|
||||
for ( $i = 0; $i < 3; $i++ ) {
|
||||
WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() );
|
||||
WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
|
||||
WP_Theme_JSON_Resolver::clean_cached_data();
|
||||
}
|
||||
$query_count = count( $this->queries ) - $query_count;
|
||||
$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()' );
|
||||
$this->assertEquals( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' );
|
||||
|
||||
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() );
|
||||
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
|
||||
$this->assertEmpty( $user_cpt );
|
||||
|
||||
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme(), true );
|
||||
$user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
|
||||
$this->assertNotEmpty( $user_cpt );
|
||||
|
||||
$query_count = count( $this->queries );
|
||||
for ( $i = 0; $i < 3; $i++ ) {
|
||||
WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() );
|
||||
for ( $i = 0; $i < 3; $i ++ ) {
|
||||
$new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
|
||||
WP_Theme_JSON_Resolver::clean_cached_data();
|
||||
$this->assertSameSets( $user_cpt, $new_user_cpt );
|
||||
}
|
||||
$query_count = count( $this->queries ) - $query_count;
|
||||
$this->assertEquals( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type' );
|
||||
remove_filter( 'query', array( $this, 'filter_db_query' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55392
|
||||
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
|
||||
*/
|
||||
function test_get_user_data_from_wp_global_styles_does_exist() {
|
||||
$theme = wp_get_theme();
|
||||
$post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
|
||||
$this->assertIsArray( $post1 );
|
||||
$this->assertArrayHasKey( 'ID', $post1 );
|
||||
wp_delete_post( $post1['ID'], true );
|
||||
$post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
|
||||
$this->assertIsArray( $post2 );
|
||||
$this->assertArrayHasKey( 'ID', $post2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55392
|
||||
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
|
||||
*/
|
||||
function test_get_user_data_from_wp_global_styles_create_post() {
|
||||
$theme = wp_get_theme( 'testing' );
|
||||
$post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
|
||||
$this->assertIsArray( $post1 );
|
||||
$this->assertSameSets( array(), $post1 );
|
||||
$post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
|
||||
$this->assertIsArray( $post2 );
|
||||
$this->assertSameSets( array(), $post2 );
|
||||
$post3 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true );
|
||||
$this->assertIsArray( $post3 );
|
||||
$this->assertArrayHasKey( 'ID', $post3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55392
|
||||
* @covers WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles
|
||||
*/
|
||||
function test_get_user_data_from_wp_global_styles_filter_state() {
|
||||
$theme = wp_get_theme( 'foo' );
|
||||
$post1 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, true, array( 'publish' ) );
|
||||
$this->assertIsArray( $post1 );
|
||||
$this->assertArrayHasKey( 'ID', $post1 );
|
||||
$post2 = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme, false, array( 'draft' ) );
|
||||
$this->assertIsArray( $post2 );
|
||||
$this->assertSameSets( array(), $post2 );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user