diff --git a/amp.php b/amp.php index e9c3c730c22..c25777ca299 100644 --- a/amp.php +++ b/amp.php @@ -64,6 +64,15 @@ function amp_deactivate() { flush_rewrite_rules(); } +/* + * Register AMP scripts regardless of whether AMP is enabled or it is the AMP endpoint + * for the sake of being able to use AMP components on non-AMP documents ("dirty AMP"). + */ +add_action( 'wp_default_scripts', 'amp_register_default_scripts' ); + +// Ensure async and custom-element/custom-template attributes are present on script tags. +add_filter( 'script_loader_tag', 'amp_filter_script_loader_tag', PHP_INT_MAX, 2 ); + /** * Set up AMP. * diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index be8fd0e3619..1123e072b38 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -167,6 +167,7 @@ function amp_get_asset_url( $file ) { * * @since 0.7 * @link https://www.ampproject.org/docs/reference/spec#boilerplate + * * @return string Boilerplate code. */ function amp_get_boilerplate_code() { @@ -174,6 +175,125 @@ function amp_get_boilerplate_code() { . ''; } +/** + * Register default scripts for AMP components. + * + * @param WP_Scripts $wp_scripts Scripts. + */ +function amp_register_default_scripts( $wp_scripts ) { + + // AMP Runtime. + $handle = 'amp-runtime'; + $wp_scripts->add( + $handle, + 'https://cdn.ampproject.org/v0.js', + array(), + null + ); + $wp_scripts->add_data( $handle, 'amp_script_attributes', array( + 'async' => true, + ) ); + + // Shadow AMP API. + $handle = 'amp-shadow'; + $wp_scripts->add( + $handle, + 'https://cdn.ampproject.org/shadow-v0.js', + array(), + null + ); + $wp_scripts->add_data( $handle, 'amp_script_attributes', array( + 'async' => true, + ) ); + + // Get all AMP components as defined in the spec. + $extensions = array(); + foreach ( AMP_Allowed_Tags_Generated::get_allowed_tags() as $allowed_tag ) { + foreach ( $allowed_tag as $rule_spec ) { + if ( ! isset( $rule_spec[ AMP_Rule_Spec::TAG_SPEC ] ) ) { + continue; + } + $tag_spec = $rule_spec[ AMP_Rule_Spec::TAG_SPEC ]; + if ( ! empty( $tag_spec['also_requires_tag_warning'] ) ) { + $extensions[] = strtok( $tag_spec['also_requires_tag_warning'][0], ' ' ); + } + if ( ! empty( $tag_spec['requires_extension'] ) ) { + $extensions = array_merge( $extensions, $tag_spec['requires_extension'] ); + } + } + } + + /** + * List of components that are custom elements. + * + * Per the spec, "Most extensions are custom-elements." In fact, there is only one custom template. + * + * @link https://github.com/ampproject/amphtml/blob/cd685d4e62153557519553ffa2183aedf8c93d62/validator/validator.proto#L326-L328 + * @link https://github.com/ampproject/amphtml/blob/cd685d4e62153557519553ffa2183aedf8c93d62/extensions/amp-mustache/validator-amp-mustache.protoascii#L27 + */ + $custom_templates = array( 'amp-mustache' ); + + foreach ( $extensions as $extension ) { + $src = sprintf( + 'https://cdn.ampproject.org/v0/%s-%s.js', + $extension, + 'latest' + ); + + $wp_scripts->add( + $extension, + $src, + array( 'amp-runtime' ), + null + ); + $attributes = array( + 'async' => true, + ); + if ( in_array( $extension, $custom_templates, true ) ) { + $attributes['custom-template'] = $extension; + } else { + $attributes['custom-element'] = $extension; + } + $wp_scripts->add_data( $extension, 'amp_script_attributes', $attributes ); + } +} + +/** + * Add AMP script attributes to enqueued scripts. + * + * @link https://core.trac.wordpress.org/ticket/12009 + * @since 0.7 + * + * @param string $tag The script tag. + * @param string $handle The script handle. + * @return string Script loader tag. + */ +function amp_filter_script_loader_tag( $tag, $handle ) { + $attributes = wp_scripts()->get_data( $handle, 'amp_script_attributes' ); + if ( ! is_array( $attributes ) ) { + return $tag; + } + + // Add each attribute (if it hasn't already been added). + foreach ( $attributes as $key => $value ) { + if ( ! preg_match( ":\s$key(=|>|\s):", $tag ) ) { + if ( true === $value ) { + $attribute_string = sprintf( ' %s', esc_attr( $key ) ); + } else { + $attribute_string = sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) ); + } + $tag = preg_replace( + ':(?=>):', + $attribute_string, + $tag, + 1 + ); + } + } + + return $tag; +} + /** * Retrieve analytics data added in backend. * diff --git a/includes/amp-post-template-actions.php b/includes/amp-post-template-actions.php index c33835a65dc..b956faaf6c6 100644 --- a/includes/amp-post-template-actions.php +++ b/includes/amp-post-template-actions.php @@ -46,23 +46,27 @@ function amp_post_template_add_canonical( $amp_template ) { /** * Print scripts. * + * @see amp_register_default_scripts() + * @see amp_filter_script_loader_tag() * @param AMP_Post_Template $amp_template Template. */ function amp_post_template_add_scripts( $amp_template ) { + + // Just in case the runtime has been overridden by amp_post_template_data filter. + wp_scripts()->registered['amp-runtime']->src = $amp_template->get( 'amp_runtime_script' ); + + // Make sure any filtered extension script URLs get updated in registered scripts before printing. $scripts = $amp_template->get( 'amp_component_scripts', array() ); - foreach ( $scripts as $element => $script ) { - $custom_type = ( 'amp-mustache' === $element ) ? 'template' : 'element'; - printf( - '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript - esc_attr( $custom_type ), - esc_attr( $element ), - esc_url( $script ) - ); + foreach ( $scripts as $handle => $value ) { + if ( is_string( $value ) && wp_script_is( $handle, 'registered' ) ) { + wp_scripts()->registered[ $handle ]->src = $value; + } } - printf( - '', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript - esc_url( $amp_template->get( 'amp_runtime_script' ) ) - ); + + wp_print_scripts( array_merge( + array( 'amp-runtime' ), + array_keys( $scripts ) + ) ); } /** diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index ee7da221ad0..c08b84b904e 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -119,6 +119,12 @@ public static function finish_init() { self::register_paired_hooks(); } + // Enqueue AMP runtime. + wp_enqueue_script( 'amp-runtime' ); + + // Enqueue default styles expected by sanitizer. + wp_enqueue_style( 'amp-default', amp_get_asset_url( 'css/amp-default.css' ) ); + self::add_hooks(); self::$sanitizer_classes = amp_get_content_sanitizers(); self::$embed_handlers = self::register_content_embed_handlers(); @@ -212,9 +218,7 @@ public static function add_hooks() { * in this case too we should defer to the theme as well to output the meta charset because it is possible the * install is not on utf-8 and we may need to do a encoding conversion. */ - add_action( 'wp_head', array( __CLASS__, 'add_amp_component_scripts' ), 10 ); add_action( 'wp_print_styles', array( __CLASS__, 'print_amp_styles' ), 0 ); // Print boilerplate before theme and plugin stylesheets. - add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_amp_default_styles' ), 9 ); add_action( 'wp_head', 'amp_add_generator_metadata', 20 ); add_action( 'wp_head', 'amp_print_schemaorg_metadata' ); @@ -566,16 +570,6 @@ public static function filter_paired_template_include( $template ) { return $template; } - /** - * Print AMP script and placeholder for others. - * - * @link https://www.ampproject.org/docs/reference/spec#scrpt - */ - public static function add_amp_component_scripts() { - // Replaced after output buffering with all AMP component scripts. - echo self::SCRIPTS_PLACEHOLDER; // phpcs:ignore WordPress.Security.EscapeOutput, WordPress.XSS.EscapeOutput - } - /** * Get canonical URL for current request. * @@ -748,69 +742,6 @@ public static function print_amp_styles() { echo "\n"; // This will by populated by AMP_Style_Sanitizer. } - /** - * Adds default styles expected by sanitizer. - */ - public static function enqueue_amp_default_styles() { - wp_enqueue_style( 'amp-default', amp_get_asset_url( 'css/amp-default.css' ) ); - } - - /** - * Determine required AMP scripts. - * - * @param array $amp_scripts Initial scripts. - * @return string Scripts to inject into the HEAD. - */ - public static function get_amp_scripts( $amp_scripts ) { - - foreach ( self::$embed_handlers as $embed_handler ) { - $amp_scripts = array_merge( - $amp_scripts, - $embed_handler->get_scripts() - ); - } - - /** - * List of components that are custom elements. - * - * Per the spec, "Most extensions are custom-elements." In fact, there is only one custom template. - * - * @link https://github.com/ampproject/amphtml/blob/cd685d4e62153557519553ffa2183aedf8c93d62/validator/validator.proto#L326-L328 - * @link https://github.com/ampproject/amphtml/blob/cd685d4e62153557519553ffa2183aedf8c93d62/extensions/amp-mustache/validator-amp-mustache.protoascii#L27 - */ - $custom_templates = array( 'amp-mustache' ); - - /** - * Filters AMP component scripts before they are injected onto the output buffer for the response. - * - * Plugins may add their own component scripts which have been rendered but which the plugin doesn't yet - * recognize. - * - * @since 0.7 - * - * @param array $amp_scripts AMP Component scripts, mapping component names to component source URLs. - */ - $amp_scripts = apply_filters( 'amp_component_scripts', $amp_scripts ); - - $scripts = ''; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript - foreach ( $amp_scripts as $amp_script_component => $amp_script_source ) { - - $custom_type = 'custom-element'; - if ( in_array( $amp_script_component, $custom_templates, true ) ) { - $custom_type = 'custom-template'; - } - - $scripts .= sprintf( - '', // phpcs:ignore WordPress.WP.EnqueuedResources, WordPress.XSS.EscapeOutput.OutputNotEscaped - $custom_type, - $amp_script_component, - $amp_script_source - ); - } - - return $scripts; - } - /** * Ensure markup required by AMP . * @@ -1032,13 +963,33 @@ public static function prepare_response( $response, $args = array() ) { $response = "\n"; $response .= AMP_DOM_Utils::get_content_from_dom_node( $dom, $dom->documentElement ); - // Inject required scripts. - $response = preg_replace( - '#' . preg_quote( self::SCRIPTS_PLACEHOLDER, '#' ) . '#', - self::get_amp_scripts( $assets['scripts'] ), - $response, - 1 - ); + $amp_scripts = $assets['scripts']; + foreach ( self::$embed_handlers as $embed_handler ) { + $amp_scripts = array_merge( + $amp_scripts, + $embed_handler->get_scripts() + ); + } + + // Allow for embed handlers to override the default extension version by defining a different URL. + foreach ( $amp_scripts as $handle => $value ) { + if ( is_string( $value ) && wp_script_is( $handle, 'registered' ) ) { + wp_scripts()->registered[ $handle ]->src = $value; + } + } + + // Print all scripts, some of which may have already been printed and inject into head. + ob_start(); + wp_print_scripts( array_keys( $amp_scripts ) ); + $script_tags = ob_get_clean(); + if ( ! empty( $script_tags ) ) { + $response = preg_replace( + '#(?=)#', + $script_tags, + $response, + 1 + ); + } return $response; } diff --git a/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php b/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php index 4a367cee3a9..2f884c14b27 100644 --- a/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php +++ b/includes/sanitizers/class-amp-tag-and-attribute-sanitizer.php @@ -193,21 +193,15 @@ public function __construct( $dom, $args = array() ) { * Javascript URLs from https://cdn.ampproject.org * * @since 0.7 + * @see amp_register_default_scripts() * - * @return string[] Returns component name as array key and JavaScript URL as array value, - * respectively. Will return an empty array if sanitization has yet to be run + * @return array() Returns component name as array key and true as value (or JavaScript URL string), + * respectively. When true then the default component script URL will be used. + * Will return an empty array if sanitization has yet to be run * or if it did not find any HTML elements to convert to AMP equivalents. */ public function get_scripts() { - $scripts = array(); - foreach ( $this->script_components as $component ) { - $scripts[ $component ] = sprintf( - 'https://cdn.ampproject.org/v0/%s-%s.js', - $component, - 'latest' - ); - } - return $scripts; + return array_fill_keys( $this->script_components, true ); } /** @@ -314,6 +308,27 @@ private function process_node( $node ) { } foreach ( $rule_spec_list as $id => $rule_spec ) { if ( $this->validate_tag_spec_for_node( $node, $rule_spec[ AMP_Rule_Spec::TAG_SPEC ] ) ) { + + // Expand extension_spec into a set of attr_spec_list. + if ( isset( $rule_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec'] ) ) { + $extension_spec = $rule_spec[ AMP_Rule_Spec::TAG_SPEC ]['extension_spec']; + $custom_attr = 'amp-mustache' === $extension_spec['name'] ? 'custom-template' : 'custom-element'; + + $rule_spec[ AMP_Rule_Spec::ATTR_SPEC_LIST ][ $custom_attr ] = array( + AMP_Rule_Spec::VALUE => $extension_spec['name'], + AMP_Rule_Spec::MANDATORY => true, + ); + + $rule_spec[ AMP_Rule_Spec::ATTR_SPEC_LIST ]['src'] = array( + AMP_Rule_Spec::VALUE_REGEX => implode( '', array( + '^', + preg_quote( 'https://cdn.ampproject.org/v0/' . $extension_spec['name'] . '-' ), + '(' . implode( '|', $extension_spec['allowed_versions'] ) . ')', + '\.js$', + ) ), + ); + } + $rule_spec_list_to_validate[ $id ] = $rule_spec; } } @@ -530,12 +545,12 @@ private function validate_cdata_for_node( $element, $cdata_spec ) { */ private function validate_tag_spec_for_node( $node, $tag_spec ) { - // Always skip extension spec scripts since we manage the injection of these ourselves. - if ( isset( $tag_spec['extension_spec'] ) ) { + if ( ! empty( $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) && ! $this->has_parent( $node, $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) ) { return false; } - if ( ! empty( $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) && ! $this->has_parent( $node, $tag_spec[ AMP_Rule_Spec::MANDATORY_PARENT ] ) ) { + // Extension scripts must be in the head. + if ( isset( $tag_spec['extension_spec'] ) && ! $this->has_parent( $node, 'head' ) ) { return false; } @@ -580,6 +595,14 @@ private function validate_attr_spec_list_for_node( $node, $attr_spec_list ) { return 0.5; } + if ( ! $node instanceof DOMElement ) { + /* + * A DOMNode is not valid for checks so might + * as well bail here is not an DOMElement. + */ + return 0; + } + foreach ( $node->attributes as $attr_name => $attr_node ) { if ( ! isset( $attr_spec_list[ $attr_name ][ AMP_Rule_Spec::ALTERNATIVE_NAMES ] ) ) { continue; @@ -589,14 +612,6 @@ private function validate_attr_spec_list_for_node( $node, $attr_spec_list ) { } } - if ( ! $node instanceof DOMElement ) { - /* - * A DOMNode is not valid for checks so might - * as well bail here is not an DOMElement. - */ - return 0; - } - $score = 0; /* diff --git a/includes/utils/class-amp-validation-utils.php b/includes/utils/class-amp-validation-utils.php index 9016a51a0d6..0387a2f4489 100644 --- a/includes/utils/class-amp-validation-utils.php +++ b/includes/utils/class-amp-validation-utils.php @@ -917,7 +917,7 @@ public static function wrapped_callback( $callback ) { foreach ( array_diff( $wp_scripts->queue, $before_scripts_enqueued ) as $handle ) { AMP_Validation_Utils::$enqueued_script_sources[ $handle ][] = $callback['source']; - if ( isset( $wp_scripts->registered[ $handle ] ) ) { + if ( ! $wp_scripts->get_data( $handle, 'amp_script_attributes' ) ) { self::add_validation_error( array( 'code' => self::ENQUEUED_SCRIPT_CODE, 'handle' => $handle, diff --git a/tests/test-amp-audio-converter.php b/tests/test-amp-audio-converter.php index 53d5cb246b1..2e24e2bc948 100644 --- a/tests/test-amp-audio-converter.php +++ b/tests/test-amp-audio-converter.php @@ -134,7 +134,7 @@ public function test_get_scripts__didnt_convert() { public function test_get_scripts__did_convert() { $source = ''; - $expected = array( 'amp-audio' => 'https://cdn.ampproject.org/v0/amp-audio-latest.js' ); + $expected = array( 'amp-audio' => true ); $dom = AMP_DOM_Utils::get_dom_from_content( $source ); $sanitizer = new AMP_Audio_Sanitizer( $dom ); diff --git a/tests/test-amp-dailymotion-embed.php b/tests/test-amp-dailymotion-embed.php index 60eb1da4c03..54378a678dc 100644 --- a/tests/test-amp-dailymotion-embed.php +++ b/tests/test-amp-dailymotion-embed.php @@ -50,7 +50,7 @@ public function get_scripts_data() { ), 'converted' => array( 'https://www.dailymotion.com/video/x5awwth' . PHP_EOL, - array( 'amp-dailymotion' => 'https://cdn.ampproject.org/v0/amp-dailymotion-latest.js' ), + array( 'amp-dailymotion' => true ), ), ); } diff --git a/tests/test-amp-facebook-embed.php b/tests/test-amp-facebook-embed.php index a0a1d453f8e..69a2be1f587 100644 --- a/tests/test-amp-facebook-embed.php +++ b/tests/test-amp-facebook-embed.php @@ -54,7 +54,7 @@ public function get_scripts_data() { ), 'converted' => array( 'https://www.facebook.com/zuck/posts/10102593740125791' . PHP_EOL, - array( 'amp-facebook' => 'https://cdn.ampproject.org/v0/amp-facebook-latest.js' ), + array( 'amp-facebook' => true ), ), ); } diff --git a/tests/test-amp-form-sanitizer.php b/tests/test-amp-form-sanitizer.php index 9d1690d3620..954d0da4e94 100644 --- a/tests/test-amp-form-sanitizer.php +++ b/tests/test-amp-form-sanitizer.php @@ -99,7 +99,7 @@ public function test_converter( $source, $expected = null ) { */ public function test_scripts() { $source = '
'; - $expected = array( 'amp-form' => 'https://cdn.ampproject.org/v0/amp-form-latest.js' ); + $expected = array( 'amp-form' => true ); $dom = AMP_DOM_Utils::get_dom_from_content( $source ); $whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom ); diff --git a/tests/test-amp-helper-functions.php b/tests/test-amp-helper-functions.php index f39b3350233..d458e4bac04 100644 --- a/tests/test-amp-helper-functions.php +++ b/tests/test-amp-helper-functions.php @@ -15,6 +15,8 @@ class Test_AMP_Helper_Functions extends WP_UnitTestCase { */ public function tearDown() { remove_theme_support( 'amp' ); + global $wp_scripts; + $wp_scripts = null; parent::tearDown(); } @@ -167,6 +169,41 @@ public function capture_filter_call( $value ) { return $value; } + /** + * Test script registering. + * + * @covers amp_register_default_scripts() + * @covers amp_filter_script_loader_tag() + * @global WP_Scripts $wp_scripts + */ + public function test_script_registering() { + global $wp_scripts; + $wp_scripts = null; + $this->assertEquals( 10, has_action( 'wp_default_scripts', 'amp_register_default_scripts' ) ); + $this->assertEquals( PHP_INT_MAX, has_action( 'script_loader_tag', 'amp_filter_script_loader_tag' ) ); + + $this->assertTrue( wp_script_is( 'amp-runtime', 'registered' ) ); + $this->assertTrue( wp_script_is( 'amp-mustache', 'registered' ) ); + $this->assertTrue( wp_script_is( 'amp-list', 'registered' ) ); + $this->assertTrue( wp_script_is( 'amp-bind', 'registered' ) ); + + wp_enqueue_script( 'amp-mathml' ); + wp_enqueue_script( 'amp-mustache' ); + $this->assertTrue( wp_script_is( 'amp-mathml', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'amp-mustache', 'enqueued' ) ); + + // Try overriding URL. + wp_scripts()->registered['amp-mustache']->src = 'https://cdn.ampproject.org/v0/amp-mustache-0.1.js'; + + ob_start(); + wp_print_scripts(); + $output = ob_get_clean(); + + $this->assertStringStartsWith( '', $output ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + $this->assertContains( '', $output ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + $this->assertContains( '', $output ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + } + /** * Test amp_get_content_embed_handlers(). * diff --git a/tests/test-amp-iframe-sanitizer.php b/tests/test-amp-iframe-sanitizer.php index cf67b0f489f..d451f4715be 100644 --- a/tests/test-amp-iframe-sanitizer.php +++ b/tests/test-amp-iframe-sanitizer.php @@ -142,7 +142,7 @@ public function test_get_scripts__didnt_convert() { public function test_get_scripts__did_convert() { $source = ''; - $expected = array( 'amp-iframe' => 'https://cdn.ampproject.org/v0/amp-iframe-latest.js' ); + $expected = array( 'amp-iframe' => true ); $dom = AMP_DOM_Utils::get_dom_from_content( $source ); $sanitizer = new AMP_Iframe_Sanitizer( $dom ); diff --git a/tests/test-amp-img-sanitizer.php b/tests/test-amp-img-sanitizer.php index c0cc0f10a3e..a6a400264a1 100644 --- a/tests/test-amp-img-sanitizer.php +++ b/tests/test-amp-img-sanitizer.php @@ -152,7 +152,7 @@ public function test_no_gif_no_image_scripts() { public function test_no_gif_image_scripts() { $source = 'Placeholder!'; - $expected = array( 'amp-anim' => 'https://cdn.ampproject.org/v0/amp-anim-latest.js' ); + $expected = array( 'amp-anim' => true ); $dom = AMP_DOM_Utils::get_dom_from_content( $source ); $sanitizer = new AMP_Img_Sanitizer( $dom ); diff --git a/tests/test-amp-instagram-embed.php b/tests/test-amp-instagram-embed.php index 7035d77a3c7..9b28b056d8d 100644 --- a/tests/test-amp-instagram-embed.php +++ b/tests/test-amp-instagram-embed.php @@ -53,7 +53,7 @@ public function get_scripts_data() { ), 'converted' => array( 'https://instagram.com/p/7-l0z_p4A4/' . PHP_EOL, - array( 'amp-instagram' => 'https://cdn.ampproject.org/v0/amp-instagram-latest.js' ), + array( 'amp-instagram' => true ), ), ); } diff --git a/tests/test-amp-pinterest-embed.php b/tests/test-amp-pinterest-embed.php index 857a77471bd..bfe36c86afb 100644 --- a/tests/test-amp-pinterest-embed.php +++ b/tests/test-amp-pinterest-embed.php @@ -37,7 +37,7 @@ public function get_scripts_data() { ), 'converted' => array( 'https://www.pinterest.com/pin/606156431067611861/' . PHP_EOL, - array( 'amp-pinterest' => 'https://cdn.ampproject.org/v0/amp-pinterest-latest.js' ), + array( 'amp-pinterest' => true ), ), ); } diff --git a/tests/test-amp-playbuzz-sanitizer.php b/tests/test-amp-playbuzz-sanitizer.php index fcf85cc4a57..5988dcb9735 100644 --- a/tests/test-amp-playbuzz-sanitizer.php +++ b/tests/test-amp-playbuzz-sanitizer.php @@ -101,7 +101,7 @@ public function test_get_scripts__didnt_convert(){ */ public function test_get_scripts__did_convert() { $source = '
'; - $expected = array( 'amp-playbuzz' => 'https://cdn.ampproject.org/v0/amp-playbuzz-latest.js' ); + $expected = array( 'amp-playbuzz' => true ); $dom = AMP_DOM_Utils::get_dom_from_content( $source ); $sanitizer = new AMP_Playbuzz_Sanitizer( $dom ); diff --git a/tests/test-amp-soundcloud-embed.php b/tests/test-amp-soundcloud-embed.php index cc7b1435478..389a8a4ebb0 100644 --- a/tests/test-amp-soundcloud-embed.php +++ b/tests/test-amp-soundcloud-embed.php @@ -162,7 +162,7 @@ public function get_scripts_data() { ), 'converted' => array( $this->oembed_url . PHP_EOL, - array( 'amp-soundcloud' => 'https://cdn.ampproject.org/v0/amp-soundcloud-latest.js' ), + array( 'amp-soundcloud' => true ), ), ); } diff --git a/tests/test-amp-twitter-embed.php b/tests/test-amp-twitter-embed.php index e103701fade..b9cbf98c00d 100644 --- a/tests/test-amp-twitter-embed.php +++ b/tests/test-amp-twitter-embed.php @@ -66,7 +66,7 @@ public function get_scripts_data() { ), 'converted' => array( 'https://twitter.com/altjoen/status/118252236836061184' . PHP_EOL, - array( 'amp-twitter' => 'https://cdn.ampproject.org/v0/amp-twitter-latest.js' ), + array( 'amp-twitter' => true ), ), ); } diff --git a/tests/test-amp-video-sanitizer.php b/tests/test-amp-video-sanitizer.php index f08f194db9e..e5cb4308651 100644 --- a/tests/test-amp-video-sanitizer.php +++ b/tests/test-amp-video-sanitizer.php @@ -123,7 +123,7 @@ public function test_get_scripts__didnt_convert() { public function test_get_scripts__did_convert() { $source = ''; - $expected = array( 'amp-video' => 'https://cdn.ampproject.org/v0/amp-video-latest.js' ); + $expected = array( 'amp-video' => true ); $dom = AMP_DOM_Utils::get_dom_from_content( $source ); $sanitizer = new AMP_Video_Sanitizer( $dom ); diff --git a/tests/test-amp-vimeo-embed.php b/tests/test-amp-vimeo-embed.php index e922f4eed02..41a1702e6c0 100644 --- a/tests/test-amp-vimeo-embed.php +++ b/tests/test-amp-vimeo-embed.php @@ -50,7 +50,7 @@ public function get_scripts_data() { ), 'converted' => array( 'https://vimeo.com/172355597' . PHP_EOL, - array( 'amp-vimeo' => 'https://cdn.ampproject.org/v0/amp-vimeo-latest.js' ), + array( 'amp-vimeo' => true ), ), ); } diff --git a/tests/test-amp-vine-embed.php b/tests/test-amp-vine-embed.php index 0106e7106e9..862eb3b5ff8 100644 --- a/tests/test-amp-vine-embed.php +++ b/tests/test-amp-vine-embed.php @@ -33,7 +33,7 @@ public function get_scripts_data() { ), 'converted' => array( 'https://vine.co/v/MdKjXez002d' . PHP_EOL, - array( 'amp-vine' => 'https://cdn.ampproject.org/v0/amp-vine-latest.js' ), + array( 'amp-vine' => true ), ), ); } diff --git a/tests/test-amp-youtube-embed.php b/tests/test-amp-youtube-embed.php index 747995412e5..be7ed6eb09e 100644 --- a/tests/test-amp-youtube-embed.php +++ b/tests/test-amp-youtube-embed.php @@ -55,7 +55,7 @@ public function get_scripts_data() { ), 'converted' => array( 'https://www.youtube.com/watch?v=kfVsfOSbJY0' . PHP_EOL, - array( 'amp-youtube' => 'https://cdn.ampproject.org/v0/amp-youtube-latest.js' ), + array( 'amp-youtube' => true ), ), ); } diff --git a/tests/test-class-amp-theme-support.php b/tests/test-class-amp-theme-support.php index 9f4e38078c6..7a8d1a60122 100644 --- a/tests/test-class-amp-theme-support.php +++ b/tests/test-class-amp-theme-support.php @@ -15,8 +15,12 @@ class Test_AMP_Theme_Support extends WP_UnitTestCase { /** * After a test method runs, reset any state in WordPress the test method might have changed. + * + * @global WP_Scripts $wp_scripts */ public function tearDown() { + global $wp_scripts; + $wp_scripts = null; parent::tearDown(); remove_theme_support( 'amp' ); $_REQUEST = array(); // phpcs:ignore WordPress.CSRF.NonceVerification.NoNonceVerification @@ -91,16 +95,23 @@ public function test_register_widgets() { * Test prepare_response. * * @global WP_Widget_Factory $wp_widget_factory + * @global WP_Scripts $wp_scripts * @covers AMP_Theme_Support::prepare_response() */ public function test_prepare_response() { - global $wp_widget_factory; + global $wp_widget_factory, $wp_scripts; + $wp_scripts = null; + add_theme_support( 'amp' ); AMP_Theme_Support::init(); AMP_Theme_Support::finish_init(); $wp_widget_factory = new WP_Widget_Factory(); wp_widgets_init(); + add_action( 'wp_enqueue_scripts', function() { + wp_enqueue_script( 'amp-mathml' ); + } ); + ob_start(); ?> @@ -138,7 +149,8 @@ public function test_prepare_response() { $this->assertContains( '', $sanitized_html ); $this->assertContains( '