From f46490ed9b2d9928041cbd4432305e6091732ee0 Mon Sep 17 00:00:00 2001 From: juanchaur1 Date: Wed, 6 Jun 2018 15:37:40 -0400 Subject: [PATCH 1/6] Adding sanitizer to support amp-o2-player --- includes/amp-helper-functions.php | 1 + includes/class-amp-autoloader.php | 1 + .../class-amp-o2-player-sanitizer.php | 137 ++++++++++++++++++ tests/test-amp-o2-player-sanitizer.php | 101 +++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 includes/sanitizers/class-amp-o2-player-sanitizer.php create mode 100644 tests/test-amp-o2-player-sanitizer.php diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index 35e5b16cc1e..531df0b12ce 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -532,6 +532,7 @@ function amp_get_content_sanitizers( $post = null ) { 'AMP_Form_Sanitizer' => array(), 'AMP_Comments_Sanitizer' => array(), 'AMP_Video_Sanitizer' => array(), + 'AMP_O2_Player_Sanitizer' => array(), 'AMP_Audio_Sanitizer' => array(), 'AMP_Playbuzz_Sanitizer' => array(), 'AMP_Iframe_Sanitizer' => array( diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index 1ad252863eb..8a3825c28d1 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -78,6 +78,7 @@ class AMP_Autoloader { 'AMP_Img_Sanitizer' => 'includes/sanitizers/class-amp-img-sanitizer', 'AMP_Comments_Sanitizer' => 'includes/sanitizers/class-amp-comments-sanitizer', 'AMP_Form_Sanitizer' => 'includes/sanitizers/class-amp-form-sanitizer', + 'AMP_O2_Player_Sanitizer' => 'includes/sanitizers/class-amp-o2-player-sanitizer', 'AMP_Playbuzz_Sanitizer' => 'includes/sanitizers/class-amp-playbuzz-sanitizer', 'AMP_Style_Sanitizer' => 'includes/sanitizers/class-amp-style-sanitizer', 'AMP_Tag_And_Attribute_Sanitizer' => 'includes/sanitizers/class-amp-tag-and-attribute-sanitizer', diff --git a/includes/sanitizers/class-amp-o2-player-sanitizer.php b/includes/sanitizers/class-amp-o2-player-sanitizer.php new file mode 100644 index 00000000000..1d1d9b272dc --- /dev/null +++ b/includes/sanitizers/class-amp-o2-player-sanitizer.php @@ -0,0 +1,137 @@ + embed to + * + * @see https://www.ampproject.org/docs/reference/components/amp-o2-player + */ +class AMP_O2_Player_Sanitizer extends AMP_Base_Sanitizer { + const URL_PATTERN = '#.*delivery.vidible.tv\/jsonp\/pid=(.*)\/vid=(.*)\/(.*).js.*#i'; + + /** + * AMP Tag. + * + * @var string AMP Tag. + */ + public static $amp_tag = 'amp-o2-player'; + + /** + * Amp O2 Player class. + * + * @var string CSS class to identify O2 Player
to replace with AMP version. + */ + public static $xpath_selector = '//div[ contains( @class, \'vdb_player\' ) ]/script'; + + /** + * Hardcoded height to set for 02 Player elements. + * + * @var string + */ + private static $height = '270'; + + /** + * Hardcoded height to set for 02 Player elements. + * + * @var string + */ + private static $width = '600'; + + /** + * XPath. + * + * @var DOMXPath + */ + private $xpath; + + /** + * AMP_O2_Player_Sanitizer constructor. + * + * @param DOMDocument $dom Represents the HTML document to sanitize. + * @param array $args Args. + */ + public function __construct( DOMDocument $dom, array $args = array() ) { + parent::__construct( $dom, $args ); + $this->xpath = new DOMXPath( $dom ); + } + + /** + * Sanitize the O2 Player elements from the HTML contained in this instance's DOMDocument. + */ + public function sanitize() { + /** + * Node list. + * + * @var DOMNodeList $nodes + */ + $nodes = $this->xpath->query( self::$xpath_selector ); + $num_nodes = $nodes->length; + + if ( 0 === $num_nodes ) { + return; + } + + for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { + $node = $nodes->item( $i ); + + $this->create_amp_o2_player( $this->dom, $node ); + } + + } + + /** + * Replaces node with amp-o2-player + * + * @param DOMDocument $dom The HTML Document. + * @param DOMNode $node The DOMNode to adjust and replace. + */ + private function create_amp_o2_player( $dom, $node ) { + $o2_attributes = $this->get_o2_player_attributes( $node->getAttribute( 'src' ) ); + + if ( ! empty( $o2_attributes ) ) { + $component_attributes = array_merge( + $o2_attributes, array( + 'data-macros' => 'm.playback=click', + 'layout' => 'responsive', + 'width' => self::$width, + 'height' => self::$height, + ) + ); + + $amp_o2_player = AMP_DOM_Utils::create_node( $dom, self::$amp_tag, $component_attributes ); + + $parent_node = $node->parentNode; + + // replaces the wrapper that contains the script with amp-o2-player element. + $parent_node->parentNode->replaceChild( $amp_o2_player, $parent_node ); + + $this->did_convert_elements = true; + } + } + + /** + * Gets O2 Player's required attributes from script src + * + * @param string $src script src. + * + * @return array data-attributes for o2 player. + */ + private function get_o2_player_attributes( $src ) { + $found = preg_match( self::URL_PATTERN, $src, $matches ); + if ( $found ) { + return array( + 'data-pid' => $matches[1], + 'data-vid' => $matches[2], + 'data-bcid' => $matches[3], + ); + } + return array(); + } + +} diff --git a/tests/test-amp-o2-player-sanitizer.php b/tests/test-amp-o2-player-sanitizer.php new file mode 100644 index 00000000000..65a4fc3992a --- /dev/null +++ b/tests/test-amp-o2-player-sanitizer.php @@ -0,0 +1,101 @@ + array( + '

Im Not A O2 Player

', + '

Im Not A O2 Player

', + ), + 'o2_player_item_without_script' => array( + '
', + '
', + ), + + 'o2_player_item' => array( + '
',// phpcs:ignore + '', + ), + + 'o2_player_item_without_required_fields' => array( + '
',// phpcs:ignore + '
',// phpcs:ignore + ), + + ); + } + + /** + * Dataset to test amp-o2-player sanitizer/ + * + * @param string $source Content. + * @param string $expected Expected content. + * + * @dataProvider get_data + */ + public function test_converter( $source, $expected ) { + $dom = AMP_DOM_Utils::get_dom_from_content( $source ); + $sanitizer = new AMP_O2_Player_Sanitizer( $dom ); + + $sanitizer->sanitize(); + + $content = AMP_DOM_Utils::get_content_from_dom( $dom ); + + $this->assertEquals( $expected, $content ); + } + + /** + * Tests get script when required attributes missing. + */ + public function test_get_script__pid__vid__required() { + $source = '
';// phpcs:ignore + $expected = array(); + + $dom = AMP_DOM_Utils::get_dom_from_content( $source ); + $sanitizer = new AMP_O2_Player_Sanitizer( $dom ); + $sanitizer->sanitize(); + + $whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom ); + $whitelist_sanitizer->sanitize(); + + $scripts = array_merge( + $sanitizer->get_scripts(), + $whitelist_sanitizer->get_scripts() + ); + $this->assertEquals( $expected, $scripts ); + } + + /** + * Test that get_scripts() did convert. + */ + public function test_get_scripts__did_convert() { + $source = '
';// phpcs:ignore + $expected = array( 'amp-o2-player' => true ); + + $dom = AMP_DOM_Utils::get_dom_from_content( $source ); + $sanitizer = new AMP_O2_Player_Sanitizer( $dom ); + $sanitizer->sanitize(); + $whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom ); + $whitelist_sanitizer->sanitize(); + + $scripts = array_merge( + $sanitizer->get_scripts(), + $whitelist_sanitizer->get_scripts() + ); + $this->assertEquals( $expected, $scripts ); + } +} From 1dcb92350e3047df0cbc7aaf2f56878a9df4f98b Mon Sep 17 00:00:00 2001 From: juanchaur1 Date: Thu, 7 Jun 2018 11:36:10 -0400 Subject: [PATCH 2/6] Fixing aspect ratio of player --- includes/sanitizers/class-amp-o2-player-sanitizer.php | 6 +++--- tests/test-amp-o2-player-sanitizer.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/sanitizers/class-amp-o2-player-sanitizer.php b/includes/sanitizers/class-amp-o2-player-sanitizer.php index 1d1d9b272dc..f70bf102745 100644 --- a/includes/sanitizers/class-amp-o2-player-sanitizer.php +++ b/includes/sanitizers/class-amp-o2-player-sanitizer.php @@ -30,18 +30,18 @@ class AMP_O2_Player_Sanitizer extends AMP_Base_Sanitizer { public static $xpath_selector = '//div[ contains( @class, \'vdb_player\' ) ]/script'; /** - * Hardcoded height to set for 02 Player elements. + * Height to set for 02 Player elements. * * @var string */ private static $height = '270'; /** - * Hardcoded height to set for 02 Player elements. + * Width to set for 02 Player elements. * * @var string */ - private static $width = '600'; + private static $width = '480'; /** * XPath. diff --git a/tests/test-amp-o2-player-sanitizer.php b/tests/test-amp-o2-player-sanitizer.php index 65a4fc3992a..70d5723ba63 100644 --- a/tests/test-amp-o2-player-sanitizer.php +++ b/tests/test-amp-o2-player-sanitizer.php @@ -28,7 +28,7 @@ public function get_data() { 'o2_player_item' => array( '
',// phpcs:ignore - '', + '', ), 'o2_player_item_without_required_fields' => array( From bd1ff450b7953ab804bb802705fee3a1b9e00a0e Mon Sep 17 00:00:00 2001 From: juanchaur1 Date: Tue, 19 Jun 2018 16:14:34 -0400 Subject: [PATCH 3/6] remove constructor, add name capturin groups and add version --- .../class-amp-o2-player-sanitizer.php | 54 ++++++++++--------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/includes/sanitizers/class-amp-o2-player-sanitizer.php b/includes/sanitizers/class-amp-o2-player-sanitizer.php index f70bf102745..5e20beb9aac 100644 --- a/includes/sanitizers/class-amp-o2-player-sanitizer.php +++ b/includes/sanitizers/class-amp-o2-player-sanitizer.php @@ -10,14 +10,22 @@ * * Converts