From b4a8af7e35327888a27ea087b284d47e8334fc19 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 13 Aug 2017 00:56:24 +0000 Subject: [PATCH] Customize: Prevent `_delete_option_fresh_site()` from hitting DB if `fresh_site` flag already cleared. Amends [38991]. Props dlh, westonruter. Fixes #41039. git-svn-id: https://develop.svn.wordpress.org/trunk@41244 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 2 +- tests/phpunit/tests/customize/manager.php | 28 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index ffb09f22d6..b08191ebbb 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3303,7 +3303,7 @@ function _config_wp_siteurl( $url = '' ) { * @access private */ function _delete_option_fresh_site() { - update_option( 'fresh_site', 0 ); + update_option( 'fresh_site', '0' ); } /** diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index b38ddbd1f3..36905e292d 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -170,17 +170,41 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $this->assertInstanceOf( 'WPDieException', $exception ); $this->assertContains( 'Invalid changeset UUID', $exception->getMessage() ); - update_option( 'fresh_site', 0 ); + update_option( 'fresh_site', '0' ); $wp_customize = new WP_Customize_Manager(); $wp_customize->setup_theme(); $this->assertFalse( has_action( 'after_setup_theme', array( $wp_customize, 'import_theme_starter_content' ) ) ); // Make sure that starter content import gets queued on a fresh site. - update_option( 'fresh_site', 1 ); + update_option( 'fresh_site', '1' ); $wp_customize->setup_theme(); $this->assertEquals( 100, has_action( 'after_setup_theme', array( $wp_customize, 'import_theme_starter_content' ) ) ); } + /** + * Test that clearing a fresh site is a no-op if the site is already fresh. + * + * @see _delete_option_fresh_site() + * @ticket 41039 + */ + function test_fresh_site_flag_clearing() { + global $wp_customize, $wpdb; + + // Make sure fresh site flag is cleared when publishing a changeset. + update_option( 'fresh_site', '1' ); + do_action( 'customize_save_after', $wp_customize ); + $this->assertEquals( '0', get_option( 'fresh_site' ) ); + + // Simulate a new, uncached request. + wp_cache_delete( 'alloptions', 'options' ); + wp_load_alloptions(); + + // Make sure no DB write is done when publishing and a site is already non-fresh. + $query_count = $wpdb->num_queries; + do_action( 'customize_save_after', $wp_customize ); + $this->assertSame( $query_count, $wpdb->num_queries ); + } + /** * Test WP_Customize_Manager::setup_theme() for frontend. *