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
This commit is contained in:
Peter Wilson 2016-11-22 11:41:34 +00:00
parent d87fe366a9
commit 0b8740d868
2 changed files with 11 additions and 6 deletions

View File

@ -1639,7 +1639,7 @@ function wp_get_custom_css_post( $stylesheet = '' ) {
'post_type' => 'custom_css', 'post_type' => 'custom_css',
'post_status' => get_post_stati(), 'post_status' => get_post_stati(),
'name' => sanitize_title( $stylesheet ), 'name' => sanitize_title( $stylesheet ),
'number' => 1, 'posts_per_page' => 1,
'no_found_rows' => true, 'no_found_rows' => true,
'cache_results' => true, 'cache_results' => true,
'update_post_meta_cache' => false, 'update_post_meta_cache' => false,
@ -1649,16 +1649,21 @@ function wp_get_custom_css_post( $stylesheet = '' ) {
$post = null; $post = null;
if ( get_stylesheet() === $stylesheet ) { if ( get_stylesheet() === $stylesheet ) {
$post_id = get_theme_mod( 'custom_css_post_id' ); $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 ); $query = new WP_Query( $custom_css_query_vars );
$post = $query->post; $post = $query->post;
/* /*
* Cache the lookup. See WP_Customize_Custom_CSS_Setting::update(). * Cache the lookup. See WP_Customize_Custom_CSS_Setting::update().
* @todo This should get cleared if a custom_css post is added/removed. * @todo This should get cleared if a custom_css post is added/removed.
*/ */
set_theme_mod( 'custom_css_post_id', $post ? $post->ID : -1 ); if ( $post ) {
} elseif ( $post_id > 0 ) { set_theme_mod( 'custom_css_post_id', $post->ID );
$post = get_post( $post_id ); } elseif ( -1 !== $post_id ) {
set_theme_mod( 'custom_css_post_id', -1 );
}
} }
} else { } else {
$query = new WP_Query( $custom_css_query_vars ); $query = new WP_Query( $custom_css_query_vars );

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.