PDF rausgenommen

This commit is contained in:
aschwarz
2023-01-23 11:03:31 +01:00
parent 82d562a322
commit a6523903eb
28078 changed files with 4247552 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,222 @@
<?php
/**
* Class AMP_Audio_Sanitizer
*
* @package AMP
*/
/**
* Class AMP_Audio_Sanitizer
*
* Converts <audio> tags to <amp-audio>
*/
class AMP_Audio_Sanitizer extends AMP_Base_Sanitizer {
use AMP_Noscript_Fallback;
/**
* Tag.
*
* @var string HTML audio tag to identify and replace with AMP version.
* @since 0.2
*/
public static $tag = 'audio';
/**
* Default args.
*
* @var array
*/
protected $DEFAULT_ARGS = array(
'add_noscript_fallback' => true,
);
/**
* Get mapping of HTML selectors to the AMP component selectors which they may be converted into.
*
* @return array Mapping.
*/
public function get_selector_conversion_mapping() {
return array(
'audio' => array( 'amp-audio' ),
);
}
/**
* Sanitize the <audio> elements from the HTML contained in this instance's DOMDocument.
*
* @since 0.2
*/
public function sanitize() {
$nodes = $this->dom->getElementsByTagName( self::$tag );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return;
}
if ( $this->args['add_noscript_fallback'] ) {
$this->initialize_noscript_allowed_attributes( self::$tag );
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
// Skip element if already inside of an AMP element as a noscript fallback.
if ( $this->is_inside_amp_noscript( $node ) ) {
continue;
}
$old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
// For amp-audio, the default width and height are inferred from browser.
$sources = array();
$new_attributes = $this->filter_attributes( $old_attributes );
if ( ! empty( $new_attributes['src'] ) ) {
$sources[] = $new_attributes['src'];
}
/**
* Original node.
*
* @var DOMElement $old_node
*/
$old_node = $node->cloneNode( false );
// Gather all child nodes and supply empty video dimensions from sources.
$fallback = null;
$child_nodes = array();
while ( $node->firstChild ) {
$child_node = $node->removeChild( $node->firstChild );
if ( $child_node instanceof DOMElement && 'source' === $child_node->nodeName && $child_node->hasAttribute( 'src' ) ) {
$src = $this->maybe_enforce_https_src( $child_node->getAttribute( 'src' ), true );
if ( ! $src ) {
// @todo $this->remove_invalid_child( $child_node ), but this will require refactoring the while loop since it uses firstChild.
continue; // Skip adding source.
}
$sources[] = $src;
$child_node->setAttribute( 'src', $src );
$new_attributes = $this->filter_attributes( $new_attributes );
}
if ( ! $fallback && $child_node instanceof DOMElement && ! ( 'source' === $child_node->nodeName || 'track' === $child_node->nodeName ) ) {
$fallback = $child_node;
$fallback->setAttribute( 'fallback', '' );
}
$child_nodes[] = $child_node;
}
/*
* Add fallback for audio shortcode which is not present by default since wp_mediaelement_fallback()
* is not called when wp_audio_shortcode_library is filtered from mediaelement to amp.
*/
if ( ! $fallback && ! empty( $sources ) ) {
$fallback = $this->dom->createElement( 'a' );
$fallback->setAttribute( 'href', $sources[0] );
$fallback->setAttribute( 'fallback', '' );
$fallback->appendChild( $this->dom->createTextNode( $sources[0] ) );
$child_nodes[] = $fallback;
}
/*
* Audio in WordPress is responsive with 100% width, so this infers fixed-layout.
* In AMP, the amp-audio's default height is inferred from the browser.
*/
$new_attributes['width'] = 'auto';
// @todo Make sure poster and artwork attributes are HTTPS.
$new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-audio', $new_attributes );
foreach ( $child_nodes as $child_node ) {
$new_node->appendChild( $child_node );
if ( ! ( $child_node instanceof DOMElement ) || ! $child_node->hasAttribute( 'fallback' ) ) {
$old_node->appendChild( $child_node->cloneNode( true ) );
}
}
// Make sure the updated src and poster are applied to the original.
foreach ( array( 'src', 'poster', 'artwork' ) as $attr_name ) {
if ( $new_node->hasAttribute( $attr_name ) ) {
$old_node->setAttribute( $attr_name, $new_node->getAttribute( $attr_name ) );
}
}
/*
* If the node has at least one valid source, replace the old node with it.
* Otherwise, just remove the node.
*
* @todo Add a fallback handler.
* See: https://github.com/ampproject/amphtml/issues/2261
*/
if ( empty( $sources ) ) {
$this->remove_invalid_child( $node );
} else {
$node->parentNode->replaceChild( $new_node, $node );
if ( $this->args['add_noscript_fallback'] ) {
// Preserve original node in noscript for no-JS environments.
$this->append_old_node_noscript( $new_node, $old_node, $this->dom );
}
}
$this->did_convert_elements = true;
}
}
/**
* "Filter" HTML attributes for <amp-audio> elements.
*
* @since 0.2
*
* @param string[] $attributes {
* Attributes.
*
* @type string $src Audio URL - Empty if HTTPS required per $this->args['require_https_src']
* @type int $width <audio> attribute - Set to numeric value if px or %
* @type int $height <audio> attribute - Set to numeric value if px or %
* @type string $class <audio> attribute - Pass along if found
* @type bool $loop <audio> attribute - Convert 'false' to empty string ''
* @type bool $muted <audio> attribute - Convert 'false' to empty string ''
* @type bool $autoplay <audio> attribute - Convert 'false' to empty string ''
* }
* @return array Returns HTML attributes; removes any not specifically declared above from input.
*/
private function filter_attributes( $attributes ) {
$out = array();
foreach ( $attributes as $name => $value ) {
switch ( $name ) {
case 'src':
$out[ $name ] = $this->maybe_enforce_https_src( $value );
break;
case 'width':
case 'height':
$out[ $name ] = $this->sanitize_dimension( $value, $name );
break;
case 'class':
$out[ $name ] = $value;
break;
case 'loop':
case 'muted':
case 'autoplay':
if ( 'false' !== $value ) {
$out[ $name ] = '';
}
break;
case 'data-amp-layout':
$out['layout'] = $value;
break;
case 'data-amp-noloading':
$out['noloading'] = $value;
break;
default:
$out[ $name ] = $value;
}
}
return $out;
}
}

View File

@ -0,0 +1,587 @@
<?php
/**
* Class AMP_Base_Sanitizer
*
* @package AMP
*/
/**
* Class AMP_Base_Sanitizer
*/
abstract class AMP_Base_Sanitizer {
/**
* Value used with the height attribute in an $attributes parameter is empty.
*
* @since 0.3.3
*
* @const int
*/
const FALLBACK_HEIGHT = 400;
/**
* Value for <amp-image-lightbox> ID.
*
* @since 1.0
*
* @const string
*/
const AMP_IMAGE_LIGHTBOX_ID = 'amp-image-lightbox';
/**
* Placeholder for default args, to be set in child classes.
*
* @since 0.2
*
* @var array
*/
protected $DEFAULT_ARGS = array();
/**
* DOM.
*
* @var DOMDocument A standard PHP representation of an HTML document in object form.
*
* @since 0.2
*/
protected $dom;
/**
* Array of flags used to control sanitization.
*
* @var array {
* @type int $content_max_width
* @type bool $add_placeholder
* @type bool $use_document_element
* @type bool $require_https_src
* @type string[] $amp_allowed_tags
* @type string[] $amp_globally_allowed_attributes
* @type string[] $amp_layout_allowed_attributes
* @type array $amp_allowed_tags
* @type array $amp_globally_allowed_attributes
* @type array $amp_layout_allowed_attributes
* @type array $amp_bind_placeholder_prefix
* @type bool $allow_dirty_styles
* @type bool $allow_dirty_scripts
* @type bool $should_locate_sources
* @type callable $validation_error_callback
* }
*/
protected $args;
/**
* Flag to be set in child class' sanitize() method indicating if the
* HTML contained in the DOMDocument has been sanitized yet or not.
*
* @since 0.2
*
* @var bool
*/
protected $did_convert_elements = false;
/**
* The root element used for sanitization. Either html or body.
*
* @var DOMElement
*/
protected $root_element;
/**
* Keep track of nodes that should not be removed to prevent duplicated validation errors since sanitization is rejected.
*
* @var array
*/
private $should_not_removed_nodes = array();
/**
* AMP_Base_Sanitizer constructor.
*
* @since 0.2
*
* @param DOMDocument $dom Represents the HTML document to sanitize.
* @param array $args {
* Args.
*
* @type int $content_max_width
* @type bool $add_placeholder
* @type bool $require_https_src
* @type string[] $amp_allowed_tags
* @type string[] $amp_globally_allowed_attributes
* @type string[] $amp_layout_allowed_attributes
* }
*/
public function __construct( $dom, $args = array() ) {
$this->dom = $dom;
$this->args = array_merge( $this->DEFAULT_ARGS, $args );
if ( ! empty( $this->args['use_document_element'] ) ) {
$this->root_element = $this->dom->documentElement;
} else {
$this->root_element = $this->dom->getElementsByTagName( 'body' )->item( 0 );
}
}
/**
* Add filters to manipulate output during output buffering before the DOM is constructed.
*
* Add actions and filters before the page is rendered so that the sanitizer can fix issues during output buffering.
* This provides an alternative to manipulating the DOM in the sanitize method. This is a static function because
* it is invoked before the class is instantiated, as the DOM is not available yet. This method is only called
* when 'amp' theme support is present. It is conceptually similar to the AMP_Base_Embed_Handler class's register_embed
* method.
*
* @since 1.0
* @see \AMP_Base_Embed_Handler::register_embed()
*
* @param array $args Args.
*/
public static function add_buffering_hooks( $args = array() ) {}
/**
* Get mapping of HTML selectors to the AMP component selectors which they may be converted into.
*
* @return array Mapping.
*/
public function get_selector_conversion_mapping() {
return array();
}
/**
* Run logic before any sanitizers are run.
*
* After the sanitizers are instantiated but before calling sanitize on each of them, this
* method is called with list of all the instantiated sanitizers.
*
* @param AMP_Base_Sanitizer[] $sanitizers Sanitizers.
*/
public function init( $sanitizers ) {}
/**
* Sanitize the HTML contained in the DOMDocument received by the constructor
*/
abstract public function sanitize();
/**
* Return array of values that would be valid as an HTML `script` element.
*
* Array keys are AMP element names and array values are their respective
* Javascript URLs from https://cdn.ampproject.org
*
* @since 0.2
*
* @return string[] Returns component name as array key and JavaScript URL as array value,
* respectively. Will return an empty array if sanitization has yet to be run
* or if it did not find any HTML elements to convert to AMP equivalents.
*/
public function get_scripts() {
return array();
}
/**
* Return array of values that would be valid as an HTML `style` attribute.
*
* @since 0.4
* @deprecated As of 1.0, use get_stylesheets().
*
* @return array[][] Mapping of CSS selectors to arrays of properties.
*/
public function get_styles() {
return array();
}
/**
* Get stylesheets.
*
* @since 0.7
* @returns array Values are the CSS stylesheets. Keys are MD5 hashes of the stylesheets.
*/
public function get_stylesheets() {
$stylesheets = array();
foreach ( $this->get_styles() as $selector => $properties ) {
$stylesheet = sprintf( '%s { %s }', $selector, join( '; ', $properties ) . ';' );
$stylesheets[ md5( $stylesheet ) ] = $stylesheet;
}
return $stylesheets;
}
/**
* Get HTML body as DOMElement from DOMDocument received by the constructor.
*
* @deprecated Just reference $root_element instead.
* @return DOMElement The body element.
*/
protected function get_body_node() {
return $this->dom->getElementsByTagName( 'body' )->item( 0 );
}
/**
* Sanitizes a CSS dimension specifier while being sensitive to dimension context.
*
* @param string $value A valid CSS dimension specifier; e.g. 50, 50px, 50%.
* @param string $dimension 'width' or ignored. 'width' only affects $values ending in '%'.
*
* @return float|int|string Returns a numeric dimension value, or an empty string.
*/
public function sanitize_dimension( $value, $dimension ) {
// Allows 0 to be used as valid dimension.
if ( null === $value ) {
return '';
}
// Accepts both integers and floats & prevents negative values.
if ( is_numeric( $value ) ) {
return max( 0, floatval( $value ) );
}
if ( AMP_String_Utils::endswith( $value, 'px' ) ) {
return absint( $value );
}
if ( AMP_String_Utils::endswith( $value, '%' ) ) {
if ( 'width' === $dimension && isset( $this->args['content_max_width'] ) ) {
$percentage = absint( $value ) / 100;
return round( $percentage * $this->args['content_max_width'] );
}
}
return '';
}
/**
* Sets the layout, and possibly the 'height' and 'width' attributes.
*
* @param string[] $attributes {
* Attributes.
*
* @type int $height
* @type int $width
* @type string $sizes
* @type string $class
* @type string $layout
* }
* @return array Attributes.
*/
public function set_layout( $attributes ) {
if ( isset( $attributes['layout'] ) && ( 'fill' === $attributes['layout'] || 'flex-item' !== $attributes['layout'] ) ) {
return $attributes;
}
if ( empty( $attributes['height'] ) ) {
unset( $attributes['width'] );
$attributes['height'] = self::FALLBACK_HEIGHT;
}
if ( empty( $attributes['width'] ) ) {
$attributes['layout'] = 'fixed-height';
}
return $attributes;
}
/**
* Adds or appends key and value to list of attributes
*
* Adds key and value to list of attributes, or if the key already exists in the array
* it concatenates to existing attribute separator by a space or other supplied separator.
*
* @param string[] $attributes {
* Attributes.
*
* @type int $height
* @type int $width
* @type string $sizes
* @type string $class
* @type string $layout
* }
* @param string $key Valid associative array index to add.
* @param string $value Value to add or append to array indexed at the key.
* @param string $separator Optional; defaults to space but some other separator if needed.
*/
public function add_or_append_attribute( &$attributes, $key, $value, $separator = ' ' ) {
if ( isset( $attributes[ $key ] ) ) {
$attributes[ $key ] = trim( $attributes[ $key ] . $separator . $value );
} else {
$attributes[ $key ] = $value;
}
}
/**
* Decide if we should remove a src attribute if https is required.
*
* If not required, the implementing class may want to try and force https instead.
*
* @param string $src URL to convert to HTTPS if forced, or made empty if $args['require_https_src'].
* @param boolean $force_https Force setting of HTTPS if true.
* @return string URL which may have been updated with HTTPS, or may have been made empty.
*/
public function maybe_enforce_https_src( $src, $force_https = false ) {
$protocol = strtok( $src, ':' ); // @todo What about relative URLs? This should use wp_parse_url( $src, PHP_URL_SCHEME )
if ( 'https' !== $protocol ) {
// Check if https is required.
if ( isset( $this->args['require_https_src'] ) && true === $this->args['require_https_src'] ) {
// Remove the src. Let the implementing class decide what do from here.
$src = '';
} elseif ( ( ! isset( $this->args['require_https_src'] ) || false === $this->args['require_https_src'] )
&& true === $force_https ) {
// Don't remove the src, but force https instead.
$src = set_url_scheme( $src, 'https' );
}
}
return $src;
}
/**
* Removes an invalid child of a node.
*
* Also, calls the mutation callback for it.
* This tracks all the nodes that were removed.
*
* @since 0.7
*
* @param DOMNode|DOMElement $node The node to remove.
* @param array $validation_error Validation error details.
* @return bool Whether the node should have been removed, that is, that the node was sanitized for validity.
*/
public function remove_invalid_child( $node, $validation_error = array() ) {
// Prevent double-reporting nodes that are rejected for sanitization.
if ( isset( $this->should_not_removed_nodes[ $node->nodeName ] ) && in_array( $node, $this->should_not_removed_nodes[ $node->nodeName ], true ) ) {
return false;
}
$should_remove = $this->should_sanitize_validation_error( $validation_error, compact( 'node' ) );
if ( $should_remove ) {
$node->parentNode->removeChild( $node );
} else {
$this->should_not_removed_nodes[ $node->nodeName ][] = $node;
}
return $should_remove;
}
/**
* Removes an invalid attribute of a node.
*
* Also, calls the mutation callback for it.
* This tracks all the attributes that were removed.
*
* @since 0.7
*
* @param DOMElement $element The node for which to remove the attribute.
* @param DOMAttr|string $attribute The attribute to remove from the element.
* @param array $validation_error Validation error details.
* @return bool Whether the node should have been removed, that is, that the node was sanitized for validity.
*/
public function remove_invalid_attribute( $element, $attribute, $validation_error = array() ) {
if ( is_string( $attribute ) ) {
$node = $element->getAttributeNode( $attribute );
} else {
$node = $attribute;
}
$should_remove = $this->should_sanitize_validation_error( $validation_error, compact( 'node' ) );
if ( $should_remove ) {
$element->removeAttributeNode( $node );
}
return $should_remove;
}
/**
* Check whether or not sanitization should occur in response to validation error.
*
* @since 1.0
*
* @param array $validation_error Validation error.
* @param array $data Data including the node.
* @return bool Whether to sanitize.
*/
public function should_sanitize_validation_error( $validation_error, $data = array() ) {
if ( empty( $this->args['validation_error_callback'] ) || ! is_callable( $this->args['validation_error_callback'] ) ) {
return true;
}
$validation_error = $this->prepare_validation_error( $validation_error, $data );
return false !== call_user_func( $this->args['validation_error_callback'], $validation_error, $data );
}
/**
* Prepare validation error.
*
* @param array $error {
* Error.
*
* @type string $code Error code.
* }
* @param array $data {
* Data.
*
* @type DOMElement|DOMNode $node The removed node.
* }
* @return array Error.
*/
public function prepare_validation_error( array $error = array(), array $data = array() ) {
$node = null;
$matches = null;
if ( isset( $data['node'] ) && $data['node'] instanceof DOMNode ) {
$node = $data['node'];
$error['node_name'] = $node->nodeName;
if ( $node->parentNode ) {
$error['parent_name'] = $node->parentNode->nodeName;
}
}
if ( $node instanceof DOMElement ) {
if ( ! isset( $error['code'] ) ) {
$error['code'] = AMP_Validation_Error_Taxonomy::INVALID_ELEMENT_CODE;
}
if ( ! isset( $error['type'] ) ) {
$error['type'] = 'script' === $node->nodeName ? AMP_Validation_Error_Taxonomy::JS_ERROR_TYPE : AMP_Validation_Error_Taxonomy::HTML_ELEMENT_ERROR_TYPE;
}
if ( ! isset( $error['node_attributes'] ) ) {
$error['node_attributes'] = array();
foreach ( $node->attributes as $attribute ) {
$error['node_attributes'][ $attribute->nodeName ] = $attribute->nodeValue;
}
}
// Capture script contents.
if ( 'script' === $node->nodeName && ! $node->hasAttribute( 'src' ) ) {
$error['text'] = $node->textContent;
}
// Suppress 'ver' param from enqueued scripts and styles.
if ( 'script' === $node->nodeName && isset( $error['node_attributes']['src'] ) && false !== strpos( $error['node_attributes']['src'], 'ver=' ) ) {
$error['node_attributes']['src'] = add_query_arg( 'ver', '__normalized__', $error['node_attributes']['src'] );
} elseif ( 'link' === $node->nodeName && isset( $error['node_attributes']['href'] ) && false !== strpos( $error['node_attributes']['href'], 'ver=' ) ) {
$error['node_attributes']['href'] = add_query_arg( 'ver', '__normalized__', $error['node_attributes']['href'] );
}
} elseif ( $node instanceof DOMAttr ) {
if ( ! isset( $error['code'] ) ) {
$error['code'] = AMP_Validation_Error_Taxonomy::INVALID_ATTRIBUTE_CODE;
}
if ( ! isset( $error['type'] ) ) {
// If this is an attribute that begins with on, like onclick, it should be a js_error.
$error['type'] = preg_match( '/^on\w+/', $node->nodeName ) ? AMP_Validation_Error_Taxonomy::JS_ERROR_TYPE : AMP_Validation_Error_Taxonomy::HTML_ATTRIBUTE_ERROR_TYPE;
}
if ( ! isset( $error['element_attributes'] ) ) {
$error['element_attributes'] = array();
if ( $node->parentNode && $node->parentNode->hasAttributes() ) {
foreach ( $node->parentNode->attributes as $attribute ) {
$error['element_attributes'][ $attribute->nodeName ] = $attribute->nodeValue;
}
}
}
}
return $error;
}
/**
* Get data-amp-* values from the parent node 'figure' added by editor block.
*
* @param DOMElement $node Base node.
* @return array AMP data array.
*/
public function get_data_amp_attributes( $node ) {
$attributes = array();
// Editor blocks add 'figure' as the parent node for images. If this node has data-amp-layout then we should add this as the layout attribute.
$parent_node = $node->parentNode;
if ( 'figure' === $parent_node->tagName ) {
$parent_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $parent_node );
if ( isset( $parent_attributes['data-amp-layout'] ) ) {
$attributes['layout'] = $parent_attributes['data-amp-layout'];
}
if ( isset( $parent_attributes['data-amp-noloading'] ) && true === filter_var( $parent_attributes['data-amp-noloading'], FILTER_VALIDATE_BOOLEAN ) ) {
$attributes['noloading'] = $parent_attributes['data-amp-noloading'];
}
}
return $attributes;
}
/**
* Set AMP attributes.
*
* @param array $attributes Array of attributes.
* @param array $amp_data Array of AMP attributes.
* @return array Updated attributes.
*/
public function filter_data_amp_attributes( $attributes, $amp_data ) {
if ( isset( $amp_data['layout'] ) ) {
$attributes['data-amp-layout'] = $amp_data['layout'];
}
if ( isset( $amp_data['noloading'] ) ) {
$attributes['data-amp-noloading'] = '';
}
return $attributes;
}
/**
* Set attributes to node's parent element according to layout.
*
* @param DOMElement $node Node.
* @param array $new_attributes Attributes array.
* @param string $layout Layout.
* @return array New attributes.
*/
public function filter_attachment_layout_attributes( $node, $new_attributes, $layout ) {
// The width has to be unset / auto in case of fixed-height.
if ( 'fixed-height' === $layout ) {
if ( ! isset( $new_attributes['height'] ) ) {
$new_attributes['height'] = self::FALLBACK_HEIGHT;
}
$new_attributes['width'] = 'auto';
$node->parentNode->setAttribute( 'style', 'height: ' . $new_attributes['height'] . 'px; width: auto;' );
// The parent element should have width/height set and position set in case of 'fill'.
} elseif ( 'fill' === $layout ) {
if ( ! isset( $new_attributes['height'] ) ) {
$new_attributes['height'] = self::FALLBACK_HEIGHT;
}
$node->parentNode->setAttribute( 'style', 'position:relative; width: 100%; height: ' . $new_attributes['height'] . 'px;' );
unset( $new_attributes['width'] );
unset( $new_attributes['height'] );
} elseif ( 'responsive' === $layout ) {
$node->parentNode->setAttribute( 'style', 'position:relative; width: 100%; height: auto' );
} elseif ( 'fixed' === $layout ) {
if ( ! isset( $new_attributes['height'] ) ) {
$new_attributes['height'] = self::FALLBACK_HEIGHT;
}
}
return $new_attributes;
}
/**
* Add <amp-image-lightbox> element to body tag if it doesn't exist yet.
*/
public function maybe_add_amp_image_lightbox_node() {
$nodes = $this->dom->getElementById( self::AMP_IMAGE_LIGHTBOX_ID );
if ( null !== $nodes ) {
return;
}
$nodes = $this->dom->getElementsByTagName( 'body' );
if ( ! $nodes->length ) {
return;
}
$body_node = $nodes->item( 0 );
$amp_image_lightbox = AMP_DOM_Utils::create_node(
$this->dom,
'amp-image-lightbox',
array(
'id' => self::AMP_IMAGE_LIGHTBOX_ID,
'layout' => 'nodisplay',
'data-close-button-aria-label' => __( 'Close', 'amp' ),
)
);
$body_node->appendChild( $amp_image_lightbox );
}
}

View File

@ -0,0 +1,310 @@
<?php
/**
* Class AMP_Blacklist_Sanitizer
*
* @package AMP
*/
/**
* Strips blacklisted tags and attributes from content.
*
* See following for blacklist:
* https://github.com/ampproject/amphtml/blob/master/spec/amp-html-format.md#html-tags
*
* @since 0.5 This has been replaced by AMP_Tag_And_Attribute_Sanitizer but is kept around for back-compat.
* @deprecated
*/
class AMP_Blacklist_Sanitizer extends AMP_Base_Sanitizer {
const PATTERN_REL_WP_ATTACHMENT = '#wp-att-([\d]+)#';
/**
* Default args.
*
* @var array
*/
protected $DEFAULT_ARGS = array(
'add_blacklisted_protocols' => array(),
'add_blacklisted_tags' => array(),
'add_blacklisted_attributes' => array(),
);
/**
* Sanitize.
*/
public function sanitize() {
_deprecated_function( __METHOD__, '0.7', 'AMP_Tag_And_Attribute_Sanitizer::sanitize' );
$blacklisted_tags = $this->get_blacklisted_tags();
$blacklisted_attributes = $this->get_blacklisted_attributes();
$blacklisted_protocols = $this->get_blacklisted_protocols();
$body = $this->root_element;
$this->strip_tags( $body, $blacklisted_tags );
$this->strip_attributes_recursive( $body, $blacklisted_attributes, $blacklisted_protocols );
}
/**
* Strip attributes recursively.
*
* @param DOMNode $node DOM Node.
* @param array $bad_attributes Bad attributes.
* @param array $bad_protocols Bad protocols.
*/
private function strip_attributes_recursive( $node, $bad_attributes, $bad_protocols ) {
if ( XML_ELEMENT_NODE !== $node->nodeType ) {
return;
}
$node_name = $node->nodeName;
// Some nodes may contain valid content but are themselves invalid.
// Remove the node but preserve the children.
if ( 'font' === $node_name ) {
$this->replace_node_with_children( $node, $bad_attributes, $bad_protocols );
return;
} elseif ( 'a' === $node_name && false === $this->validate_a_node( $node ) ) {
$this->replace_node_with_children( $node, $bad_attributes, $bad_protocols );
return;
}
if ( $node->hasAttributes() ) {
$length = $node->attributes->length;
for ( $i = $length - 1; $i >= 0; $i-- ) {
$attribute = $node->attributes->item( $i );
$attribute_name = strtolower( $attribute->name );
if ( in_array( $attribute_name, $bad_attributes, true ) ) {
$this->remove_invalid_attribute( $node, $attribute_name );
continue;
}
// The on* attributes (like onclick) are a special case.
if ( 0 === stripos( $attribute_name, 'on' ) && 'on' !== $attribute_name ) {
$this->remove_invalid_attribute( $node, $attribute_name );
continue;
} elseif ( 'a' === $node_name ) {
$this->sanitize_a_attribute( $node, $attribute );
}
}
}
$length = $node->childNodes->length;
for ( $i = $length - 1; $i >= 0; $i-- ) {
$child_node = $node->childNodes->item( $i );
$this->strip_attributes_recursive( $child_node, $bad_attributes, $bad_protocols );
}
}
/**
* Strip tags.
*
* @param DOMElement $node Node.
* @param string[] $tag_names Tag names.
*/
private function strip_tags( $node, $tag_names ) {
foreach ( $tag_names as $tag_name ) {
$elements = $node->getElementsByTagName( $tag_name );
$length = $elements->length;
if ( 0 === $length ) {
continue;
}
for ( $i = $length - 1; $i >= 0; $i-- ) {
$element = $elements->item( $i );
$parent_node = $element->parentNode;
$this->remove_invalid_child( $element );
if ( 'body' !== $parent_node->nodeName && AMP_DOM_Utils::is_node_empty( $parent_node ) ) {
$this->remove_invalid_child( $parent_node );
}
}
}
}
/**
* Sanitize attribute.
*
* @param DOMElement $node Node.
* @param DOMAttr $attribute Attribute.
*/
private function sanitize_a_attribute( $node, $attribute ) {
$attribute_name = strtolower( $attribute->name );
if ( 'rel' === $attribute_name ) {
$old_value = $attribute->value;
$new_value = trim( preg_replace( self::PATTERN_REL_WP_ATTACHMENT, '', $old_value ) );
if ( empty( $new_value ) ) {
$this->remove_invalid_attribute( $node, $attribute_name );
} elseif ( $old_value !== $new_value ) {
$node->setAttribute( $attribute_name, $new_value );
}
} elseif ( 'rev' === $attribute_name ) {
// rev removed from HTML5 spec, which was used by Jetpack Markdown.
$this->remove_invalid_attribute( $node, $attribute_name );
} elseif ( 'target' === $attribute_name ) {
// _blank is the only allowed value and it must be lowercase.
// replace _new with _blank and others should simply be removed.
$old_value = strtolower( $attribute->value );
if ( '_blank' === $old_value || '_new' === $old_value ) {
// _new is not allowed; swap with _blank
$node->setAttribute( $attribute_name, '_blank' );
} else {
// Only _blank is allowed.
$this->remove_invalid_attribute( $node, $attribute_name );
}
}
}
/**
* Validate node.
*
* @param DOMElement $node Node.
* @return bool
*/
private function validate_a_node( $node ) {
// Get the href attribute.
$href = $node->getAttribute( 'href' );
if ( empty( $href ) ) {
/*
* If no href, check that a is an anchor or not.
* We don't need to validate anchors any further.
*/
return $node->hasAttribute( 'name' ) || $node->hasAttribute( 'id' );
}
// If this is an anchor link, just return true.
if ( 0 === strpos( $href, '#' ) ) {
return true;
}
// If the href starts with a '/', append the home_url to it for validation purposes.
if ( 0 === stripos( $href, '/' ) ) {
$href = untrailingslashit( get_home_url() ) . $href;
}
$valid_protocols = array( 'http', 'https', 'mailto', 'sms', 'tel', 'viber', 'whatsapp' );
$special_protocols = array( 'tel', 'sms' ); // These ones don't valid with `filter_var+FILTER_VALIDATE_URL`.
$protocol = strtok( $href, ':' );
if ( false === filter_var( $href, FILTER_VALIDATE_URL )
&& ! in_array( $protocol, $special_protocols, true ) ) {
return false;
}
if ( ! in_array( $protocol, $valid_protocols, true ) ) {
return false;
}
return true;
}
/**
* Replace node with children.
*
* @param DOMElement $node Node.
* @param array $bad_attributes Bad attributes.
* @param array $bad_protocols Bad protocols.
*/
private function replace_node_with_children( $node, $bad_attributes, $bad_protocols ) {
// If the node has children and also has a parent node,
// clone and re-add all the children just before current node.
if ( $node->hasChildNodes() && $node->parentNode ) {
foreach ( $node->childNodes as $child_node ) {
$new_child = $child_node->cloneNode( true );
$this->strip_attributes_recursive( $new_child, $bad_attributes, $bad_protocols );
$node->parentNode->insertBefore( $new_child, $node );
}
}
// Remove the node from the parent, if defined.
if ( $node->parentNode ) {
$this->remove_invalid_child( $node );
}
}
/**
* Merge defaults with args.
*
* @param string $key Key.
* @param array $values Values.
* @return array Merged args.
*/
private function merge_defaults_with_args( $key, $values ) {
// Merge default values with user specified args.
if ( ! empty( $this->args[ $key ] )
&& is_array( $this->args[ $key ] ) ) {
$values = array_merge( $values, $this->args[ $key ] );
}
return $values;
}
/**
* Get blacklisted protocols.
*
* @return array Protocols.
*/
private function get_blacklisted_protocols() {
return $this->merge_defaults_with_args(
'add_blacklisted_protocols',
array(
'javascript',
)
);
}
/**
* Get blacklisted tags.
*
* @return array Tags.
*/
private function get_blacklisted_tags() {
return $this->merge_defaults_with_args(
'add_blacklisted_tags',
array(
'script',
'noscript',
'style',
'frame',
'frameset',
'object',
'param',
'applet',
'form',
'label',
'input',
'textarea',
'select',
'option',
'link',
'picture',
// Sanitizers run after embed handlers, so if anything wasn't matched, it needs to be removed.
'embed',
'embedvideo',
// Other weird ones.
'comments-count',
)
);
}
/**
* Get blacklisted attributes.
*
* @return array Attributes.
*/
private function get_blacklisted_attributes() {
return $this->merge_defaults_with_args(
'add_blacklisted_attributes',
array(
'style',
'size',
'clear',
'align',
'valign',
)
);
}
}

View File

@ -0,0 +1,137 @@
<?php
/**
* Class AMP_Block_Sanitizer.
*
* @package AMP
*/
/**
* Class AMP_Block_Sanitizer
*
* Modifies elements created as blocks to match the blocks' AMP-specific configuration.
*/
class AMP_Block_Sanitizer extends AMP_Base_Sanitizer {
/**
* Tag.
*
* @var string Figure tag to identify wrapper around AMP elements.
* @since 1.0
*/
public static $tag = 'figure';
/**
* Sanitize the AMP elements contained by <figure> element where necessary.
*
* @since 0.2
*/
public function sanitize() {
$nodes = $this->dom->getElementsByTagName( self::$tag );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return;
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
// We are only looking for <figure> elements which have wp-block-embed as class.
$class = (string) $node->getAttribute( 'class' );
if ( false === strpos( $class, 'wp-block-embed' ) ) {
continue;
}
// Remove classes like wp-embed-aspect-16-9 since responsive layout is handled by AMP's layout system.
$node->setAttribute( 'class', preg_replace( '/(?<=^|\s)wp-embed-aspect-\d+-\d+(?=\s|$)/', '', $class ) );
// We're looking for <figure> elements that have one child node only.
if ( 1 !== count( $node->childNodes ) ) {
continue;
}
$attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
// We are looking for <figure> elements with layout attribute only.
if (
! isset( $attributes['data-amp-layout'] ) &&
! isset( $attributes['data-amp-noloading'] ) &&
! isset( $attributes['data-amp-lightbox'] )
) {
continue;
}
$amp_el_found = false;
foreach ( $node->childNodes as $child_node ) {
// We are looking for child elements which start with 'amp-'.
if ( 0 !== strpos( $child_node->tagName, 'amp-' ) ) {
continue;
}
$amp_el_found = true;
$this->set_attributes( $child_node, $node, $attributes );
}
if ( false === $amp_el_found ) {
continue;
}
$this->did_convert_elements = true;
}
}
/**
* Sets necessary attributes to both parent and AMP element node.
*
* @param DOMNode $node AMP element node.
* @param DOMNode $parent_node <figure> node.
* @param array $attributes Current attributes of the AMP element.
*/
protected function set_attributes( $node, $parent_node, $attributes ) {
if ( isset( $attributes['data-amp-layout'] ) ) {
$node->setAttribute( 'layout', $attributes['data-amp-layout'] );
}
if ( isset( $attributes['data-amp-noloading'] ) && true === filter_var( $attributes['data-amp-noloading'], FILTER_VALIDATE_BOOLEAN ) ) {
$node->setAttribute( 'noloading', '' );
}
$layout = $node->getAttribute( 'layout' );
// The width has to be unset / auto in case of fixed-height.
if ( 'fixed-height' === $layout ) {
if ( ! isset( $attributes['height'] ) ) {
$node->setAttribute( 'height', self::FALLBACK_HEIGHT );
}
$node->setAttribute( 'width', 'auto' );
$height = $node->getAttribute( 'height' );
if ( is_numeric( $height ) ) {
$height .= 'px';
}
$parent_node->setAttribute( 'style', "height: $height; width: auto;" );
// The parent element should have width/height set and position set in case of 'fill'.
} elseif ( 'fill' === $layout ) {
if ( ! isset( $attributes['height'] ) ) {
$attributes['height'] = self::FALLBACK_HEIGHT;
}
$parent_node->setAttribute( 'style', 'position:relative; width: 100%; height: ' . $attributes['height'] . 'px;' );
$node->removeAttribute( 'width' );
$node->removeAttribute( 'height' );
} elseif ( 'responsive' === $layout ) {
$parent_node->setAttribute( 'style', 'position:relative; width: 100%; height: auto' );
} elseif ( 'fixed' === $layout ) {
if ( ! isset( $attributes['height'] ) ) {
$node->setAttribute( 'height', self::FALLBACK_HEIGHT );
}
}
// Set the fallback layout in case needed.
$attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
$attributes = $this->set_layout( $attributes );
if ( $layout !== $attributes['layout'] ) {
$node->setAttribute( 'layout', $attributes['layout'] );
}
}
}

View File

@ -0,0 +1,218 @@
<?php
/**
* Class AMP_Comments_Sanitizer.
*
* @package AMP
*/
/**
* Class AMP_Comments_Sanitizer
*
* Strips and corrects attributes in forms.
*/
class AMP_Comments_Sanitizer extends AMP_Base_Sanitizer {
/**
* Default args.
*
* @since 1.1
*
* @var array
*/
protected $DEFAULT_ARGS = array(
'comment_live_list' => false,
);
/**
* Pre-process the comment form and comment list for AMP.
*
* @since 0.7
*/
public function sanitize() {
foreach ( $this->dom->getElementsByTagName( 'form' ) as $comment_form ) {
/**
* Comment form.
*
* @var DOMElement $comment_form
*/
$action = $comment_form->getAttribute( 'action-xhr' );
if ( ! $action ) {
$action = $comment_form->getAttribute( 'action' );
}
$action_path = wp_parse_url( $action, PHP_URL_PATH );
if ( preg_match( '#/wp-comments-post\.php$#', $action_path ) ) {
$this->process_comment_form( $comment_form );
}
}
if ( ! empty( $this->args['comments_live_list'] ) ) {
$xpath = new DOMXPath( $this->dom );
$comments = $xpath->query( '//amp-live-list/*[ @items ]/*[ starts-with( @id, "comment-" ) ]' );
foreach ( $comments as $comment ) {
$this->add_amp_live_list_comment_attributes( $comment );
}
}
}
/**
* Comment form.
*
* @since 0.7
*
* @param DOMElement $comment_form Comment form.
*/
protected function process_comment_form( $comment_form ) {
/**
* Element.
*
* @var DOMElement $element
*/
/**
* Named input elements.
*
* @var DOMElement[][] $form_fields
*/
$form_fields = array();
foreach ( $comment_form->getElementsByTagName( 'input' ) as $element ) {
$name = $element->getAttribute( 'name' );
if ( $name ) {
$form_fields[ $name ][] = $element;
}
}
foreach ( $comment_form->getElementsByTagName( 'textarea' ) as $element ) {
$name = $element->getAttribute( 'name' );
if ( $name ) {
$form_fields[ $name ][] = $element;
}
}
if ( empty( $form_fields['comment_post_ID'] ) ) {
return;
}
$post_id = (int) $form_fields['comment_post_ID'][0]->getAttribute( 'value' );
$state_id = AMP_Theme_Support::get_comment_form_state_id( $post_id );
$form_state = array(
'values' => array(),
'submitting' => false,
'replyToName' => '',
);
if ( ! empty( $form_fields['comment_parent'] ) ) {
$comment_id = (int) $form_fields['comment_parent'][0]->getAttribute( 'value' );
if ( $comment_id ) {
$reply_comment = get_comment( $comment_id );
if ( $reply_comment ) {
$form_state['replyToName'] = $reply_comment->comment_author;
}
}
}
$amp_bind_attr_format = AMP_DOM_Utils::get_amp_bind_placeholder_prefix() . '%s';
foreach ( $form_fields as $name => $form_field ) {
foreach ( $form_field as $element ) {
// @todo Radio and checkbox inputs are not supported yet.
if ( in_array( strtolower( $element->getAttribute( 'type' ) ), array( 'checkbox', 'radio' ), true ) ) {
continue;
}
$element->setAttribute( sprintf( $amp_bind_attr_format, 'disabled' ), "$state_id.submitting" );
if ( 'textarea' === strtolower( $element->nodeName ) ) {
$form_state['values'][ $name ] = $element->textContent;
$element->setAttribute( sprintf( $amp_bind_attr_format, 'text' ), "$state_id.values.$name" );
} else {
$form_state['values'][ $name ] = $element->hasAttribute( 'value' ) ? $element->getAttribute( 'value' ) : '';
$element->setAttribute( sprintf( $amp_bind_attr_format, 'value' ), "$state_id.values.$name" );
}
// Update the state in response to changing the input.
$element->setAttribute(
'on',
sprintf(
'change:AMP.setState( { %s: { values: { %s: event.value } } } )',
$state_id,
wp_json_encode( $name )
)
);
}
}
// Add amp-state to the document.
$amp_state = $this->dom->createElement( 'amp-state' );
$amp_state->setAttribute( 'id', $state_id );
$script = $this->dom->createElement( 'script' );
$script->setAttribute( 'type', 'application/json' );
$amp_state->appendChild( $script );
$script->appendChild( $this->dom->createTextNode( wp_json_encode( $form_state ) ) );
$comment_form->insertBefore( $amp_state, $comment_form->firstChild );
// Update state when submitting form.
$form_reset_state = $form_state;
unset(
$form_reset_state['values']['author'],
$form_reset_state['values']['email'],
$form_reset_state['values']['url']
);
$on = array(
// Disable the form when submitting.
sprintf(
'submit:AMP.setState( { %s: { submitting: true } } )',
wp_json_encode( $state_id )
),
// Re-enable the form fields when the submission fails.
sprintf(
'submit-error:AMP.setState( { %s: { submitting: false } } )',
wp_json_encode( $state_id )
),
// Reset the form to its initial state (with enabled form fields), except for the author, email, and url.
sprintf(
'submit-success:AMP.setState( { %s: %s } )',
$state_id,
wp_json_encode( $form_reset_state )
),
);
$comment_form->setAttribute( 'on', implode( ';', $on ) );
}
/**
* Add attributes to comment elements when comments are being presented in amp-live-list, when comments_live_list theme support flag is present.
*
* @since 1.1
*
* @param DOMElement $comment_element Comment element.
*/
protected function add_amp_live_list_comment_attributes( $comment_element ) {
$comment_id = (int) str_replace( 'comment-', '', $comment_element->getAttribute( 'id' ) );
if ( ! $comment_id ) {
return;
}
$comment_object = get_comment( $comment_id );
// Skip if the comment is not valid or the comment has a parent, since in that case it is not relevant for amp-live-list.
if ( ! ( $comment_object instanceof WP_Comment ) || $comment_object->comment_parent ) {
return;
}
$comment_element->setAttribute( 'data-sort-time', strtotime( $comment_object->comment_date ) );
$update_time = strtotime( $comment_object->comment_date );
// Ensure the top-level data-update-time reflects the max time of the comments in the thread.
$children = $comment_object->get_children(
array(
'format' => 'flat',
'hierarchical' => 'flat',
'orderby' => 'none',
)
);
foreach ( $children as $child_comment ) {
$update_time = max( strtotime( $child_comment->comment_date ), $update_time );
}
$comment_element->setAttribute( 'data-update-time', $update_time );
}
}

View File

@ -0,0 +1,47 @@
<?php
/**
* Class AMP_Embed_Sanitizer
*
* @package AMP
*/
/**
* Class AMP_Embed_Sanitizer
*
* Calls sanitize_raw_embeds method on embed handlers.
*/
class AMP_Embed_Sanitizer extends AMP_Base_Sanitizer {
/**
* Embed handlers.
*
* @var AMP_Base_Embed_Handler[] AMP_Base_Embed_Handler[]
*/
private $embed_handlers = array();
/**
* AMP_Embed_Sanitizer constructor.
*
* @param DOMDocument $dom DOM.
* @param array $args Args.
*/
public function __construct( $dom, $args = array() ) {
parent::__construct( $dom, $args );
if ( ! empty( $this->args['embed_handlers'] ) ) {
$this->embed_handlers = $this->args['embed_handlers'];
}
}
/**
* Checks if each embed_handler has sanitize_raw_method and calls it.
*/
public function sanitize() {
foreach ( $this->embed_handlers as $embed_handler ) {
if ( method_exists( $embed_handler, 'sanitize_raw_embeds' ) ) {
$embed_handler->sanitize_raw_embeds( $this->dom );
}
}
}
}

View File

@ -0,0 +1,146 @@
<?php
/**
* Class AMP_Form_Sanitizer.
*
* @package AMP
* @since 0.7
*/
/**
* Class AMP_Form_Sanitizer
*
* Strips and corrects attributes in forms.
*
* @since 0.7
*/
class AMP_Form_Sanitizer extends AMP_Base_Sanitizer {
/**
* Tag.
*
* @var string HTML <form> tag to identify and process.
*
* @since 0.7
*/
public static $tag = 'form';
/**
* Sanitize the <form> elements from the HTML contained in this instance's DOMDocument.
*
* @link https://www.ampproject.org/docs/reference/components/amp-form
* @since 0.7
*/
public function sanitize() {
/**
* Node list.
*
* @var DOMNodeList $node
*/
$nodes = $this->dom->getElementsByTagName( self::$tag );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return;
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
if ( ! $node instanceof DOMElement ) {
continue;
}
// In HTML, the default method is 'get'.
$method = 'get';
if ( $node->getAttribute( 'method' ) ) {
$method = strtolower( $node->getAttribute( 'method' ) );
} else {
$node->setAttribute( 'method', $method );
}
/*
* In HTML, the default action is just the current URL that the page is served from.
* The action "specifies a server endpoint to handle the form input. The value must be an
* https URL and must not be a link to a CDN".
*/
if ( ! $node->getAttribute( 'action' ) ) {
$action_url = esc_url_raw( '//' . $_SERVER['HTTP_HOST'] . wp_unslash( $_SERVER['REQUEST_URI'] ) );
} else {
$action_url = $node->getAttribute( 'action' );
// Check if action_url is a relative path and add the host to it.
if ( ! preg_match( '#^(https?:)?//#', $action_url ) ) {
$action_url = esc_url_raw( '//' . $_SERVER['HTTP_HOST'] . $action_url );
}
}
$xhr_action = $node->getAttribute( 'action-xhr' );
// Make HTTP URLs protocol-less, since HTTPS is required for forms.
if ( 'http://' === strtolower( substr( $action_url, 0, 7 ) ) ) {
$action_url = substr( $action_url, 5 );
}
/*
* According to the AMP spec:
* For GET submissions, provide at least one of action or action-xhr.
* This attribute is required for method=GET. For method=POST, the
* action attribute is invalid, use action-xhr instead.
*/
if ( 'get' === $method ) {
if ( $action_url !== $node->getAttribute( 'action' ) ) {
$node->setAttribute( 'action', $action_url );
}
} elseif ( 'post' === $method ) {
$node->removeAttribute( 'action' );
if ( ! $xhr_action ) {
// record that action was converted tp action-xhr.
$action_url = add_query_arg( '_wp_amp_action_xhr_converted', 1, $action_url );
$node->setAttribute( 'action-xhr', $action_url );
// Append error handler if not found.
$this->ensure_submit_error_element( $node );
} elseif ( 'http://' === substr( $xhr_action, 0, 7 ) ) {
$node->setAttribute( 'action-xhr', substr( $xhr_action, 5 ) );
}
}
/*
* The target "indicates where to display the form response after submitting the form.
* The value must be _blank or _top". The _self and _parent values are treated
* as synonymous with _top, and anything else is treated like _blank.
*/
$target = $node->getAttribute( 'target' );
if ( '_top' !== $target ) {
if ( ! $target || in_array( $target, array( '_self', '_parent' ), true ) ) {
$node->setAttribute( 'target', '_top' );
} elseif ( '_blank' !== $target ) {
$node->setAttribute( 'target', '_blank' );
}
}
}
}
/**
* Checks if the form has an error handler else create one if not.
*
* @link https://www.ampproject.org/docs/reference/components/amp-form#success/error-response-rendering
* @since 0.7
*
* @param DOMElement $form The form node to check.
*/
public function ensure_submit_error_element( $form ) {
$templates = $form->getElementsByTagName( 'template' );
for ( $i = $templates->length - 1; $i >= 0; $i-- ) {
if ( $templates->item( $i )->parentNode->hasAttribute( 'submit-error' ) ) {
return; // Found error template, do nothing.
}
}
$div = $this->dom->createElement( 'div' );
$template = $this->dom->createElement( 'template' );
$mustache = $this->dom->createTextNode( '{{{error}}}' );
$div->setAttribute( 'submit-error', '' );
$template->setAttribute( 'type', 'amp-mustache' );
$template->appendChild( $mustache );
$div->appendChild( $template );
$form->appendChild( $div );
}
}

View File

@ -0,0 +1,211 @@
<?php
/**
* Class AMP_Gallery_Block_Sanitizer.
*
* @package AMP
*/
/**
* Class AMP_Gallery_Block_Sanitizer
*
* Modifies gallery block to match the block's AMP-specific configuration.
*/
class AMP_Gallery_Block_Sanitizer extends AMP_Base_Sanitizer {
/**
* Value used for width of amp-carousel.
*
* @since 1.0
*
* @const int
*/
const FALLBACK_WIDTH = 600;
/**
* Value used for height of amp-carousel.
*
* @since 1.0
*
* @const int
*/
const FALLBACK_HEIGHT = 480;
/**
* Tag.
*
* @since 1.0
*
* @var string Ul tag to identify wrapper around gallery block.
*/
public static $tag = 'ul';
/**
* Expected class of the wrapper around the gallery block.
*
* @since 1.0
*
* @var string
*/
public static $class = 'wp-block-gallery';
/**
* Array of flags used to control sanitization.
*
* @var array {
* @type int $content_max_width Max width of content.
* @type bool $carousel_required Whether carousels are required. This is used when amp theme support is not present, for back-compat.
* }
*/
protected $args;
/**
* Default args.
*
* @var array
*/
protected $DEFAULT_ARGS = array(
'carousel_required' => false,
);
/**
* Sanitize the gallery block contained by <ul> element where necessary.
*
* @since 0.2
*/
public function sanitize() {
$nodes = $this->dom->getElementsByTagName( self::$tag );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return;
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
// We're looking for <ul> elements that have at least one child and the proper class.
if ( 0 === count( $node->childNodes ) || false === strpos( $node->getAttribute( 'class' ), self::$class ) ) {
continue;
}
$attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
$is_amp_lightbox = isset( $attributes['data-amp-lightbox'] ) && true === filter_var( $attributes['data-amp-lightbox'], FILTER_VALIDATE_BOOLEAN );
$is_amp_carousel = ! empty( $this->args['carousel_required'] ) || ( isset( $attributes['data-amp-carousel'] ) && true === filter_var( $attributes['data-amp-carousel'], FILTER_VALIDATE_BOOLEAN ) );
// We are only looking for <ul> elements which have amp-carousel / amp-lightbox true.
if ( ! $is_amp_carousel && ! $is_amp_lightbox ) {
continue;
}
// If lightbox is set, we should add lightbox feature to the gallery images.
if ( $is_amp_lightbox ) {
$this->add_lightbox_attributes_to_image_nodes( $node );
$this->maybe_add_amp_image_lightbox_node();
}
// If amp-carousel is not set, nothing else to do here.
if ( ! $is_amp_carousel ) {
continue;
}
$images = array();
// If it's not AMP lightbox, look for links first.
if ( ! $is_amp_lightbox ) {
foreach ( $node->getElementsByTagName( 'a' ) as $element ) {
$images[] = $element;
}
}
// If not linking to anything then look for <amp-img>.
if ( empty( $images ) ) {
foreach ( $node->getElementsByTagName( 'amp-img' ) as $element ) {
$images[] = $element;
}
}
// Skip if no images found.
if ( empty( $images ) ) {
continue;
}
$amp_carousel = AMP_DOM_Utils::create_node(
$this->dom,
'amp-carousel',
array(
'height' => $this->get_carousel_height( $node ),
'type' => 'slides',
'layout' => 'fixed-height',
)
);
foreach ( $images as $image ) {
$amp_carousel->appendChild( $image );
}
$node->parentNode->replaceChild( $amp_carousel, $node );
}
$this->did_convert_elements = true;
}
/**
* Get carousel height by containing images.
*
* @param DOMElement $element The UL element.
* @return int Height.
*/
protected function get_carousel_height( $element ) {
$images = $element->getElementsByTagName( 'amp-img' );
$num_images = $images->length;
$max_height = 0;
$max_width = 0;
if ( 0 === $num_images ) {
return self::FALLBACK_HEIGHT;
}
foreach ( $images as $image ) {
/**
* Image.
*
* @var DOMElement $image
*/
$image_height = $image->getAttribute( 'height' );
if ( is_numeric( $image_height ) ) {
$max_height = max( $max_height, $image_height );
}
$image_width = $image->getAttribute( 'height' );
if ( is_numeric( $image_width ) ) {
$max_width = max( $max_width, $image_width );
}
}
if ( ! empty( $this->args['content_max_width'] ) && $max_height > 0 && $max_width > $this->args['content_max_width'] ) {
$max_height = ( $max_width * $this->args['content_max_width'] ) / $max_height;
}
return ! $max_height ? self::FALLBACK_HEIGHT : $max_height;
}
/**
* Set lightbox related attributes to <amp-img> within gallery.
*
* @param DOMElement $element The UL element.
*/
protected function add_lightbox_attributes_to_image_nodes( $element ) {
$images = $element->getElementsByTagName( 'amp-img' );
$num_images = $images->length;
if ( 0 === $num_images ) {
return;
}
$attributes = array(
'data-amp-lightbox' => '',
'on' => 'tap:' . self::AMP_IMAGE_LIGHTBOX_ID,
'role' => 'button',
'tabindex' => 0,
);
for ( $j = $num_images - 1; $j >= 0; $j-- ) {
$image_node = $images->item( $j );
foreach ( $attributes as $att => $value ) {
$image_node->setAttribute( $att, $value );
}
}
}
}

View File

@ -0,0 +1,218 @@
<?php
/**
* Class AMP_Iframe_Sanitizer
*
* @package AMP
*/
/**
* Class AMP_Iframe_Sanitizer
*
* Converts <iframe> tags to <amp-iframe>
*/
class AMP_Iframe_Sanitizer extends AMP_Base_Sanitizer {
use AMP_Noscript_Fallback;
/**
* Value used for height attribute when $attributes['height'] is empty.
*
* @since 0.2
*
* @const int
*/
const FALLBACK_HEIGHT = 400;
/**
* Default values for sandboxing IFrame.
*
* @since 0.2
*
* @const int
*/
const SANDBOX_DEFAULTS = 'allow-scripts allow-same-origin';
/**
* Tag.
*
* @var string HTML <iframe> tag to identify and replace with AMP version.
*
* @since 0.2
*/
public static $tag = 'iframe';
/**
* Default args.
*
* @var array
*/
protected $DEFAULT_ARGS = array(
'add_placeholder' => false,
'add_noscript_fallback' => true,
);
/**
* Get mapping of HTML selectors to the AMP component selectors which they may be converted into.
*
* @return array Mapping.
*/
public function get_selector_conversion_mapping() {
return array(
'iframe' => array(
'amp-iframe',
),
);
}
/**
* Sanitize the <iframe> elements from the HTML contained in this instance's DOMDocument.
*
* @since 0.2
*/
public function sanitize() {
$nodes = $this->dom->getElementsByTagName( self::$tag );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return;
}
if ( $this->args['add_noscript_fallback'] ) {
$this->initialize_noscript_allowed_attributes( self::$tag );
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
// Skip element if already inside of an AMP element as a noscript fallback.
if ( $this->is_inside_amp_noscript( $node ) ) {
continue;
}
$normalized_attributes = $this->normalize_attributes( AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ) );
/**
* If the src doesn't exist, remove the node. Either it never
* existed or was invalidated while filtering attributes above.
*
* @todo: add a filter to allow for a fallback element in this instance.
* @see: https://github.com/ampproject/amphtml/issues/2261
*/
if ( empty( $normalized_attributes['src'] ) ) {
$this->remove_invalid_child( $node );
continue;
}
$this->did_convert_elements = true;
$normalized_attributes = $this->set_layout( $normalized_attributes );
if ( empty( $normalized_attributes['layout'] ) && ! empty( $normalized_attributes['width'] ) && ! empty( $normalized_attributes['height'] ) ) {
$normalized_attributes['layout'] = 'intrinsic';
$this->add_or_append_attribute( $normalized_attributes, 'class', 'amp-wp-enforced-sizes' );
}
$new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-iframe', $normalized_attributes );
if ( true === $this->args['add_placeholder'] ) {
$placeholder_node = $this->build_placeholder( $normalized_attributes );
$new_node->appendChild( $placeholder_node );
}
$node->parentNode->replaceChild( $new_node, $node );
if ( $this->args['add_noscript_fallback'] ) {
$node->setAttribute( 'src', $normalized_attributes['src'] );
// Preserve original node in noscript for no-JS environments.
$this->append_old_node_noscript( $new_node, $node, $this->dom );
}
}
}
/**
* Normalize HTML attributes for <amp-iframe> elements.
*
* @param string[] $attributes {
* Attributes.
*
* @type string $src IFrame URL - Empty if HTTPS required per $this->args['require_https_src']
* @type int $width <iframe> width attribute - Set to numeric value if px or %
* @type int $height <iframe> width attribute - Set to numeric value if px or %
* @type string $sandbox <iframe> `sandbox` attribute - Pass along if found; default to value of self::SANDBOX_DEFAULTS
* @type string $class <iframe> `class` attribute - Pass along if found
* @type string $sizes <iframe> `sizes` attribute - Pass along if found
* @type string $id <iframe> `id` attribute - Pass along if found
* @type int $frameborder <iframe> `frameborder` attribute - Filter to '0' or '1'; default to '0'
* @type bool $allowfullscreen <iframe> `allowfullscreen` attribute - Convert 'false' to empty string ''
* @type bool $allowtransparency <iframe> `allowtransparency` attribute - Convert 'false' to empty string ''
* }
* @return array Returns HTML attributes; normalizes src, dimensions, frameborder, sandox, allowtransparency and allowfullscreen
*/
private function normalize_attributes( $attributes ) {
$out = array();
foreach ( $attributes as $name => $value ) {
switch ( $name ) {
case 'src':
$out[ $name ] = $this->maybe_enforce_https_src( $value, true );
break;
case 'width':
case 'height':
$out[ $name ] = $this->sanitize_dimension( $value, $name );
break;
case 'frameborder':
if ( '0' !== $value && '1' !== $value ) {
$value = '0';
}
$out[ $name ] = $value;
break;
case 'allowfullscreen':
case 'allowtransparency':
if ( 'false' !== $value ) {
$out[ $name ] = '';
}
break;
default:
$out[ $name ] = $value;
break;
}
}
if ( ! isset( $out['sandbox'] ) ) {
$out['sandbox'] = self::SANDBOX_DEFAULTS;
}
return $out;
}
/**
* Builds a DOMElement to use as a placeholder for an <iframe>.
*
* Important: The element returned must not be block-level (e.g. div) as the PHP DOM parser
* will move it out from inside any containing paragraph. So this is why a span is used.
*
* @since 0.2
*
* @param string[] $parent_attributes {
* Attributes.
*
* @type string $placeholder AMP HTML <amp-iframe> `placeholder` attribute; default to 'amp-wp-iframe-placeholder'
* @type string $class AMP HTML <amp-iframe> `class` attribute; default to 'amp-wp-iframe-placeholder'
* }
* @return DOMElement|false
*/
private function build_placeholder( $parent_attributes ) {
$placeholder_node = AMP_DOM_Utils::create_node(
$this->dom,
'span',
array(
'placeholder' => '',
'class' => 'amp-wp-iframe-placeholder',
)
);
return $placeholder_node;
}
}

View File

@ -0,0 +1,357 @@
<?php
/**
* Class AMP_Img_Sanitizer.
*
* @package AMP
*/
/**
* Class AMP_Img_Sanitizer
*
* Converts <img> tags to <amp-img> or <amp-anim>
*/
class AMP_Img_Sanitizer extends AMP_Base_Sanitizer {
use AMP_Noscript_Fallback;
/**
* Value used for width attribute when $attributes['width'] is empty.
*
* @since 0.2
*
* @const int
*/
const FALLBACK_WIDTH = 600;
/**
* Value used for height attribute when $attributes['height'] is empty.
*
* @since 0.2
*
* @const int
*/
const FALLBACK_HEIGHT = 400;
/**
* Tag.
*
* @var string HTML <img> tag to identify and replace with AMP version.
*
* @since 0.2
*/
public static $tag = 'img';
/**
* Default args.
*
* @var array
*/
protected $DEFAULT_ARGS = array(
'add_noscript_fallback' => true,
);
/**
* Animation extension.
*
* @var string
*/
private static $anim_extension = '.gif';
/**
* Get mapping of HTML selectors to the AMP component selectors which they may be converted into.
*
* @return array Mapping.
*/
public function get_selector_conversion_mapping() {
return array(
'img' => array(
'amp-img',
'amp-anim',
),
);
}
/**
* Sanitize the <img> elements from the HTML contained in this instance's DOMDocument.
*
* @since 0.2
*/
public function sanitize() {
/**
* Node list.
*
* @var DOMNodeList $node
*/
$nodes = $this->dom->getElementsByTagName( self::$tag );
$need_dimensions = array();
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return;
}
if ( $this->args['add_noscript_fallback'] ) {
$this->initialize_noscript_allowed_attributes( self::$tag );
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
if ( ! $node instanceof DOMElement ) {
continue;
}
// Skip element if already inside of an AMP element as a noscript fallback.
if ( $this->is_inside_amp_noscript( $node ) ) {
continue;
}
if ( ! $node->hasAttribute( 'src' ) || '' === trim( $node->getAttribute( 'src' ) ) ) {
$this->remove_invalid_child( $node );
continue;
}
// Determine which images need their dimensions determined/extracted.
if ( ! is_numeric( $node->getAttribute( 'width' ) ) || ! is_numeric( $node->getAttribute( 'height' ) ) ) {
$need_dimensions[ $node->getAttribute( 'src' ) ][] = $node;
} else {
$this->adjust_and_replace_node( $node );
}
}
$this->determine_dimensions( $need_dimensions );
$this->adjust_and_replace_nodes_in_array_map( $need_dimensions );
/*
* Opt-in to amp-img-auto-sizes experiment.
* This is needed because the sizes attribute is removed from all img elements converted to amp-img
* in order to prevent the undesirable setting of the width. This $meta tag can be removed once the
* experiment ends (and the feature has been fully launched).
* See <https://github.com/ampproject/amphtml/issues/21371> and <https://github.com/ampproject/amp-wp/pull/2036>.
*/
$head = $this->dom->getElementsByTagName( 'head' )->item( 0 );
if ( $head ) {
$meta = $this->dom->createElement( 'meta' );
$meta->setAttribute( 'name', 'amp-experiments-opt-in' );
$meta->setAttribute( 'content', 'amp-img-auto-sizes' );
$head->insertBefore( $meta, $head->firstChild );
}
}
/**
* "Filter" HTML attributes for <amp-anim> elements.
*
* @since 0.2
*
* @param string[] $attributes {
* Attributes.
*
* @type string $src Image URL - Pass along if found
* @type string $alt <img> `alt` attribute - Pass along if found
* @type string $class <img> `class` attribute - Pass along if found
* @type string $srcset <img> `srcset` attribute - Pass along if found
* @type string $sizes <img> `sizes` attribute - Pass along if found
* @type string $on <img> `on` attribute - Pass along if found
* @type string $attribution <img> `attribution` attribute - Pass along if found
* @type int $width <img> width attribute - Set to numeric value if px or %
* @type int $height <img> width attribute - Set to numeric value if px or %
* }
* @return array Returns HTML attributes; removes any not specifically declared above from input.
*/
private function filter_attributes( $attributes ) {
$out = array();
foreach ( $attributes as $name => $value ) {
switch ( $name ) {
case 'width':
case 'height':
$out[ $name ] = $this->sanitize_dimension( $value, $name );
break;
case 'data-amp-layout':
$out['layout'] = $value;
break;
case 'data-amp-noloading':
$out['noloading'] = $value;
break;
default:
$out[ $name ] = $value;
break;
}
}
return $out;
}
/**
* Determine width and height attribute values for images without them.
*
* Attempt to determine actual dimensions, otherwise set reasonable defaults.
*
* @param DOMElement[][] $need_dimensions Map <img> @src URLs to node for images with missing dimensions.
*/
private function determine_dimensions( $need_dimensions ) {
$dimensions_by_url = AMP_Image_Dimension_Extractor::extract( array_keys( $need_dimensions ) );
foreach ( $dimensions_by_url as $url => $dimensions ) {
foreach ( $need_dimensions[ $url ] as $node ) {
if ( ! $node instanceof DOMElement ) {
continue;
}
$class = $node->getAttribute( 'class' );
if ( ! $class ) {
$class = '';
}
if ( ! $dimensions ) {
$class .= ' amp-wp-unknown-size';
}
$width = isset( $this->args['content_max_width'] ) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH;
$height = self::FALLBACK_HEIGHT;
if ( isset( $dimensions['width'] ) ) {
$width = $dimensions['width'];
}
if ( isset( $dimensions['height'] ) ) {
$height = $dimensions['height'];
}
if ( ! is_numeric( $node->getAttribute( 'width' ) ) ) {
// Let width have the right aspect ratio based on the height attribute.
if ( is_numeric( $node->getAttribute( 'height' ) ) && isset( $dimensions['height'] ) && isset( $dimensions['width'] ) ) {
$width = ( floatval( $node->getAttribute( 'height' ) ) * $dimensions['width'] ) / $dimensions['height'];
}
$node->setAttribute( 'width', $width );
if ( ! isset( $dimensions['width'] ) ) {
$class .= ' amp-wp-unknown-width';
}
}
if ( ! is_numeric( $node->getAttribute( 'height' ) ) ) {
// Let height have the right aspect ratio based on the width attribute.
if ( is_numeric( $node->getAttribute( 'width' ) ) && isset( $dimensions['width'] ) && isset( $dimensions['height'] ) ) {
$height = ( floatval( $node->getAttribute( 'width' ) ) * $dimensions['height'] ) / $dimensions['width'];
}
$node->setAttribute( 'height', $height );
if ( ! isset( $dimensions['height'] ) ) {
$class .= ' amp-wp-unknown-height';
}
}
$node->setAttribute( 'class', trim( $class ) );
}
}
}
/**
* Now that all images have width and height attributes, make final tweaks and replace original image nodes
*
* @param DOMNodeList[] $node_lists Img DOM nodes (now with width and height attributes).
*/
private function adjust_and_replace_nodes_in_array_map( $node_lists ) {
foreach ( $node_lists as $node_list ) {
foreach ( $node_list as $node ) {
$this->adjust_and_replace_node( $node );
}
}
}
/**
* Make final modifications to DOMNode
*
* @param DOMElement $node The img element to adjust and replace.
*/
private function adjust_and_replace_node( $node ) {
$amp_data = $this->get_data_amp_attributes( $node );
$old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
$old_attributes = $this->filter_data_amp_attributes( $old_attributes, $amp_data );
$old_attributes = $this->maybe_add_lightbox_attributes( $old_attributes, $node );
$new_attributes = $this->filter_attributes( $old_attributes );
$layout = isset( $amp_data['layout'] ) ? $amp_data['layout'] : false;
$new_attributes = $this->filter_attachment_layout_attributes( $node, $new_attributes, $layout );
$this->add_or_append_attribute( $new_attributes, 'class', 'amp-wp-enforced-sizes' );
if ( empty( $new_attributes['layout'] ) && ! empty( $new_attributes['height'] ) && ! empty( $new_attributes['width'] ) ) {
// Use responsive images when a theme supports wide and full-bleed images.
if ( ! empty( $this->args['align_wide_support'] ) && $node->parentNode && 'figure' === $node->parentNode->nodeName && preg_match( '/(^|\s)(alignwide|alignfull)(\s|$)/', $node->parentNode->getAttribute( 'class' ) ) ) {
$new_attributes['layout'] = 'responsive';
} else {
$new_attributes['layout'] = 'intrinsic';
}
}
// Remove sizes attribute since it causes headaches in AMP and because AMP will generate it for us. See <https://github.com/ampproject/amphtml/issues/21371>.
unset( $new_attributes['sizes'] );
if ( $this->is_gif_url( $new_attributes['src'] ) ) {
$this->did_convert_elements = true;
$new_tag = 'amp-anim';
} else {
$new_tag = 'amp-img';
}
$img_node = AMP_DOM_Utils::create_node( $this->dom, $new_tag, $new_attributes );
$node->parentNode->replaceChild( $img_node, $node );
$can_include_noscript = (
$this->args['add_noscript_fallback']
&&
( $node->hasAttribute( 'src' ) && ! preg_match( '/^http:/', $node->getAttribute( 'src' ) ) )
&&
( ! $node->hasAttribute( 'srcset' ) || ! preg_match( '/http:/', $node->getAttribute( 'srcset' ) ) )
);
if ( $can_include_noscript ) {
// Preserve original node in noscript for no-JS environments.
$this->append_old_node_noscript( $img_node, $node, $this->dom );
}
}
/**
* Set lightbox attributes.
*
* @param array $attributes Array of attributes.
* @param DomNode $node Array of AMP attributes.
* @return array Updated attributes.
*/
private function maybe_add_lightbox_attributes( $attributes, $node ) {
$parent_node = $node->parentNode;
if ( ! ( $parent_node instanceof DOMElement ) || 'figure' !== $parent_node->tagName ) {
return $attributes;
}
$parent_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $parent_node );
if ( isset( $parent_attributes['data-amp-lightbox'] ) && true === filter_var( $parent_attributes['data-amp-lightbox'], FILTER_VALIDATE_BOOLEAN ) ) {
$attributes['data-amp-lightbox'] = '';
$attributes['on'] = 'tap:' . self::AMP_IMAGE_LIGHTBOX_ID;
$attributes['role'] = 'button';
$attributes['tabindex'] = 0;
$this->maybe_add_amp_image_lightbox_node();
}
return $attributes;
}
/**
* Determines is a URL is considered a GIF URL
*
* @since 0.2
*
* @param string $url URL to inspect for GIF vs. JPEG or PNG.
*
* @return bool Returns true if $url ends in `.gif`
*/
private function is_gif_url( $url ) {
$ext = self::$anim_extension;
$path = wp_parse_url( $url, PHP_URL_PATH );
return substr( $path, -strlen( $ext ) ) === $ext;
}
}

View File

@ -0,0 +1,164 @@
<?php
/**
* Class AMP_Nav_Menu_Dropdown_Sanitizer
*
* @package AMP
*/
/**
* Class AMP_Nav_Menu_Dropdown_Sanitizer
*
* Handles state for navigation menu dropdown toggles, based on theme support.
*
* @since 1.1.0
*/
class AMP_Nav_Menu_Dropdown_Sanitizer extends AMP_Base_Sanitizer {
/**
* Default args.
*
* @since 1.1.0
* @var array
*/
protected $DEFAULT_ARGS = array(
'sub_menu_button_class' => '',
'sub_menu_button_toggle_class' => '',
'expand_text' => '',
'collapse_text' => '',
'icon' => null, // Optional.
'sub_menu_item_state_id' => 'navMenuItemExpanded',
);
/**
* AMP_Nav_Menu_Dropdown_Sanitizer constructor.
*
* @since 1.1.0
*
* @param DOMDocument $dom DOM.
* @param array $args Args.
*/
public function __construct( $dom, $args = array() ) {
parent::__construct( $dom, $args );
$this->args = self::ensure_defaults( $this->args );
}
/**
* Add filter to manipulate output during output buffering to add AMP-compatible dropdown toggles.
*
* @since 1.0
*
* @param array $args Args.
*/
public static function add_buffering_hooks( $args = array() ) {
if ( empty( $args['sub_menu_button_class'] ) || empty( $args['sub_menu_button_toggle_class'] ) ) {
return;
}
$args = self::ensure_defaults( $args );
/**
* Filter the HTML output of a nav menu item to add the AMP dropdown button to reveal the sub-menu.
*
* @param string $item_output Nav menu item HTML.
* @param object $item Nav menu item.
* @return string Modified nav menu item HTML.
*/
add_filter(
'walker_nav_menu_start_el',
function( $item_output, $item, $depth, $nav_menu_args ) use ( $args ) {
unset( $depth );
// Skip adding buttons to nav menu widgets for now.
if ( empty( $nav_menu_args->theme_location ) ) {
return $item_output;
}
if ( ! in_array( 'menu-item-has-children', $item->classes, true ) ) {
return $item_output;
}
static $nav_menu_item_number = 0;
$nav_menu_item_number++;
$expanded = in_array( 'current-menu-ancestor', $item->classes, true );
$expanded_state_id = $args['nav_menu_item_state_id'] . $nav_menu_item_number;
// Create new state for managing storing the whether the sub-menu is expanded.
$item_output .= sprintf(
'<amp-state id="%s"><script type="application/json">%s</script></amp-state>',
esc_attr( $expanded_state_id ),
wp_json_encode( $expanded )
);
$dropdown_button = '<button';
$dropdown_button .= sprintf(
' class="%s" [class]="%s"',
esc_attr( $args['sub_menu_button_class'] . ( $expanded ? ' ' . $args['sub_menu_button_toggle_class'] : '' ) ),
esc_attr( sprintf( "%s + ( $expanded_state_id ? %s : '' )", wp_json_encode( $args['sub_menu_button_class'] ), wp_json_encode( ' ' . $args['sub_menu_button_toggle_class'] ) ) )
);
$dropdown_button .= sprintf(
' aria-expanded="%s" [aria-expanded]="%s"',
esc_attr( wp_json_encode( $expanded ) ),
esc_attr( "$expanded_state_id ? 'true' : 'false'" )
);
$dropdown_button .= sprintf(
' on="%s"',
esc_attr( "tap:AMP.setState( { $expanded_state_id: ! $expanded_state_id } )" )
);
$dropdown_button .= '>';
if ( isset( $args['icon'] ) ) {
$dropdown_button .= $args['icon'];
}
if ( isset( $args['expand_text'] ) && isset( $args['collapse_text'] ) ) {
$dropdown_button .= sprintf(
'<span class="screen-reader-text" [text]="%s">%s</span>',
esc_attr( sprintf( "$expanded_state_id ? %s : %s", wp_json_encode( $args['collapse_text'] ), wp_json_encode( $args['expand_text'] ) ) ),
esc_html( $expanded ? $args['collapse_text'] : $args['expand_text'] )
);
}
$dropdown_button .= '</button>';
$item_output .= $dropdown_button;
return $item_output;
},
10,
4
);
}
/**
* Method needs to be stubbed to fulfill base class requirements.
*
* @since 1.1.0
*/
public function sanitize() {
// Empty method body.
}
/**
* Ensure that some defaults are always set as fallback.
*
* @param array $args Arguments to set the defaults in as necessary.
* @return array Arguments with defaults filled.
*/
protected static function ensure_defaults( $args ) {
// Ensure accessibility labels are always set.
if ( empty( $args['expand_text'] ) ) {
$args['expand_text'] = __( 'expand child menu', 'amp' );
}
if ( empty( $args['collapse_text'] ) ) {
$args['collapse_text'] = __( 'collapse child menu', 'amp' );
}
// Ensure the state ID is always set.
if ( empty( $args['nav_menu_item_state_id'] ) ) {
$args['nav_menu_item_state_id'] = 'navMenuItemExpanded';
}
return $args;
}
}

View File

@ -0,0 +1,152 @@
<?php
/**
* Class AMP_Nav_Menu_Toggle_Sanitizer
*
* @package AMP
*/
/**
* Class AMP_Nav_Menu_Toggle_Sanitizer
*
* Handles state for navigation menu toggles, based on theme support.
*
* @since 1.1.0
*/
class AMP_Nav_Menu_Toggle_Sanitizer extends AMP_Base_Sanitizer {
/**
* Default args.
*
* @since 1.1.0
* @var array
*/
protected $DEFAULT_ARGS = array(
'nav_container_id' => '',
'nav_container_xpath' => '', // Alternative for 'nav_container_id', if no ID available.
'menu_button_id' => '',
'menu_button_xpath' => '', // Alternative for 'menu_button_id', if no ID available.
'nav_container_toggle_class' => '',
'menu_button_toggle_class' => '', // Optional.
'nav_menu_toggle_state_id' => 'navMenuToggledOn',
);
/**
* XPath.
*
* @since 1.1.0
* @var DOMXPath
*/
protected $xpath;
/**
* AMP_Nav_Menu_Toggle_Sanitizer constructor.
*
* @since 1.1.0
*
* @param DOMDocument $dom DOM.
* @param array $args Args.
*/
public function __construct( $dom, $args = array() ) {
parent::__construct( $dom, $args );
// Ensure the state ID is always set.
if ( empty( $this->args['nav_menu_toggle_state_id'] ) ) {
$this->args['nav_menu_toggle_state_id'] = $this->DEFAULT_ARGS['nav_menu_toggle_state_id'];
}
}
/**
* If supported per the constructor arguments, inject `amp-state` and bind dynamic classes accordingly.
*
* @since 1.1.0
*/
public function sanitize() {
$this->xpath = new DOMXPath( $this->dom );
$nav_el = $this->get_nav_container();
$button_el = $this->get_menu_button();
// If no navigation element or no toggle class provided, bail.
if ( ! $nav_el || empty( $this->args['nav_container_toggle_class'] ) ) {
if ( $button_el ) {
// Remove the button since it won't be used.
$button_el->parentNode->removeChild( $button_el );
}
return;
}
if ( ! $button_el ) {
return;
}
$state_id = 'navMenuToggledOn';
$expanded = false;
$nav_el->setAttribute(
AMP_DOM_Utils::get_amp_bind_placeholder_prefix() . 'class',
sprintf(
"%s + ( $state_id ? %s : '' )",
wp_json_encode( $nav_el->getAttribute( 'class' ) ),
wp_json_encode( ' ' . $this->args['nav_container_toggle_class'] )
)
);
$state_el = $this->dom->createElement( 'amp-state' );
$state_el->setAttribute( 'id', $state_id );
$script_el = $this->dom->createElement( 'script' );
$script_el->setAttribute( 'type', 'application/json' );
$script_el->appendChild( $this->dom->createTextNode( wp_json_encode( $expanded ) ) );
$state_el->appendChild( $script_el );
$nav_el->parentNode->insertBefore( $state_el, $nav_el );
$button_on = sprintf( "tap:AMP.setState({ $state_id: ! $state_id })" );
$button_el->setAttribute( 'on', $button_on );
$button_el->setAttribute( 'aria-expanded', 'false' );
$button_el->setAttribute( AMP_DOM_Utils::get_amp_bind_placeholder_prefix() . 'aria-expanded', "$state_id ? 'true' : 'false'" );
if ( ! empty( $this->args['menu_button_toggle_class'] ) ) {
$button_el->setAttribute(
AMP_DOM_Utils::get_amp_bind_placeholder_prefix() . 'class',
sprintf( "%s + ( $state_id ? %s : '' )", wp_json_encode( $button_el->getAttribute( 'class' ) ), wp_json_encode( ' ' . $this->args['menu_button_toggle_class'] ) )
);
}
}
/**
* Retrieves the navigation container element.
*
* @since 1.1.0
*
* @return DOMElement|null Navigation container element, or null if not provided or found.
*/
protected function get_nav_container() {
if ( ! empty( $this->args['nav_container_id'] ) ) {
return $this->dom->getElementById( $this->args['nav_container_id'] );
}
if ( ! empty( $this->args['nav_container_xpath'] ) ) {
return $this->xpath->query( $this->args['nav_container_xpath'] )->item( 0 );
}
return null;
}
/**
* Retrieves the navigation menu button element.
*
* @since 1.1.0
*
* @return DOMElement|null Navigation menu button element, or null if not provided or found.
*/
protected function get_menu_button() {
if ( ! empty( $this->args['menu_button_id'] ) ) {
return $this->dom->getElementById( $this->args['menu_button_id'] );
}
if ( ! empty( $this->args['menu_button_xpath'] ) ) {
return $this->xpath->query( $this->args['menu_button_xpath'] )->item( 0 );
}
return null;
}
}

View File

@ -0,0 +1,141 @@
<?php
/**
* Class AMP_O2_Player_Sanitizer
*
* @package AMP
*/
/**
* Class AMP_O2_Player_Sanitizer
*
* Converts <div class="vdb_player><script></script></div> embed to <amp-o2-player>
*
* @since 1.0
* @see https://www.ampproject.org/docs/reference/components/amp-o2-player
*/
class AMP_O2_Player_Sanitizer extends AMP_Base_Sanitizer {
/**
* Pattern to extract the information required for amp-o2-player element: data-pid, data-vid, data-bcid.
*
* @since 1.0
*/
const URL_PATTERN = '#.*delivery.vidible.tv\/jsonp\/pid=(?<data_pid>.*)\/vid=(?<data_vid>.*)\/(?<data_bcid>.*).js.*#i';
/**
* AMP Tag.
*
* @since 1.0
* @var string AMP Tag.
*/
private static $amp_tag = 'amp-o2-player';
/**
* Amp O2 Player class.
*
* @since 1.0
* @var string CSS class to identify O2 Player <div> to replace with AMP version.
*/
private static $xpath_selector = '//div[ contains( @class, \'vdb_player\' ) ]/script';
/**
* Height to set for O2 Player elements.
*
* @since 1.0
* @var string
*/
private static $height = '270';
/**
* Width to set for O2 Player elements.
*
* @since 1.0
* @var string
*/
private static $width = '480';
/**
* Sanitize the O2 Player elements from the HTML contained in this instance's DOMDocument.
*
* @since 1.0
*/
public function sanitize() {
/**
* XPath.
*
* @var DOMXPath $xpath
*/
$xpath = new DOMXPath( $this->dom );
/**
* Node list.
*
* @var DOMNodeList $nodes
*/
$nodes = $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
*
* @since 1.0
* @param DOMDocument $dom The HTML Document.
* @param DOMElement $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
*
* @since 1.0
* @param string $src Script src.
*
* @return array The 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['data_pid'],
'data-vid' => $matches['data_vid'],
'data-bcid' => $matches['data_bcid'],
);
}
return array();
}
}

View File

@ -0,0 +1,146 @@
<?php
/**
* Class AMP_Playbuzz_Sanitizer
*
* @package AMP
*/
/**
* Class AMP_Playbuzz_Sanitizer
*
* Converts Playbuzz embed to <amp-playbuzz>
*
* @see https://www.playbuzz.com/
*/
class AMP_Playbuzz_Sanitizer extends AMP_Base_Sanitizer {
/**
* Tag.
*
* @var string HTML tag to identify and replace with AMP version.
* @since 0.2
*/
public static $tag = 'div';
/**
* PlayBuzz class.
*
* @var string CSS class to identify Playbuzz <div> to replace with AMP version.
*
* @since 0.2
*/
public static $pb_class = 'pb_feed';
/**
* Hardcoded height to set for Playbuzz elements.
*
* @var string
*
* @since 0.2
*/
private static $height = '500';
/**
* Get mapping of HTML selectors to the AMP component selectors which they may be converted into.
*
* @return array Mapping.
*/
public function get_selector_conversion_mapping() {
return array(
'div.pb_feed' => array( 'amp-playbuzz.pb_feed' ),
);
}
/**
* Sanitize the Playbuzz elements from the HTML contained in this instance's DOMDocument.
*
* @since 0.2
*/
public function sanitize() {
$nodes = $this->dom->getElementsByTagName( self::$tag );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return;
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
if ( self::$pb_class !== $node->getAttribute( 'class' ) ) {
continue;
}
$old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
$new_attributes = $this->filter_attributes( $old_attributes );
if ( ! isset( $new_attributes['data-item'] ) && ! isset( $new_attributes['src'] ) ) {
continue;
}
$new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-playbuzz', $new_attributes );
$node->parentNode->replaceChild( $new_node, $node );
$this->did_convert_elements = true;
}
}
/**
* "Filter" HTML attributes for <amp-audio> elements.
*
* @since 0.2
*
* @param string[] $attributes {
* Attributes.
*
* @type string $data-item Playbuzz <div> attribute - Pass along if found and not empty.
* @type string $data-game Playbuzz <div> attribute - Assign to its value to $attributes['src'] if found and not empty.
* @type string $data-game-info Playbuzz <div> attribute - Assign to its value to $attributes['data-item-info'] if found.
* @type string $data-shares Playbuzz <div> attribute - Assign to its value to $attributes['data-share-buttons'] if found.
* @type string $data-comments Playbuzz <div> attribute - Pass along if found.
* @type int $height Playbuzz <div> attribute - Set to hardcoded value of 500.
* }
* @return array Returns HTML attributes; removes any not specifically declared above from input.
*/
private function filter_attributes( $attributes ) {
$out = array();
foreach ( $attributes as $name => $value ) {
switch ( $name ) {
case 'data-item':
if ( ! empty( $value ) ) {
$out['data-item'] = $value;
}
break;
case 'data-game':
if ( ! empty( $value ) ) {
$out['src'] = $value;
}
break;
case 'data-shares':
$out['data-share-buttons'] = $value;
break;
case 'data-game-info':
case 'data-comments':
case 'class':
$out[ $name ] = $value;
break;
default:
break;
}
}
$out['height'] = self::$height;
return $out;
}
}

View File

@ -0,0 +1,141 @@
<?php
/**
* Class AMP_Rule_Spec
*
* @package AMP
*/
/**
* Class AMP_Rule_Spec
*
* Set of constants used throughout the sanitizer.
*/
abstract class AMP_Rule_Spec {
/**
* AMP rule_spec types
*/
const ATTR_SPEC_LIST = 'attr_spec_list';
const TAG_SPEC = 'tag_spec';
const CDATA = 'cdata';
/**
* AMP attr_spec value check results.
*
* In 0.7 these changed from strings to integers to speed up comparisons.
*/
const PASS = 1;
const FAIL = 0;
const NOT_APPLICABLE = -1;
/**
* HTML Element Tag rule names
*/
const DISALLOWED_ANCESTOR = 'disallowed_ancestor';
const MANDATORY_ANCESTOR = 'mandatory_ancestor';
const MANDATORY_PARENT = 'mandatory_parent';
const DESCENDANT_TAG_LIST = 'descendant_tag_list';
const CHILD_TAGS = 'child_tags';
/**
* HTML Element Attribute rule names
*/
const ALLOW_EMPTY = 'allow_empty';
const ALLOW_RELATIVE = 'allow_relative';
const ALLOWED_PROTOCOL = 'protocol';
const ALTERNATIVE_NAMES = 'alternative_names';
const BLACKLISTED_VALUE_REGEX = 'blacklisted_value_regex';
const DISALLOWED_DOMAIN = 'disallowed_domain';
const MANDATORY = 'mandatory';
const VALUE = 'value';
const VALUE_CASEI = 'value_casei';
const VALUE_REGEX = 'value_regex';
const VALUE_REGEX_CASEI = 'value_regex_casei';
const VALUE_PROPERTIES = 'value_properties';
const VALUE_URL = 'value_url';
/**
* Supported layout values.
*
* @since 1.0
* @var array
*/
public static $layout_enum = array(
1 => 'nodisplay',
2 => 'fixed',
3 => 'fixed-height',
4 => 'responsive',
5 => 'container',
6 => 'fill',
7 => 'flex-item',
8 => 'fluid',
9 => 'intrinsic',
);
/**
* List of boolean attributes.
*
* @since 0.7
* @var array
*/
public static $boolean_attributes = array(
'allowfullscreen',
'async',
'autofocus',
'autoplay',
'checked',
'compact',
'controls',
'declare',
'default',
'defaultchecked',
'defaultmuted',
'defaultselected',
'defer',
'disabled',
'draggable',
'enabled',
'formnovalidate',
'hidden',
'indeterminate',
'inert',
'ismap',
'itemscope',
'loop',
'multiple',
'muted',
'nohref',
'noresize',
'noshade',
'novalidate',
'nowrap',
'open',
'pauseonexit',
'readonly',
'required',
'reversed',
'scoped',
'seamless',
'selected',
'sortable',
'spellcheck',
'translate',
'truespeed',
'typemustmatch',
'visible',
);
/**
* Additional allowed tags.
*
* @var array
*/
public static $additional_allowed_tags = array(
// An experimental tag with no protoascii.
'amp-share-tracking' => array(
'attr_spec_list' => array(),
'tag_spec' => array(),
),
);
}

View File

@ -0,0 +1,54 @@
<?php
/**
* Class AMP_Script_Sanitizer
*
* @since 1.0
* @package AMP
*/
/**
* Class AMP_Script_Sanitizer
*
* @since 1.0
*/
class AMP_Script_Sanitizer extends AMP_Base_Sanitizer {
/**
* Sanitize noscript elements.
*
* Eventually this should also handle script elements, if there is a known AMP equivalent.
* If nothing is done with script elements, the whitelist sanitizer will deal with them ultimately.
*
* @todo Eventually this try to automatically convert script tags to AMP when they are recognized. See <https://github.com/ampproject/amp-wp/issues/1032>.
* @todo When a script has an adjacent noscript, consider removing the script here to prevent validation error later. See <https://github.com/ampproject/amp-wp/issues/1213>.
*
* @since 1.0
*/
public function sanitize() {
$noscripts = $this->dom->getElementsByTagName( 'noscript' );
for ( $i = $noscripts->length - 1; $i >= 0; $i-- ) {
$noscript = $noscripts->item( $i );
// Skip AMP boilerplate.
if ( $noscript->firstChild instanceof DOMElement && $noscript->firstChild->hasAttribute( 'amp-boilerplate' ) ) {
continue;
}
// Skip noscript elements inside of amp-img or other AMP components for fallbacks. See \AMP_Img_Sanitizer::adjust_and_replace_node().
if ( 'amp-' === substr( $noscript->parentNode->nodeName, 0, 4 ) ) {
continue;
}
$fragment = $this->dom->createDocumentFragment();
$fragment->appendChild( $this->dom->createComment( 'noscript' ) );
while ( $noscript->firstChild ) {
$fragment->appendChild( $noscript->firstChild );
}
$fragment->appendChild( $this->dom->createComment( '/noscript' ) );
$noscript->parentNode->replaceChild( $fragment, $noscript );
$this->did_convert_elements = true;
}
}
}

View File

@ -0,0 +1,290 @@
<?php
/**
* Class AMP_Video_Sanitizer.
*
* @package AMP
*/
/**
* Class AMP_Video_Sanitizer
*
* @since 0.2
*
* Converts <video> tags to <amp-video>
*/
class AMP_Video_Sanitizer extends AMP_Base_Sanitizer {
use AMP_Noscript_Fallback;
/**
* Value used for height attribute when $attributes['height'] is empty.
*
* @since 0.2
*
* @const int
*/
const FALLBACK_HEIGHT = 400;
/**
* Tag.
*
* @var string HTML <video> tag to identify and replace with AMP version.
*
* @since 0.2
*/
public static $tag = 'video';
/**
* Default args.
*
* @var array
*/
protected $DEFAULT_ARGS = array(
'add_noscript_fallback' => true,
);
/**
* Get mapping of HTML selectors to the AMP component selectors which they may be converted into.
*
* @return array Mapping.
*/
public function get_selector_conversion_mapping() {
return array(
'video' => array( 'amp-video', 'amp-youtube' ),
);
}
/**
* Sanitize the <video> elements from the HTML contained in this instance's DOMDocument.
*
* @since 0.2
* @since 1.0 Set the filtered child node's src attribute.
*/
public function sanitize() {
$nodes = $this->dom->getElementsByTagName( self::$tag );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return;
}
if ( $this->args['add_noscript_fallback'] ) {
$this->initialize_noscript_allowed_attributes( self::$tag );
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
/**
* Node.
*
* @var DOMElement $node
*/
$node = $nodes->item( $i );
// Skip element if already inside of an AMP element as a noscript fallback.
if ( $this->is_inside_amp_noscript( $node ) ) {
continue;
}
$amp_data = $this->get_data_amp_attributes( $node );
$old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
$old_attributes = $this->filter_data_amp_attributes( $old_attributes, $amp_data );
$sources = array();
$new_attributes = $this->filter_attributes( $old_attributes );
$layout = isset( $amp_data['layout'] ) ? $amp_data['layout'] : false;
if ( isset( $new_attributes['src'] ) ) {
$new_attributes = $this->filter_video_dimensions( $new_attributes, $new_attributes['src'] );
if ( $new_attributes['src'] ) {
$sources[] = $new_attributes['src'];
}
}
/**
* Original node.
*
* @var DOMElement $old_node
*/
$old_node = $node->cloneNode( false );
// Gather all child nodes and supply empty video dimensions from sources.
$fallback = null;
$child_nodes = array();
while ( $node->firstChild ) {
$child_node = $node->removeChild( $node->firstChild );
if ( $child_node instanceof DOMElement && 'source' === $child_node->nodeName && $child_node->hasAttribute( 'src' ) ) {
$src = $this->maybe_enforce_https_src( $child_node->getAttribute( 'src' ), true );
if ( ! $src ) {
// @todo $this->remove_invalid_child( $child_node ), but this will require refactoring the while loop since it uses firstChild.
continue; // Skip adding source.
}
$sources[] = $src;
$child_node->setAttribute( 'src', $src );
$new_attributes = $this->filter_video_dimensions( $new_attributes, $src );
}
if ( ! $fallback && $child_node instanceof DOMElement && ! ( 'source' === $child_node->nodeName || 'track' === $child_node->nodeName ) ) {
$fallback = $child_node;
$fallback->setAttribute( 'fallback', '' );
}
$child_nodes[] = $child_node;
}
/*
* Add fallback for audio shortcode which is not present by default since wp_mediaelement_fallback()
* is not called when wp_audio_shortcode_library is filtered from mediaelement to amp.
*/
if ( ! $fallback && ! empty( $sources ) ) {
$fallback = $this->dom->createElement( 'a' );
$fallback->setAttribute( 'href', $sources[0] );
$fallback->setAttribute( 'fallback', '' );
$fallback->appendChild( $this->dom->createTextNode( $sources[0] ) );
$child_nodes[] = $fallback;
}
$new_attributes = $this->filter_attachment_layout_attributes( $node, $new_attributes, $layout );
if ( empty( $new_attributes['layout'] ) && ! empty( $new_attributes['width'] ) && ! empty( $new_attributes['height'] ) ) {
$new_attributes['layout'] = 'responsive';
}
$new_attributes = $this->set_layout( $new_attributes );
// @todo Make sure poster and artwork attributes are HTTPS.
$new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-video', $new_attributes );
foreach ( $child_nodes as $child_node ) {
$new_node->appendChild( $child_node );
if ( ! ( $child_node instanceof DOMElement ) || ! $child_node->hasAttribute( 'fallback' ) ) {
$old_node->appendChild( $child_node->cloneNode( true ) );
}
}
// Make sure the updated src and poster are applied to the original.
foreach ( array( 'src', 'poster', 'artwork' ) as $attr_name ) {
if ( $new_node->hasAttribute( $attr_name ) ) {
$old_node->setAttribute( $attr_name, $new_node->getAttribute( $attr_name ) );
}
}
/*
* If the node has at least one valid source, replace the old node with it.
* Otherwise, just remove the node.
*
* @todo Add a fallback handler.
* See: https://github.com/ampproject/amphtml/issues/2261
*/
if ( empty( $sources ) ) {
$this->remove_invalid_child( $node );
} else {
$node->parentNode->replaceChild( $new_node, $node );
if ( $this->args['add_noscript_fallback'] ) {
// Preserve original node in noscript for no-JS environments.
$this->append_old_node_noscript( $new_node, $old_node, $this->dom );
}
}
$this->did_convert_elements = true;
}
}
/**
* Filter video dimensions, try to get width and height from original file if missing.
*
* @param array $new_attributes Attributes.
* @param string $src Video URL.
* @return array Modified attributes.
*/
protected function filter_video_dimensions( $new_attributes, $src ) {
if ( empty( $new_attributes['width'] ) || empty( $new_attributes['height'] ) ) {
// Get the width and height from the file.
$path = wp_parse_url( $src, PHP_URL_PATH );
$ext = pathinfo( $path, PATHINFO_EXTENSION );
$name = sanitize_title( wp_basename( $path, ".$ext" ) );
$args = array(
'name' => $name,
'post_type' => 'attachment',
'post_status' => 'inherit',
'numberposts' => 1,
);
$attachment = get_posts( $args );
if ( ! empty( $attachment ) ) {
$meta_data = wp_get_attachment_metadata( $attachment[0]->ID );
if ( empty( $new_attributes['width'] ) && ! empty( $meta_data['width'] ) ) {
$new_attributes['width'] = $meta_data['width'];
}
if ( empty( $new_attributes['height'] ) && ! empty( $meta_data['height'] ) ) {
$new_attributes['height'] = $meta_data['height'];
}
}
}
return $new_attributes;
}
/**
* "Filter" HTML attributes for <amp-audio> elements.
*
* @since 0.2
* @since 1.0 Force HTTPS for the src attribute.
*
* @param string[] $attributes {
* Attributes.
*
* @type string $src Video URL - Empty if HTTPS required per $this->args['require_https_src']
* @type int $width <video> attribute - Set to numeric value if px or %
* @type int $height <video> attribute - Set to numeric value if px or %
* @type string $poster <video> attribute - Pass along if found
* @type string $class <video> attribute - Pass along if found
* @type bool $controls <video> attribute - Convert 'false' to empty string ''
* @type bool $loop <video> attribute - Convert 'false' to empty string ''
* @type bool $muted <video> attribute - Convert 'false' to empty string ''
* @type bool $autoplay <video> attribute - Convert 'false' to empty string ''
* }
* @return array Returns HTML attributes; removes any not specifically declared above from input.
*/
private function filter_attributes( $attributes ) {
$out = array();
foreach ( $attributes as $name => $value ) {
switch ( $name ) {
case 'src':
$out[ $name ] = $this->maybe_enforce_https_src( $value, true );
break;
case 'width':
case 'height':
$out[ $name ] = $this->sanitize_dimension( $value, $name );
break;
// @todo Convert to HTTPS when is_ssl().
case 'poster':
case 'artwork':
$out[ $name ] = $value;
break;
case 'controls':
case 'loop':
case 'muted':
case 'autoplay':
if ( 'false' !== $value ) {
$out[ $name ] = '';
}
break;
case 'data-amp-layout':
$out['layout'] = $value;
break;
case 'data-amp-noloading':
$out['noloading'] = $value;
break;
default:
$out[ $name ] = $value;
}
}
return $out;
}
}

View File

@ -0,0 +1,79 @@
<?php
/**
* Trait AMP_Noscript_Fallback.
*
* @package AMP
*/
/**
* Trait AMP_Noscript_Fallback
*
* @since 1.1
*
* Used for sanitizers that place <noscript> tags with the original nodes on error.
*/
trait AMP_Noscript_Fallback {
/**
* Attributes allowed on noscript fallback elements.
*
* This is used to prevent duplicated validation errors.
*
* @since 1.1
* @var array
*/
private $noscript_fallback_allowed_attributes = array();
/**
* Initializes the internal allowed attributes array.
*
* @since 1.1
*
* @param string $tag Tag name to get allowed attributes for.
*/
protected function initialize_noscript_allowed_attributes( $tag ) {
$this->noscript_fallback_allowed_attributes = array_fill_keys(
array_merge(
array_keys( current( AMP_Allowed_Tags_Generated::get_allowed_tag( $tag ) )['attr_spec_list'] ),
array_keys( AMP_Allowed_Tags_Generated::get_allowed_attributes() )
),
true
);
}
/**
* Checks whether the given node is within an AMP-specific <noscript> element.
*
* @since 1.1
*
* @param \DOMNode $node DOM node to check.
* @return bool True if in an AMP noscript element, false otherwise.
*/
protected function is_inside_amp_noscript( \DOMNode $node ) {
return 'noscript' === $node->parentNode->nodeName && $node->parentNode->parentNode && 'amp-' === substr( $node->parentNode->parentNode->nodeName, 0, 4 );
}
/**
* Appends the given old node in a <noscript> element to the new node.
*
* @since 1.1
*
* @param \DOMNode $new_node New node to append a noscript with the old node to.
* @param \DOMNode $old_node Old node to append in a noscript.
* @param \DOMDocument $dom DOM document instance.
*/
protected function append_old_node_noscript( \DOMNode $new_node, \DOMNode $old_node, \DOMDocument $dom ) {
$noscript = $dom->createElement( 'noscript' );
$noscript->appendChild( $old_node );
$new_node->appendChild( $noscript );
// Remove all non-allowed attributes preemptively to prevent doubled validation errors.
for ( $i = $old_node->attributes->length - 1; $i >= 0; $i-- ) {
$attribute = $old_node->attributes->item( $i );
if ( isset( $this->noscript_fallback_allowed_attributes[ $attribute->nodeName ] ) ) {
continue;
}
$old_node->removeAttribute( $attribute->nodeName );
}
}
}