forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWpEmbed.php
More file actions
380 lines (290 loc) · 12.1 KB
/
WpEmbed.php
File metadata and controls
380 lines (290 loc) · 12.1 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* @group oembed
*/
class Tests_WP_Embed extends WP_UnitTestCase {
/**
* @var WP_Embed
*/
protected $wp_embed;
public function setUp() {
parent::setUp();
$this->wp_embed = new WP_Embed();
}
public function _embed_handler_callback( $matches, $attr, $url, $rawattr ) {
return sprintf( 'Embedded %s', $url );
}
public function _pre_oembed_result_callback() {
return '<b>Embedded content</b>';
}
public function test_maybe_run_ajax_cache_should_return_nothing_if_there_is_no_post() {
$this->expectOutputString( '' );
$this->wp_embed->maybe_run_ajax_cache();
}
public function test_maybe_run_ajax_cache_should_return_nothing_if_there_is_no_message() {
$GLOBALS['post'] = $this->factory()->post->create_and_get(
array(
'post_title' => 'Hello World',
)
);
$this->expectOutputString( '' );
$this->wp_embed->maybe_run_ajax_cache();
unset( $GLOBALS['post'] );
}
public function test_maybe_run_ajax_cache_should_return_javascript() {
$GLOBALS['post'] = $this->factory()->post->create_and_get(
array(
'post_title' => 'Hello World',
)
);
$_GET['message'] = 'foo';
$url = admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $GLOBALS['post']->ID, 'relative' );
$actual = get_echo( array( $this->wp_embed, 'maybe_run_ajax_cache' ) );
unset( $GLOBALS['post'] );
unset( $GLOBALS['message'] );
$this->assertStringContainsString( $url, $actual );
}
public function test_wp_maybe_load_embeds() {
$this->assertSameSets( array( 10, 9999 ), array_keys( $GLOBALS['wp_embed']->handlers ) );
$this->assertSameSets(
array(
'youtube_embed_url',
),
array_keys( $GLOBALS['wp_embed']->handlers[10] )
);
$this->assertSameSets(
array(
'audio',
'video',
),
array_keys( $GLOBALS['wp_embed']->handlers[9999] )
);
}
public function test_wp_embed_register_handler() {
$handle = __FUNCTION__;
$regex = '#https?://example\.com/embed/([^/]+)#i';
$callback = array( $this, '_embed_handler_callback' );
wp_embed_register_handler( $handle, $regex, $callback );
$expected = array(
'regex' => $regex,
'callback' => $callback,
);
$actual = $GLOBALS['wp_embed']->handlers[10];
wp_embed_unregister_handler( $handle );
$this->assertContains( $expected, $actual );
}
public function test_wp_embed_unregister_handler() {
$this->assertArrayHasKey( 'youtube_embed_url', $GLOBALS['wp_embed']->handlers[10] );
wp_embed_unregister_handler( 'youtube_embed_url' );
$handlers = $GLOBALS['wp_embed']->handlers[10];
// Restore.
wp_embed_register_handler( 'youtube_embed_url', '#https?://(www.)?youtube\.com/(?:v|embed)/([^/]+)#i', 'wp_embed_handler_youtube' );
$this->assertArrayNotHasKey( 'youtube_embed_url', $handlers );
}
/**
* @group external-http
*/
public function test_autoembed_should_do_nothing_without_matching_handler() {
$content = "\nhttp://example.com/embed/foo\n";
$actual = $this->wp_embed->autoembed( $content );
$this->assertSame( $content, $actual );
}
/**
* @group external-http
*/
public function test_autoembed_should_return_modified_content() {
$handle = __FUNCTION__;
$regex = '#https?://example\.com/embed/([^/]+)#i';
$callback = array( $this, '_embed_handler_callback' );
wp_embed_register_handler( $handle, $regex, $callback );
$content = "\nhttp://example.com/embed/foo\n";
$actual = $GLOBALS['wp_embed']->autoembed( $content );
wp_embed_unregister_handler( $handle );
$this->assertSame( "\nEmbedded http://example.com/embed/foo\n", $actual );
}
public function test_delete_oembed_caches() {
$post_id = $this->factory()->post->create();
add_post_meta( $post_id, '_oembed_foo', 'bar' );
add_post_meta( $post_id, '_oembed_foo', 'baz' );
add_post_meta( $post_id, '_oembed_baz', 'foobar', true );
$this->wp_embed->delete_oembed_caches( $post_id );
$this->assertSame( array(), get_post_meta( $post_id, '_oembed_foo' ) );
$this->assertSame( array(), get_post_meta( $post_id, '_oembed_baz' ) );
}
public function test_cache_oembed_invalid_post_type() {
$post_id = $this->factory()->post->create( array( 'post_type' => 'nav_menu_item' ) );
$this->wp_embed->cache_oembed( $post_id );
$this->assertNotSame( $post_id, $this->wp_embed->post_ID );
}
public function test_cache_oembed_empty_content() {
$post_id = $this->factory()->post->create( array( 'post_content' => '' ) );
$this->wp_embed->cache_oembed( $post_id );
$this->assertNotSame( $post_id, $this->wp_embed->post_ID );
}
public function test_cache_oembed_for_post() {
$url = 'https://example.com/';
$expected = '<b>Embedded content</b>';
$key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
$cachekey = '_oembed_' . $key_suffix;
$cachekey_time = '_oembed_time_' . $key_suffix;
$post_id = $this->factory()->post->create( array( 'post_content' => 'https://example.com/' ) );
add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
$this->wp_embed->cache_oembed( $post_id );
remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
$this->assertSame( $post_id, $this->wp_embed->post_ID );
$this->assertSame( $expected, get_post_meta( $post_id, $cachekey, true ) );
$this->assertNotEmpty( get_post_meta( $post_id, $cachekey_time, true ) );
}
public function test_shortcode_should_get_cached_data_from_post_meta_for_known_post() {
global $post;
$post = $this->factory()->post->create_and_get();
$url = 'https://example.com/';
$expected = '<b>Embedded content</b>';
$key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
$cachekey = '_oembed_' . $key_suffix;
add_post_meta( $post->ID, $cachekey, $expected );
add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
$actual = $this->wp_embed->shortcode( array(), $url );
remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
$actual_2 = $this->wp_embed->shortcode( array(), $url );
$cached = get_post_meta( $post->ID, $cachekey, true );
// Cleanup.
unset( $post );
$this->assertSame( $expected, $actual );
$this->assertSame( $expected, $actual_2 );
$this->assertSame( $expected, $cached );
}
public function test_shortcode_should_get_cached_failure_from_post_meta_for_known_post() {
global $post;
$post = $this->factory()->post->create_and_get();
$url = 'https://example.com/';
$expected = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
$key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
$cachekey = '_oembed_' . $key_suffix;
$cachekey_time = '_oembed_time_' . $key_suffix;
add_post_meta( $post->ID, $cachekey, '{{unknown}}' );
add_post_meta( $post->ID, $cachekey_time, 0 );
add_filter( 'pre_oembed_result', '__return_empty_string' );
$actual = $this->wp_embed->shortcode( array(), $url );
remove_filter( 'pre_oembed_result', '__return_empty_string' );
// Result should be cached.
$actual_2 = $this->wp_embed->shortcode( array(), $url );
$cached = get_post_meta( $post->ID, $cachekey, true );
$cached_time = get_post_meta( $post->ID, $cachekey_time, true );
// Cleanup.
unset( $post );
$this->assertSame( $expected, $actual );
$this->assertSame( '{{unknown}}', $cached );
$this->assertEmpty( $cached_time );
$this->assertSame( $expected, $actual_2 );
}
/**
* @ticket 34115
*/
public function test_shortcode_should_cache_data_in_custom_post() {
$url = 'https://example.com/';
$expected = '<b>Embedded content</b>';
$key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
$actual = $this->wp_embed->shortcode( array(), $url );
remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
$oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
$post_content = get_post( $oembed_post_id )->post_content;
// Result should be cached.
$actual_2 = $this->wp_embed->shortcode( array(), $url );
wp_delete_post( $oembed_post_id );
$this->assertNotNull( $oembed_post_id );
$this->assertSame( $expected, $post_content );
$this->assertSame( $expected, $actual );
$this->assertSame( $expected, $actual_2 );
}
/**
* @ticket 34115
*/
public function test_shortcode_should_cache_failure_in_custom_post() {
$url = 'https://example.com/';
$expected = '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>';
$key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
add_filter( 'pre_oembed_result', '__return_empty_string' );
$actual = $this->wp_embed->shortcode( array(), $url );
remove_filter( 'pre_oembed_result', '__return_empty_string' );
$oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
$post_content = get_post( $oembed_post_id )->post_content;
// Result should be cached.
$actual_2 = $this->wp_embed->shortcode( array(), $url );
wp_delete_post( $oembed_post_id );
$this->assertSame( $expected, $actual );
$this->assertSame( $expected, $actual_2 );
$this->assertNotNull( $oembed_post_id );
$this->assertSame( '{{unknown}}', $post_content );
}
/**
* Test that parsing an embed shortcode should cause oembed_cache to be updated.
*
* @ticket 42310
*/
public function test_shortcode_should_update_custom_post() {
add_filter( 'oembed_ttl', '__return_zero' );
$url = 'https://example.com/';
$embedded = '<b>Embedded content</b>';
$key_suffix = md5( $url . serialize( wp_embed_defaults( $url ) ) );
add_filter( 'pre_oembed_result', '__return_empty_string' );
$this->wp_embed->shortcode( array(), $url );
remove_filter( 'pre_oembed_result', '__return_empty_string' );
$oembed_post_id = $this->wp_embed->find_oembed_post_id( $key_suffix );
$this->assertSame( '{{unknown}}', get_post( $oembed_post_id )->post_content );
$previous_usecache = $this->wp_embed->usecache;
$this->wp_embed->usecache = false;
// The update cannot be empty because empty responses won't overwrite the cache.
add_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
$this->wp_embed->shortcode( array(), $url );
remove_filter( 'pre_oembed_result', array( $this, '_pre_oembed_result_callback' ) );
$this->assertSame( $embedded, get_post( $oembed_post_id )->post_content );
$this->wp_embed->usecache = $previous_usecache;
remove_filter( 'oembed_ttl', '__return_zero' );
}
/**
* @group external-http
*/
public function test_shortcode_should_get_url_from_src_attribute() {
$url = 'http://example.com/embed/foo';
$actual = $this->wp_embed->shortcode( array( 'src' => $url ) );
$this->assertSame( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
}
/**
* @group external-http
*/
public function test_shortcode_should_return_empty_string_for_missing_url() {
$this->assertEmpty( $this->wp_embed->shortcode( array() ) );
}
/**
* @group external-http
*/
public function test_shortcode_should_make_link_for_unknown_url() {
$url = 'http://example.com/embed/foo';
$actual = $this->wp_embed->shortcode( array(), $url );
$this->assertSame( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
}
/**
* @group external-http
*/
public function test_run_shortcode_url_only() {
$url = 'http://example.com/embed/foo';
$actual = $this->wp_embed->run_shortcode( '[embed]' . $url . '[/embed]' );
$this->assertSame( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
}
public function test_maybe_make_link() {
$url = 'http://example.com/embed/foo';
$actual = $this->wp_embed->maybe_make_link( $url );
$this->assertSame( '<a href="' . esc_url( $url ) . '">' . esc_html( $url ) . '</a>', $actual );
}
public function test_maybe_make_link_return_false_on_fail() {
$this->wp_embed->return_false_on_fail = true;
$this->assertFalse( $this->wp_embed->maybe_make_link( 'http://example.com/' ) );
}
public function test_maybe_make_link_do_not_link_if_unknown() {
$url = 'http://example.com/';
$this->wp_embed->linkifunknown = false;
$this->assertSame( $url, $this->wp_embed->maybe_make_link( $url ) );
}
}