Opened 9 years ago
Closed 9 years ago
#38866 closed defect (bug) (fixed)
Custom CSS generates an update SQL query on every front end request
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 4.7 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Themes | Keywords: | has-patch |
| Focuses: | Cc: |
Description
The wp_get_custom_css_post() can generate an UPDATE query on every front end request in some cases.
On sites that haven't saved the custom CSS setting in the customizer, a custom_css CPT won't exist, so that function runs set_theme_mod( 'custom_css_post_id', -1 ); on every request. Normally, update_option() would short circuit the update query if the new value is the same as the old, but when a header image is set, it saves an object in the header_image_data theme mod.
I didn't dig too deep, but in update_option(), the header_image_data objects have different identifiers, so the strict equality check fails and the update goes through.
Here's a reduced theme mod for the new $value in update_option():
array(3) {
["custom_css_post_id"]=>
int(-1)
["header_image"]=>
string(85) "http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg"
["header_image_data"]=>
object(stdClass)#370 (5) {
["attachment_id"]=>
int(292)
["url"]=>
string(85) "http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg"
["thumbnail_url"]=>
string(85) "http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg"
["height"]=>
int(708)
["width"]=>
int(1260)
}
}
And the var dump for the $old_value:
array(3) {
["custom_css_post_id"]=>
int(-1)
["header_image"]=>
string(85) "http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg"
["header_image_data"]=>
object(stdClass)#626 (5) {
["attachment_id"]=>
int(292)
["url"]=>
string(85) "http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg"
["thumbnail_url"]=>
string(85) "http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg"
["height"]=>
int(708)
["width"]=>
int(1260)
}
}
Attachments (3)
Change History (10)
#2
@
9 years ago
- Milestone changed from Awaiting Review to 4.7
Pushing into 4.7 for visibility and testing
#3
@
9 years ago
Tested Brady's patch and it behaves as described.
In 38866.2.diff I've attempted to fix underlying problem in update_option() by comparing old an new values as serialised data. I'd like some feedback on the perf implications here.
This ticket was mentioned in Slack in #core by helen. View the logs.
9 years ago
#6
@
9 years ago
Running serialize() on every update_option() call is something for 4.8-early, probably.
In 38866.3.diff I flipped and split some of the logic to make it easier to understand and maintain in the future. Do test to make sure I got it right, though :)
38866.diff does a few things:
$post_idis not-1before callingget_post()to eliminate an extra SELECT query.set_theme_mod(), which eliminates the UPDATE query and a SHOW FULL COLUMNS query.numberquery arg toposts_per_page. Neither seem to do anything whenis_singularis true, but I don't thinknumberis valid.To test the patch:
header_image_datatheme mod exists.custom_csspost.Every request on the front end should execute an UPDATE query. From what I can tell, this would affect any install that upgrades to 4.7 with a header image, as well as new installs that set a header image. The extra queries would continue running until the custom CSS setting is modified in the customizer.
After applying the patch, the first request will execute the UPDATE query if the
custom_css_post_iddoesn't exist or has an invalid post ID, but subsequent requests should have 3 fewer queries.