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

AMP: Render amp-gist from gist shortcode/oEmbed during AMP requests #10053

Merged
merged 2 commits into from
Sep 26, 2018
Merged

AMP: Render amp-gist from gist shortcode/oEmbed during AMP requests #10053

merged 2 commits into from
Sep 26, 2018

Conversation

westonruter
Copy link
Contributor

@westonruter westonruter commented Aug 27, 2018

Changes proposed in this Pull Request:

  • Return amp-gist from a gist shortcode or oEmbed when in an AMP response.

Testing instructions:

  1. Install the AMP plugin.
  2. Add a gist shortcode or gist oEmbed URL to a post. Then view that post in AMP.
  3. View the AMP version of the post.

Example content:

oEmbed: Full gist:

https://gist.github.com/sebastianbenz/1d449dee039202d8b7464f1131eae449

oEmbed: Linking to a file via in a Gist:

https://gist.github.com/sebastianbenz/1d449dee039202d8b7464f1131eae449#file-sw-html

oEmbed: Linking to file without username in URL.

https://gist.github.com/1d449dee039202d8b7464f1131eae449#file-sw-html

Example from WordPress.com docs:

[gist https://gist.github.com/2314628 /]

Second example from WordPress.com docs:

[gist]2314628[/gist]

Originally proposed in ampproject/amp-wp#375

See also ampproject/amphtml#17738 for details on the amp-gist's height attribute.

Proposed changelog entry for your changes:

  • Add Gist support for AMP responses.

@jetpackbot
Copy link

jetpackbot commented Aug 27, 2018

That's a great PR description, thank you so much for your effort!

Generated by 🚫 dangerJS

@westonruter westonruter changed the title Render amp-gist from gist shortcode/oEmbed during AMP requests AMP: Render amp-gist from gist shortcode/oEmbed during AMP requests Aug 27, 2018
@abidhahmed abidhahmed added [Feature] Shortcodes / Embeds [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. AMP labels Aug 28, 2018
@jeherve jeherve added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Sep 5, 2018
@jeherve
Copy link
Member

jeherve commented Sep 7, 2018

Do you think you could move most of this to the existing AMP compatibility file, to keep things in one place?
https://github.com/Automattic/jetpack/blob/master/3rd-party/class.jetpack-amp-support.php

It may also be useful to have unit tests for this, as introducing new regex in shortcodes has already bitten us in the past :)

@westonruter
Copy link
Contributor Author

I think it would be harder to maintain if the AMP code is disconnected from the modules' non-AMP code.

You're right about this needing unit tests. I'll try to add some.

@westonruter
Copy link
Contributor Author

westonruter commented Sep 8, 2018

@jeherve It seems there aren't any tests for the Gist shortcode/oEmbed yet. Is that right? I'd rather not start from scratch.

@westonruter
Copy link
Contributor Author

Temporary Jetpack extension which can be used until this is part of a release: https://gist.github.com/westonruter/8c91e3a218eeb1403ce2eacb832c0783

@jeherve
Copy link
Member

jeherve commented Sep 11, 2018

It seems there aren't any tests for the Gist shortcode/oEmbed yet. Is that right? I'd rather not start from scratch.

You're right, I thought we had some. I will add a first set of tests in #10127.

@jeherve jeherve added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Sep 11, 2018
@jeherve
Copy link
Member

jeherve commented Sep 11, 2018

I think it would be harder to maintain if the AMP code is disconnected from the modules' non-AMP code.

This is true. Let's keep that code here. That said, I think it would be useful to create a new method such as Jetpack::is_amp_request() instead of using Jetpack_AMP_Support::is_amp_request() everywhere. This makes the code easier to maintain compatible with the WordPress.com codebase for us, where Jetpack_AMP_Support does not exist. This will allow us to easily bypass sections on WordPress.com for now.

Does that make sense? Do you think you could add that in until the unit tests get merged?

Thank you!

@westonruter
Copy link
Contributor Author

Does that make sense?

Yes, that makes sense to me! It doesn't matter to me where the is_amp_request() method lives.

However, there are some changes need to be made to the code in Jetpack_AMP_Support. In particular, the method \Jetpack_AMP_Support::is_amp_canonical() doesn't make sense as of 1.0-beta, nor does checking for isset( $_GET[ amp_get_slug() ] ). The is_amp_request() method should really just be simply:

return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();

The reason why Jetpack_AMP_Support is as it is today is due to how the AMP plugin was in 1.0-alpha as outlined in this reverted issue ampproject/amp-wp#1148. This necessarily had to be undone in ampproject/amp-wp#1235 because the template mode (native vs paired) and the presence of the ?amp query var is not sufficient to determine whether it is an AMP response. Since AMP now can be disabled for certain template types or for individual posts/pages—even in native mode when there is no AMP query var—a plugin must wait until the parse_query action is triggered so that the query vars are populated and the queried object is available.

This will mean that Jetpack will need updated in places such as this:

// disable Likes metabox for post editor if AMP canonical disabled
add_filter( 'post_flair_disable', array( 'Jetpack_AMP_Support', 'is_amp_canonical' ), 99 );

So all of this to say: when Jetpack::is_amp_endpoint() is added, it should abandon how the checks are being done in Jetpack_AMP_Support. I think this should be done in a separate PR as it will require more changes than are in scope for adding AMP support for Gist embeds.

In the mean time, I can add unit tests for amp-gist once you've merged the PR with the unit tests.

@jeherve
Copy link
Member

jeherve commented Sep 12, 2018

Thanks for all the details; that makes perfect sense! I'll let you know once the Unit Tests are in.

@jeherve jeherve added [Status] In Progress and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Sep 12, 2018
@kraftbj
Copy link
Contributor

kraftbj commented Sep 18, 2018

Unit tests have been merged.

@westonruter
Copy link
Contributor Author

@jeherve tests have been added.

@jeherve jeherve added the [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. label Sep 25, 2018
@kraftbj
Copy link
Contributor

kraftbj commented Sep 25, 2018

I'm having trouble testing this. Using my own gist or directly using the snippet above with this branch, tested on the stable AMP plugin (q: do I need a beta version of AMP 1.0?)

No console errors. Validates as valid AMP. It produces <div class="gist-oembed" data-gist="kraftbj/f39f6e93a4e7e63934bb097bf7ccc525.json"></div>. At the end of my day so can attempt to dig in to determine what might be amiss tonight or tomorrow.

@westonruter
Copy link
Contributor Author

@kraftbj I just checked on the 0.7.2 release and 1.0-beta4 alike and both are working for me. Is your test environment on HTTPS? That is required.

@kraftbj
Copy link
Contributor

kraftbj commented Sep 26, 2018

False alarm @westonruter — something was weird with pulling down our beta server's built version of the branch.

On desktop Chrome, everything looks as expected. For kicks, in Firefox Dev on desktop, as more items are embedded, it begins to cut off the ends of the embed.

Notice how the gists start complete, then slowly the height is reduced until it fully loses the footer, plus some:

screen shot 2018-09-25 at 22 23 44-fullpage

I'm open to accepting this with that defect present given the normal usage of AMP and it is somewhat a corner case, but want to get your thoughts on where the issue may be with it.

@kraftbj kraftbj added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Sep 26, 2018
@westonruter
Copy link
Contributor Author

@kraftbj Interesting. I think you've identified a bug with the amp-gist component itself, and this isn't something that could be fixed with the integration here.

@jeherve jeherve added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Sep 26, 2018
@kraftbj kraftbj added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Sep 26, 2018
@kraftbj kraftbj added this to the 6.7 milestone Sep 26, 2018
@kraftbj
Copy link
Contributor

kraftbj commented Sep 26, 2018

Thanks @westonruter! That makes sense.

Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

This works for me. Thank you! Merging.

@jeherve jeherve merged commit 45551d8 into Automattic:master Sep 26, 2018
@ghost ghost removed the [Status] Ready to Merge Go ahead, you can push that green button! label Sep 26, 2018
@jeherve jeherve added the [Status] Needs Testing We need to add this change to the testing call for this month's release label Sep 26, 2018
@westonruter westonruter deleted the add/amp-gist branch September 26, 2018 16:24
jeherve added a commit that referenced this pull request Oct 26, 2018
jeherve added a commit that referenced this pull request Oct 26, 2018
@jeherve jeherve removed the [Status] Needs Testing We need to add this change to the testing call for this month's release label Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AMP [Feature] Shortcodes / Embeds Touches WP.com Files [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants