Skip to content
Draft
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 @@ -1413,6 +1413,7 @@ function amp_get_content_embed_handlers( $post = null ) {
AMP_Scribd_Embed_Handler::class => [],
AMP_WordPress_Embed_Handler::class => [],
AMP_WordPress_TV_Embed_Handler::class => [],
AMP_Iframe_Embed_Handler::class => [],
],
$post
);
Expand Down
20 changes: 19 additions & 1 deletion includes/embeds/class-amp-base-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected function unwrap_p_element( DOMElement $node ) {
if (
$parent_node instanceof DOMElement
&&
'p' === $parent_node->tagName
Tag::P === $parent_node->tagName
&&
false === $parent_node->hasAttributes()
&&
Expand All @@ -160,6 +160,24 @@ protected function unwrap_p_element( DOMElement $node ) {
}
}

/**
* Unwrap `<p>` element based on passed child tag name.
*
* @since 2.4.2
*
* @param Document $dom Document.
* @param string $child_tag_name Child tag name.
*/
protected function unwrap_p_element_by_child_tag_name( $dom, $child_tag_name ) {
$nodes = $dom->xpath->query( "//p/{$child_tag_name}" );

if ( $nodes->length && $nodes instanceof DOMNodeList ) {
Copy link
Member

Choose a reason for hiding this comment

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

Seems like the order is backwards, and I don't think $nodes->length needs to be checked since the foreach will do that:

Suggested change
if ( $nodes->length && $nodes instanceof DOMNodeList ) {
if ( $nodes instanceof DOMNodeList ) {

foreach ( $nodes as $node ) {
$this->unwrap_p_element( $node );
}
}
}

/**
* Removes the node's nearest `<script>` sibling with a `src` attribute containing the base `src` URL provided.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/embeds/class-amp-crowdsignal-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function unregister_embed() {
*/
public function filter_embed_oembed_html( $cache, $url, $attr ) {
$parsed_url = wp_parse_url( $url );
if ( empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) || ! preg_match( '#(^|\.)(?P<host>polldaddy\.com|crowdsignal\.com|survey\.fm|poll\.fm)#', $parsed_url['host'], $matches ) ) {
if ( empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) || ! preg_match( '#(^|\.)(?P<host>polldaddy\.com|poll\.fm|crowdsignal\.com|survey\.fm|poll\.fm)#', $parsed_url['host'], $matches ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Seems like poll.fm is now duplicated in the regex.

return $cache;
}

Expand Down
15 changes: 15 additions & 0 deletions includes/embeds/class-amp-dailymotion-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @package AMP
*/

use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
* Class AMP_DailyMotion_Embed_Handler
*
Expand Down Expand Up @@ -117,6 +120,18 @@ public function render( $args ) {
);
}

/**
* Sanitize raw embeds.
*
* @param Document $dom Document.
*
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::DAILYMOTION );
}

/**
* Determine the video ID from the URL.
*
Expand Down
8 changes: 2 additions & 6 deletions includes/embeds/class-amp-facebook-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @package AMP
*/

use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
Expand Down Expand Up @@ -114,12 +115,7 @@ public function render( $args ) {
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$embed_nodes = $dom->xpath->query( "//p/{$this->amp_tag}" );
if ( $embed_nodes->length ) {
foreach ( $embed_nodes as $embed_node ) {
$this->unwrap_p_element( $embed_node );
}
}
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::FACEBOOK );
Copy link
Member

Choose a reason for hiding this comment

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

Might as well reuse this constant in where $this->amp_tag is set in the class above, i.e.:

private $amp_tag = Extension::FACEBOOK;


$nodes = $dom->getElementsByTagName( $this->sanitize_tag );
$num_nodes = $nodes->length;
Expand Down
15 changes: 15 additions & 0 deletions includes/embeds/class-amp-gfycat-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* @since 1.0
*/

use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
* Class AMP_Gfycat_Embed_Handler
*
Expand Down Expand Up @@ -79,5 +82,17 @@ public function filter_embed_oembed_html( $return, $url, $attr ) {
}
return $return;
}

/**
* Sanitize raw embeds.
*
* @param Document $dom Document.
*
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::GFYCAT );
}
}

47 changes: 47 additions & 0 deletions includes/embeds/class-amp-iframe-embed-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Class for handling `amp-iframe` embeds.
*
* @package AMP
* @since 2.4.1
*/

use AmpProject\Html\Tag;
use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
* Class AMP_Iframe_Embed_Handler
*
* @since 2.4.1
*/
class AMP_Iframe_Embed_Handler extends AMP_Base_Embed_Handler {

/**
* Register embed.
*/
public function register_embed() {
// Not implemented.
}

/**
* Unregister embed.
*/
public function unregister_embed() {
// Not implemented.
}

/**
* Sanitize `amp-iframe` raw embeds.
*
* @param Document $dom Document.
*
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
foreach ( [ Extension::IFRAME, Tag::IFRAME ] as $tag_name ) {
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 wary of doing this for all iframes. I think it should be targeted specifically to embeds we know about. There could legitimately be a non-embed iframe which is inside of a p, and this could break styling.

Do we need AMP_Iframe_Embed_Handler?

$this->unwrap_p_element_by_child_tag_name( $dom, $tag_name );
}
}
}
15 changes: 15 additions & 0 deletions includes/embeds/class-amp-imgur-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* @since 1.0
*/

use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
* Class AMP_Imgur_Embed_Handler
*
Expand Down Expand Up @@ -73,6 +76,18 @@ public function filter_embed_oembed_html( $return, $url, $attr ) {
return $return;
}

/**
* Sanitize raw embeds.
*
* @param Document $dom Document.
*
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::IMGUR );
}

/**
* Get Imgur ID from URL.
*
Expand Down
4 changes: 4 additions & 0 deletions includes/embeds/class-amp-instagram-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AmpProject\Dom\Document;
use AmpProject\Html\Attribute;
use AmpProject\Html\Tag;
use AmpProject\Extension;

/**
* Class AMP_Instagram_Embed_Handler
Expand Down Expand Up @@ -142,6 +143,9 @@ private function get_instagram_id_from_url( $url ) {
* @param Document $dom DOM.
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::INSTAGRAM );

/**
* Node list.
*
Expand Down
15 changes: 15 additions & 0 deletions includes/embeds/class-amp-pinterest-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @package AMP
*/

use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
* Class AMP_Pinterest_Embed_Handler
*
Expand Down Expand Up @@ -89,4 +92,16 @@ public function render( $args ) {
]
);
}

/**
* Sanitize raw embeds.
*
* @param Document $dom Document.
*
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::PINTEREST );
}
}
16 changes: 15 additions & 1 deletion includes/embeds/class-amp-reddit-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* @since 0.7
*/

use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
* Class AMP_Reddit_Embed_Handler
*
Expand Down Expand Up @@ -73,5 +76,16 @@ public function render( $args ) {
]
);
}
}

/**
* Sanitize raw embeds.
*
* @param Document $dom Document.
*
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::EMBEDLY_CARD );
}
}
15 changes: 15 additions & 0 deletions includes/embeds/class-amp-soundcloud-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @package AMP
*/

use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
* Class AMP_SoundCloud_Embed_Handler
*
Expand Down Expand Up @@ -174,6 +177,18 @@ private function render_embed_fallback( $url ) {
);
}

/**
* Sanitize raw embeds.
*
* @param Document $dom Document.
*
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::SOUNDCLOUD );
}

/**
* Get params from Soundcloud iframe src.
*
Expand Down
1 change: 1 addition & 0 deletions includes/embeds/class-amp-tiktok-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function sanitize_raw_embeds( Document $dom ) {

foreach ( $nodes as $node ) {
$this->make_embed_amp_compatible( $node );
$this->unwrap_p_element( $node );
}
}

Expand Down
15 changes: 15 additions & 0 deletions includes/embeds/class-amp-vimeo-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @package AMP
*/

use AmpProject\Extension;
use AmpProject\Dom\Document;

/**
* Class AMP_Vimeo_Embed_Handler
*
Expand Down Expand Up @@ -131,6 +134,18 @@ public function render( $args ) {
);
}

/**
* Sanitize raw embeds.
*
* @param Document $dom Document.
*
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::VIMEO );
}

/**
* Determine the video ID from the URL.
*
Expand Down
2 changes: 2 additions & 0 deletions includes/embeds/class-amp-youtube-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ public function render( $html, $url, $video_id ) {
* @return void
*/
public function sanitize_raw_embeds( Document $dom ) {
// If there were any previous embeds in the DOM that were wrapped by `wpautop()`, unwrap them.
$this->unwrap_p_element_by_child_tag_name( $dom, Extension::YOUTUBE );

$query_segments = array_map(
static function ( $domain ) {
Expand Down