-
Notifications
You must be signed in to change notification settings - Fork 385
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
Facilitate using AMP components outside of AMP documents (AMP in PWA, “dirty AMP”) #1013
Conversation
It would be nice if this were further enhanced to automatically move the AMP scripts from the footer or anywhere else in the add_action( 'wp_footer', function() {
wp_print_scripts( 'amp-mathml' );
?>
<amp-mathml layout="container" data-formula="\[x = {-b \pm \sqrt{b^2-4ac} \over 2a}.\]"></amp-mathml>
<?php
} ); |
* Add AMP scripts the WordPress way: register all AMP component scripts as defined in spec via WP_Scripts via new function amp_register_default_scripts(), allowing simple enqueueing via wp_enqueue_script( 'amp-list' ), for example. * Also register the AMP Shadow API as a default script. * Use wp_print_scripts() with filtering script_loader_tag to output scripts with necessary attributes. * Update whitelist sanitizer to allow, validate, and sanitize AMP scripts. * Eliminate needless amp_component_scripts filter.
Call ensure_required_markup prior to sanitizing document
3860e77
to
0154620
Compare
This is done on 0154620. |
includes/amp-helper-functions.php
Outdated
*/ | ||
$custom_templates = array( 'amp-mustache' ); | ||
|
||
foreach ( $extensions as $extension ) { |
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.
Probably should do an array_unique call here.
includes/amp-helper-functions.php
Outdated
continue; | ||
} | ||
$tag_spec = $rule_spec[ AMP_Rule_Spec::TAG_SPEC ]; | ||
if ( ! empty( $tag_spec['also_requires_tag_warning'] ) ) { |
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 don't think this statement is relevant here
includes/amp-helper-functions.php
Outdated
} else { | ||
$attributes['custom-element'] = $extension; | ||
} | ||
$wp_scripts->add_data( $extension, 'amp_script_attributes', $attributes ); |
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.
This really isn't necessary. In the script_loader_tag filter we can just look to see if the script comes from the AMP CDN and if so then automatically add async and the required custom attribute.
* Eliminate amp_script_attributes script data in favor of just looking at the URL to determine whether to add additional script attributes. * Add array_unique when iterating over extensions to add. * Eliminate looking at also_requires_tag_warning when gathering extension scripts.
Ready for review. |
Reviewing Now Thanks, @westonruter. I'm reviewing this now. |
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.
Approved, Minor Points
Hi @westonruter,
This looks good, and it's interesting how it uses DOMXPath.
There are a few minor questions here. If you're satisfied, feel free to merge this.
$attribute_string = sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) ); | ||
} | ||
$tag = preg_replace( | ||
':(?=></script>):', |
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.
Should the slash be escaped?
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.
No. This is doing a lookahead search to find ></script>
in the string to inject content before it.
includes/class-amp-theme-support.php
Outdated
wp_enqueue_script( 'amp-runtime' ); | ||
|
||
// Enqueue default styles expected by sanitizer. | ||
wp_enqueue_style( 'amp-default', amp_get_asset_url( 'css/amp-default.css' ) ); |
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.
It looks like this is just moved from enqueue_amp_default_styles(), which only passed 2 arguments to wp_enqueue_style()
. But could this use a $version
as the 4th argument: AMP__VERSION
?
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 good point! Yes, this is needed.
$script_tags = ob_get_clean(); | ||
if ( ! empty( $script_tags ) ) { | ||
$response = preg_replace( | ||
'#(?=</head>)#', |
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.
Should the slash be escaped?
amp_register_default_scripts()
, allowing simple enqueueing viawp_enqueue_script( 'amp-list' )
, for example.wp_print_scripts()
with filteringscript_loader_tag
to output scripts with necessary attributes. See https://core.trac.wordpress.org/ticket/12009With this in place, you can leverage AMP components entirely outside of pages served as AMP. For example, to add the quadradic formula to the footer of a site, served as AMP or not, all that is needed is:
This not only adds the
amp-mathml
component script to thehead
, but it also automatically outputs the AMP runtime script, since it is marked as a dependency of all registered AMP component scripts.Fixes #982.