-
Notifications
You must be signed in to change notification settings - Fork 384
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
Improve handling of YouTube embeds #3358
Conversation
This PR is free for anyone to take over. |
b517472
to
68d68d8
Compare
All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the ℹ️ Googlers: Go here for more info. |
@googlebot I consent. |
1 similar comment
@googlebot I consent. |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
68d68d8
to
5225f4e
Compare
* | ||
* @param array $matches URL pattern matches. | ||
* @param array $attr Shortcode attribues. | ||
* @param string $url URL. | ||
* @return string Rendered oEmbed. | ||
*/ | ||
public function oembed( $matches, $attr, $url ) { | ||
_deprecated_function( __METHOD__, '1.3.1' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_deprecated_function( __METHOD__, '1.3.1' ); | |
_deprecated_function( __METHOD__, '1.5.0' ); |
] | ||
); | ||
|
||
if ( empty( $args['video_id'] ) ) { | ||
return AMP_HTML_Utils::build_tag( | ||
'a', | ||
[ | ||
'href' => esc_url( $args['url'] ), | ||
'href' => esc_url( $url ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized that this needs to rather be esc_url_raw()
since the HTML entity encoding will be handled by the DOM. If there are other places where esc_url()
is being used in this way, it should be updated to be esc_url_raw()
as well.
@@ -162,7 +223,7 @@ private function get_video_id_from_url( $url ) { | |||
} else { | |||
/* phpcs:ignore Squiz.PHP.CommentedOutCode.Found | |||
The query looks like ?v={id} or ?list={id} */ | |||
parse_str( $parsed_url['query'], $query_args ); | |||
parse_str( $parsed_url['query'], $query_args ); // @todo Bug! See <https://github.com/ampproject/amp-wp/issues/3348>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be done in this PR or another?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That needs to be removed as the embed_oembed_html
filter correctly handles the URL and transforms it into an embeddable one.
); | ||
} | ||
|
||
/** | ||
* Determine the video ID from the URL. | ||
* | ||
* @todo Needs to be totally refactored. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR or another?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will be done in this PR.
/** | ||
* @dataProvider get_conversion_data | ||
*/ | ||
public function test__conversion( $source, $expected, $fallback = null ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename $fallback
to be something else here? It gets confusing with fallback
content in AMP. Perhaps $expected_without_fallback
would be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, OK. $expected_without_fallback
seems a bit confusing as well, how about something direct like $fallback_for_expected
?
if ( preg_match( '#<iframe[^>]*?title="(?P<title>[^"]+)"#s', $html, $matches ) ) { | ||
$props['title'] = $matches['title']; | ||
$props['fallback'] = sprintf( | ||
'<a fallback href="%s">%s</a>', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I keep arguing with myself whether this should be a fallback
or a placeholder
: https://amp.dev/documentation/guides-and-tutorials/develop/style_and_layout/placeholders/
Actually, I think the answer is this should be changed to rather be an amp-img
as a placeholder
as shown on the amp-youtube
docs:
<amp-youtube
id="myLiveChannel"
data-live-channelid="UCB8Kb4pxYzsDsHxzBfnid4Q"
width="358"
height="204"
layout="responsive">
<amp-img
src="https://i.ytimg.com/vi/Wm1fWz-7nLQ/hqdefault_live.jpg"
placeholder
layout="fill"
/>
</amp-youtube>
The example there is incomplete however. In particular, the amp-img
should have an alt
attribute provided which is the title
of the video.
Also, instead of actually generating an amp-img
here, just output a regular img
as it will get converted into an amp-img
later by the image sanitizer.
Also, the amp-img
placeholder would be suitable even when there is no title
available, it's just that the alt
would have to be blank.
Also, keeping the link to the original YouTube video is good.
So what to do:
- Unconditionally construct the link regardless of whether the
iframe
has atitle
. - Change this to be
placeholder
instead offallback
. - Parse construct the HQ video thumbnail from the video ID (e.g.
https://i.ytimg.com/vi/$video_id/hqdefault.jpg
). - Supply an
img
element as the child of thea
link. - Supply the
title
of theiframe
as theimg[alt]
, if available.
So in the end, it should look like this:
<p>
<amp-youtube data-videoid="kfVsfOSbJY0" layout="responsive" width="480" height="270" title="Rebecca Black - Friday">
<a placeholder href="https://www.youtube.com/watch?v=kfVsfOSbJY0">
<img src="https://i.ytimg.com/vi/kfVsfOSbJY0/hqdefault.jpg" alt="Rebecca Black – Friday" data-amp-layout="fill">
</a>
</amp-youtube>
</p>
And in WordPress<5.2, the only difference would be no alt
attribute on the img
and no title
on the amp-youtube
.
Hey @schlessera, you're needed to give consent over here again 😄 |
@pierlon It's already done. |
Ah, thanks! |
@westonruter on WP <5.2, the |
I've improved the display of the placeholder image in fc0e9e0 by using Before: After: |
Aside: I'm curious why the |
* @param string[] $attribute_names Attribute names. | ||
* @return string Regular expression pattern. | ||
*/ | ||
protected function get_html_attribute_pattern( $tag_name, $attribute_names ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you make this reusable, why not include the preg_match()
call? Something like match_element_attributes( $tag_name, $attribute_names )
.
This way, usage could be simplified:
$props = $this->match_element_attributes( 'iframe', [ 'src', 'title', 'width', 'height' ] );
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent point. I didn't refactor far enough. See d45fd59.
Verified in QA |
This pull request aims to clean up handling of YouTube embed handling, in large part to apply similar improvements as #2722 for SoundCloud.
title
asplaceholder
content:<a placeholder href="https://youtube.com/watch?v=tiQcA7cwEMo"><img alt="AMP for JavaScripters" ...></a>
. See Improve handling of YouTube embeds #3358 (comment).title
attribute to theamp-youtube
element for a11y, in addition to thefallback
content. See iFrame Title Attribute #3313 (comment).test-amp-youtube-embed.php
andtest-class-amp-youtube-embed-handler.php
, update tests following model of Improve Soundcloud embed: support playlists, preserve visual/height params, include fallback content #2722.Fixes #3348. Fixes #3313.
Relates to #3309 in that the shortcode logic needs to be removed/moved to Jetpack.