dom = $dom; $this->args = array_merge( $this->DEFAULT_ARGS, $args ); } /** * 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[] This are empty in this the base class. */ public function get_scripts() { return array(); } /** * Return array of values that would be valid as an HTML `style` attribute. * * @since 0.4 * * @return string[] This are empty in this the base class. */ public function get_styles() { return array(); } /** * Get HTML body as DOMElement from DOMDocument received by the constructor. * * @return DOMElement */ 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 ) { if ( empty( $value ) ) { return ''; } if ( false !== filter_var( $value, FILTER_VALIDATE_INT ) ) { return absint( $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 ''; } /** * Enforce fixed height. * * @param string[] $attributes { * Attributes. * * @type int $height * @type int $width * @type string $sizes * @type string $class * @type string $layout * } * @return string[] */ public function enforce_fixed_height( $attributes ) { if ( empty( $attributes['height'] ) ) { unset( $attributes['width'] ); $attributes['height'] = self::FALLBACK_HEIGHT; } if ( empty( $attributes['width'] ) ) { $attributes['layout'] = 'fixed-height'; } return $attributes; } /** * This is our workaround to enforce max sizing with layout=responsive. * * We want elements to not grow beyond their width and shrink to fill the screen on viewports smaller than their width. * * See https://github.com/ampproject/amphtml/issues/1280#issuecomment-171533526 * See https://github.com/Automattic/amp-wp/issues/101 * * @param string[] $attributes { * Attributes. * * @type int $height * @type int $width * @type string $sizes * @type string $class * @type string $layout * } * @return string[] */ public function enforce_sizes_attribute( $attributes ) { if ( ! isset( $attributes['width'], $attributes['height'] ) ) { return $attributes; } $max_width = $attributes['width']; if ( isset( $this->args['content_max_width'] ) && $max_width >= $this->args['content_max_width'] ) { $max_width = $this->args['content_max_width']; } $attributes['sizes'] = sprintf( '(min-width: %1$dpx) %1$dpx, 100vw', absint( $max_width ) ); $this->add_or_append_attribute( $attributes, 'class', 'amp-wp-enforced-sizes' ); 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 ] .= $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, ':' ); 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; } }