-
Notifications
You must be signed in to change notification settings - Fork 328
Fix AMP dev mode support #1143
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
Fix AMP dev mode support #1143
Changes from 3 commits
6fe9b48
7e3ddbf
5422198
a4d9cc7
705bb1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -288,7 +288,8 @@ private function add_amp_dev_mode_attributes( $assets ) { | |
| add_filter( | ||
| 'script_loader_tag', | ||
| function ( $tag, $handle ) use ( $assets ) { | ||
| if ( $this->context->is_amp() && isset( $assets[ $handle ] ) && $assets[ $handle ] instanceof Script ) { | ||
| // TODO: 'hoverintent-js' can be removed from here at some point, see https://github.com/ampproject/amp-wp/pull/3928. | ||
| if ( $this->context->is_amp() && ( isset( $assets[ $handle ] ) && $assets[ $handle ] instanceof Script || 'hoverintent-js' === $handle ) ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wasn't the need to manually check for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes indeed (see above), however this was only in the quite recent 1.4.2 version. We'll keep this around for a bit more to have support for older AMP plugin versions. I'd be curious to see what the AMP plugin version distribution is :) |
||
| $tag = preg_replace( '/(?<=<script)(?=\s|>)/i', ' data-ampdevmode', $tag ); | ||
| } | ||
| return $tag; | ||
|
|
@@ -376,7 +377,7 @@ private function get_assets() { | |
| 'nonceEndpoint' => admin_url( 'admin-ajax.php?action=rest-nonce' ), | ||
| 'nonceMiddleware' => ( wp_installing() && ! is_multisite() ) ? '' : wp_create_nonce( 'wp_rest' ), | ||
| 'rootURL' => esc_url_raw( get_rest_url() ), | ||
| ) | ||
| ) | ||
| ), | ||
| 'before' | ||
| ); | ||
|
|
@@ -719,6 +720,8 @@ private function add_async_defer_attribute( $tag, $handle ) { | |
| * @param array $handles List of handles to run before print callbacks for. | ||
| */ | ||
| private function run_before_print_callbacks( WP_Dependencies $dependencies, array $handles ) { | ||
| $is_amp = $this->context->is_amp(); | ||
|
|
||
| foreach ( $handles as $handle ) { | ||
| if ( isset( $this->print_callbacks_done[ $handle ] ) ) { | ||
| continue; | ||
|
|
@@ -728,11 +731,34 @@ private function run_before_print_callbacks( WP_Dependencies $dependencies, arra | |
|
|
||
| if ( isset( $this->assets[ $handle ] ) ) { | ||
| $this->assets[ $handle ]->before_print(); | ||
|
|
||
| // TODO: This can be removed at some point, see https://github.com/ampproject/amp-wp/pull/4001. | ||
| if ( $is_amp && $this->assets[ $handle ] instanceof Script ) { | ||
| $this->add_extra_script_amp_dev_mode( $handle ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that this should no longer be necessary after ampproject/amp-wp#4001. As long as the dependency has the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reminding me of that! I've added two TODOs in the respective areas to remove our own fix in the future. |
||
| } | ||
| } | ||
|
|
||
| if ( isset( $dependencies->registered[ $handle ] ) && is_array( $dependencies->registered[ $handle ]->deps ) ) { | ||
| $this->run_before_print_callbacks( $dependencies, $dependencies->registered[ $handle ]->deps ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adds a comment to all extra scripts so that they are considered compatible with AMP dev mode. | ||
| * | ||
| * {@see Assets::add_amp_dev_mode_attributes()} makes all registered scripts and stylesheets compatible, including | ||
| * their potential inline additions. This method does the same for extra scripts, which are registered under the | ||
| * 'data' key. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $handle The handle of a registered script. | ||
| */ | ||
| private function add_extra_script_amp_dev_mode( $handle ) { | ||
| $data = wp_scripts()->get_data( $handle, 'data' ) ?: ''; | ||
| if ( ! empty( $data ) && is_string( $data ) ) { | ||
| wp_scripts()->add_data( $handle, 'data', '/*googlesitekit*/ ' . $data ); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the right PR reference? Perhaps instead ampproject/amp-wp#4001?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is. ampproject/amp-wp#3928 is where you covered
hoverintent-jsso that it's properly flagged.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see. It's handling cases where users are running older versions of the AMP plugin.