From 0b8740d868559065812b6b7201d44ed9affdaae9 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 22 Nov 2016 11:41:34 +0000 Subject: [PATCH] Themes: Prevent unneeded database updates in `wp_get_custom_css_post()`. When a custom header image was set but custom CSS was not, `wp_get_custom_css_post()` was generating an UPDATE query on every frontend request. In theme options the header image meta data is stored as an object. In `update_option()` this hits an edge case as the resource IDs of the old and new values never match. This changes the logic of `wp_get_custom_css_post()` to ensure `set_theme_mod()` is only called when the custom CSS has changed. Props bradyvercher, helen. Fixes #38866. Built from https://develop.svn.wordpress.org/trunk@39338 git-svn-id: http://core.svn.wordpress.org/trunk@39278 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/theme.php | 15 ++++++++++----- wp-includes/version.php | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/wp-includes/theme.php b/wp-includes/theme.php index 13bf9ca1a1..eea50a50dc 100644 --- a/wp-includes/theme.php +++ b/wp-includes/theme.php @@ -1639,7 +1639,7 @@ function wp_get_custom_css_post( $stylesheet = '' ) { 'post_type' => 'custom_css', 'post_status' => get_post_stati(), 'name' => sanitize_title( $stylesheet ), - 'number' => 1, + 'posts_per_page' => 1, 'no_found_rows' => true, 'cache_results' => true, 'update_post_meta_cache' => false, @@ -1649,16 +1649,21 @@ function wp_get_custom_css_post( $stylesheet = '' ) { $post = null; if ( get_stylesheet() === $stylesheet ) { $post_id = get_theme_mod( 'custom_css_post_id' ); - if ( ! $post_id || ! get_post( $post_id ) ) { + + if ( $post_id > 0 && get_post( $post_id ) ) { + $post = get_post( $post_id ); + } else { $query = new WP_Query( $custom_css_query_vars ); $post = $query->post; /* * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update(). * @todo This should get cleared if a custom_css post is added/removed. */ - set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 ); - } elseif ( $post_id > 0 ) { - $post = get_post( $post_id ); + if ( $post ) { + set_theme_mod( 'custom_css_post_id', $post->ID ); + } elseif ( -1 !== $post_id ) { + set_theme_mod( 'custom_css_post_id', -1 ); + } } } else { $query = new WP_Query( $custom_css_query_vars ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 19abed8869..3be541c1d3 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-beta4-39337'; +$wp_version = '4.7-beta4-39338'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.