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

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Mar 15, 2018

Request For Code Review

Hi @westonruter,
@kopepasah found an issue where the amp-runtime script appeared in the wp-admin/edit.php page.

As you mentioned, this should be enqueued on the action wp_enqeue_scripts.

So this does that, and wraps the enqueuing in if ( is_amp_endpoint() ) This also appeared where they were enqueued before.

@kopepasah found an issue, where the 'amp-runtime'
script appeared in the admin menu.
As Weston mentioned, this should be enqueued in 'wp_enqeue_scripts.'
Also, this wraps the enqueuing in if ( is_amp_endpoint() ).
This also appeared where they were enqueued before.
/**
* 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.

@kienstra kienstra requested a review from westonruter March 15, 2018 22:11
As Weston mentioned, finish_init already checks for that.
@todo: make is_amp_endpoint() return false if:
is_admin() or is_feed(), and if it's a REST response.
@westonruter westonruter changed the base branch from develop to 0.7 March 15, 2018 22:21
@kienstra
Copy link
Contributor Author

kienstra commented Mar 15, 2018

Working On is_amp_endpoint()

Hi @westonruter,
I'm working on is_amp_endpoint():

function is_amp_endpoint() {
+	if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
+		return false;
+	}

	if ( amp_is_canonical() ) {
		return true;
	}

I need to address some failed unit tests from that change, and update the unit tests for that function. If it's alright, I'll come back to this in about an hour.

@westonruter westonruter added this to the v0.7 milestone Mar 15, 2018
is_admin(), is_feed(), and REST_REQUEST.
As Weston mentioned, this shouldn't be true for admin pages.
Also, modify PHPUnit tests for this.
@kienstra
Copy link
Contributor Author

With the commit above, this PR is ready review.

@@ -67,6 +67,7 @@ public function test_enqueue_admin_assets() {
// Test inline script boot.
$this->assertTrue( false !== stripos( wp_json_encode( $script_data ), 'ampPostMetaBox.boot(' ) );
unset( $GLOBALS['post'] );
unset( $GLOBALS['current_screen'] );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed to counteract set_current_screen( 'post.php' ) above. This caused is_admin() to be true on later tests.

Copy link
Member

Choose a reason for hiding this comment

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

FYI: Probably better to such things in tearDown to ensure they are run regardless of whether the tests fail in this function. If a test fails, then the vars wouldn't get cleared.

@@ -171,6 +171,10 @@ function post_supports_amp( $post ) {
* @return bool Whether it is the AMP endpoint.
*/
function is_amp_endpoint() {
if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

We may need to revisit this with #1014 since we may want to render AMP content for a single property in the REST API response. We don't need to worry about it yet.

@westonruter westonruter merged commit 0c0a2b4 into 0.7 Mar 17, 2018
@westonruter westonruter deleted the fix/assets-enqueued-on-admin branch March 17, 2018 00:38
@westonruter westonruter restored the fix/assets-enqueued-on-admin branch March 17, 2018 00:38
@@ -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

@westonruter westonruter deleted the fix/assets-enqueued-on-admin branch July 5, 2018 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants