forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.php
More file actions
542 lines (451 loc) · 17.1 KB
/
filters.php
File metadata and controls
542 lines (451 loc) · 17.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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
/**
* Test apply_filters() and related functions
*
* @group hooks
*/
class Tests_Filters extends WP_UnitTestCase {
public function test_simple_filter() {
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
add_filter( $hook_name, array( $a, 'filter' ) );
$this->assertSame( $val, apply_filters( $hook_name, $val ) );
// Only one event occurred for the hook, with empty args.
$this->assertSame( 1, $a->get_call_count() );
// Only our hook was called.
$this->assertSame( array( $hook_name ), $a->get_hook_names() );
$argsvar = $a->get_args();
$args = array_pop( $argsvar );
$this->assertSame( array( $val ), $args );
}
public function test_remove_filter() {
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
add_filter( $hook_name, array( $a, 'filter' ) );
$this->assertSame( $val, apply_filters( $hook_name, $val ) );
// Make sure our hook was called correctly.
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $hook_name ), $a->get_hook_names() );
// Now remove the filter, do it again, and make sure it's not called this time.
remove_filter( $hook_name, array( $a, 'filter' ) );
$this->assertSame( $val, apply_filters( $hook_name, $val ) );
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $hook_name ), $a->get_hook_names() );
}
public function test_has_filter() {
$hook_name = __FUNCTION__;
$callback = __FUNCTION__ . '_func';
$this->assertFalse( has_filter( $hook_name, $callback ) );
$this->assertFalse( has_filter( $hook_name ) );
add_filter( $hook_name, $callback );
$this->assertSame( 10, has_filter( $hook_name, $callback ) );
$this->assertTrue( has_filter( $hook_name ) );
remove_filter( $hook_name, $callback );
$this->assertFalse( has_filter( $hook_name, $callback ) );
$this->assertFalse( has_filter( $hook_name ) );
}
// One tag with multiple filters.
public function test_multiple_filters() {
$a1 = new MockAction();
$a2 = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
// Add both filters to the hook.
add_filter( $hook_name, array( $a1, 'filter' ) );
add_filter( $hook_name, array( $a2, 'filter' ) );
$this->assertSame( $val, apply_filters( $hook_name, $val ) );
// Both filters called once each.
$this->assertSame( 1, $a1->get_call_count() );
$this->assertSame( 1, $a2->get_call_count() );
}
public function test_filter_args_1() {
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
$arg1 = __FUNCTION__ . '_arg1';
add_filter( $hook_name, array( $a, 'filter' ), 10, 2 );
// Call the filter with a single argument.
$this->assertSame( $val, apply_filters( $hook_name, $val, $arg1 ) );
$this->assertSame( 1, $a->get_call_count() );
$argsvar = $a->get_args();
$this->assertSame( array( $val, $arg1 ), array_pop( $argsvar ) );
}
public function test_filter_args_2() {
$a1 = new MockAction();
$a2 = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
$arg1 = __FUNCTION__ . '_arg1';
$arg2 = __FUNCTION__ . '_arg2';
// $a1 accepts two arguments, $a2 doesn't.
add_filter( $hook_name, array( $a1, 'filter' ), 10, 3 );
add_filter( $hook_name, array( $a2, 'filter' ) );
// Call the filter with two arguments.
$this->assertSame( $val, apply_filters( $hook_name, $val, $arg1, $arg2 ) );
// $a1 should be called with both args.
$this->assertSame( 1, $a1->get_call_count() );
$argsvar1 = $a1->get_args();
$this->assertSame( array( $val, $arg1, $arg2 ), array_pop( $argsvar1 ) );
// $a2 should be called with one only.
$this->assertSame( 1, $a2->get_call_count() );
$argsvar2 = $a2->get_args();
$this->assertSame( array( $val ), array_pop( $argsvar2 ) );
}
/**
* @ticket 60193
*
* @dataProvider data_priority_callback_order_with_integers
* @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
*
* @covers ::apply_filters
*
* @param array $priorities {
* Indexed array of the priorities for the MockAction callbacks.
*
* @type mixed $0 Priority for 'action' callback.
* @type mixed $1 Priority for 'action2' callback.
* }
* @param array $expected_call_order An array of callback names in expected call order.
* @param string $expected_deprecation Optional. Deprecation message. Default ''.
*/
public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
$mock = new MockAction();
$hook_name = __FUNCTION__;
if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
$this->expectDeprecation();
$this->expectDeprecationMessage( $expected_deprecation );
}
add_filter( $hook_name, array( $mock, 'filter' ), $priorities[0] );
add_filter( $hook_name, array( $mock, 'filter2' ), $priorities[1] );
apply_filters( $hook_name, __FUNCTION__ . '_val' );
$this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
$actual_call_order = wp_list_pluck( $mock->get_events(), 'filter' );
$this->assertSame( $expected_call_order, $actual_call_order, 'The filter callback order does not match the expected order' );
}
/**
* Happy path data provider.
*
* @return array[]
*/
public function data_priority_callback_order_with_integers() {
return array(
'int DESC' => array(
'priorities' => array( 10, 9 ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'int ASC' => array(
'priorities' => array( 9, 10 ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
);
}
/**
* Unhappy path data provider.
*
* @return array[]
*/
public function data_priority_callback_order_with_unhappy_path_nonintegers() {
return array(
// Numbers as strings and floats.
'int as string DESC' => array(
'priorities' => array( '10', '9' ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'int as string ASC' => array(
'priorities' => array( '9', '10' ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'float DESC' => array(
'priorities' => array( 10.0, 9.5 ),
'expected_call_order' => array( 'filter2', 'filter' ),
'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
),
'float ASC' => array(
'priorities' => array( 9.5, 10.0 ),
'expected_call_order' => array( 'filter', 'filter2' ),
'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
),
'float as string DESC' => array(
'priorities' => array( '10.0', '9.5' ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'float as string ASC' => array(
'priorities' => array( '9.5', '10.0' ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
// Non-numeric.
'null' => array(
'priorities' => array( null, null ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'bool DESC' => array(
'priorities' => array( true, false ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'bool ASC' => array(
'priorities' => array( false, true ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'non-numerical string DESC' => array(
'priorities' => array( 'test1', 'test2' ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'non-numerical string ASC' => array(
'priorities' => array( 'test1', 'test2' ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'int, non-numerical string DESC' => array(
'priorities' => array( 10, 'test' ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'int, non-numerical string ASC' => array(
'priorities' => array( 'test', 10 ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
'float, non-numerical string DESC' => array(
'priorities' => array( 10.0, 'test' ),
'expected_call_order' => array( 'filter2', 'filter' ),
),
'float, non-numerical string ASC' => array(
'priorities' => array( 'test', 10.0 ),
'expected_call_order' => array( 'filter', 'filter2' ),
),
);
}
/**
* @covers ::did_filter
*/
public function test_did_filter() {
$hook_name1 = 'filter1';
$hook_name2 = 'filter2';
$val = __FUNCTION__ . '_val';
// Apply filter $hook_name1 but not $hook_name2.
apply_filters( $hook_name1, $val );
$this->assertSame( 1, did_filter( $hook_name1 ) );
$this->assertSame( 0, did_filter( $hook_name2 ) );
// Apply filter $hook_name2 10 times.
$count = 10;
for ( $i = 0; $i < $count; $i++ ) {
apply_filters( $hook_name2, $val );
}
// $hook_name1's count hasn't changed, $hook_name2 should be correct.
$this->assertSame( 1, did_filter( $hook_name1 ) );
$this->assertSame( $count, did_filter( $hook_name2 ) );
}
public function test_all_filter() {
$a = new MockAction();
$hook_name1 = __FUNCTION__ . '_1';
$hook_name2 = __FUNCTION__ . '_2';
$val = __FUNCTION__ . '_val';
// Add an 'all' filter.
add_filter( 'all', array( $a, 'filterall' ) );
// Apply some filters.
$this->assertSame( $val, apply_filters( $hook_name1, $val ) );
$this->assertSame( $val, apply_filters( $hook_name2, $val ) );
$this->assertSame( $val, apply_filters( $hook_name1, $val ) );
$this->assertSame( $val, apply_filters( $hook_name1, $val ) );
// Our filter should have been called once for each apply_filters call.
$this->assertSame( 4, $a->get_call_count() );
// The right hooks should have been called in order.
$this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_hook_names() );
remove_filter( 'all', array( $a, 'filterall' ) );
$this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) );
}
public function test_remove_all_filter() {
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
add_filter( 'all', array( $a, 'filterall' ) );
$this->assertTrue( has_filter( 'all' ) );
$this->assertSame( 10, has_filter( 'all', array( $a, 'filterall' ) ) );
$this->assertSame( $val, apply_filters( $hook_name, $val ) );
// Make sure our hook was called correctly.
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $hook_name ), $a->get_hook_names() );
// Now remove the filter, do it again, and make sure it's not called this time.
remove_filter( 'all', array( $a, 'filterall' ) );
$this->assertFalse( has_filter( 'all', array( $a, 'filterall' ) ) );
$this->assertFalse( has_filter( 'all' ) );
$this->assertSame( $val, apply_filters( $hook_name, $val ) );
// Call count should remain at 1.
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $hook_name ), $a->get_hook_names() );
}
/**
* @ticket 20920
*/
public function test_remove_all_filters_should_respect_the_priority_argument() {
$a = new MockAction();
$hook_name = __FUNCTION__;
add_filter( $hook_name, array( $a, 'filter' ), 12 );
$this->assertTrue( has_filter( $hook_name ) );
// Should not be removed.
remove_all_filters( $hook_name, 11 );
$this->assertTrue( has_filter( $hook_name ) );
remove_all_filters( $hook_name, 12 );
$this->assertFalse( has_filter( $hook_name ) );
}
/**
* @ticket 53218
*/
public function test_filter_with_ref_value() {
$obj = new stdClass();
$ref = &$obj;
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $hook_name, array( $a, 'filter' ) );
$filtered = apply_filters( $hook_name, $ref );
$args = $a->get_args();
$this->assertSame( $args[0][0], $obj );
$this->assertSame( $filtered, $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][0]->foo );
$this->assertNotEmpty( $filtered->foo );
}
/**
* @ticket 53218
*/
public function test_filter_with_ref_argument() {
$obj = new stdClass();
$ref = &$obj;
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = 'Hello';
add_action( $hook_name, array( $a, 'filter' ), 10, 2 );
apply_filters( $hook_name, $val, $ref );
$args = $a->get_args();
$this->assertSame( $args[0][1], $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][1]->foo );
}
/**
* @ticket 9886
*/
public function test_filter_ref_array() {
$obj = new stdClass();
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $hook_name, array( $a, 'filter' ) );
apply_filters_ref_array( $hook_name, array( &$obj ) );
$args = $a->get_args();
$this->assertSame( $args[0][0], $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][0]->foo );
}
/**
* @ticket 12723
*/
public function test_filter_ref_array_result() {
$obj = new stdClass();
$a = new MockAction();
$b = new MockAction();
$hook_name = __FUNCTION__;
add_action( $hook_name, array( $a, 'filter_append' ), 10, 2 );
add_action( $hook_name, array( $b, 'filter_append' ), 10, 2 );
$result = apply_filters_ref_array( $hook_name, array( 'string', &$obj ) );
$this->assertSame( $result, 'string_append_append' );
$args = $a->get_args();
$this->assertSame( $args[0][1], $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][1]->foo );
$args = $b->get_args();
$this->assertSame( $args[0][1], $obj );
// Just in case we don't trust assertSame().
$obj->foo = true;
$this->assertNotEmpty( $args[0][1]->foo );
}
/**
* @ticket 29070
*/
public function test_has_filter_after_remove_all_filters() {
$a = new MockAction();
$hook_name = __FUNCTION__;
// No priority.
add_filter( $hook_name, array( $a, 'filter' ), 11 );
add_filter( $hook_name, array( $a, 'filter' ), 12 );
$this->assertTrue( has_filter( $hook_name ) );
remove_all_filters( $hook_name );
$this->assertFalse( has_filter( $hook_name ) );
// Remove priorities one at a time.
add_filter( $hook_name, array( $a, 'filter' ), 11 );
add_filter( $hook_name, array( $a, 'filter' ), 12 );
$this->assertTrue( has_filter( $hook_name ) );
remove_all_filters( $hook_name, 11 );
remove_all_filters( $hook_name, 12 );
$this->assertFalse( has_filter( $hook_name ) );
}
/**
* @ticket 10441
* @expectedDeprecated tests_apply_filters_deprecated
*/
public function test_apply_filters_deprecated() {
$p = 'Foo';
add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
$p = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p ), '4.6.0' );
remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback' ) );
$this->assertSame( 'Bar', $p );
}
public static function deprecated_filter_callback( $p ) {
$p = 'Bar';
return $p;
}
/**
* @ticket 10441
* @expectedDeprecated tests_apply_filters_deprecated
*/
public function test_apply_filters_deprecated_with_multiple_params() {
$p1 = 'Foo1';
$p2 = 'Foo2';
add_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
$p1 = apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $p1, $p2 ), '4.6.0' );
remove_filter( 'tests_apply_filters_deprecated', array( __CLASS__, 'deprecated_filter_callback_multiple_params' ), 10, 2 );
$this->assertSame( 'Bar1', $p1 );
// Not passed by reference, so not modified.
$this->assertSame( 'Foo2', $p2 );
}
public static function deprecated_filter_callback_multiple_params( $p1, $p2 ) {
$p1 = 'Bar1';
$p2 = 'Bar2';
return $p1;
}
/**
* @ticket 10441
*/
public function test_apply_filters_deprecated_without_filter() {
$val = 'Foobar';
$this->assertSame( $val, apply_filters_deprecated( 'tests_apply_filters_deprecated', array( $val ), '4.6.0' ) );
}
private $current_priority;
/**
* @ticket 39007
*/
public function test_current_priority() {
add_action( 'test_current_priority', array( $this, 'current_priority_action' ), 99 );
do_action( 'test_current_priority' );
remove_action( 'test_current_priority', array( $this, 'current_priority_action' ), 99 );
$this->assertSame( 99, $this->current_priority );
}
public function current_priority_action() {
global $wp_filter;
$this->current_priority = $wp_filter[ current_filter() ]->current_priority();
}
/**
* @ticket 39007
*/
public function test_other_priority() {
add_action( 'test_current_priority', array( $this, 'other_priority_action' ), 99 );
do_action( 'test_current_priority' );
remove_action( 'test_current_priority', array( $this, 'other_priority_action' ), 99 );
$this->assertFalse( $this->current_priority );
}
public function other_priority_action() {
global $wp_filter;
$this->current_priority = $wp_filter['the_content']->current_priority();
}
}