From 9956c90e2d27f2183be41471cf42275f0b51585f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Thu, 14 Nov 2019 23:15:38 -0800 Subject: [PATCH] Implement method to get all extension_specs --- bin/amphtml-update.py | 17 +++++++++++++++++ includes/amp-helper-functions.php | 13 +++---------- includes/class-amp-theme-support.php | 3 ++- .../class-amp-allowed-tags-generated.php | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/bin/amphtml-update.py b/bin/amphtml-update.py index 07dd23af353..1948c4cb592 100644 --- a/bin/amphtml-update.py +++ b/bin/amphtml-update.py @@ -211,6 +211,23 @@ def GenerateFooterPHP(out): return self::$allowed_tags; } + /** + * Get extension specs. + * + * @since 1.5 + * @internal + * @return array Extension specs, keyed by extension name. + */ + public static function get_extension_specs() { + $extension_specs = []; + foreach ( self::get_allowed_tag( 'script' ) as $script_spec ) { + if ( isset( $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec'] ) ) { + $extension_specs[ $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']['name'] ] = $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']; + } + } + return $extension_specs; + } + /** * Get allowed tag. * diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 69adbf8e148..91394bfc453 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -465,26 +465,19 @@ function amp_register_default_scripts( $wp_scripts ) { ); // Register all AMP components as defined in the spec. - foreach ( AMP_Allowed_Tags_Generated::get_allowed_tag( 'script' ) as $script_spec ) { - if ( ! isset( $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec'] ) ) { - continue; - } - $extension_spec = $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']; - + foreach ( AMP_Allowed_Tags_Generated::get_extension_specs() as $extension_name => $extension_spec ) { $src = sprintf( 'https://cdn.ampproject.org/v0/%s-%s.js', - $extension_spec['name'], + $extension_name, end( $extension_spec['version'] ) ); $wp_scripts->add( - $extension_spec['name'], + $extension_name, $src, [ 'amp-runtime' ], null ); - - $wp_scripts->add_data( $extension_spec['name'], 'amp_requires_usage', ! empty( $extension_spec['requires_usage'] ) ); } if ( $wp_scripts->query( 'amp-carousel', 'registered' ) ) { diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 3fd1e650d40..e881f72f8b0 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -1746,12 +1746,13 @@ public static function ensure_required_markup( DOMDocument $dom, $script_handles } // Remove scripts that had already been added but couldn't be detected from output buffering. + $extension_specs = AMP_Allowed_Tags_Generated::get_extension_specs(); $superfluous_script_handles = array_diff( array_keys( $amp_scripts ), array_merge( $script_handles, [ 'amp-runtime' ] ) ); foreach ( $superfluous_script_handles as $superfluous_script_handle ) { - if ( true === wp_scripts()->get_data( $superfluous_script_handle, 'amp_requires_usage' ) ) { + if ( ! empty( $extension_specs[ $superfluous_script_handle ]['requires_usage'] ) ) { unset( $amp_scripts[ $superfluous_script_handle ] ); } } diff --git a/includes/sanitizers/class-amp-allowed-tags-generated.php b/includes/sanitizers/class-amp-allowed-tags-generated.php index 3f3c62e0088..be5345bde8e 100644 --- a/includes/sanitizers/class-amp-allowed-tags-generated.php +++ b/includes/sanitizers/class-amp-allowed-tags-generated.php @@ -17595,6 +17595,23 @@ public static function get_allowed_tags() { return self::$allowed_tags; } + /** + * Get extension specs. + * + * @since 1.5 + * @internal + * @return array Extension specs, keyed by extension name. + */ + public static function get_extension_specs() { + $extension_specs = []; + foreach ( self::get_allowed_tag( 'script' ) as $script_spec ) { + if ( isset( $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec'] ) ) { + $extension_specs[ $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']['name'] ] = $script_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']; + } + } + return $extension_specs; + } + /** * Get allowed tag. *