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_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 );

View File

@ -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.