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

#841 Improve support for default embeds #889

Merged
merged 18 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
2fda05e
Issuu, WP post, and WP plugin embed output updates.
kaitnyl Jan 23, 2018
754fe97
Reddit and Tumblr embeds added.
kaitnyl Jan 23, 2018
657b227
New files to use correct comment formatting.
kaitnyl Jan 23, 2018
807a84e
Update file names to follow class names.
kaitnyl Jan 23, 2018
941a858
Remove variable overrides and use inline to avoid updating all parent…
kaitnyl Jan 23, 2018
5c33545
Merge branch 'develop' into fix/841-default-embeds
kaitnyl Jan 26, 2018
eefdb70
Add @since to each new class doc, remove script include from Reddit e…
kaitnyl Jan 26, 2018
6c9079f
MeetUp embed class to help output the CSS returned from ombed.
kaitnyl Jan 27, 2018
1d7cacd
Use responsive width/height for Reddit embeds.
kaitnyl Jan 27, 2018
35a62f4
Remove echo of Meetup style before returning content
westonruter Jan 28, 2018
e2cc3e1
Filter embed_oembed_html to capture CSS; move to amp-custom styles
westonruter Jan 28, 2018
e1b74cd
Mock soundcloud oEmbed response to speed up phpunit tests
westonruter Jan 28, 2018
634392b
Merge branch 'develop' of https://github.com/Automattic/amp-wp into f…
westonruter Jan 28, 2018
509b74e
Add support for sanitizing style elements
westonruter Jan 28, 2018
c6d6258
Remove illegal wp_post_preview_js() from wp_head when AMP
westonruter Jan 28, 2018
c2efa47
Obtain issuu width/height via embed_oembed_html filter
westonruter Jan 28, 2018
708e577
Restore visibility of blockquote.wp-embedded-content since iframe can…
westonruter Jan 29, 2018
be133bf
Fix displaying Tumblr embeds on HTTPS
westonruter Jan 29, 2018
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
4 changes: 4 additions & 0 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ function amp_get_content_embed_handlers( $post = null ) {
'AMP_Vimeo_Embed_Handler' => array(),
'AMP_SoundCloud_Embed_Handler' => array(),
'AMP_Instagram_Embed_Handler' => array(),
'AMP_Issuu_Embed_Handler' => array(),
'AMP_MeetUp_Embed_Handler' => array(),
'AMP_Vine_Embed_Handler' => array(),
'AMP_Facebook_Embed_Handler' => array(),
'AMP_Pinterest_Embed_Handler' => array(),
'AMP_Reddit_Embed_Handler' => array(),
'AMP_Tumblr_Embed_Handler' => array(),
'AMP_Gallery_Embed_Handler' => array(),
'WPCOM_AMP_Polldaddy_Embed' => array(),
),
Expand Down
4 changes: 4 additions & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ class AMP_Autoloader {
'AMP_Facebook_Embed_Handler' => 'includes/embeds/class-amp-facebook-embed',
'AMP_Gallery_Embed_Handler' => 'includes/embeds/class-amp-gallery-embed',
'AMP_Instagram_Embed_Handler' => 'includes/embeds/class-amp-instagram-embed',
'AMP_Issuu_Embed_Handler' => 'includes/embeds/class-amp-issuu-embed-handler',
'AMP_MeetUp_Embed_Handler' => 'includes/embeds/class-amp-meetup-embed-handler',
'AMP_Pinterest_Embed_Handler' => 'includes/embeds/class-amp-pinterest-embed',
'AMP_Reddit_Embed_Handler' => 'includes/embeds/class-amp-reddit-embed-handler',
'AMP_SoundCloud_Embed_Handler' => 'includes/embeds/class-amp-soundcloud-embed',
'AMP_Tumblr_Embed_Handler' => 'includes/embeds/class-amp-tumblr-embed-handler',
'AMP_Twitter_Embed_Handler' => 'includes/embeds/class-amp-twitter-embed',
'AMP_Vimeo_Embed_Handler' => 'includes/embeds/class-amp-vimeo-embed',
'AMP_Vine_Embed_Handler' => 'includes/embeds/class-amp-vine-embed',
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-base-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function __construct( $args = array() ) {
/**
* Get mapping of AMP component names to AMP script URLs.
*
* This is normally no longer needed because the wnitelist
* This is normally no longer needed because the whitelist
* sanitizer will automatically detect the need for them via
* the spec.
*
Expand Down
72 changes: 72 additions & 0 deletions includes/embeds/class-amp-issuu-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Class AMP_Issuu_Embed_Handler
*
* @package AMP
* @since 0.7
*/

/**
* Class AMP_Issuu_Embed_Handler
*/
class AMP_Issuu_Embed_Handler extends AMP_Base_Embed_Handler {
/**
* Regex matched to produce output amp-iframe.
*
* @const string
*/
const URL_PATTERN = '#https?://(www\.)?issuu\.com/.+/docs/.+#i';

/**
* Register embed.
*/
public function register_embed() {
wp_embed_register_handler( 'issuu', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
wp_embed_unregister_handler( 'issuu', -1 );
}

/**
* Embed found with matching URL callback.
*
* @param array $matches URL regex matches.
* @param array $attr Additional parameters.
* @param array $url URL.
* @return string Embed.
*/
public function oembed( $matches, $attr, $url ) {
return $this->render( array( 'url' => $url ) );
}

/**
* Output the Issuu iframe.
*
* @param array $args parameters used for output.
* @return string Rendered content.
*/
public function render( $args ) {
$args = wp_parse_args( $args, array(
'url' => false,
) );

if ( empty( $args['url'] ) ) {
return '';
}

return AMP_HTML_Utils::build_tag(
'amp-iframe',
array(
'width' => $this->args['width'],
'height' => $this->args['height'],
'src' => $args['url'],
'sandbox' => 'allow-scripts allow-same-origin',
)
);
}
}

71 changes: 71 additions & 0 deletions includes/embeds/class-amp-meetup-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Class AMP_MeetUp_Embed_Handler
*
* @package AMP
* @since 0.7
*/

/**
* Class AMP_MeetUp_Embed_Handler
*/
class AMP_MeetUp_Embed_Handler extends AMP_Base_Embed_Handler {

/**
* Regex matched.
*
* @const string
*/
const URL_PATTERN = '#https?://(www\.)?meetu(\.ps|p\.com)/.*#i';

/**
* Register embed.
*/
public function register_embed() {
wp_embed_register_handler( 'meetup', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
wp_embed_unregister_handler( 'meetup', -1 );
}

/**
* Embed found with matching URL callback.
*
* @param array $matches URL regex matches.
* @param array $attr Additional parameters.
* @param array $url URL.
* @return string Embed.
*/
public function oembed( $matches, $attr, $url ) {
return $this->render( array( 'url' => $url ) );
}

/**
* Extract the MeetUp CSS and output in header (otherwise stripped).
* Output the MeetUp oEmbed as usual.
*
* @param array $args parameters used for output.
* @return string Rendered content.
*/
public function render( $args ) {
$args = wp_parse_args( $args, array(
'url' => false,
) );

if ( empty( $args['url'] ) ) {
return '';
}

$content = wp_oembed_get( $args['url'] );
Copy link
Member

Choose a reason for hiding this comment

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

I'm seeing that wp_oembed_get() here is resulting in an HTTP request with every call. I was expecting it to be cached. Apparently to cache we need to use $wp_embed->shortcode( array(), $args['url'] ). This seems to be a problem in core as well with the Video widget: https://github.com/WordPress/wordpress-develop/blob/afb4f45e62520397b147e560e051d3940797121e/src/wp-includes/widgets/class-wp-widget-media-video.php#L144

I think core is erroneously doing an HTTP request for every load of a page with a video widget that is a non-YouTube and non-Vimeo oEmbed.


// Strip AMP-illegal style from response.
$content = preg_replace( '#<style.+</style>#', '', $content );

return $content;
}
}

73 changes: 73 additions & 0 deletions includes/embeds/class-amp-reddit-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Class AMP_Reddit_Embed_Handler
*
* @package AMP
* @since 0.7
*/

/**
* Class AMP_Reddit_Embed_Handler
Copy link
Member

Choose a reason for hiding this comment

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

Please add @since tags to the phpdoc.

*/
class AMP_Reddit_Embed_Handler extends AMP_Base_Embed_Handler {
/**
* Regex matched to produce output amp-reddit.
*
* @const string
*/
const URL_PATTERN = '#https?://(www\.)?reddit\.com/r/[^/]+/comments/.*#i';

/**
* Register embed.
*/
public function register_embed() {
wp_embed_register_handler( 'amp-reddit', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
wp_embed_unregister_handler( 'amp-reddit', -1 );
}

/**
* Embed found with matching URL callback.
*
* @param array $matches URL regex matches.
* @param array $attr Additional parameters.
* @param array $url URL.
* @return string Embed.
*/
public function oembed( $matches, $attr, $url ) {
return $this->render( array( 'url' => $url ) );
}

/**
* Output the Reddit amp element.
*
* @param array $args parameters used for output.
* @return string Rendered content.
*/
public function render( $args ) {
$args = wp_parse_args( $args, array(
'url' => false,
) );

if ( empty( $args['url'] ) ) {
return '';
}

return AMP_HTML_Utils::build_tag(
'amp-reddit',
array(
'layout' => 'responsive',
'data-embedtype' => 'post',
'width' => '100',
'height' => '100',
'data-src' => $args['url'],
)
);
}
}

74 changes: 74 additions & 0 deletions includes/embeds/class-amp-tumblr-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Class AMP_Tumblr_Embed_Handler
*
* @package AMP
* @since 0.7
*/

/**
* Class AMP_Tumblr_Embed_Handler
Copy link
Member

Choose a reason for hiding this comment

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

Please add @since 0.7 tags to the phpdoc.

*/
class AMP_Tumblr_Embed_Handler extends AMP_Base_Embed_Handler {
/**
* Regex matched to produce output amp-iframe.
*
* @const string
*/
const URL_PATTERN = '#https?://(.+)\.tumblr\.com/post/(.*)#i';

/**
* Register embed.
*/
public function register_embed() {
wp_embed_register_handler( 'tumblr', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
wp_embed_unregister_handler( 'tumblr', -1 );
}

/**
* Embed found with matching URL callback.
*
* @param array $matches URL regex matches.
* @param array $attr Additional parameters.
* @param array $url URL.
* @return string Embed.
*/
public function oembed( $matches, $attr, $url ) {
return $this->render( array( 'url' => $url ) );
}

/**
* Output the Tumblr iframe.
*
* @param array $args parameters used for output.
* @return string Rendered content.
*/
public function render( $args ) {
$args = wp_parse_args( $args, array(
'url' => false,
) );

if ( empty( $args['url'] ) ) {
return '';
}

// Must enforce https for amp-iframe, but editors can supply either on desktop.
$args['url'] = str_replace( 'http://', 'https://', $args['url'] );
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately it seems this is not working fully. When attempting to embed https://ifpaintingscouldtext.tumblr.com/post/92003045635/grant-wood-american-gothic-1930 then Tumblr will redirect from HTTPS to HTTP for some reason. When serving a page over HTTPS, as they should be, then the iframe is blocked from rendering.


return AMP_HTML_Utils::build_tag(
'amp-iframe',
array(
'width' => $this->args['width'],
'height' => $this->args['height'],
'src' => $args['url'],
)
);
}
}

4 changes: 4 additions & 0 deletions templates/style.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
padding: 16px;
}

blockquote.wp-embedded-content {
display: none;
}

blockquote p:last-child {
margin-bottom: 0;
}
Expand Down