-
Notifications
You must be signed in to change notification settings - Fork 378
Expand file tree
/
Copy pathclass-amp-facebook-embed-handler.php
More file actions
252 lines (212 loc) · 6.16 KB
/
Copy pathclass-amp-facebook-embed-handler.php
File metadata and controls
252 lines (212 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
* Class AMP_Facebook_Embed_Handler
*
* @package AMP
*/
use AmpProject\Dom\Document;
/**
* Class AMP_Facebook_Embed_Handler
*
* @internal
*/
class AMP_Facebook_Embed_Handler extends AMP_Base_Embed_Handler {
/**
* Default height.
*
* @var int
*/
protected $DEFAULT_HEIGHT = 400;
/**
* Tag.
*
* @var string embed HTML blockquote tag to identify and replace with AMP version.
*/
protected $sanitize_tag = 'div';
/**
* Tag.
*
* @var string AMP amp-facebook tag
*/
private $amp_tag = 'amp-facebook';
/**
* Registers embed.
*/
public function register_embed() {
// Not implemented.
}
/**
* Unregisters embed.
*/
public function unregister_embed() {
// Not implemented.
}
/**
* Sanitize raw embeds.
*
* @param Document $dom DOM.
*/
public function sanitize_raw_embeds( Document $dom ) {
$replaced = 0;
$replaced += $this->sanitize_raw_embed_divs( $dom );
$replaced += $this->sanitize_raw_embed_fb_components( $dom );
if ( 0 === $replaced ) {
return;
}
/*
* Remove the fb-root div and the Facebook Connect JS script since irrelevant.
* <div id="fb-root"></div>
* <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.2"></script>
*/
$fb_root = $dom->getElementById( 'fb-root' );
if ( $fb_root ) {
$script_elements = $dom->xpath->query( '//script[ starts-with( @src, "https://connect.facebook.net" ) and contains( @src, "sdk.js" ) ]' );
foreach ( $script_elements as $script ) {
$parent_node = $script->parentNode;
$parent_node->removeChild( $script );
// Remove parent node if it is an empty <p> tag.
if ( 'p' === $parent_node->nodeName && null === $parent_node->firstChild ) {
$parent_node->parentNode->removeChild( $parent_node );
}
}
// Remove other instances of <div id="fb-root">.
$fb_root_elements = $dom->xpath->query( '//div[ @id = "fb-root" ]' );
foreach ( $fb_root_elements as $fb_root ) {
$fb_root->parentNode->removeChild( $fb_root );
}
}
}
/**
* Sanitize <div class="fb-video" data-href=> tags to <amp-facebook>.
*
* @param Document $dom DOM.
* @return int Replacements made.
*/
private function sanitize_raw_embed_divs( Document $dom ) {
$replaced = 0;
$nodes = $dom->getElementsByTagName( $this->sanitize_tag );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return 0;
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
if ( ! $node instanceof DOMElement ) {
continue;
}
$embed_type = $this->get_embed_type( $node );
if ( null !== $embed_type ) {
$this->create_amp_facebook_and_replace_node( $dom, $node, $embed_type );
$replaced++;
}
}
return $replaced;
}
/**
* Sanitize <fb:post> tags to <amp-facebook>.
*
* @param Document $dom DOM.
* @return int Replacements made.
*/
private function sanitize_raw_embed_fb_components( Document $dom ) {
$replaced = 0;
$nodes = $dom->getElementsByTagName( 'post' );
$num_nodes = $nodes->length;
if ( 0 === $num_nodes ) {
return 0;
}
for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
$node = $nodes->item( $i );
if ( ! $node instanceof DOMElement ) {
continue;
}
$embed_type = 'post';
$this->create_amp_facebook_and_replace_node( $dom, $node, $embed_type );
$replaced++;
}
return $replaced;
}
/**
* Get embed type.
*
* @param DOMElement $node The DOMNode to adjust and replace.
* @return string|null Embed type or null if not detected.
*/
private function get_embed_type( DOMElement $node ) {
$class_attr = $node->getAttribute( 'class' );
if ( null === $class_attr || ! $node->hasAttribute( 'data-href' ) ) {
return null;
}
if ( false !== strpos( $class_attr, 'fb-post' ) ) {
return 'post';
}
if ( false !== strpos( $class_attr, 'fb-video' ) ) {
return 'video';
}
if ( false !== strpos( $class_attr, 'fb-page' ) ) {
return 'page';
}
if ( false !== strpos( $class_attr, 'fb-like' ) ) {
return 'like';
}
if ( false !== strpos( $class_attr, 'fb-comments' ) ) {
return 'comments';
}
if ( false !== strpos( $class_attr, 'fb-comment-embed' ) ) {
return 'comment';
}
return null;
}
/**
* Create amp-facebook and replace node.
*
* @param Document $dom The HTML Document.
* @param DOMElement $node The DOMNode to adjust and replace.
* @param string $embed_type Embed type.
*/
private function create_amp_facebook_and_replace_node( Document $dom, DOMElement $node, $embed_type ) {
$attributes = [
// The layout sanitizer will convert this to `layout` when being sanitized.
// The data attribute needs to be used so that the layout sanitizer will process it.
'data-amp-layout' => 'responsive',
'width' => $node->hasAttribute( 'data-width' ) ? $node->getAttribute( 'data-width' ) : $this->DEFAULT_WIDTH,
'height' => $node->hasAttribute( 'data-height' ) ? $node->getAttribute( 'data-height' ) : $this->DEFAULT_HEIGHT,
];
$node->removeAttribute( 'data-width' );
$node->removeAttribute( 'data-height' );
foreach ( $node->attributes as $attribute ) {
if ( 'data-' === substr( $attribute->nodeName, 0, 5 ) ) {
$attributes[ $attribute->nodeName ] = $attribute->nodeValue;
}
}
if ( 'page' === $embed_type ) {
$amp_tag = 'amp-facebook-page';
} elseif ( 'like' === $embed_type ) {
$amp_tag = 'amp-facebook-like';
} elseif ( 'comments' === $embed_type ) {
$amp_tag = 'amp-facebook-comments';
} else {
$amp_tag = $this->amp_tag;
$attributes['data-embed-as'] = $embed_type;
}
$amp_facebook_node = AMP_DOM_Utils::create_node(
$dom,
$amp_tag,
$attributes
);
$fallback = null;
foreach ( $node->childNodes as $child_node ) {
if ( $child_node instanceof DOMElement && false !== strpos( $child_node->getAttribute( 'class' ), 'fb-xfbml-parse-ignore' ) ) {
$fallback = $child_node;
$child_node->parentNode->removeChild( $child_node );
$fallback->setAttribute( 'fallback', '' );
break;
}
}
$node->parentNode->replaceChild( $amp_facebook_node, $node );
if ( $fallback ) {
$amp_facebook_node->appendChild( $fallback );
}
$this->did_convert_elements = true;
}
}