Skip to content
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

Move enqueuing of assets to 'wp_enqueue_scripts' #1023

Merged
merged 3 commits into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public static function init() {
* action to template_redirect--the wp action--is used instead.
*/
add_action( 'wp', array( __CLASS__, 'finish_init' ), PHP_INT_MAX );

add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting this here caused a regression where the amp-runtime is enqueued even on non-AMP pages. PR to fix in #1033

}

/**
Expand All @@ -119,12 +121,6 @@ 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' ), array(), AMP__VERSION );

self::add_hooks();
self::$sanitizer_classes = amp_get_content_sanitizers();
self::$embed_handlers = self::register_content_embed_handlers();
Expand Down Expand Up @@ -1016,4 +1012,20 @@ public static function prepare_response( $response, $args = array() ) {

return $response;
}

/**
* Enqueue AMP assets if this is an AMP endpoint.
*
* @since 0.7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be 1.0 instead. I'm happy to change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.7 is correct. I changed the branch to merge this PR.

*
* @return void
*/
public static function enqueue_assets() {
if ( is_amp_endpoint() ) {
// 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' ), array(), AMP__VERSION );
}
}
}
21 changes: 21 additions & 0 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public function test_ensure_required_markup( $script, $expected ) {
AMP_Theme_Support::ensure_required_markup( $dom );
$this->assertEquals( $expected, substr_count( $dom->saveHTML(), 'schema.org' ) );
}

/**
* Data provider for test_ensure_required_markup.
*
Expand All @@ -371,4 +372,24 @@ public function get_script_data() {
),
);
}

/**
* Test enqueue_assets().
*
* @covers AMP_Theme_Support::enqueue_assets()
*/
public function test_enqueue_assets() {
$script_slug = 'amp-runtime';
$style_slug = 'amp-default';
wp_dequeue_script( $script_slug );
wp_dequeue_style( $style_slug );
AMP_Theme_Support::enqueue_assets();
$this->assertFalse( in_array( $script_slug, wp_scripts()->queue, true ) );
$this->assertFalse( in_array( $style_slug, wp_styles()->queue, true ) );

add_theme_support( 'amp' );
AMP_Theme_Support::enqueue_assets();
$this->assertTrue( in_array( $script_slug, wp_scripts()->queue, true ) );
$this->assertTrue( in_array( $style_slug, wp_styles()->queue, true ) );
}
}