|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once( AMP__ROOT__ . '/includes/utils/class-amp-dom-utils.php' ); |
|
4
|
|
|
require_once( AMP__ROOT__ . '/includes/sanitizers/class-amp-base-sanitizer.php' ); |
|
5
|
|
|
require_once( AMP__ROOT__ . '/includes/embeds/class-amp-base-embed-handler.php' ); |
|
6
|
|
|
|
|
7
|
|
|
class AMP_Content { |
|
8
|
|
|
private $content; |
|
9
|
|
|
private $amp_content = ''; |
|
10
|
|
|
private $amp_scripts = array(); |
|
11
|
|
|
private $amp_styles = array(); |
|
12
|
|
|
private $args = array(); |
|
13
|
|
|
private $embed_handler_classes = array(); |
|
14
|
|
|
private $sanitizer_classes = array(); |
|
15
|
|
|
|
|
16
|
|
|
public function __construct( $content, $embed_handler_classes, $sanitizer_classes, $args = array() ) { |
|
17
|
|
|
$this->content = $content; |
|
18
|
|
|
$this->args = $args; |
|
19
|
|
|
$this->embed_handler_classes = $embed_handler_classes; |
|
20
|
|
|
$this->sanitizer_classes = $sanitizer_classes; |
|
21
|
|
|
|
|
22
|
|
|
$this->transform(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function get_amp_content() { |
|
26
|
|
|
return $this->amp_content; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function get_amp_scripts() { |
|
30
|
|
|
return $this->amp_scripts; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function get_amp_styles() { |
|
34
|
|
|
return $this->amp_styles; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function transform() { |
|
38
|
|
|
$content = $this->content; |
|
39
|
|
|
|
|
40
|
|
|
// First, embeds + the_content filter |
|
41
|
|
|
$embed_handlers = $this->register_embed_handlers(); |
|
42
|
|
|
$content = apply_filters( 'the_content', $content ); |
|
43
|
|
|
$this->unregister_embed_handlers( $embed_handlers ); |
|
44
|
|
|
|
|
45
|
|
|
// Then, sanitize to strip and/or convert non-amp content |
|
46
|
|
|
$content = $this->sanitize( $content ); |
|
47
|
|
|
|
|
48
|
|
|
$this->amp_content = $content; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function add_scripts( $scripts ) { |
|
52
|
|
|
$this->amp_scripts = array_merge( $this->amp_scripts, $scripts ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function add_styles( $styles ) { |
|
56
|
|
|
$this->amp_styles = array_merge( $this->amp_styles, $styles ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function register_embed_handlers() { |
|
60
|
|
|
$embed_handlers = array(); |
|
61
|
|
|
|
|
62
|
|
|
foreach ( $this->embed_handler_classes as $embed_handler_class => $args ) { |
|
63
|
|
|
$embed_handler = new $embed_handler_class( array_merge( $this->args, $args ) ); |
|
64
|
|
|
|
|
65
|
|
|
if ( ! is_subclass_of( $embed_handler, 'AMP_Base_Embed_Handler' ) ) { |
|
66
|
|
|
_doing_it_wrong( __METHOD__, sprintf( __( 'Embed Handler (%s) must extend `AMP_Embed_Handler`', 'amp' ), $embed_handler_class ), '0.1' ); |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$embed_handler->register_embed(); |
|
71
|
|
|
$embed_handlers[] = $embed_handler; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $embed_handlers; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function unregister_embed_handlers( $embed_handlers ) { |
|
78
|
|
|
foreach ( $embed_handlers as $embed_handler ) { |
|
79
|
|
|
$this->add_scripts( $embed_handler->get_scripts() ); |
|
80
|
|
|
$embed_handler->unregister_embed(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function sanitize( $content ) { |
|
85
|
|
|
list( $sanitized_content, $scripts, $styles ) = AMP_Content_Sanitizer::sanitize( $content, $this->sanitizer_classes, $this->args ); |
|
86
|
|
|
|
|
87
|
|
|
$this->add_scripts( $scripts ); |
|
88
|
|
|
$this->add_styles( $styles ); |
|
89
|
|
|
|
|
90
|
|
|
return $sanitized_content; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
class AMP_Content_Sanitizer { |
|
|
|
|
|
|
95
|
|
|
public static function sanitize( $content, $sanitizer_classes, $global_args = array() ) { |
|
96
|
|
|
$scripts = array(); |
|
97
|
|
|
$styles = array(); |
|
98
|
|
|
$dom = AMP_DOM_Utils::get_dom_from_content( $content ); |
|
99
|
|
|
|
|
100
|
|
|
foreach ( $sanitizer_classes as $sanitizer_class => $args ) { |
|
101
|
|
|
if ( ! class_exists( $sanitizer_class ) ) { |
|
102
|
|
|
_doing_it_wrong( __METHOD__, sprintf( __( 'Sanitizer (%s) class does not exist', 'amp' ), esc_html( $sanitizer_class ) ), '0.4.1' ); |
|
103
|
|
|
continue; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$sanitizer = new $sanitizer_class( $dom, array_merge( $global_args, $args ) ); |
|
107
|
|
|
|
|
108
|
|
|
if ( ! is_subclass_of( $sanitizer, 'AMP_Base_Sanitizer' ) ) { |
|
109
|
|
|
_doing_it_wrong( __METHOD__, sprintf( __( 'Sanitizer (%s) must extend `AMP_Base_Sanitizer`', 'amp' ), esc_html( $sanitizer_class ) ), '0.1' ); |
|
110
|
|
|
continue; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$sanitizer->sanitize(); |
|
114
|
|
|
|
|
115
|
|
|
$scripts = array_merge( $scripts, $sanitizer->get_scripts() ); |
|
116
|
|
|
$styles = array_merge( $styles, $sanitizer->get_styles() ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$sanitized_content = AMP_DOM_Utils::get_content_from_dom( $dom ); |
|
120
|
|
|
|
|
121
|
|
|
return array( $sanitized_content, $scripts, $styles ); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.