forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.php
More file actions
339 lines (276 loc) · 10.2 KB
/
template.php
File metadata and controls
339 lines (276 loc) · 10.2 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
<?php
/**
* @group oembed
*/
class Tests_Embed_Template extends WP_UnitTestCase {
function test_oembed_output_post() {
$user = self::factory()->user->create_and_get(
array(
'display_name' => 'John Doe',
)
);
$post_id = self::factory()->post->create(
array(
'post_author' => $user->ID,
'post_title' => 'Hello World',
'post_content' => 'Foo Bar',
'post_excerpt' => 'Bar Baz',
)
);
$this->go_to( get_post_embed_url( $post_id ) );
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_embed' );
ob_start();
include( ABSPATH . WPINC . '/theme-compat/embed.php' );
$actual = ob_get_clean();
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertFalse( strpos( $actual, 'That embed can’t be found.' ) );
$this->assertNotFalse( strpos( $actual, 'Hello World' ) );
}
function test_oembed_output_post_with_thumbnail() {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Hello World',
'post_content' => 'Foo Bar',
'post_excerpt' => 'Bar Baz',
)
);
$file = DIR_TESTDATA . '/images/canola.jpg';
$attachment_id = self::factory()->attachment->create_object(
$file,
$post_id,
array(
'post_mime_type' => 'image/jpeg',
)
);
set_post_thumbnail( $post_id, $attachment_id );
$this->go_to( get_post_embed_url( $post_id ) );
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_embed' );
ob_start();
include( ABSPATH . WPINC . '/theme-compat/embed.php' );
$actual = ob_get_clean();
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertFalse( strpos( $actual, 'That embed can’t be found.' ) );
$this->assertNotFalse( strpos( $actual, 'Hello World' ) );
$this->assertNotFalse( strpos( $actual, 'canola.jpg' ) );
}
function test_oembed_output_404() {
$this->go_to( home_url( '/?p=123&embed=true' ) );
$GLOBALS['wp_query']->query_vars['embed'] = true;
$this->assertQueryTrue( 'is_404', 'is_embed' );
ob_start();
include( ABSPATH . WPINC . '/theme-compat/embed.php' );
$actual = ob_get_clean();
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertNotFalse( strpos( $actual, 'That embed can’t be found.' ) );
}
function test_oembed_output_attachment() {
$post = self::factory()->post->create_and_get();
$file = DIR_TESTDATA . '/images/canola.jpg';
$attachment_id = self::factory()->attachment->create_object(
$file,
$post->ID,
array(
'post_mime_type' => 'image/jpeg',
'post_title' => 'Hello World',
'post_content' => 'Foo Bar',
'post_excerpt' => 'Bar Baz',
)
);
$this->go_to( get_post_embed_url( $attachment_id ) );
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_attachment', 'is_embed' );
ob_start();
include( ABSPATH . WPINC . '/theme-compat/embed.php' );
$actual = ob_get_clean();
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertFalse( strpos( $actual, 'That embed can’t be found.' ) );
$this->assertNotFalse( strpos( $actual, 'Hello World' ) );
$this->assertNotFalse( strpos( $actual, 'canola.jpg' ) );
}
function test_oembed_output_draft_post() {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Hello World',
'post_content' => 'Foo Bar',
'post_excerpt' => 'Bar Baz',
'post_status' => 'draft',
)
);
$this->go_to( get_post_embed_url( $post_id ) );
$this->assertQueryTrue( 'is_404', 'is_embed' );
ob_start();
include( ABSPATH . WPINC . '/theme-compat/embed.php' );
$actual = ob_get_clean();
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertNotFalse( strpos( $actual, 'That embed can’t be found.' ) );
}
function test_oembed_output_scheduled_post() {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Hello World',
'post_content' => 'Foo Bar',
'post_excerpt' => 'Bar Baz',
'post_status' => 'future',
'post_date' => strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 day' ) ),
)
);
$this->go_to( get_post_embed_url( $post_id ) );
$this->assertQueryTrue( 'is_404', 'is_embed' );
ob_start();
include( ABSPATH . WPINC . '/theme-compat/embed.php' );
$actual = ob_get_clean();
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertNotFalse( strpos( $actual, 'That embed can’t be found.' ) );
}
function test_oembed_output_private_post() {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Hello World',
'post_content' => 'Foo Bar',
'post_excerpt' => 'Bar Baz',
'post_status' => 'private',
)
);
$this->go_to( get_post_embed_url( $post_id ) );
$this->assertQueryTrue( 'is_404', 'is_embed' );
ob_start();
include( ABSPATH . WPINC . '/theme-compat/embed.php' );
$actual = ob_get_clean();
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertNotFalse( strpos( $actual, 'That embed can’t be found.' ) );
}
function test_oembed_output_private_post_with_permissions() {
$user_id = self::factory()->user->create( array( 'role' => 'editor' ) );
wp_set_current_user( $user_id );
$post_id = self::factory()->post->create(
array(
'post_title' => 'Hello World',
'post_content' => 'Foo Bar',
'post_excerpt' => 'Bar Baz',
'post_status' => 'private',
'post_author' => $user_id,
)
);
$this->go_to( get_post_embed_url( $post_id ) );
$this->assertQueryTrue( 'is_single', 'is_singular', 'is_embed' );
ob_start();
include( ABSPATH . WPINC . '/theme-compat/embed.php' );
$actual = ob_get_clean();
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertFalse( strpos( $actual, 'That embed can’t be found.' ) );
$this->assertNotFalse( strpos( $actual, 'Hello World' ) );
}
function test_wp_embed_excerpt_more_no_embed() {
$GLOBALS['wp_query'] = new WP_Query();
$this->assertEquals( 'foo bar', wp_embed_excerpt_more( 'foo bar' ) );
}
function test_wp_embed_excerpt_more() {
$post_id = self::factory()->post->create(
array(
'post_title' => 'Foo Bar',
'post_content' => 'Bar Baz',
)
);
$this->assertEquals( '', wp_embed_excerpt_more( '' ) );
$this->go_to( get_post_embed_url( $post_id ) );
$actual = wp_embed_excerpt_more( '' );
$expected = sprintf(
' … <a href="%s" class="wp-embed-more" target="_top">Continue reading <span class="screen-reader-text">Foo Bar</span></a>',
get_the_permalink()
);
$this->assertEquals( $expected, $actual );
}
function test_is_embed_post() {
$this->assertFalse( is_embed() );
$post_id = self::factory()->post->create();
$this->go_to( get_post_embed_url( $post_id ) );
$this->assertTrue( is_embed() );
}
function test_is_embed_attachment() {
$post_id = self::factory()->post->create();
$file = DIR_TESTDATA . '/images/canola.jpg';
$attachment_id = self::factory()->attachment->create_object(
$file,
$post_id,
array(
'post_mime_type' => 'image/jpeg',
)
);
$this->go_to( get_post_embed_url( $attachment_id ) );
$this->assertTrue( is_embed() );
}
function test_is_embed_404() {
$this->go_to( home_url( '/?p=12345&embed=true' ) );
$this->assertTrue( is_embed() );
}
function test_get_post_embed_html_non_existent_post() {
$this->assertFalse( get_post_embed_html( 200, 200, 0 ) );
$this->assertFalse( get_post_embed_html( 200, 200 ) );
}
function test_get_post_embed_html() {
$post_id = self::factory()->post->create();
$title = esc_attr(
sprintf(
__( '“%1$s” — %2$s' ),
get_the_title( $post_id ),
get_bloginfo( 'name' )
)
);
$expected = '<iframe sandbox="allow-scripts" security="restricted" src="' . esc_url( get_post_embed_url( $post_id ) ) . '" width="200" height="200" title="' . $title . '" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>';
$this->assertStringEndsWith( $expected, get_post_embed_html( 200, 200, $post_id ) );
}
function test_add_host_js() {
wp_oembed_add_host_js();
$this->assertTrue( wp_script_is( 'wp-embed' ) );
}
/**
* @ticket 34698
*/
function test_js_no_ampersands() {
$this->assertNotContains( '&', file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' ) );
}
/**
* @ticket 34698
*
* @depends test_js_no_ampersands
*
* The previous test confirms that no ampersands exist in src/wp-includes/js/wp-embed.js.
* However, we must also confirm that UglifyJS does not add ampersands during its
* optimizations (which we tweak to avoid, but indirectly -- understandably, there's
* no "don't add ampersands to my JavaScript file" option).
*
* So this test checks for ampersands in build/wp-includes/js/wp-embed.min.js.
* In many cases, this file will not exist; in those cases, we simply skip the test.
*
* So when would it be run? We have Travis CI run `grunt test` which then runs, in order,
* `qunit:compiled` (which runs the build) and then `phpunit`. Thus, this test will at least be
* run during continuous integration.
*
* However, we need to verify that `qunit:compiled` runs before `phpunit`. So this test also
* does a cheap check for a registered Grunt task called `test` that contains both
* `qunit:compiled` and `phpunit`, in that order.
*
* One final failsafe: The Gruntfile.js assertion takes place before checking for the existence
* of wp-embed.min.js. If the Grunt tasks are significantly refactored later, it could indicate
* that wp-embed.min.js doesn't exist anymore. We wouldn't want the test to silently become one
* that is always skipped, and thus useless.
*/
function test_js_no_ampersands_in_compiled() {
$gruntfile = file_get_contents( dirname( ABSPATH ) . '/Gruntfile.js' );
// Confirm this file *should* exist, otherwise this test will always be skipped.
$test = '/grunt.registerTask\(\s*\'test\',.*\'qunit:compiled\'.*\'phpunit\'/';
$this->assertTrue( (bool) preg_match( $test, $gruntfile ) );
$file = dirname( ABSPATH ) . '/build/' . WPINC . '/js/wp-embed.min.js';
if ( ! file_exists( $file ) ) {
return;
}
$this->assertNotContains( '&', file_get_contents( $file ) );
}
}