forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.php
More file actions
424 lines (365 loc) · 13.7 KB
/
meta.php
File metadata and controls
424 lines (365 loc) · 13.7 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
<?php
/**
* @group meta
*/
class Tests_Meta extends WP_UnitTestCase {
protected $updated_mids = array();
function setUp() {
parent::setUp();
$this->author = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
$this->meta_id = add_metadata( 'user', $this->author->ID, 'meta_key', 'meta_value' );
$this->delete_meta_id = add_metadata( 'user', $this->author->ID, 'delete_meta_key', 'delete_meta_value' );
}
function _meta_sanitize_cb( $meta_value, $meta_key, $meta_type ) {
return 'sanitized';
}
function test_sanitize_meta() {
$meta = sanitize_meta( 'some_meta', 'unsanitized', 'post' );
$this->assertEquals( 'unsanitized', $meta );
register_meta( 'post', 'some_meta', array( $this, '_meta_sanitize_cb' ) );
$meta = sanitize_meta( 'some_meta', 'unsanitized', 'post' );
$this->assertEquals( 'sanitized', $meta );
}
function test_delete_metadata_by_mid() {
// Let's try and delete a non-existing ID, non existing meta
$this->assertFalse( delete_metadata_by_mid( 'user', 0 ) );
$this->assertFalse( delete_metadata_by_mid( 'non_existing_meta', $this->delete_meta_id ) );
// Now let's delete the real meta data
$this->assertTrue( delete_metadata_by_mid( 'user', $this->delete_meta_id ) );
// And make sure it's been deleted
$this->assertFalse( get_metadata_by_mid( 'user', $this->delete_meta_id ) );
// Make sure the caches are cleared
$this->assertFalse( (bool) get_user_meta( $this->author->ID, 'delete_meta_key' ) );
}
function test_update_metadata_by_mid() {
// Setup
$meta = get_metadata_by_mid( 'user', $this->meta_id );
// Update the meta value
$this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value' ) );
$meta = get_metadata_by_mid( 'user', $this->meta_id );
$this->assertEquals( 'meta_new_value', $meta->meta_value );
// Update the meta value
$this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_new_value', 'meta_new_key' ) );
$meta = get_metadata_by_mid( 'user', $this->meta_id );
$this->assertEquals( 'meta_new_key', $meta->meta_key );
// Update the key and value
$this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', 'meta_key' ) );
$meta = get_metadata_by_mid( 'user', $this->meta_id );
$this->assertEquals( 'meta_key', $meta->meta_key );
$this->assertEquals( 'meta_value', $meta->meta_value );
// Update the value that has to be serialized
$this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, array( 'first', 'second' ) ) );
$meta = get_metadata_by_mid( 'user', $this->meta_id );
$this->assertEquals( array( 'first', 'second' ), $meta->meta_value );
// Let's try some invalid meta data
$this->assertFalse( update_metadata_by_mid( 'user', 0, 'meta_value' ) );
$this->assertFalse( update_metadata_by_mid( 'user', $this->meta_id, 'meta_value', array( 'invalid', 'key' ) ) );
// Let's see if caches get cleared after updates.
$meta = get_metadata_by_mid( 'user', $this->meta_id );
$first = get_user_meta( $meta->user_id, $meta->meta_key );
$this->assertTrue( update_metadata_by_mid( 'user', $this->meta_id, 'other_meta_value' ) );
$second = get_user_meta( $meta->user_id, $meta->meta_key );
$this->assertFalse( $first === $second );
}
/**
* @ticket 11683
*/
public function test_update_metadata_hooks_for_multiple_updated_rows() {
add_metadata( 'post', 1, 'test_key', 'value_1' );
add_metadata( 'post', 1, 'test_key', 'value_2' );
add_action( 'update_post_meta', array( $this, 'updated_meta' ) );
add_action( 'update_postmeta', array( $this, 'updated_meta' ) );
add_action( 'updated_post_meta', array( $this, 'updated_meta' ) );
add_action( 'updated_postmeta', array( $this, 'updated_meta' ) );
update_metadata( 'post', 1, 'test_key', 'value_3' );
remove_action( 'update_post_meta', array( $this, 'updated_meta' ) );
remove_action( 'update_postmeta', array( $this, 'updated_meta' ) );
remove_action( 'updated_post_meta', array( $this, 'updated_meta' ) );
remove_action( 'updated_postmeta', array( $this, 'updated_meta' ) );
$found = $this->updated_mids;
$this->updated_mids = array();
foreach ( $found as $action => $mids ) {
$this->assertSame( 2, count( $mids ) );
}
}
function test_metadata_exists() {
$this->assertFalse( metadata_exists( 'user', $this->author->ID, 'foobarbaz' ) );
$this->assertTrue( metadata_exists( 'user', $this->author->ID, 'meta_key' ) );
$this->assertFalse( metadata_exists( 'user', 1234567890, 'foobarbaz' ) );
$this->assertFalse( metadata_exists( 'user', 1234567890, 'meta_key' ) );
}
/**
* @ticket 22746
*/
function test_metadata_exists_with_filter() {
// Let's see if it returns the correct value when adding a filter.
add_filter( 'get_user_metadata', '__return_zero' );
$this->assertFalse( metadata_exists( 'user', $this->author->ID, 'meta_key' ) ); // existing meta key
$this->assertFalse( metadata_exists( 'user', 1234567890, 'meta_key' ) );
remove_filter( 'get_user_metadata', '__return_zero' );
}
/**
* @ticket 18158
*/
function test_user_metadata_not_exists() {
$u = get_users(
array(
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => 'NOT EXISTS',
),
),
)
);
$this->assertEquals( 1, count( $u ) );
// User found is not locally defined author (it's the admin)
$this->assertNotEquals( $this->author->user_login, $u[0]->user_login );
// Test EXISTS and NOT EXISTS together, no users should be found
$this->assertEquals(
0,
count(
get_users(
array(
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'delete_meta_key',
'compare' => 'EXISTS',
),
),
)
)
)
);
$this->assertEquals(
2,
count(
get_users(
array(
'meta_query' => array(
array(
'key' => 'non_existing_meta',
'compare' => 'NOT EXISTS',
),
),
)
)
)
);
delete_metadata( 'user', $this->author->ID, 'meta_key' );
$this->assertEquals(
2,
count(
get_users(
array(
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => 'NOT EXISTS',
),
),
)
)
)
);
}
function test_metadata_slashes() {
$key = __FUNCTION__;
$value = 'Test\\singleslash';
$expected = 'Testsingleslash';
$value2 = 'Test\\\\doubleslash';
$expected2 = 'Test\\doubleslash';
$this->assertFalse( metadata_exists( 'user', $this->author->ID, $key ) );
$this->assertFalse( delete_metadata( 'user', $this->author->ID, $key ) );
$this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
$this->assertInternalType( 'int', add_metadata( 'user', $this->author->ID, $key, $value ) );
$this->assertEquals( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
$this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
$this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
$this->assertInternalType( 'int', update_metadata( 'user', $this->author->ID, $key, $value ) );
$this->assertEquals( $expected, get_metadata( 'user', $this->author->ID, $key, true ) );
$this->assertTrue( update_metadata( 'user', $this->author->ID, $key, 'blah' ) );
$this->assertEquals( 'blah', get_metadata( 'user', $this->author->ID, $key, true ) );
$this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
$this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
$this->assertFalse( metadata_exists( 'user', $this->author->ID, $key ) );
// Test overslashing
$this->assertInternalType( 'int', add_metadata( 'user', $this->author->ID, $key, $value2 ) );
$this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
$this->assertTrue( delete_metadata( 'user', $this->author->ID, $key ) );
$this->assertSame( '', get_metadata( 'user', $this->author->ID, $key, true ) );
$this->assertInternalType( 'int', update_metadata( 'user', $this->author->ID, $key, $value2 ) );
$this->assertEquals( $expected2, get_metadata( 'user', $this->author->ID, $key, true ) );
}
/**
* @ticket 16814
*/
function test_meta_type_cast() {
$post_id1 = self::factory()->post->create();
add_post_meta( $post_id1, 'num_as_longtext', 123 );
add_post_meta( $post_id1, 'num_as_longtext_desc', 10 );
$post_id2 = self::factory()->post->create();
add_post_meta( $post_id2, 'num_as_longtext', 99 );
add_post_meta( $post_id2, 'num_as_longtext_desc', 100 );
$posts = new WP_Query(
array(
'fields' => 'ids',
'post_type' => 'any',
'meta_key' => 'num_as_longtext',
'meta_value' => '0',
'meta_compare' => '>',
'meta_type' => 'UNSIGNED',
'orderby' => 'meta_value',
'order' => 'ASC',
)
);
$this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
$this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
// Make sure the newer meta_query syntax behaves in a consistent way
$posts = new WP_Query(
array(
'fields' => 'ids',
'post_type' => 'any',
'meta_query' => array(
array(
'key' => 'num_as_longtext',
'value' => '0',
'compare' => '>',
'type' => 'UNSIGNED',
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
)
);
$this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
$this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
// The legacy `meta_key` value should take precedence.
$posts = new WP_Query(
array(
'fields' => 'ids',
'post_type' => 'any',
'meta_key' => 'num_as_longtext',
'meta_compare' => '>',
'meta_type' => 'UNSIGNED',
'meta_query' => array(
array(
'key' => 'num_as_longtext_desc',
'value' => '0',
'compare' => '>',
'type' => 'UNSIGNED',
),
),
'orderby' => 'meta_value',
'order' => 'ASC',
)
);
$this->assertEquals( array( $post_id2, $post_id1 ), $posts->posts );
$this->assertEquals( 2, substr_count( $posts->request, 'CAST(' ) );
}
function test_meta_cache_order_asc() {
$post_id = self::factory()->post->create();
$colors = array( 'red', 'blue', 'yellow', 'green' );
foreach ( $colors as $color ) {
add_post_meta( $post_id, 'color', $color );
}
foreach ( range( 1, 10 ) as $i ) {
$meta = get_post_meta( $post_id, 'color' );
$this->assertEquals( $meta, $colors );
if ( 0 === $i % 2 ) {
wp_cache_delete( $post_id, 'post_meta' );
}
}
}
/**
* @ticket 28315
*/
function test_non_numeric_object_id() {
$this->assertFalse( add_metadata( 'user', array( 1 ), 'meta_key', 'meta_value' ) );
$this->assertFalse( update_metadata( 'user', array( 1 ), 'meta_key', 'meta_new_value' ) );
$this->assertFalse( delete_metadata( 'user', array( 1 ), 'meta_key' ) );
$this->assertFalse( get_metadata( 'user', array( 1 ) ) );
$this->assertFalse( metadata_exists( 'user', array( 1 ), 'meta_key' ) );
}
/**
* @ticket 28315
*/
function test_non_numeric_meta_id() {
$this->assertFalse( get_metadata_by_mid( 'user', array( 1 ) ) );
$this->assertFalse( update_metadata_by_mid( 'user', array( 1 ), 'meta_new_value' ) );
$this->assertFalse( delete_metadata_by_mid( 'user', array( 1 ) ) );
}
/**
* @ticket 37746
*/
function test_negative_meta_id() {
$negative_mid = $this->meta_id * -1;
$this->assertTrue( $negative_mid < 0 );
$this->assertFalse( get_metadata_by_mid( 'user', $negative_mid ) );
$this->assertFalse( update_metadata_by_mid( 'user', $negative_mid, 'meta_new_value' ) );
$this->assertFalse( delete_metadata_by_mid( 'user', $negative_mid ) );
}
/**
* @ticket 37746
*/
function test_floating_meta_id() {
$floating_mid = $this->meta_id + 0.1337;
$this->assertTrue( floor( $floating_mid ) !== $floating_mid );
$this->assertFalse( get_metadata_by_mid( 'user', $floating_mid ) );
$this->assertFalse( update_metadata_by_mid( 'user', $floating_mid, 'meta_new_value' ) );
$this->assertFalse( delete_metadata_by_mid( 'user', $floating_mid ) );
}
/**
* @ticket 37746
*/
function test_string_point_zero_meta_id() {
$meta_id = add_metadata( 'user', $this->author->ID, 'meta_key', 'meta_value_2' );
$string_mid = "{$meta_id}.0";
$this->assertTrue( floor( $string_mid ) == $string_mid );
$this->assertNotEquals( false, get_metadata_by_mid( 'user', $string_mid ) );
$this->assertNotEquals( false, update_metadata_by_mid( 'user', $string_mid, 'meta_new_value_2' ) );
$this->assertNotEquals( false, delete_metadata_by_mid( 'user', $string_mid ) );
}
/**
* @ticket 15030
*/
public function test_get_metadata_with_empty_key_array_value() {
$data = array( 1, 2 );
$value = serialize( $data );
add_metadata( 'user', $this->author->ID, 'foo', $data );
$found = get_metadata( 'user', $this->author->ID );
$this->assertSame( array( $value ), $found['foo'] );
}
/**
* @ticket 15030
*/
public function test_get_metadata_with_empty_key_object_value() {
$data = new stdClass;
$data->foo = 'bar';
$value = serialize( $data );
add_metadata( 'user', $this->author->ID, 'foo', $data );
$found = get_metadata( 'user', $this->author->ID );
$this->assertEquals( array( $value ), $found['foo'] );
}
/**
* @ticket 15030
*/
public function test_get_metadata_with_empty_key_nested_array_value() {
$data = array(
array( 1, 2 ),
array( 3, 4 ),
);
$value = serialize( $data );
add_metadata( 'user', $this->author->ID, 'foo', $data );
$found = get_metadata( 'user', $this->author->ID );
$this->assertSame( array( $value ), $found['foo'] );
}
/** Helpers */
public function updated_meta( $meta_id ) {
$this->updated_mids[ current_action() ][] = $meta_id;
}
}