Skip to content

Implement ReaderThemeLoader service to load selected Reader theme for AMP pages#4984

Merged
westonruter merged 38 commits intodevelopfrom
add/reader-theme-loader
Jul 8, 2020
Merged

Implement ReaderThemeLoader service to load selected Reader theme for AMP pages#4984
westonruter merged 38 commits intodevelopfrom
add/reader-theme-loader

Conversation

@westonruter
Copy link
Copy Markdown
Member

@westonruter westonruter commented Jul 4, 2020

Summary

Fixes #4478.
Fixes #4476.
Fixes #4708.
Closes #2044.
See #4560.
See #4475.

This PR introduces the ability to select from among AMP-compatible themes to be used when in Reader mode, in addition to a “Legacy” theme which behaves as the current Reader mode. If not using legacy, then selecting a theme allows for all templates on a site to be used as AMP, including singular, homepage, archives, etc.

👉 The UI for selecting the Reader theme is limited to the wizard. The ability to select the Reader theme from the admin screen will be implemented via #4560 as part of #4704. Of particular note, the ability to select individual templates will need to be allowed in Reader mode when a non-legacy template is selected.

Customizer

  • When a Reader theme has been selected, the Customizer can be used to customize it either by clicking on the Customize link in the admin bar on an AMP page, or by clicking the AMP menu item under Appearance in the admin menu, or by clicking Customize at the end of the wizard.
  • The AMP panel in the Customizer which is normally shown in Reader mode is omitted entirely when a Reader theme has been selected, since the Customizer is loaded with that Reader theme loaded in its entirety. The AMP toggle appearing next to the device switcher is not shown when a Reader theme is selected; this is only applicable to legacy Reader mode. When a Reader theme is selected, the Customizer loaded up-front with either the active theme or the reader theme applied.
  • Theme-specific customizations (e.g. color scheme, custom header, background, custom CSS) are only applied to the selected theme, leaving the active theme's settings intact.
  • Nav menus can be fully managed in AMP. Nav menu locations are specific to the Reader theme or active theme, but the menus assigned to those locations are shared between the AMP and non-AMP versions of the sites.
  • Widgets are disabled in Reader themes; this includes hiding the Widgets panel in the Customizer. Since WordPress lacks the concept of “sidebar locations” as it has with “nav menu locations”, being able to manage widgets in Reader mode would be very complicated. Since widgets are not deemed necessary for Reader mode (especially given the mobile viewport), they are disabled.
  • The Themes panel is removed when opening AMP Customizer for a Reader theme. Selecting a different Reader theme must be done currently from the AMP settings screen.
  • When the Customizer is opened for a Reader theme, then the previewed device will default to tablet (which is a modern smartphone resolution).

Opening the Customizer from an AMP page will hide the themes panel, widgets panel (since widgets are disabled), default to the mobile device, and include a note indicating the AMP version of the site is being customized:

image

Changelog

  • When the active theme is switched to be the same as the selected Reader theme, transparently switch the template mode from reader to transitional. This is necessary because Reader mode with the a Reader theme selected which is the same as the active theme is no different than Transitional mode. If the active theme is switched then to a different theme, then Reader mode is resumed.
  • Reuse Customizer logic to switch the active theme when a Reader theme is selected.
  • Add Reader theme to Site Health info.
  • Open AMP Customizer when clicking the Customize admin bar menu item on an AMP page in the new Reader mode.
  • Add amp_is_legacy() helper function which is used to determine if legacy Reader mode is being used. This is essentially the same as !current_theme_support('amp').
  • Split out Customizer JS and CSS for legacy Reader mode.
  • Limit extending Customizer if Legacy Reader mode is active or if requesting Customizer with query var.
  • Eliminate use of amp theme support to indicate whether Standard or Transitional mode are active; rely on option alone, except for narrow case of the default value. Deprecate various methods used for this purpose, including read_theme_support. Nevertheless, when serving a page with legacy Reader mode, make sure the amp theme support is removed; conversely, when serving a non-legacy AMP page ensure the amp theme support is present. Some plugins check current_theme_supports('amp') in order to determine whether they should use standard WordPress theme hooks or else the hooks for the legacy AMP post templates.
  • Eliminate the long-deprecated active_callback theme support arg.
  • Ensure AMP Customizer admin menu link navigates to a supported URL by default, and that it opens the AMP Customizer panel in legacy Reader mode or else the AMP Customizer as a whole when a Reader theme is selected.
  • Ensure that is_amp_available() always returns true if in Customizer preview and new Reader mode is enabled.
  • Add reader theme to meta generator tag when in Reader mode to add a theme tag (e.g. mode=reader; theme=legacy or mode=reader; theme=twentynineteen).
  • Paired browsing is restricted to Transitional mode.
  • Ensure changes to header video cause full-page refreshes when AMP in preview. See Prevent sanitization of Customizer preview styles #4977 (review).
  • Remove long-deprecated AMP_CUSTOMIZER_QUERY_VAR constant.

Cherry-picks logic from prototype PR #4644.

Todo

  • When the active theme is switched to be the same as the theme that is selected for the Reader theme, then the template mode is automatically switched from Reader to Transitional.
  • Determine whether we need to make sure that current_theme_supports('amp') when serving Standard/Transitional mode for compatibility with plugins that look at this: https://wpdirectory.net/search/01ECKV6G437XCTS2ZMX0QBT0JK
  • Being able to select a Reader theme is dependent on a site not customizing the AMP endpoint (query var). So if a site changes the query var from amp to something else like lite, then a warning needs to be displayed. Same goes for a theme registering a custom post type, etc.
  • Finish tests for ReaderThemeLoader.

Checklist

  • My pull request is addressing an open issue (please create one otherwise).
  • 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).

… AMP pages

* Reuse Customizer logic to switch the active theme when a Reader theme is selected.
* Add Reader theme to Site Health info.
* Open AMP Customizer when clicking the Customize admin bar menu item on an AMP page in the new Reader mode.
* Add `amp_is_legacy()` helper function which is used to determine if legacy Reader mode is being used. This is essentially the same as `!current_theme_support('amp')`.
* Split out Customizer JS and CSS for legacy Reader mode.
* Limit extending Customizer if Legacy Reader mode is active or if requesting Customizer with query var.
* Eliminate use of `amp` theme support to indicate whether Standard or Transitional mode are active; rely on option alone, except for narrow case of the default value. Deprecate various methods used for this purpose, including `read_theme_support`.
* Eliminate the long-deprecated active_callback theme support arg.
* Ensure AMP Customizer admin menu link navigates to a supported URL by default.
* Ensure that `is_amp_available()` always returns true if in Customizer preview and new Reader mode is enabled.
* Add reader theme to meta generator tag.
@westonruter westonruter added this to the v1.6 milestone Jul 4, 2020
@googlebot googlebot added the cla: yes Signed the Google CLA label Jul 4, 2020

/* eslint no-magic-numbers: [ "error", { "ignore": [ 0, 1, 250] } ] */

window.ampCustomizeControls = ( function( api, $ ) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This file is simply renamed from amp-customize-controls.js. No changes have been made otherwise.

@westonruter westonruter mentioned this pull request Jul 4, 2020
3 tasks
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jul 4, 2020

Plugin builds for 8bd2038 are ready 🛎️!

@@ -132,7 +132,7 @@ public function add_menu_items() {
* @since 1.0
*/
public function render_theme_support() {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@johnwatkins0 FYI: These changes here are incomplete as I expect you'll be revamping everything as part of #4704.

@googlebot

This comment has been minimized.

@googlebot googlebot added cla: no Has not signed the Google CLA and removed cla: yes Signed the Google CLA labels Jul 4, 2020
…r-theme-loader

* 'develop' of github.com:ampproject/amp-wp:
  Remove last references to allow_dirty_styles and allow_dirty_scripts
  Reduce indentation
  Remove unused sanitizer args
  Ensure all dependencies of the `customizer-preview` style handle are not sanitized
  Prevent sanitization of Customizer preview styles
@westonruter westonruter force-pushed the add/reader-theme-loader branch from 2c04ccd to 7943dc6 Compare July 4, 2020 05:52
@googlebot

This comment has been minimized.

@googlebot googlebot added cla: yes Signed the Google CLA and removed cla: no Has not signed the Google CLA labels Jul 4, 2020
westonruter added a commit that referenced this pull request Jul 4, 2020
Note that this will need to be restored when in non-legacy Reader mode as part of #4984.
…r-theme-loader

* 'develop' of github.com:ampproject/amp-wp: (49 commits)
  Escape admin bar item title text
  Add Settings link to the AMP admin bar item when Dev Tools enabled
  Remove needless conditional
  Handle all removed and accepted case in same way as otherwise
  Make validate link text more concise and consistent
  Improve displaying error counts in validate link
  Show warning in admin bar when kept invalid markup in standard mode
  Use empty() instead of isset()
  Update 'Validate' admin bar item link to 'Validate URL'
  Fix lint issue
  Set mobile_redirect to true from `originalOptions` when value is unchanged in first run
  Remove previously completed todo
  Optimize AMP_Tag_And_Attribute_Sanitizer::get_missing_mandatory_attributes()
  Run astra theme e2e tests separately so theme can be activated/removed only once
  Recommend transitional/reader mode to technical users with amp-compatible active theme
  Fix Jest tests
  Onboarding wizard close link: return to previous location
  Persist default mobile_redirect setting when it is not interacted with
  Prevent wpfooter from preventing clicks
  Fix lint issues
  ...
…r-theme-loader

* 'develop' of github.com:ampproject/amp-wp:
  Bump stable tag to 1.5.5
@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Jul 8, 2020

Re this point in the changelog:

  • Paired browsing is restricted to Transitional mode.

Should AMP_Theme_Support::serve_paired_browsing_experience() bail if Transitional mode is not active? Otherwise, I can manually append ?amp-paired-browsing=1 the page URL to access paired browsing:

image

@westonruter
Copy link
Copy Markdown
Member Author

Should AMP_Theme_Support::serve_paired_browsing_experience() bail if Transitional mode is not active? Otherwise, I can manually append ?amp-paired-browsing=1 the page URL to access paired browsing:

It's a good question, and I thought about that, but I decided to leave it as a “power user” feature. Also, we may want to allow paired browsing in Reader mode. To do that, we'll need to introduce a toggle for enabling/disabling scroll locking. In Transitional mode, the toggle would be on by default. In Reader mode, it would be off by default.

@westonruter
Copy link
Copy Markdown
Member Author

Todos complete!

@westonruter westonruter requested a review from pierlon July 8, 2020 19:41
@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Jul 8, 2020

Issue with legacy reader mode:

  • I can't preview the AMP page for posts:

image

  • Clicking on the "Navigate to an AMP compatible page" produces this stacktrace:
[08-Jul-2020 19:44:25 UTC] PHP Warning:  require(/app/public/content/plugins/amp/assets/js/amp-customizer-design-preview-legacy.asset.php): failed to open stream: No such file or directory in /app/public/content/plugins/amp/includes/settings/class-amp-customizer-design-settings.php on line 227
[08-Jul-2020 19:44:25 UTC] PHP Stack trace:
[08-Jul-2020 19:44:25 UTC] PHP   1. {main}() /app/public/index.php:0
[08-Jul-2020 19:44:25 UTC] PHP   2. require() /app/public/index.php:24
[08-Jul-2020 19:44:25 UTC] PHP   3. require_once() /app/public/core-dev/src/wp-blog-header.php:19
[08-Jul-2020 19:44:25 UTC] PHP   4. include() /app/public/core-dev/src/wp-includes/template-loader.php:106
[08-Jul-2020 19:44:25 UTC] PHP   5. AMP_Post_Template->load() /app/public/content/plugins/amp/includes/templates/reader-template-loader.php:36
[08-Jul-2020 19:44:25 UTC] PHP   6. AMP_Post_Template->load_parts() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:226
[08-Jul-2020 19:44:25 UTC] PHP   7. AMP_Post_Template->verify_and_include() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:237
[08-Jul-2020 19:44:25 UTC] PHP   8. include() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:457
[08-Jul-2020 19:44:25 UTC] PHP   9. AMP_Post_Template->load_parts() /app/public/content/plugins/amp/templates/single.php:46
[08-Jul-2020 19:44:25 UTC] PHP  10. AMP_Post_Template->verify_and_include() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:237
[08-Jul-2020 19:44:25 UTC] PHP  11. include() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:457
[08-Jul-2020 19:44:25 UTC] PHP  12. do_action() /app/public/content/plugins/amp/templates/html-end.php:31
[08-Jul-2020 19:44:25 UTC] PHP  13. WP_Hook->do_action() /app/public/core-dev/src/wp-includes/plugin.php:478
[08-Jul-2020 19:44:25 UTC] PHP  14. WP_Hook->apply_filters() /app/public/core-dev/src/wp-includes/class-wp-hook.php:311
[08-Jul-2020 19:44:25 UTC] PHP  15. AMP_Template_Customizer->add_legacy_preview_scripts() /app/public/core-dev/src/wp-includes/class-wp-hook.php:287
[08-Jul-2020 19:44:25 UTC] PHP  16. do_action() /app/public/content/plugins/amp/includes/admin/class-amp-template-customizer.php:355
[08-Jul-2020 19:44:25 UTC] PHP  17. WP_Hook->do_action() /app/public/core-dev/src/wp-includes/plugin.php:478
[08-Jul-2020 19:44:25 UTC] PHP  18. WP_Hook->apply_filters() /app/public/core-dev/src/wp-includes/class-wp-hook.php:311
[08-Jul-2020 19:44:25 UTC] PHP  19. AMP_Customizer_Design_Settings::enqueue_customizer_preview_scripts() /app/public/core-dev/src/wp-includes/class-wp-hook.php:287
[08-Jul-2020 19:44:25 UTC] PHP Fatal error:  require(): Failed opening required '/app/public/content/plugins/amp/assets/js/amp-customizer-design-preview-legacy.asset.php' (include_path='.:/usr/local/lib/php') in /app/public/content/plugins/amp/includes/settings/class-amp-customizer-design-settings.php on line 227
[08-Jul-2020 19:44:25 UTC] PHP Stack trace:
[08-Jul-2020 19:44:25 UTC] PHP   1. {main}() /app/public/index.php:0
[08-Jul-2020 19:44:25 UTC] PHP   2. require() /app/public/index.php:24
[08-Jul-2020 19:44:25 UTC] PHP   3. require_once() /app/public/core-dev/src/wp-blog-header.php:19
[08-Jul-2020 19:44:25 UTC] PHP   4. include() /app/public/core-dev/src/wp-includes/template-loader.php:106
[08-Jul-2020 19:44:25 UTC] PHP   5. AMP_Post_Template->load() /app/public/content/plugins/amp/includes/templates/reader-template-loader.php:36
[08-Jul-2020 19:44:25 UTC] PHP   6. AMP_Post_Template->load_parts() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:226
[08-Jul-2020 19:44:25 UTC] PHP   7. AMP_Post_Template->verify_and_include() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:237
[08-Jul-2020 19:44:25 UTC] PHP   8. include() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:457
[08-Jul-2020 19:44:25 UTC] PHP   9. AMP_Post_Template->load_parts() /app/public/content/plugins/amp/templates/single.php:46
[08-Jul-2020 19:44:25 UTC] PHP  10. AMP_Post_Template->verify_and_include() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:237
[08-Jul-2020 19:44:25 UTC] PHP  11. include() /app/public/content/plugins/amp/includes/templates/class-amp-post-template.php:457
[08-Jul-2020 19:44:25 UTC] PHP  12. do_action() /app/public/content/plugins/amp/templates/html-end.php:31
[08-Jul-2020 19:44:25 UTC] PHP  13. WP_Hook->do_action() /app/public/core-dev/src/wp-includes/plugin.php:478
[08-Jul-2020 19:44:25 UTC] PHP  14. WP_Hook->apply_filters() /app/public/core-dev/src/wp-includes/class-wp-hook.php:311
[08-Jul-2020 19:44:25 UTC] PHP  15. AMP_Template_Customizer->add_legacy_preview_scripts() /app/public/core-dev/src/wp-includes/class-wp-hook.php:287
[08-Jul-2020 19:44:25 UTC] PHP  16. do_action() /app/public/content/plugins/amp/includes/admin/class-amp-template-customizer.php:355
[08-Jul-2020 19:44:25 UTC] PHP  17. WP_Hook->do_action() /app/public/core-dev/src/wp-includes/plugin.php:478
[08-Jul-2020 19:44:25 UTC] PHP  18. WP_Hook->apply_filters() /app/public/core-dev/src/wp-includes/class-wp-hook.php:311
[08-Jul-2020 19:44:25 UTC] PHP  19. AMP_Customizer_Design_Settings::enqueue_customizer_preview_scripts() /app/public/core-dev/src/wp-includes/class-wp-hook.php:287

@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Jul 8, 2020

Nevermind! Just forgot to rebuild the JS scripts 😅 .

@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Jul 8, 2020

My first point from #4984 (comment) still seems valid, however.

@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Jul 8, 2020

The AMP toggle is still on when navigating to a non-singular template; I can also toggle it on said template:

@westonruter
Copy link
Copy Markdown
Member Author

My first point from #4984 (comment) still seems valid, however.

This would be the same point as you raised in #4984 (comment). Let me make your suggestion to limit that logic to when legacy Reader mode is enabled.

@westonruter
Copy link
Copy Markdown
Member Author

The AMP toggle is still on when navigating to a non-singular template; I can also toggle it on said template:

I believe this would be an existing issue not introduced by this PR, is that right?

@pierlon
Copy link
Copy Markdown
Contributor

pierlon commented Jul 8, 2020

I don't think so. I just did a quick test on the develop branch and the issue is not present.

@westonruter westonruter force-pushed the add/reader-theme-loader branch from a008c15 to d9c8de3 Compare July 8, 2020 20:55
@westonruter
Copy link
Copy Markdown
Member Author

I don't think so. I just did a quick test on the develop branch and the issue is not present.

I see what happened. Fixed in a008c15!

Co-authored-by: Pierre Gordon <16200219+pierlon@users.noreply.github.com>
Copy link
Copy Markdown
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.

¡Muy bien trabajo!

public function register() {
// The following needs to run at plugins_loaded because that is when _wp_customize_include runs. Otherwise, the
// most logical action would be setup_theme.
add_action( 'plugins_loaded', [ $this, 'override_theme' ], 9 );
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should use the Delayed interface instead, and then override_theme would be the new register.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not quite, as it has to happen specifically at priority 9. The Delayed interface only provides for the action name generally, so that would mean priority 10.

$theme_support = AMP_Options_Manager::get_option( Option::THEME_SUPPORT );

/** @var AmpSlugCustomizationWatcher $amp_slug_customization_watcher */
$amp_slug_customization_watcher = Services::get( 'amp_slug_customization_watcher' );
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should be injected instead, provided that AMP_Options_Menu is instantiated via the injector.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agreed. Since AMP_Options_Menu is not yet instantiated that way, I used Services in the time being.

}

/** @var AmpSlugCustomizationWatcher $amp_slug_customization_watcher */
$amp_slug_customization_watcher = Services::get( 'amp_slug_customization_watcher' );
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should be injected instead, provided that AMP_Setup_Wizard_Submenu_Page is instantiated via the injector.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelogged Whether the issue/PR has been added to release notes. cla: yes Signed the Google CLA

Projects

None yet

4 participants