Skip to content

Commit

Permalink
Use whitelist sanitizer to determine AMP component scripts needed for…
Browse files Browse the repository at this point in the history
… embeds
  • Loading branch information
westonruter committed Jan 19, 2018
1 parent 89e4227 commit 6f0c0e9
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 153 deletions.
10 changes: 10 additions & 0 deletions includes/embeds/class-amp-base-embed-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ function __construct( $args = array() ) {
) );
}

/**
* Get mapping of AMP component names to AMP script URLs.
*
* This is normally no longer needed because the wnitelist
* sanitizer will automatically detect the need for them via
* the spec.
*
* @see AMP_Tag_And_Attribute_Sanitizer::get_scripts()
* @return array Scripts.
*/
public function get_scripts() {
return array();
}
Expand Down
11 changes: 0 additions & 11 deletions includes/embeds/class-amp-dailymotion-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class AMP_DailyMotion_Embed_Handler extends AMP_Base_Embed_Handler {
protected $DEFAULT_WIDTH = 600;
protected $DEFAULT_HEIGHT = 338;

private static $script_slug = 'amp-dailymotion';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-dailymotion-0.1.js';

function __construct( $args = array() ) {
parent::__construct( $args );

Expand All @@ -41,14 +38,6 @@ public function unregister_embed() {
remove_shortcode( 'dailymotion' );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function shortcode( $attr ) {
$video_id = false;

Expand Down
11 changes: 0 additions & 11 deletions includes/embeds/class-amp-facebook-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class AMP_Facebook_Embed_Handler extends AMP_Base_Embed_Handler {
protected $DEFAULT_WIDTH = 600;
protected $DEFAULT_HEIGHT = 400;

private static $script_slug = 'amp-facebook';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-facebook-0.1.js';

public function register_embed() {
wp_embed_register_handler( 'amp-facebook', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
}
Expand All @@ -25,14 +22,6 @@ public function unregister_embed() {
wp_embed_unregister_handler( 'amp-facebook', -1 );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function oembed( $matches, $attr, $url, $rawattr ) {
return $this->render( array( 'url' => $url ) );
}
Expand Down
10 changes: 0 additions & 10 deletions includes/embeds/class-amp-gallery-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* Class AMP_Gallery_Embed_Handler
*/
class AMP_Gallery_Embed_Handler extends AMP_Base_Embed_Handler {
private static $script_slug = 'amp-carousel';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-carousel-0.1.js';

public function register_embed() {
add_shortcode( 'gallery', array( $this, 'shortcode' ) );
Expand All @@ -20,14 +18,6 @@ public function unregister_embed() {
remove_shortcode( 'gallery' );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function shortcode( $attr ) {
$post = get_post();

Expand Down
11 changes: 0 additions & 11 deletions includes/embeds/class-amp-instagram-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class AMP_Instagram_Embed_Handler extends AMP_Base_Embed_Handler {
protected $DEFAULT_WIDTH = 600;
protected $DEFAULT_HEIGHT = 600;

private static $script_slug = 'amp-instagram';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-instagram-0.1.js';

public function register_embed() {
wp_embed_register_handler( 'amp-instagram', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
add_shortcode( 'instagram', array( $this, 'shortcode' ) );
Expand All @@ -30,14 +27,6 @@ public function unregister_embed() {
remove_shortcode( 'instagram' );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function shortcode( $attr ) {
$url = false;

Expand Down
11 changes: 0 additions & 11 deletions includes/embeds/class-amp-pinterest-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class AMP_Pinterest_Embed_Handler extends AMP_Base_Embed_Handler {
protected $DEFAULT_WIDTH = 450;
protected $DEFAULT_HEIGHT = 750;

private static $script_slug = 'amp-pinterest';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-pinterest-0.1.js';

public function register_embed() {
wp_embed_register_handler(
'amp-pinterest',
Expand All @@ -30,14 +27,6 @@ public function unregister_embed() {
wp_embed_unregister_handler('amp-pinterest', -1);
}

public function get_scripts() {
if ( ! $this->did_convert_elements) {
return array();
}

return array( self::$script_slug => self::$script_src);
}

public function oembed( $matches, $attr, $url, $rawattr ) {
return $this->render( array( 'url' => $url ) );
}
Expand Down
29 changes: 1 addition & 28 deletions includes/embeds/class-amp-soundcloud-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,7 @@ class AMP_SoundCloud_Embed_Handler extends AMP_Base_Embed_Handler {
*
* @var int
*/
protected $DEFAULT_HEIGHT = 200;

/**
* Script slug.
*
* @var string
*/
private static $script_slug = 'amp-soundcloud';

/**
* Script source.
*
* @var string
*/
private static $script_src = 'https://cdn.ampproject.org/v0/amp-soundcloud-0.1.js';
protected $DEFAULT_HEIGHT = 200; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase

/**
* Register embed.
Expand All @@ -49,19 +35,6 @@ public function unregister_embed() {
remove_filter( 'embed_oembed_html', array( $this, 'filter_embed_oembed_html' ), 10 );
}

/**
* Get scripts needed by component.
*
* @return array Scripts.
*/
public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

/**
* Render oEmbed.
*
Expand Down
11 changes: 0 additions & 11 deletions includes/embeds/class-amp-twitter-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
class AMP_Twitter_Embed_Handler extends AMP_Base_Embed_Handler {
const URL_PATTERN = '#http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)#i';

private static $script_slug = 'amp-twitter';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-twitter-0.1.js';

public function register_embed() {
add_shortcode( 'tweet', array( $this, 'shortcode' ) );
wp_embed_register_handler( 'amp-twitter', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
Expand All @@ -26,14 +23,6 @@ public function unregister_embed() {
wp_embed_unregister_handler( 'amp-twitter', -1 );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

function shortcode( $attr ) {
$attr = wp_parse_args( $attr, array(
'tweet' => false,
Expand Down
11 changes: 0 additions & 11 deletions includes/embeds/class-amp-vimeo-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class AMP_Vimeo_Embed_Handler extends AMP_Base_Embed_Handler {
protected $DEFAULT_WIDTH = 600;
protected $DEFAULT_HEIGHT = 338;

private static $script_slug = 'amp-vimeo';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-vimeo-0.1.js';

function __construct( $args = array() ) {
parent::__construct( $args );

Expand All @@ -42,14 +39,6 @@ public function unregister_embed() {
remove_shortcode( 'vimeo' );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function shortcode( $attr ) {
$video_id = false;

Expand Down
11 changes: 0 additions & 11 deletions includes/embeds/class-amp-vine-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class AMP_Vine_Embed_Handler extends AMP_Base_Embed_Handler {
protected $DEFAULT_WIDTH = 400;
protected $DEFAULT_HEIGHT = 400;

private static $script_slug = 'amp-vine';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-vine-0.1.js';

public function register_embed() {
wp_embed_register_handler( 'amp-vine', self::URL_PATTERN, array( $this, 'oembed' ), -1 );
}
Expand All @@ -25,14 +22,6 @@ public function unregister_embed() {
wp_embed_unregister_handler( 'amp-vine', -1 );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function oembed( $matches, $attr, $url, $rawattr ) {
return $this->render( array( 'url' => $url, 'vine_id' => end( $matches ) ) );
}
Expand Down
11 changes: 0 additions & 11 deletions includes/embeds/class-amp-youtube-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class AMP_YouTube_Embed_Handler extends AMP_Base_Embed_Handler {
protected $DEFAULT_WIDTH = 600;
protected $DEFAULT_HEIGHT = 338;

private static $script_slug = 'amp-youtube';
private static $script_src = 'https://cdn.ampproject.org/v0/amp-youtube-0.1.js';

function __construct( $args = array() ) {
parent::__construct( $args );

Expand All @@ -42,14 +39,6 @@ public function unregister_embed() {
remove_shortcode( 'youtube' );
}

public function get_scripts() {
if ( ! $this->did_convert_elements ) {
return array();
}

return array( self::$script_slug => self::$script_src );
}

public function shortcode( $attr ) {
$url = false;
$video_id = false;
Expand Down
13 changes: 10 additions & 3 deletions tests/test-amp-dailymotion-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function get_scripts_data() {
),
'converted' => array(
'https://www.dailymotion.com/video/x5awwth' . PHP_EOL,
array( 'amp-dailymotion' => 'https://cdn.ampproject.org/v0/amp-dailymotion-0.1.js' ),
array( 'amp-dailymotion' => 'https://cdn.ampproject.org/v0/amp-dailymotion-latest.js' ),
),
);
}
Expand All @@ -61,8 +61,15 @@ public function get_scripts_data() {
public function test__get_scripts( $source, $expected ) {
$embed = new AMP_DailyMotion_Embed_Handler();
$embed->register_embed();
apply_filters( 'the_content', $source );
$scripts = $embed->get_scripts();
$source = apply_filters( 'the_content', $source );

$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( AMP_DOM_Utils::get_dom_from_content( $source ) );
$whitelist_sanitizer->sanitize();

$scripts = array_merge(
$embed->get_scripts(),
$whitelist_sanitizer->get_scripts()
);

$this->assertEquals( $expected, $scripts );
}
Expand Down
13 changes: 10 additions & 3 deletions tests/test-amp-facebook-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function get_scripts_data() {
),
'converted' => array(
'https://www.facebook.com/zuck/posts/10102593740125791' . PHP_EOL,
array( 'amp-facebook' => 'https://cdn.ampproject.org/v0/amp-facebook-0.1.js' ),
array( 'amp-facebook' => 'https://cdn.ampproject.org/v0/amp-facebook-latest.js' ),
),
);
}
Expand All @@ -65,8 +65,15 @@ public function get_scripts_data() {
public function test__get_scripts( $source, $expected ) {
$embed = new AMP_Facebook_Embed_Handler();
$embed->register_embed();
apply_filters( 'the_content', $source );
$scripts = $embed->get_scripts();
$source = apply_filters( 'the_content', $source );

$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( AMP_DOM_Utils::get_dom_from_content( $source ) );
$whitelist_sanitizer->sanitize();

$scripts = array_merge(
$embed->get_scripts(),
$whitelist_sanitizer->get_scripts()
);

$this->assertEquals( $expected, $scripts );
}
Expand Down
13 changes: 10 additions & 3 deletions tests/test-amp-instagram-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function get_scripts_data() {
),
'converted' => array(
'https://instagram.com/p/7-l0z_p4A4/' . PHP_EOL,
array( 'amp-instagram' => 'https://cdn.ampproject.org/v0/amp-instagram-0.1.js' ),
array( 'amp-instagram' => 'https://cdn.ampproject.org/v0/amp-instagram-latest.js' ),
),
);
}
Expand All @@ -64,8 +64,15 @@ public function get_scripts_data() {
public function test__get_scripts( $source, $expected ) {
$embed = new AMP_Instagram_Embed_Handler();
$embed->register_embed();
apply_filters( 'the_content', $source );
$scripts = $embed->get_scripts();
$source = apply_filters( 'the_content', $source );

$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( AMP_DOM_Utils::get_dom_from_content( $source ) );
$whitelist_sanitizer->sanitize();

$scripts = array_merge(
$embed->get_scripts(),
$whitelist_sanitizer->get_scripts()
);

$this->assertEquals( $expected, $scripts );
}
Expand Down
13 changes: 10 additions & 3 deletions tests/test-amp-pinterest-embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function get_scripts_data() {
),
'converted' => array(
'https://www.pinterest.com/pin/606156431067611861/' . PHP_EOL,
array( 'amp-pinterest' => 'https://cdn.ampproject.org/v0/amp-pinterest-0.1.js' ),
array( 'amp-pinterest' => 'https://cdn.ampproject.org/v0/amp-pinterest-latest.js' ),
),
);
}
Expand All @@ -48,8 +48,15 @@ public function get_scripts_data() {
public function test__get_scripts( $source, $expected ) {
$embed = new AMP_Pinterest_Embed_Handler();
$embed->register_embed();
apply_filters( 'the_content', $source );
$scripts = $embed->get_scripts();
$source = apply_filters( 'the_content', $source );

$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( AMP_DOM_Utils::get_dom_from_content( $source ) );
$whitelist_sanitizer->sanitize();

$scripts = array_merge(
$embed->get_scripts(),
$whitelist_sanitizer->get_scripts()
);

$this->assertEquals( $expected, $scripts );
}
Expand Down
Loading

0 comments on commit 6f0c0e9

Please sign in to comment.