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 17 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
40 changes: 9 additions & 31 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@ class AMP_Theme_Support {
*/
const CUSTOM_STYLES_PLACEHOLDER = '/* AMP:CUSTOM_STYLES_PLACEHOLDER */';

/**
* AMP Scripts.
*
* @var array
*/
protected static $amp_scripts = array();

/**
* AMP Styles.
*
* @var array
*/
protected static $amp_styles = array();

/**
* Sanitizer classes.
*
Expand Down Expand Up @@ -155,6 +141,7 @@ public static function register_paired_hooks() {
public static function register_hooks() {

// Remove core actions which are invalid AMP.
remove_action( 'wp_head', 'wp_post_preview_js', 1 );
remove_action( 'wp_head', 'locale_stylesheet' ); // Replaced below in add_amp_custom_style_placeholder() method.
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_head', 'wp_print_styles', 8 ); // Replaced below in add_amp_custom_style_placeholder() method.
Expand Down Expand Up @@ -433,20 +420,14 @@ public static function add_amp_custom_style_placeholder() {
/**
* Get custom styles.
*
* @param string[] $stylesheets Initial stylesheets.
* @see wp_custom_css_cb()
* @return string Styles.
* @return string Concatenated stylesheets.
*/
public static function get_amp_custom_styles() {
public static function get_amp_custom_styles( $stylesheets ) {
$css = wp_styles()->print_code;

// Add styles gleaned from sanitizers.
foreach ( self::$amp_styles as $selector => $properties ) {
$css .= sprintf(
'%s{%s}',
$selector,
join( ';', $properties ) . ';'
);
}
$css .= join( $stylesheets );

/**
* Filters AMP custom CSS before it is injected onto the output buffer for the response.
Expand All @@ -466,10 +447,10 @@ public static function get_amp_custom_styles() {
/**
* Determine required AMP scripts.
*
* @param array $amp_scripts Initial scripts.
* @return string Scripts to inject into the HEAD.
*/
public static function get_amp_component_scripts() {
$amp_scripts = self::$amp_scripts;
public static function get_amp_component_scripts( $amp_scripts ) {

foreach ( self::$embed_handlers as $embed_handler ) {
$amp_scripts = array_merge(
Expand Down Expand Up @@ -527,9 +508,6 @@ public static function finish_output_buffering( $output ) {

$assets = AMP_Content_Sanitizer::sanitize_document( $dom, self::$sanitizer_classes, $args );

self::$amp_scripts = array_merge( self::$amp_scripts, $assets['scripts'] );
self::$amp_styles = array_merge( self::$amp_styles, $assets['styles'] );

/*
* @todo The sanitize method needs to be updated to sanitize the entire HTML element and not just the BODY.
* This will require updating mandatory_parent_blacklist in amphtml-update.py to include elements that appear in the HEAD.
Expand All @@ -547,15 +525,15 @@ public static function finish_output_buffering( $output ) {
// Inject required scripts.
$output = preg_replace(
'#' . preg_quote( self::COMPONENT_SCRIPTS_PLACEHOLDER, '#' ) . '#',
self::get_amp_component_scripts(),
self::get_amp_component_scripts( $assets['scripts'] ),
$output,
1
);

// Inject styles.
$output = preg_replace(
'#' . preg_quote( self::CUSTOM_STYLES_PLACEHOLDER, '#' ) . '#',
self::get_amp_custom_styles(),
self::get_amp_custom_styles( $assets['stylesheets'] ),
$output,
1
);
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
65 changes: 65 additions & 0 deletions includes/embeds/class-amp-issuu-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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() {
add_filter( 'embed_oembed_html', array( $this, 'filter_embed_oembed_html' ), 10, 3 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
remove_filter( 'embed_oembed_html', array( $this, 'filter_embed_oembed_html' ), 10 );
}

/**
* Filter oEmbed HTML for Meetup to prepare it for AMP.
*
* @param mixed $return The shortcode callback function to call.
* @param string $url The attempted embed URL.
* @param array $attr An array of shortcode attributes.
* @return string Embed.
*/
public function filter_embed_oembed_html( $return, $url, $attr ) {
$parsed_url = wp_parse_url( $url );
if ( false !== strpos( $parsed_url['host'], 'issuu.com' ) ) {
if ( preg_match( '/width\s*:\s*(\d+)px/', $return, $matches ) ) {
$attr['width'] = $matches[1];
}
if ( preg_match( '/height\s*:\s*(\d+)px/', $return, $matches ) ) {
$attr['height'] = $matches[1];
}

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

52 changes: 52 additions & 0 deletions includes/embeds/class-amp-meetup-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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() {
add_filter( 'embed_oembed_html', array( $this, 'filter_embed_oembed_html' ), 10, 2 );
}

/**
* Unregister embed.
*/
public function unregister_embed() {
remove_filter( 'embed_oembed_html', array( $this, 'filter_embed_oembed_html' ), 10 );
}

/**
* Filter oEmbed HTML for Meetup to prepare it for AMP.
*
* @param string $cache Cache for oEmbed.
* @param string $url Embed URL.
* @return string Embed.
*/
public function filter_embed_oembed_html( $cache, $url ) {
$parsed_url = wp_parse_url( $url );
if ( false !== strpos( $parsed_url['host'], 'meetup.com' ) ) {

// Supply the width/height so that we don't have to make requests to look them up later.
$cache = str_replace( '<img ', '<img width="50" height="50" ', $cache );
}
return $cache;
}
}

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'],
)
);
}
}

Loading