From e174fdf21b2886d20d81a580222451315281458d Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 22 Nov 2016 11:40:22 +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. git-svn-id: https://develop.svn.wordpress.org/trunk@39338 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/theme.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 13bf9ca1a1..eea50a50dc 100644 --- a/src/wp-includes/theme.php +++ b/src/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 );