forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPages.php
More file actions
393 lines (317 loc) · 14.2 KB
/
getPages.php
File metadata and controls
393 lines (317 loc) · 14.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
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
<?php
/**
* @group post
*/
class Tests_Post_getPages extends WP_UnitTestCase {
function setUp() {
parent::setUp();
}
/**
* @ticket 23167
*/
function test_get_pages_cache() {
global $wpdb;
self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) );
wp_cache_delete( 'last_changed', 'posts' );
$this->assertFalse( wp_cache_get( 'last_changed', 'posts' ) );
$pages = get_pages();
$this->assertEquals( 3, count( $pages ) );
$this->assertNotEmpty( $time1 = wp_cache_get( 'last_changed', 'posts' ) );
$num_queries = $wpdb->num_queries;
foreach ( $pages as $page )
$this->assertInstanceOf( 'WP_Post', $page );
// Again. num_queries and last_changed should remain the same.
$pages = get_pages();
$this->assertEquals( 3, count( $pages ) );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
foreach ( $pages as $page )
$this->assertInstanceOf( 'WP_Post', $page );
// Again with different args. last_changed should not increment because of
// different args to get_pages(). num_queries should bump by 1.
$pages = get_pages( array( 'number' => 2 ) );
$this->assertEquals( 2, count( $pages ) );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
foreach ( $pages as $page )
$this->assertInstanceOf( 'WP_Post', $page );
$num_queries = $wpdb->num_queries;
// Again. num_queries and last_changed should remain the same.
$pages = get_pages( array( 'number' => 2 ) );
$this->assertEquals( 2, count( $pages ) );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
foreach ( $pages as $page )
$this->assertInstanceOf( 'WP_Post', $page );
// Do the first query again. The interim queries should not affect it.
$pages = get_pages();
$this->assertEquals( 3, count( $pages ) );
$this->assertEquals( $time1, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries, $wpdb->num_queries );
foreach ( $pages as $page )
$this->assertInstanceOf( 'WP_Post', $page );
// Force last_changed to increment.
clean_post_cache( $pages[0]->ID );
$this->assertNotEquals( $time1, $time2 = wp_cache_get( 'last_changed', 'posts' ) );
$num_queries = $wpdb->num_queries;
// last_changed bumped so num_queries should increment.
$pages = get_pages( array( 'number' => 2 ) );
$this->assertEquals( 2, count( $pages ) );
$this->assertEquals( $time2, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
foreach ( $pages as $page )
$this->assertInstanceOf( 'WP_Post', $page );
$last_changed = wp_cache_get( 'last_changed', 'posts' );
// This should bump last_changed.
wp_delete_post( $pages[0]->ID );
$old_changed_float = $this->_microtime_to_float( $last_changed );
$new_changed_float = $this->_microtime_to_float( wp_cache_get( 'last_changed', 'posts' ) );
$this->assertGreaterThan( $old_changed_float, $new_changed_float );
$num_queries = $wpdb->num_queries;
$last_changed = wp_cache_get( 'last_changed', 'posts' );
// num_queries should bump after wp_delete_post() bumps last_changed.
$pages = get_pages();
$this->assertEquals( 2, count( $pages ) );
$this->assertEquals( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
$this->assertEquals( $num_queries + 1, $wpdb->num_queries );
foreach ( $pages as $page )
$this->assertInstanceOf( 'WP_Post', $page );
}
/**
* @ticket 20376
*/
function test_get_pages_meta() {
$posts = self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) );
add_post_meta( $posts[0], 'some-meta-key', '0' );
add_post_meta( $posts[1], 'some-meta-key', '' );
add_post_meta( $posts[2], 'some-meta-key', '1' );
$this->assertEquals( 1, count( get_pages( array( 'meta_key' => 'some-meta-key', 'meta_value' => '0' ) ) ) );
$this->assertEquals( 1, count( get_pages( array( 'meta_key' => 'some-meta-key', 'meta_value' => '1' ) ) ) );
$this->assertEquals( 3, count( get_pages( array( 'meta_key' => 'some-meta-key' ) ) ) );
}
/**
* @ticket 22074
*/
function test_get_pages_include_exclude() {
$page_ids = array();
foreach ( range( 1, 20 ) as $i )
$page_ids[] = self::factory()->post->create( array( 'post_type' => 'page' ) );
$inc = array_slice( $page_ids, 0, 10 );
sort( $inc );
$exc = array_slice( $page_ids, 10 );
sort( $exc );
$include = get_pages( array( 'include' => $inc ) );
$inc_result = wp_list_pluck( $include, 'ID' );
sort( $inc_result );
$this->assertEquals( $inc, $inc_result );
$exclude = get_pages( array( 'exclude' => $exc ) );
$exc_result = wp_list_pluck( $exclude, 'ID' );
sort( $exc_result );
$this->assertEquals( $inc, $exc_result );
}
/**
* @ticket 9470
*/
function test_get_pages_parent() {
$page_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_id2 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
$page_id3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id2 ) );
$page_id4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_id1 ) );
$pages = get_pages( array( 'parent' => 0, 'hierarchical' => false ) );
$this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => $page_id1, 'hierarchical' => false ) );
$this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ), 'hierarchical' => false ) );
$this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => 0 ) );
$this->assertEqualSets( array( $page_id1 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => $page_id1 ) );
$this->assertEqualSets( array( $page_id2, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => array( $page_id1, $page_id2 ) ) );
$this->assertEqualSets( array( $page_id2, $page_id3, $page_id4 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 22389
*/
function test_wp_dropdown_pages() {
self::factory()->post->create_many( 5, array( 'post_type' => 'page' ) );
preg_match_all( '#<option#', wp_dropdown_pages( 'echo=0' ), $matches );
$this->assertEquals( 5, count( $matches[0] ) );
}
/**
* @ticket 22208
*/
function test_get_chidren_fields_ids() {
$post_id = self::factory()->post->create();
$child_ids = self::factory()->post->create_many( 5, array( 'post_parent' => $post_id ) );
$post_ids = get_children( array( 'fields' => 'ids', 'post_parent' => $post_id ) );
$this->assertEqualSets( $child_ids, $post_ids );
}
/**
* @ticket 25750
*/
function test_get_pages_hierarchical_and_no_parent() {
global $wpdb;
$page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_2 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_2 ) );
$pages = get_pages(); // Defaults: hierarchical = true, parent = -1
$pages_default_args = get_pages( array( 'hierarchical' => true, 'parent' => -1 ) );
// Confirm the defaults.
$this->assertEquals( $pages, $pages_default_args );
/*
* Here's the tree we are testing:
*
* page 1
* - page 2
* -- page 4
* - page 3
*
* If hierarchical => true works, the order will be 1,2,4,3.
* If it doesn't, they will be in the creation order, 1,2,3,4.
*/
$this->assertEqualSets( array( $page_1, $page_2, $page_4, $page_3 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 18701
*/
public function test_get_pages_hierarchical_empty_child_of() {
$page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$pages = get_pages(); // Defaults: hierarchical = true, child_of = '', parent = -1
$default_args = get_pages( array(
'hierarchical' => true,
'child_of' => ''
) );
$this->assertEquals( $pages, $default_args );
/*
* Page tree:
*
* page 1 (parent 0)
* – page 3 (parent 1)
* – page 4 (parent 1)
* page 2 (parent 0)
*
* With default arguments, if child_of is empty (normalized to 0), only pages with a matching
* post_parent will be returned, in the order they were created: 1, 2.
*/
$found_pages = wp_list_filter( $pages, array( 'post_parent' => 0 ) );
$this->assertEqualSets( array( $page_1, $page_2 ), wp_list_pluck( $found_pages, 'ID' ) );
}
/**
* @ticket 18701
*/
public function test_get_pages_non_hierarchical_empty_child_of() {
$page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$pages = get_pages( array( 'hierarchical' => false ) ); // child_of = '', parent = -1
/*
* Page tree:
*
* page 1 (parent 0)
* – page 3 (parent 1)
* – page 4 (parent 1)
* page 2 (parent 0)
*
* If hierarchical is false and child_of is empty (normalized to 0), pages will be returned
* in order of creation: 1, 2, 3, 4, regardless of parent.
*/
$this->assertEqualSets( array( $page_1, $page_2, $page_3, $page_4 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 18701
*/
public function test_get_pages_hierarchical_non_empty_child_of() {
$page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_3 ) );
$page_5 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$pages = get_pages( array( 'child_of' => $page_1 ) ); // Defaults: hierarchical = true, parent = -1.
/*
* Page tree:
*
* page 1 (parent 0)
* – page 3 (parent 1)
* –– page 4 (parent 3)
* – page 5 (parent 1)
* page 2 (parent 0)
*
* If hierarchical is true (default), and child_of is not empty, pages will be returned
* hierarchically in order of creation: 3, 4, 5.
*/
$this->assertEqualSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
}
/**
* @ticket 18701
*/
public function test_get_pages_non_hierarchical_non_empty_child_of() {
$page_1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_2 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$page_3 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$page_4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_3 ) );
$page_5 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $page_1 ) );
$pages = get_pages( array( 'hierarchical' => false, 'child_of' => $page_1 ) );
/*
* Page tree:
*
* page 1 (parent 0)
* – page 3 (parent 1)
* –– page 4 (parent 3)
* – page 5 (parent 1)
* page 2 (parent 0)
*
* If hierarchical is false, and child_of is not empty, pages will (apparently) be returned
* hierarchically anyway in order of creation: 3, 4, 5.
*/
$this->assertEqualSets( array( $page_3, $page_4, $page_5 ), wp_list_pluck( $pages, 'ID' ) );
// How it should work.
$found_pages = wp_list_filter( $pages, array( 'post_parent' => $page_1 ) );
$this->assertEqualSets( array( $page_3, $page_5 ), wp_list_pluck( $found_pages, 'ID' ) );
}
function test_wp_list_pages_classes() {
$type = 'taco';
register_post_type( $type, array( 'hierarchical' => true, 'public' => true ) );
$posts = self::factory()->post->create_many( 2, array( 'post_type' => $type ) );
$post_id = reset( $posts );
$this->go_to( "/?p=$post_id&post_type=$type" );
$this->assertEquals( $post_id, get_queried_object_id() );
$output = wp_list_pages( array(
'echo' => false,
'title_li' => '',
'post_type' => $type
) );
$this->assertNotEmpty( $output );
$this->assertEquals( 2, substr_count( $output, 'class="page_item ' ) );
$this->assertContains( 'current_page_item', $output );
$this->assertEquals( 1, substr_count( $output, 'current_page_item' ) );
_unregister_post_type( $type );
}
function test_exclude_tree() {
$post_id1 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$post_id2 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id1 ) );
$post_id3 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$post_id4 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id3 ) );
$all = get_pages();
$this->assertCount( 4, $all );
$exclude1 = get_pages( "exclude_tree=$post_id1" );
$this->assertCount( 2, $exclude1 );
$exclude2 = get_pages( array( 'exclude_tree' => $post_id1 ) );
$this->assertCount( 2, $exclude2 );
$exclude3 = get_pages( array( 'exclude_tree' => array( $post_id1 ) ) );
$this->assertCount( 2, $exclude3 );
$exclude4 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id2 ) ) );
$this->assertCount( 2, $exclude4 );
$exclude5 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
$this->assertCount( 0, $exclude5 );
$post_id5 = self::factory()->post->create( array( 'post_type' => 'page' ) );
$post_id6 = self::factory()->post->create( array( 'post_type' => 'page', 'post_parent' => $post_id5 ) );
$exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
$this->assertCount( 2, $exclude6 );
}
}