tags to or */ class AMPforWP_Img_Sanitizer extends AMP_Base_Sanitizer { /** * 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; protected $is_lightbox = false; protected $scripts = array(); private static $script_slug = 'amp-anim'; private static $script_src = 'https://cdn.ampproject.org/v0/amp-anim-0.1.js'; private static $script_slug_lightbox = 'amp-image-lightbox'; private static $script_src_lightbox = 'https://cdn.ampproject.org/v0/amp-image-lightbox-0.1.js'; /** * Tag. * * @var string HTML tag to identify and replace with AMP version. * * @since 0.2 */ public static $tag = 'img'; /** * Animation extension. * * @var string */ private static $anim_extension = '.gif'; /** * Sanitize the 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; } for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { $node = $nodes->item( $i ); if ( ! $node instanceof DOMElement ) { continue; } // Add Foo Gallery Support if ( $node->hasAttribute( 'data-src-fg' ) ) { $image_scr_from_data_src = $node->getAttribute( 'data-src-fg' ) ; if ( ! $node->hasAttribute( 'src' ) || '' === $node->getAttribute( 'src' ) ) { $node->setAttribute( 'src', $image_scr_from_data_src ); } } 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 ); } /** * "Filter" HTML attributes for elements. * * @since 0.2 * * @param string[] $attributes { * Attributes. * * @type string $src Image URL - Pass along if found * @type string $alt `alt` attribute - Pass along if found * @type string $class `class` attribute - Pass along if found * @type string $srcset `srcset` attribute - Pass along if found * @type string $sizes `sizes` attribute - Pass along if found * @type string $on `on` attribute - Pass along if found * @type string $attribution `attribution` attribute - Pass along if found * @type int $width width attribute - Set to numeric value if px or % * @type int $height 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 'src': case 'alt': case 'class': case 'srcset': case 'on': case 'attribution': case 'role': case 'tabindex': $out[ $name ] = $value; break; case 'width': case 'height': $out[ $name ] = $this->sanitize_dimension( $value, $name ); break; case 'data-amp-layout': $out['layout'] = $value; break; default: 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 @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; } if ( ! is_numeric( $node->getAttribute( 'width' ) ) && ! is_numeric( $node->getAttribute( 'height' ) ) ) { $height = self::FALLBACK_HEIGHT; $width = self::FALLBACK_WIDTH; $node->setAttribute( 'width', $width ); $node->setAttribute( 'height', $height ); $class = $node->hasAttribute( 'class' ) ? $node->getAttribute( 'class' ) . ' amp-wp-unknown-size' : 'amp-wp-unknown-size'; $node->setAttribute( 'class', $class ); } elseif ( ! is_numeric( $node->getAttribute( 'height' ) ) ) { $height = self::FALLBACK_HEIGHT; $node->setAttribute( 'height', $height ); $class = $node->hasAttribute( 'class' ) ? $node->getAttribute( 'class' ) . ' amp-wp-unknown-size amp-wp-unknown-height' : 'amp-wp-unknown-size amp-wp-unknown-height'; $node->setAttribute( 'class', $class ); } elseif ( ! is_numeric( $node->getAttribute( 'width' ) ) ) { $width = self::FALLBACK_WIDTH; $node->setAttribute( 'width', $width ); $class = $node->hasAttribute( 'class' ) ? $node->getAttribute( 'class' ) . ' amp-wp-unknown-size amp-wp-unknown-width' : 'amp-wp-unknown-size amp-wp-unknown-width'; $node->setAttribute( 'class', $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 DOMNode $node The DOMNode to adjust and replace. */ private function adjust_and_replace_node( $node ) { $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); $old_attributes = apply_filters('amp_img_attributes', $old_attributes); $new_attributes = $this->filter_attributes( $old_attributes ); $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'] ) ) { $new_attributes['layout'] = 'intrinsic'; } if ( $this->is_gif_url( $new_attributes['src'] ) ) { $this->did_convert_elements = true; $new_tag = 'amp-anim'; } else { $new_tag = 'amp-img'; } $new_node = AMP_DOM_Utils::create_node( $this->dom, $new_tag, $new_attributes ); $new_node = $this->handle_centering( $new_node ); $node->parentNode->replaceChild( $new_node, $node ); if ( isset($new_attributes['on']) && '' != $new_attributes['on'] ) { add_action('amp_post_template_footer', 'ampforwp_amp_img_lightbox'); $this->is_lightbox = true; } } /** * 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 = AMP_WP_Utils::parse_url( $url, PHP_URL_PATH ); return substr( $path, -strlen( $ext ) ) === $ext; } /** * Handles an issue with the aligncenter class. * * If the has the class aligncenter, this strips the class and wraps it in a
to center the image. * There was an issue where the aligncenter class overrode the "display: inline-block" rule of AMP's layout="intrinsic" attribute. * So this strips that class, and instead wraps the image in a
to center it. * * @since 0.7 * @see https://github.com/Automattic/amp-wp/issues/1104 * * @param DOMElement $node The node. * @return DOMElement $node The node, possibly wrapped in a
. */ public function handle_centering( $node ) { $align_class = 'aligncenter'; $classes = $node->getAttribute( 'class' ); $width = $node->getAttribute( 'width' ); // If this doesn't have a width attribute, centering it in the
wrapper won't work. if ( empty( $width ) || ! in_array( $align_class, preg_split( '/\s+/', trim( $classes ) ), true ) ) { return $node; } // Strip the class, and wrap the in a
. $classes = trim( str_replace( $align_class, '', $classes ) ); $node->setAttribute( 'class', $classes ); $figure = AMP_DOM_Utils::create_node( $this->dom, 'figure', array( 'class' => $align_class, //'style' => "max-width: {$width}px;", ) ); $figure->appendChild( $node ); return $figure; } public function get_scripts() { if ( $this->is_lightbox ) { $this->scripts[self::$script_slug_lightbox] = self::$script_src_lightbox; } if ( $this->did_convert_elements ) { $this->scripts[self::$script_slug] = self::$script_src; } return $this->scripts; } }