Skip to content

Commit

Permalink
Add incomplete support for post embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jan 29, 2018
1 parent 708e577 commit ab28bcf
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
21 changes: 21 additions & 0 deletions includes/amp-frontend-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ function amp_frontend_add_canonical() {

printf( '<link rel="amphtml" href="%s">', esc_url( amp_get_permalink( get_queried_object_id() ) ) );
}

/**
* Add JS required for iframe to resize itself in AMP.
*
* @todo This should be incorporated into core.
* @since ?.?
*/
function amp_add_post_embed_js() {
?>
<script>
if ( /amp=1/.test( location.hash ) ) {
window.parent.postMessage({
sentinel: 'amp',
type: 'embed-size',
height: document.body.scrollHeight
}, '*');
}
</script>
<?php
}
add_action( 'embed_footer', 'amp_add_post_embed_js' );
1 change: 1 addition & 0 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ function amp_get_content_embed_handlers( $post = null ) {
'AMP_Instagram_Embed_Handler' => array(),
'AMP_Issuu_Embed_Handler' => array(),
'AMP_Meetup_Embed_Handler' => array(),
'AMP_Post_Embed_Handler' => array(),
'AMP_Vine_Embed_Handler' => array(),
'AMP_Facebook_Embed_Handler' => array(),
'AMP_Pinterest_Embed_Handler' => array(),
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 @@ -42,6 +42,7 @@ class AMP_Autoloader {
'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_Post_Embed_Handler' => 'includes/embeds/class-amp-post-embed-handler',
'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',
Expand Down
81 changes: 81 additions & 0 deletions includes/embeds/class-amp-post-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Class AMP_Post_Embed_Handler
*
* @package AMP
* @since ?.?
*/

/**
* Class AMP_Post_Embed_Handler
*
* @todo Patch core to send embed-size message to parent when URL contains `#amp=1`. See <https://www.ampproject.org/docs/reference/components/amp-iframe#iframe-resizing>.
*/
class AMP_Post_Embed_Handler extends AMP_Base_Embed_Handler {

/**
* Register embed.
*/
public function register_embed() {

// Note that oembed_dataparse filter should not be used as the response will get cached in the DB.
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 WordPress post embeds to prepare it for AMP.
*
* @see \get_post_embed_html()
* @param string $cache Cache for oEmbed.
* @param string $url Embed URL.
* @param array $attr Embed attributes, including width and height.
* @return string Embed.
*/
public function filter_embed_oembed_html( $cache, $url, $attr ) {
unset( $url );

// See regex in wp_filter_oembed_result() where blockquote and iframe are presumed to exist.
if ( ! preg_match( '#<iframe\s[^>]*class="wp-embedded-content"[^>]*?></iframe>#s', $cache ) ) {
return $cache;
}

$dom = AMP_DOM_Utils::get_dom_from_content( $cache );
$iframe = $dom->getElementsByTagName( 'iframe' )->item( 0 );
if ( ! $iframe ) {
return $cache;
}

// Note we have to exclude the blockquote because wpautop() does not like it.
$link = $dom->getElementsByTagName( 'a' )->item( 0 );
$link->setAttribute( 'placeholder', '' );

$attributes = $attr; // Initially width and height.
foreach ( $iframe->attributes as $attribute ) {
$attributes[ $attribute->nodeName ] = $attribute->nodeValue;
}
unset( $attributes['data-secret'] );

return AMP_HTML_Utils::build_tag(
'amp-iframe',
array_merge(
$attributes,
array(
'src' => strtok( $attributes['src'], '#' ), // So that `#amp=1` can be added.
'layout' => 'responsive',
'resizable' => '',
'sandbox' => 'allow-scripts allow-top-navigation-by-user-activation', // @todo Top-navigation doesn't work because linkClickHandler() prevents it. See <https://github.com/WordPress/wordpress-develop/blob/4.9.2/src/wp-includes/js/wp-embed-template.js#L149-L170>.

)
),
$dom->saveHTML( $link ) . ' <span overflow role="button" tabindex="0">' . esc_html__( 'Read more', 'amp' ) . '</span>'
);
}
}

0 comments on commit ab28bcf

Please sign in to comment.