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

Add sanitization of XFBML raw embeds into amp-facebook #5506

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
81 changes: 65 additions & 16 deletions includes/embeds/class-amp-facebook-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,19 @@ public function unregister_embed() {
}

/**
* Sanitized <div class="fb-video" data-href=> tags to <amp-facebook>.
* Sanitize raw embeds.
*
* @param Document $dom DOM.
*/
public function sanitize_raw_embeds( Document $dom ) {
$nodes = $dom->getElementsByTagName( $this->sanitize_tag );
$num_nodes = $nodes->length;
$replaced = 0;

if ( 0 === $num_nodes ) {
return;
}
$replaced += $this->sanitize_raw_embed_divs( $dom );

for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
if ( ! $node instanceof DOMElement ) {
continue;
}

$embed_type = $this->get_embed_type( $node );
$replaced += $this->sanitize_raw_embed_fb_components( $dom );

if ( null !== $embed_type ) {
$this->create_amp_facebook_and_replace_node( $dom, $node, $embed_type );
}
if ( 0 === $replaced ) {
return;
}

/*
Expand Down Expand Up @@ -100,6 +90,65 @@ public function sanitize_raw_embeds( Document $dom ) {
}
}

/**
* Sanitize <div class="fb-video" data-href=> tags to <amp-facebook>.
*
* @param Document $dom DOM.
* @return int Replacements made.
*/
private function sanitize_raw_embed_divs( Document $dom ) {
$replaced = 0;
$nodes = $dom->getElementsByTagName( $this->sanitize_tag );
$num_nodes = $nodes->length;

if ( 0 === $num_nodes ) {
return 0;
}

for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
if ( ! $node instanceof DOMElement ) {
continue;
}

$embed_type = $this->get_embed_type( $node );

if ( null !== $embed_type ) {
$this->create_amp_facebook_and_replace_node( $dom, $node, $embed_type );
$replaced++;
}
}
return $replaced;
}

/**
* Sanitize <fb:post> tags to <amp-facebook>.
*
* @param Document $dom DOM.
* @return int Replacements made.
*/
private function sanitize_raw_embed_fb_components( Document $dom ) {
$replaced = 0;
$nodes = $dom->getElementsByTagName( 'post' );
$num_nodes = $nodes->length;

if ( 0 === $num_nodes ) {
return 0;
}

for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
if ( ! $node instanceof DOMElement ) {
continue;
}

$embed_type = 'post';
$this->create_amp_facebook_and_replace_node( $dom, $node, $embed_type );
$replaced++;
}
return $replaced;
}

/**
* Get embed type.
*
Expand Down