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
516 lines (424 loc) · 12.6 KB
/
template.php
File metadata and controls
516 lines (424 loc) · 12.6 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/**
* @group template
*/
class Tests_Post_Template extends WP_UnitTestCase {
function test_wp_link_pages() {
$contents = array( 'One', 'Two', 'Three' );
$content = implode( '<!--nextpage-->', $contents );
$post_id = self::factory()->post->create( array( 'post_content' => $content ) );
$this->go_to( '?p=' . $post_id );
setup_postdata( get_post( $post_id ) );
$permalink = sprintf( '<a href="%s" class="post-page-numbers">', get_permalink() );
$page2 = _wp_link_page( 2 );
$page3 = _wp_link_page( 3 );
$expected = '<p class="post-nav-links">Pages: <span class="post-page-numbers current" aria-current="page">1</span> ' . $page2 . '2</a> ' . $page3 . '3</a></p>';
$output = wp_link_pages( array( 'echo' => 0 ) );
$this->assertSame( $expected, $output );
$before_after = " <span class=\"post-page-numbers current\" aria-current=\"page\">1</span> {$page2}2</a> {$page3}3</a>";
$output = wp_link_pages(
array(
'echo' => 0,
'before' => '',
'after' => '',
)
);
$this->assertSame( $before_after, $output );
$separator = " <span class=\"post-page-numbers current\" aria-current=\"page\">1</span>{$page2}2</a>{$page3}3</a>";
$output = wp_link_pages(
array(
'echo' => 0,
'before' => '',
'after' => '',
'separator' => '',
)
);
$this->assertSame( $separator, $output );
$link = " <span class=\"post-page-numbers current\" aria-current=\"page\"><em>1</em></span>{$page2}<em>2</em></a>{$page3}<em>3</em></a>";
$output = wp_link_pages(
array(
'echo' => 0,
'before' => '',
'after' => '',
'separator' => '',
'link_before' => '<em>',
'link_after' => '</em>',
)
);
$this->assertSame( $link, $output );
$next = "{$page2}<em>Next page</em></a>";
$output = wp_link_pages(
array(
'echo' => 0,
'before' => '',
'after' => '',
'separator' => '',
'link_before' => '<em>',
'link_after' => '</em>',
'next_or_number' => 'next',
)
);
$this->assertSame( $next, $output );
$GLOBALS['page'] = 2;
$next_prev = "{$permalink}<em>Previous page</em></a>{$page3}<em>Next page</em></a>";
$output = wp_link_pages(
array(
'echo' => 0,
'before' => '',
'after' => '',
'separator' => '',
'link_before' => '<em>',
'link_after' => '</em>',
'next_or_number' => 'next',
)
);
$this->assertSame( $next_prev, $output );
$next_prev_link = "{$permalink}Woo page</a>{$page3}Hoo page</a>";
$output = wp_link_pages(
array(
'echo' => 0,
'before' => '',
'after' => '',
'separator' => '',
'next_or_number' => 'next',
'nextpagelink' => 'Hoo page',
'previouspagelink' => 'Woo page',
)
);
$this->assertSame( $next_prev_link, $output );
$GLOBALS['page'] = 1;
$separator = "<p class=\"post-nav-links\">Pages: <span class=\"post-page-numbers current\" aria-current=\"page\">1</span> | {$page2}2</a> | {$page3}3</a></p>";
$output = wp_link_pages(
array(
'echo' => 0,
'separator' => ' | ',
)
);
$this->assertSame( $separator, $output );
$pagelink = " <span class=\"post-page-numbers current\" aria-current=\"page\">Page 1</span> | {$page2}Page 2</a> | {$page3}Page 3</a>";
$output = wp_link_pages(
array(
'echo' => 0,
'separator' => ' | ',
'before' => '',
'after' => '',
'pagelink' => 'Page %',
)
);
$this->assertSame( $pagelink, $output );
}
function test_wp_dropdown_pages() {
$none = wp_dropdown_pages( array( 'echo' => 0 ) );
$this->assertEmpty( $none );
$bump = ' ';
$page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
$child_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_parent' => $page_id,
)
);
$grandchild_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_parent' => $child_id,
)
);
$title1 = get_post( $page_id )->post_title;
$title2 = get_post( $child_id )->post_title;
$title3 = get_post( $grandchild_id )->post_title;
$lineage = <<<LINEAGE
<select name='page_id' id='page_id'>
<option class="level-0" value="$page_id">$title1</option>
<option class="level-1" value="$child_id">{$bump}$title2</option>
<option class="level-2" value="$grandchild_id">{$bump}{$bump}$title3</option>
</select>
LINEAGE;
$output = wp_dropdown_pages( array( 'echo' => 0 ) );
$this->assertSameIgnoreEOL( $lineage, $output );
$depth = <<<DEPTH
<select name='page_id' id='page_id'>
<option class="level-0" value="$page_id">$title1</option>
</select>
DEPTH;
$output = wp_dropdown_pages(
array(
'echo' => 0,
'depth' => 1,
)
);
$this->assertSameIgnoreEOL( $depth, $output );
$option_none = <<<NONE
<select name='page_id' id='page_id'>
<option value="Woo">Hoo</option>
<option class="level-0" value="$page_id">$title1</option>
</select>
NONE;
$output = wp_dropdown_pages(
array(
'echo' => 0,
'depth' => 1,
'show_option_none' => 'Hoo',
'option_none_value' => 'Woo',
)
);
$this->assertSameIgnoreEOL( $option_none, $output );
$option_no_change = <<<NO
<select name='page_id' id='page_id'>
<option value="-1">Burrito</option>
<option value="Woo">Hoo</option>
<option class="level-0" value="$page_id">$title1</option>
</select>
NO;
$output = wp_dropdown_pages(
array(
'echo' => 0,
'depth' => 1,
'show_option_none' => 'Hoo',
'option_none_value' => 'Woo',
'show_option_no_change' => 'Burrito',
)
);
$this->assertSameIgnoreEOL( $option_no_change, $output );
}
/**
* @ticket 12494
*/
public function test_wp_dropdown_pages_value_field_should_default_to_ID() {
$p = self::factory()->post->create(
array(
'post_type' => 'page',
)
);
$found = wp_dropdown_pages(
array(
'echo' => 0,
)
);
// Should contain page ID by default.
$this->assertStringContainsString( 'value="' . $p . '"', $found );
}
/**
* @ticket 12494
*/
public function test_wp_dropdown_pages_value_field_ID() {
$p = self::factory()->post->create(
array(
'post_type' => 'page',
)
);
$found = wp_dropdown_pages(
array(
'echo' => 0,
'value_field' => 'ID',
)
);
$this->assertStringContainsString( 'value="' . $p . '"', $found );
}
/**
* @ticket 12494
*/
public function test_wp_dropdown_pages_value_field_post_name() {
$p = self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'foo',
)
);
$found = wp_dropdown_pages(
array(
'echo' => 0,
'value_field' => 'post_name',
)
);
$this->assertStringContainsString( 'value="foo"', $found );
}
/**
* @ticket 12494
*/
public function test_wp_dropdown_pages_value_field_should_fall_back_on_ID_when_an_invalid_value_is_provided() {
$p = self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'foo',
)
);
$found = wp_dropdown_pages(
array(
'echo' => 0,
'value_field' => 'foo',
)
);
$this->assertStringContainsString( 'value="' . $p . '"', $found );
}
/**
* @ticket 30082
*/
public function test_wp_dropdown_pages_should_not_contain_class_attribute_when_no_class_is_passed() {
$p = self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'foo',
)
);
$found = wp_dropdown_pages(
array(
'echo' => 0,
)
);
$this->assertNotRegExp( '/<select[^>]+class=\'/', $found );
}
/**
* @ticket 30082
*/
public function test_wp_dropdown_pages_should_obey_class_parameter() {
$p = self::factory()->post->create(
array(
'post_type' => 'page',
'post_name' => 'foo',
)
);
$found = wp_dropdown_pages(
array(
'echo' => 0,
'class' => 'bar',
)
);
$this->assertRegExp( '/<select[^>]+class=\'bar\'/', $found );
}
/**
* @ticket 31389
*/
public function test_get_page_template_slug_by_id() {
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
)
);
$this->assertSame( '', get_page_template_slug( $page_id ) );
update_post_meta( $page_id, '_wp_page_template', 'default' );
$this->assertSame( '', get_page_template_slug( $page_id ) );
update_post_meta( $page_id, '_wp_page_template', 'example.php' );
$this->assertSame( 'example.php', get_page_template_slug( $page_id ) );
}
/**
* @ticket 31389
*/
public function test_get_page_template_slug_from_loop() {
$page_id = self::factory()->post->create(
array(
'post_type' => 'page',
)
);
update_post_meta( $page_id, '_wp_page_template', 'example.php' );
$this->go_to( get_permalink( $page_id ) );
$this->assertSame( 'example.php', get_page_template_slug() );
}
/**
* @ticket 31389
* @ticket 18375
*/
public function test_get_page_template_slug_non_page() {
$post_id = self::factory()->post->create();
$this->assertSame( '', get_page_template_slug( $post_id ) );
update_post_meta( $post_id, '_wp_page_template', 'default' );
$this->assertSame( '', get_page_template_slug( $post_id ) );
update_post_meta( $post_id, '_wp_page_template', 'example.php' );
$this->assertSame( 'example.php', get_page_template_slug( $post_id ) );
}
/**
* @ticket 18375
*/
public function test_get_page_template_slug_non_page_from_loop() {
$post_id = self::factory()->post->create();
update_post_meta( $post_id, '_wp_page_template', 'example.php' );
$this->go_to( get_permalink( $post_id ) );
$this->assertSame( 'example.php', get_page_template_slug() );
}
/**
* @ticket 11095
* @ticket 33974
*/
public function test_wp_page_menu_wp_nav_menu_fallback() {
$pages = self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) );
// No menus + wp_nav_menu() falls back to wp_page_menu().
$menu = wp_nav_menu( array( 'echo' => false ) );
// After falling back, the 'before' argument should be set and output as '<ul>'.
$this->assertRegExp( '/<div class="menu"><ul>/', $menu );
// After falling back, the 'after' argument should be set and output as '</ul>'.
$this->assertRegExp( '/<\/ul><\/div>/', $menu );
// After falling back, the markup should include whitespace around <li>'s.
$this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertNotRegExp( '/><li.*>|<\/li></U', $menu );
// No menus + wp_nav_menu() falls back to wp_page_menu(), this time without a container.
$menu = wp_nav_menu(
array(
'echo' => false,
'container' => false,
)
);
// After falling back, the empty 'container' argument should still return a container element.
$this->assertRegExp( '/<div class="menu">/', $menu );
// No menus + wp_nav_menu() falls back to wp_page_menu(), this time without white-space.
$menu = wp_nav_menu(
array(
'echo' => false,
'item_spacing' => 'discard',
)
);
// After falling back, the markup should not include whitespace around <li>'s.
$this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
$this->assertRegExp( '/><li.*>|<\/li></U', $menu );
}
/**
* @ticket 33045
*/
public function test_get_parent_post() {
$post = array(
'post_status' => 'publish',
'post_type' => 'page',
);
// Insert two initial posts.
$parent_id = self::factory()->post->create( $post );
$child_id = self::factory()->post->create( $post );
// Test if child get_parent_post() post returns Null by default.
$parent = get_post_parent( $child_id );
$this->assertNull( $parent );
// Update child post with a parent.
wp_update_post(
array(
'ID' => $child_id,
'post_parent' => $parent_id,
)
);
// Test if child get_parent_post() post returns the parent object.
$parent = get_post_parent( $child_id );
$this->assertNotNull( $parent );
$this->assertSame( $parent_id, $parent->ID );
}
/**
* @ticket 33045
*/
public function test_has_parent_post() {
$post = array(
'post_status' => 'publish',
'post_type' => 'page',
);
// Insert two initial posts.
$parent_id = self::factory()->post->create( $post );
$child_id = self::factory()->post->create( $post );
// Test if child has_parent_post() post returns False by default.
$parent = has_post_parent( $child_id );
$this->assertFalse( $parent );
// Update child post with a parent.
wp_update_post(
array(
'ID' => $child_id,
'post_parent' => $parent_id,
)
);
// Test if child has_parent_post() returns True.
$parent = has_post_parent( $child_id );
$this->assertTrue( $parent );
}
}