This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WP_Block_Markup_Url_Processor.php
286 lines (245 loc) · 7.84 KB
/
WP_Block_Markup_Url_Processor.php
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
use Rowbot\URL\URL;
/**
* Reports all the URLs in the imported post and enables rewriting them.
*/
class WP_Block_Markup_Url_Processor extends WP_Block_Markup_Processor {
private $raw_url;
/**
* @var URL
*/
private $parsed_url;
private $base_url;
private $url_in_text_processor;
private $url_in_text_node_updated;
private $inspected_url_attribute_idx = - 1;
public function __construct( $html, $base_url = null ) {
parent::__construct( $html );
$this->base_url = $base_url;
}
public function get_updated_html() {
if ( $this->url_in_text_node_updated ) {
$this->set_modifiable_text( $this->url_in_text_processor->get_updated_text() );
$this->url_in_text_node_updated = false;
}
return parent::get_updated_html();
}
public function get_raw_url() {
return $this->raw_url;
}
public function get_parsed_url() {
return $this->parsed_url;
}
public function next_token() {
$this->get_updated_html();
$this->raw_url = null;
$this->parsed_url = null;
$this->inspected_url_attribute_idx = - 1;
$this->url_in_text_processor = null;
// Do not reset url_in_text_node_updated – it's reset in get_updated_html() which
// is called in parent::next_token().
return parent::next_token();
}
public function next_url() {
do {
if ( $this->next_url_in_current_token() ) {
return true;
}
} while ( $this->next_token() !== false );
return false;
}
public function next_url_in_current_token() {
$this->raw_url = null;
switch ( parent::get_token_type() ) {
case '#tag':
return $this->next_url_attribute();
case '#block-comment':
return $this->next_url_block_attribute();
case '#text':
return $this->next_url_in_text_node();
default:
return false;
}
}
private function next_url_in_text_node() {
if ( $this->get_token_type() !== '#text' ) {
return false;
}
if ( null === $this->url_in_text_processor ) {
/*
* Use the base URL for URLs matched in text nodes. This is the only
* way to recognize a substring "WordPress.org" as a URL. We might
* get some false positives this way, e.g. in this string:
*
* > And that's how you build a theme.Now let's take a look at..."
*
* `theme.Now` would be recognized as a URL. It's up to the API consumer
* to filter out such false positives e.g. by checking the domain against
* a list of accepted domains, or the TLD against a list of public suffixes.
*/
$this->url_in_text_processor = new WP_Migration_URL_In_Text_Processor( $this->get_modifiable_text(), $this->base_url );
}
while ( $this->url_in_text_processor->next_url() ) {
$this->raw_url = $this->url_in_text_processor->get_raw_url();
$this->parsed_url = $this->url_in_text_processor->get_parsed_url();
return true;
}
return false;
}
private function next_url_attribute() {
$tag = $this->get_tag();
if (
! array_key_exists( $tag, self::URL_ATTRIBUTES ) &&
$tag !== 'INPUT' // type=image => src,
) {
return false;
}
while ( ++ $this->inspected_url_attribute_idx < count( self::URL_ATTRIBUTES[ $tag ] ) ) {
$attr = self::URL_ATTRIBUTES[ $tag ][ $this->inspected_url_attribute_idx ];
if ( false === $attr ) {
return false;
}
$url_maybe = $this->get_attribute( $attr );
/*
* Use base URL to resolve known URI attributes as we are certain we're
* dealing with URI values.
* With a base URL, the string "plugins.php" in <a href="plugins.php"> will
* be correctly recognized as a URL.
* Without a base URL, this Processor would incorrectly skip it.
*/
if ( is_string( $url_maybe ) ) {
$parsed_url = WP_URL::parse( $url_maybe, $this->base_url );
if ( false !== $parsed_url ) {
$this->raw_url = $url_maybe;
$this->parsed_url = $parsed_url;
return true;
}
}
}
return false;
}
private function next_url_block_attribute() {
while ( $this->next_block_attribute() ) {
$url_maybe = $this->get_block_attribute_value();
/*
* Do not use base URL for block attributes. to avoid false positives.
* When a base URL is present, any word is a valid URL relative to the
* base URL.
* When a base URL is missing, the string must start with a protocol to
* be considered a URL.
*/
if ( is_string( $url_maybe ) ) {
$parsed_url = WP_URL::parse( $url_maybe );
if ( false !== $parsed_url ) {
$this->raw_url = $url_maybe;
$this->parsed_url = $parsed_url;
return true;
}
}
}
return false;
}
public function set_raw_url( $new_url ) {
if ( null === $this->raw_url ) {
return false;
}
switch ( parent::get_token_type() ) {
case '#tag':
$attr = $this->get_inspected_attribute_name();
if ( false === $attr ) {
return false;
}
$this->set_attribute( $attr, $new_url );
return true;
case '#block-comment':
return $this->set_block_attribute_value( $new_url );
case '#text':
if ( null === $this->url_in_text_processor ) {
return false;
}
$this->url_in_text_node_updated = true;
return $this->url_in_text_processor->set_raw_url( $new_url );
}
}
public function get_inspected_attribute_name() {
if ( '#tag' !== $this->get_token_type() ) {
return false;
}
$tag = $this->get_tag();
if ( ! array_key_exists( $tag, self::URL_ATTRIBUTES ) ) {
return false;
}
if (
$this->inspected_url_attribute_idx < 0 ||
$this->inspected_url_attribute_idx >= count( self::URL_ATTRIBUTES[ $tag ] )
) {
return false;
}
return self::URL_ATTRIBUTES[ $tag ][ $this->inspected_url_attribute_idx ];
}
/**
* A list of HTML attributes meant to contain URLs, as defined in the HTML specification.
* It includes some deprecated attributes like `lowsrc` and `highsrc` for the `IMG` element.
*
* See https://html.spec.whatwg.org/multipage/indices.html#attributes-1.
* See https://stackoverflow.com/questions/2725156/complete-list-of-html-tag-attributes-which-have-a-url-value.
*
*/
public const URL_ATTRIBUTES = [
'A' => [ 'href' ],
'APPLET' => [ 'codebase', 'archive' ],
'AREA' => [ 'href' ],
'AUDIO' => [ 'src' ],
'BASE' => [ 'href' ],
'BLOCKQUOTE' => [ 'cite' ],
'BODY' => [ 'background' ],
'BUTTON' => [ 'formaction' ],
'COMMAND' => [ 'icon' ],
'DEL' => [ 'cite' ],
'EMBED' => [ 'src' ],
'FORM' => [ 'action' ],
'FRAME' => [ 'longdesc', 'src' ],
'HEAD' => [ 'profile' ],
'HTML' => [ 'manifest' ],
'IFRAME' => [ 'longdesc', 'src' ],
// SVG <image> element
'IMAGE' => [ 'href' ],
'IMG' => [ 'longdesc', 'src', 'usemap', 'lowsrc', 'highsrc' ],
'INPUT' => [ 'src', 'usemap', 'formaction' ],
'INS' => [ 'cite' ],
'LINK' => [ 'href' ],
'OBJECT' => [ 'classid', 'codebase', 'data', 'usemap' ],
'Q' => [ 'cite' ],
'SCRIPT' => [ 'src' ],
'SOURCE' => [ 'src' ],
'TRACK' => [ 'src' ],
'VIDEO' => [ 'poster', 'src' ],
];
/**
* @TODO: Either explicitly support these attributes, or explicitly drop support for
* handling their subsyntax. A generic URL matcher might be good enough.
*/
public const URL_ATTRIBUTES_WITH_SUBSYNTAX = [
'*' => [ 'style' ], // background(), background-image()
'APPLET' => [ 'archive' ],
'IMG' => [ 'srcset' ],
'META' => [ 'content' ],
'SOURCE' => [ 'srcset' ],
'OBJECT' => [ 'archive' ],
];
/**
* Also <style> and <script> tag content can contain URLs.
* <style> has specific syntax rules we can use for matching, but perhaps a generic matcher would be good enough?
*
* <style>
* #domID { background:url(https://mysite.com/wp-content/uploads/image.png) }
* </style>
*
* @TODO: Either explicitly support these tags, or explicitly drop support for
* handling their subsyntax. A generic URL matcher might be good enough.
*/
public const URL_CONTAINING_TAGS_WITH_SUBSYNTAX = [
'STYLE',
'SCRIPT',
];
}