Skip to content

Commit

Permalink
Implement method to get all extension_specs
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 15, 2019
1 parent 17d03ad commit 9956c90
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
17 changes: 17 additions & 0 deletions bin/amphtml-update.py
Expand Up @@ -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.
*
Expand Down
13 changes: 3 additions & 10 deletions includes/amp-helper-functions.php
Expand Up @@ -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' ) ) {
Expand Down
3 changes: 2 additions & 1 deletion includes/class-amp-theme-support.php
Expand Up @@ -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 ] );
}
}
Expand Down
17 changes: 17 additions & 0 deletions includes/sanitizers/class-amp-allowed-tags-generated.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 9956c90

Please sign in to comment.