Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer obtaining URL for Customizer preview until AMP Customizer is accessed #6198

Merged

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented May 12, 2021

Summary

This addresses a support forum topic:

When working in the backend using the Classic Editor, we have noticed significant slow queries from the following call related to the AMP plugin.

We have 20k posts and 500k comments.

image

After reviewing previous threads I can confirm AMP developer tools is deactivated. Any other thoughts on how to reduce or eliminate this call on the admin backend? We don’t need to use any of the settings or meta boxes for AMP on a per-post basis, is there an option to not load this call at all?

The issue here is the AMP Customizer link which is added under the Appearance menu when Reader mode is selected:

image

In order to have the Customizer open with an AMP page in the preview, we do a query to find an AMP page and then supply it in the url parameter to customize.php. However, we don't have to supply this URL at this point. We can defer lookup up the URL until the Customizer is actually accessed. We can omit the url parameter and instead supply a custom one like amp_preview. When that is supplied when accessing customize.php and Reader mode is active, in the customize_controls_init action we can at that point do the lookup for an AMP page and pass it to WP_Customize_Manager::set_preview_url(). In this way the preview URL will be determined just in time.

This only applies in Reader mode to the aforementioned AMP admin menu item under Appearance and also to the “Customize Reader Theme” link on the AMP Settings screen:

image


This PR also fixes a deprecation notice which was something I missed in #5793.

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@westonruter westonruter added this to the v2.1.2 milestone May 12, 2021
@westonruter westonruter requested a review from pierlon May 12, 2021 05:47
@github-actions
Copy link
Contributor

github-actions bot commented May 12, 2021

Plugin builds for 5cc5658 are ready 🛎️!

@codecov
Copy link

codecov bot commented May 12, 2021

Codecov Report

Merging #6198 (dbb94b3) into develop (398cc91) will increase coverage by 0.13%.
The diff coverage is 40.00%.

❗ Current head dbb94b3 differs from pull request most recent head 2221645. Consider uploading reports for the commit 2221645 to get more accurate results
Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #6198      +/-   ##
=============================================
+ Coverage      75.10%   75.23%   +0.13%     
- Complexity      5865     5870       +5     
=============================================
  Files            234      234              
  Lines          17747    17754       +7     
=============================================
+ Hits           13329    13358      +29     
+ Misses          4418     4396      -22     
Flag Coverage Δ Complexity Δ
javascript 79.84% <ø> (ø) 0.00 <ø> (ø)
php 75.03% <40.00%> (+0.13%) 5870.00 <4.00> (+5.00)
unit 75.03% <40.00%> (+0.13%) 5870.00 <4.00> (+5.00)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
includes/admin/class-amp-template-customizer.php 93.10% <25.00%> (-2.18%) 81.00 <4.00> (+5.00) ⬇️
includes/admin/functions.php 81.13% <100.00%> (+52.83%) 0.00 <0.00> (ø)

Comment on lines +128 to +158
/**
* Add custom analytics.
*
* This is currently only used for legacy AMP post templates.
*
* @since 0.5
* @see amp_get_analytics()
* @internal
*
* @param array $analytics Analytics.
* @return array Analytics.
*/
function amp_add_custom_analytics( $analytics = [] ) {
$analytics = amp_get_analytics( $analytics );

/**
* Add amp-analytics tags.
*
* This filter allows you to easily insert any amp-analytics tags without needing much heavy lifting.
* This filter should be used to alter entries for legacy AMP templates.
*
* @since 0.4
*
* @param array $analytics An associative array of the analytics entries we want to output. Each array entry must have a unique key, and the value should be an array with the following keys: `type`, `attributes`, `script_data`. See readme for more details.
* @param WP_Post $post The current post.
*/
$analytics = apply_filters( 'amp_post_template_analytics', $analytics, get_queried_object() );

return $analytics;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was simply moved from includes/admin/functions.php

Copy link
Contributor

@pierlon pierlon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:.

@westonruter westonruter merged commit 1c7728c into develop May 13, 2021
@westonruter westonruter deleted the fix/reader-theme-customizer-preview-url-performance branch May 13, 2021 23:08
@pierlon pierlon self-assigned this May 18, 2021
@pierlon
Copy link
Contributor

pierlon commented May 18, 2021

QA Passed

With Reader mode enabled, I verified with Query Monitor that the query to find the latest AMP page is no longer being run:

image

There is no url query parameter attached to the AMP Customizer link:

image

Clicking on the customizer link does indeed display latest AMP page in the preview:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants