1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once( AMP__ROOT__ . '/includes/sanitizers/class-amp-base-sanitizer.php' ); |
4
|
|
|
require_once( AMP__ROOT__ . '/includes/utils/class-amp-image-dimension-extractor.php' ); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Converts <img> tags to <amp-img> or <amp-anim> |
8
|
|
|
*/ |
9
|
|
|
class AMP_Img_Sanitizer extends AMP_Base_Sanitizer { |
10
|
|
|
const FALLBACK_WIDTH = 600; |
11
|
|
|
const FALLBACK_HEIGHT = 400; |
12
|
|
|
|
13
|
|
|
public static $tag = 'img'; |
14
|
|
|
|
15
|
|
|
private static $anim_extension = '.gif'; |
16
|
|
|
|
17
|
|
|
private static $script_slug = 'amp-anim'; |
18
|
|
|
private static $script_src = 'https://cdn.ampproject.org/v0/amp-anim-0.1.js'; |
19
|
|
|
|
20
|
|
|
public function sanitize() { |
21
|
|
|
$nodes = $this->dom->getElementsByTagName( self::$tag ); |
22
|
|
|
$num_nodes = $nodes->length; |
23
|
|
|
if ( 0 === $num_nodes ) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) { |
28
|
|
|
$node = $nodes->item( $i ); |
29
|
|
|
$old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node ); |
30
|
|
|
|
31
|
|
|
if ( empty( $old_attributes['src'] ) ) { |
32
|
|
|
$node->parentNode->removeChild( $node ); |
33
|
|
|
continue; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$new_attributes = $this->filter_attributes( $old_attributes ); |
37
|
|
|
|
38
|
|
|
// Try to extract dimensions for the image, if not set. |
39
|
|
|
if ( ! isset( $new_attributes['width'] ) || ! isset( $new_attributes['height'] ) ) { |
40
|
|
|
$dimensions = AMP_Image_Dimension_Extractor::extract( $new_attributes['src'] ); |
41
|
|
|
if ( is_array( $dimensions ) ) { |
42
|
|
|
$new_attributes['width'] = $dimensions[0]; |
43
|
|
|
$new_attributes['height'] = $dimensions[1]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Final fallback when we have no dimensions. |
48
|
|
|
if ( ! isset( $new_attributes['width'] ) || ! isset( $new_attributes['height'] ) ) { |
49
|
|
|
$new_attributes['width'] = isset( $this->args['content_max_width'] ) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH; |
50
|
|
|
$new_attributes['height'] = self::FALLBACK_HEIGHT; |
51
|
|
|
|
52
|
|
|
$this->add_or_append_attribute( $new_attributes, 'class', 'amp-wp-unknown-size' ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$new_attributes = $this->enforce_sizes_attribute( $new_attributes ); |
56
|
|
|
|
57
|
|
|
if ( $this->is_gif_url( $new_attributes['src'] ) ) { |
58
|
|
|
$this->did_convert_elements = true; |
59
|
|
|
$new_tag = 'amp-anim'; |
60
|
|
|
} else { |
61
|
|
|
$new_tag = 'amp-img'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$new_node = AMP_DOM_Utils::create_node( $this->dom, $new_tag, $new_attributes ); |
65
|
|
|
$node->parentNode->replaceChild( $new_node, $node ); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function get_scripts() { |
70
|
|
|
if ( ! $this->did_convert_elements ) { |
71
|
|
|
return array(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return array( self::$script_slug => self::$script_src ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function filter_attributes( $attributes ) { |
78
|
|
|
$out = array(); |
79
|
|
|
|
80
|
|
|
foreach ( $attributes as $name => $value ) { |
81
|
|
|
switch ( $name ) { |
82
|
|
|
case 'src': |
83
|
|
|
case 'alt': |
84
|
|
|
case 'class': |
85
|
|
|
case 'srcset': |
86
|
|
|
case 'sizes': |
87
|
|
|
case 'on': |
88
|
|
|
$out[ $name ] = $value; |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case 'width': |
92
|
|
|
case 'height': |
93
|
|
|
$out[ $name ] = $this->sanitize_dimension( $value, $name ); |
94
|
|
|
break; |
95
|
|
|
|
96
|
|
|
default; |
|
|
|
|
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $out; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function is_gif_url( $url ) { |
105
|
|
|
$ext = self::$anim_extension; |
106
|
|
|
$path = parse_url( $url, PHP_URL_PATH ); |
107
|
|
|
return $ext === substr( $path, -strlen( $ext ) ); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.