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

Prototype support for amp-wordpress-embed component #3465

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ function amp_get_content_embed_handlers( $post = null ) {
'AMP_Gfycat_Embed_Handler' => [],
'AMP_Hulu_Embed_Handler' => [],
'AMP_Imgur_Embed_Handler' => [],
'AMP_WordPress_Embed_Handler' => [],
],
$post
);
Expand Down
1 change: 1 addition & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class AMP_Autoloader {
'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',
'AMP_WordPress_Embed_Handler' => 'includes/embeds/class-amp-wordpress-embed-handler',
'AMP_YouTube_Embed_Handler' => 'includes/embeds/class-amp-youtube-embed',
'AMP_Analytics_Options_Submenu' => 'includes/options/class-amp-analytics-options-submenu',
'AMP_Options_Menu' => 'includes/options/class-amp-options-menu',
Expand Down
94 changes: 94 additions & 0 deletions includes/embeds/class-amp-wordpress-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Class AMP_WordPress_Embed_Handler
*
* @package AMP
*/

/**
* Class AMP_WordPress_Embed_Handler
*
* @since 1.3.1
*/
class AMP_WordPress_Embed_Handler extends AMP_Base_Embed_Handler {

/**
* Default height.
*
* Note that 200px is the minimum that WordPress allows for a post embed. This minimum height is enforced by
* WordPress in the wp.receiveEmbedMessage() function, and the <amp-wordpress-embed> also enforces that same
* minimum height. It is important for the minimum height to be initially used because if the actual post embed
* window is _less_ than the initial, then no overflow button will be presented to resize the iframe to be
* _smaller_. So this ensures that the iframe will only ever overflow to grow in height.
*
* @var int
*/
protected $DEFAULT_HEIGHT = 200;

/**
* Register embed.
*/
public function register_embed() {
add_filter( 'embed_oembed_html', [ $this, 'filter_embed_oembed_html' ], 100, 2 );
}

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

/**
* Filter oEmbed HTML for WordPress to convert to AMP.
*
* @param string $cache Cache for oEmbed.
* @param string $url Embed URL.
* @return string Embed.
*/
public function filter_embed_oembed_html( $cache, $url ) {
/*
* Example WordPress embed HTML that would be present in $cache.
*
* <blockquote class="wp-embedded-content" data-secret="xUuZHketRt">
* <a href="https://make.wordpress.org/core/2015/10/28/new-embeds-feature-in-wordpress-4-4/">New Embeds Feature in WordPress 4.4</a>
* </blockquote>
* <iframe
* title="&#8220;New Embeds Feature in WordPress 4.4&#8221; &#8212; Make WordPress Core"
* class="wp-embedded-content"
* sandbox="allow-scripts"
* security="restricted"
* style="position: absolute; clip: rect(1px, 1px, 1px, 1px);"
* src="https://make.wordpress.org/core/2015/10/28/new-embeds-feature-in-wordpress-4-4/embed/#?secret=xUuZHketRt"
* data-secret="xUuZHketRt"
* width="600"
* height="338"
* frameborder="0"
* marginwidth="0"
* marginheight="0"
* scrolling="no">
* </iframe>
*/
if ( ! preg_match( '#<blockquote class="wp-embedded-content" data-secret="\w+">(.+?)</blockquote>#s', $cache, $matches ) ) {
return $cache;
}
$placeholder = sprintf( '<blockquote class="wp-embedded-content" placeholder>%s</blockquote>', $matches[1] );

$attributes = [
'height' => $this->args['height'],
'title' => '',
];
if ( preg_match( '#<iframe[^>]*?title="(?P<title>[^"]+?)"#s', $cache, $matches ) ) {
$attributes['title'] = $matches['title'];
}

return sprintf(
'<amp-wordpress-embed layout="fixed-height" height="%d" title="%s" data-url="%s">%s<button overflow>%s</button></amp-wordpress-embed>',
esc_attr( $attributes['height'] ),
esc_attr( $attributes['title'] ),
esc_url( $url ),
$placeholder,
esc_html__( 'Expand', 'amp' )
);
}
}
51 changes: 51 additions & 0 deletions includes/sanitizers/class-amp-allowed-tags-generated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6395,6 +6395,31 @@ class AMP_Allowed_Tags_Generated {
),
),
),
'amp-wordpress-embed' => array(
array(
'attr_spec_list' => array(
'data-url' => array(
'mandatory' => true,
'value_url' => array(
'allow_relative' => false,
'protocol' => array(
'https',
),
),
),
),
'tag_spec' => array(
'amp_layout' => array(
'supported_layouts' => array(
3,
),
),
'requires_extension' => array(
'amp-wordpress-embed',
),
),
),
),
'amp-yotpo' => array(
array(
'attr_spec_list' => array(
Expand Down Expand Up @@ -14928,6 +14953,32 @@ class AMP_Allowed_Tags_Generated {
),
),
),
array(
'attr_spec_list' => array(
'async' => array(
'mandatory' => true,
'value' => array(
'',
),
),
'nonce' => array(),
'type' => array(
'value_casei' => array(
'text/javascript',
),
),
),
'tag_spec' => array(
'extension_spec' => array(
'name' => 'amp-wordpress-embed',
'version' => array(
'0.1',
'latest',
),
),
'spec_url' => 'https://amp.dev/documentation/components/amp-wordpress-embed',
),
),
array(
'attr_spec_list' => array(
'async' => array(
Expand Down