forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.php
More file actions
849 lines (710 loc) · 26.1 KB
/
actions.php
File metadata and controls
849 lines (710 loc) · 26.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
<?php
/**
* Test do_action() and related functions
*
* @group hooks
*/
class Tests_Actions extends WP_UnitTestCase {
/**
* Flag to keep track whether a certain filter has been applied.
*
* Used in the `test_doing_filter_real()` test method.
*
* @var bool
*/
private $apply_testing_filter = false;
/**
* Flag to keep track whether a certain filter has been applied.
*
* Used in the `test_doing_filter_real()` test method.
*
* @var bool
*/
private $apply_testing_nested_filter = false;
/**
* Clean up after each test.
*/
public function tear_down() {
// Make sure potentially changed properties are reverted to their default value.
$this->apply_testing_filter = false;
$this->apply_testing_nested_filter = false;
parent::tear_down();
}
/**
* @covers ::do_action
*/
public function test_simple_action() {
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $hook_name, array( &$a, 'action' ) );
do_action( $hook_name );
// 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( '' ), $args );
}
/**
* @covers ::remove_action
*/
public function test_remove_action() {
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $hook_name, array( &$a, 'action' ) );
add_action( $hook_name, array( &$a, 'action' ), 100 );
do_action( $hook_name );
// Make sure our hook was called correctly.
$this->assertSame( 2, $a->get_call_count() );
$this->assertSame( array( $hook_name, $hook_name ), $a->get_hook_names() );
// Now remove the action, do it again, and make sure it's not called this time.
remove_action( $hook_name, array( &$a, 'action' ) );
remove_action( $hook_name, array( &$a, 'action' ), 100 );
do_action( $hook_name );
$this->assertSame( 2, $a->get_call_count() );
$this->assertSame( array( $hook_name, $hook_name ), $a->get_hook_names() );
}
/**
* @ticket 64186
* @covers ::has_action
*/
public function test_has_action() {
$hook_name = __FUNCTION__;
$callback = __FUNCTION__ . '_func';
$this->assertFalse( has_action( $hook_name, $callback ) );
$this->assertFalse( has_action( $hook_name ) );
add_action( $hook_name, $callback );
$this->assertSame( 10, has_action( $hook_name, $callback ) );
$this->assertFalse( has_action( $hook_name, $callback, 9 ) );
$this->assertTrue( has_action( $hook_name ) );
add_action( $hook_name, $callback, 9 );
add_action( $hook_name, $callback, 11 );
$this->assertSame( 9, has_action( $hook_name, $callback ) );
$this->assertTrue( has_action( $hook_name, $callback, 9 ) );
$this->assertTrue( has_action( $hook_name, $callback, 10 ) );
$this->assertTrue( has_action( $hook_name, $callback, 11 ) );
$this->assertTrue( has_action( $hook_name ) );
remove_action( $hook_name, $callback, 9 );
remove_action( $hook_name, $callback, 11 );
$this->assertSame( 10, has_action( $hook_name, $callback ) );
remove_action( $hook_name, $callback );
$this->assertFalse( has_action( $hook_name, $callback ) );
$this->assertFalse( has_action( $hook_name ) );
}
/**
* One tag with multiple actions.
*
* @covers ::do_action
*/
public function test_multiple_actions() {
$a1 = new MockAction();
$a2 = new MockAction();
$hook_name = __FUNCTION__;
// Add both actions to the hook.
add_action( $hook_name, array( &$a1, 'action' ) );
add_action( $hook_name, array( &$a2, 'action' ) );
do_action( $hook_name );
// Both actions called once each.
$this->assertSame( 1, $a1->get_call_count() );
$this->assertSame( 1, $a2->get_call_count() );
}
/**
* One tag with multiple actions.
*
* @covers ::do_action
*/
public function test_action_args_1() {
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = __FUNCTION__ . '_val';
add_action( $hook_name, array( &$a, 'action' ) );
// Call the action with a single argument.
do_action( $hook_name, $val );
$call_count = $a->get_call_count();
$this->assertSame( 1, $call_count );
$argsvar = $a->get_args();
$this->assertSame( array( $val ), array_pop( $argsvar ) );
}
/**
* One tag with multiple actions.
*
* @covers ::do_action
*/
public function test_action_args_2() {
$a1 = new MockAction();
$a2 = new MockAction();
$hook_name = __FUNCTION__;
$val1 = __FUNCTION__ . '_val1';
$val2 = __FUNCTION__ . '_val2';
// $a1 accepts two arguments, $a2 doesn't.
add_action( $hook_name, array( &$a1, 'action' ), 10, 2 );
add_action( $hook_name, array( &$a2, 'action' ) );
// Call the action with two arguments.
do_action( $hook_name, $val1, $val2 );
$call_count = $a1->get_call_count();
// $a1 should be called with both args.
$this->assertSame( 1, $call_count );
$argsvar1 = $a1->get_args();
$this->assertSame( array( $val1, $val2 ), array_pop( $argsvar1 ) );
// $a2 should be called with one only.
$this->assertSame( 1, $a2->get_call_count() );
$argsvar2 = $a2->get_args();
$this->assertSame( array( $val1 ), array_pop( $argsvar2 ) );
}
/**
* Test that multiple callbacks receive the correct number of args even when the number
* is less than, or greater than previous hooks.
*
* @see https://core.trac.wordpress.org/ticket/17817#comment:72
* @ticket 17817
*
* @covers ::do_action
*/
public function test_action_args_3() {
$a1 = new MockAction();
$a2 = new MockAction();
$a3 = new MockAction();
$hook_name = __FUNCTION__;
$val1 = __FUNCTION__ . '_val1';
$val2 = __FUNCTION__ . '_val2';
// $a1 accepts two arguments, $a2 doesn't, $a3 accepts two arguments.
add_action( $hook_name, array( &$a1, 'action' ), 10, 2 );
add_action( $hook_name, array( &$a2, 'action' ) );
add_action( $hook_name, array( &$a3, 'action' ), 10, 2 );
// Call the action with two arguments.
do_action( $hook_name, $val1, $val2 );
$call_count = $a1->get_call_count();
// $a1 should be called with both args.
$this->assertSame( 1, $call_count );
$argsvar1 = $a1->get_args();
$this->assertSame( array( $val1, $val2 ), array_pop( $argsvar1 ) );
// $a2 should be called with one only.
$this->assertSame( 1, $a2->get_call_count() );
$argsvar2 = $a2->get_args();
$this->assertSame( array( $val1 ), array_pop( $argsvar2 ) );
// $a3 should be called with both args.
$this->assertSame( 1, $a3->get_call_count() );
$argsvar3 = $a3->get_args();
$this->assertSame( array( $val1, $val2 ), array_pop( $argsvar3 ) );
}
/**
* Tests PHP 4 notation for calling actions while passing in an object by reference.
*
* @ticket 48312
*
* @covers ::do_action
*/
public function test_action_args_with_php4_syntax() {
$a = new MockAction();
$hook_name = __FUNCTION__;
$val = new stdClass();
add_action( $hook_name, array( &$a, 'action' ) );
// Call the action with PHP 4 notation for passing object by reference.
do_action( $hook_name, array( &$val ) );
$call_count = $a->get_call_count();
$argsvar = $a->get_args();
$this->assertSame( array( $val ), array_pop( $argsvar ) );
}
/**
* @ticket 60193
*
* @dataProvider data_priority_callback_order_with_integers
* @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
*
* @covers ::do_action
*
* @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_action( $hook_name, array( $mock, 'action' ), $priorities[0] );
add_action( $hook_name, array( $mock, 'action2' ), $priorities[1] );
do_action( $hook_name );
$this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
$actual_call_order = wp_list_pluck( $mock->get_events(), 'action' );
$this->assertSame( $expected_call_order, $actual_call_order, 'The action 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( 'action2', 'action' ),
),
'int ASC' => array(
'priorities' => array( 9, 10 ),
'expected_call_order' => array( 'action', 'action2' ),
),
);
}
/**
* 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( 'action2', 'action' ),
),
'int as string ASC' => array(
'priorities' => array( '9', '10' ),
'expected_call_order' => array( 'action', 'action2' ),
),
'float DESC' => array(
'priorities' => array( 10.0, 9.5 ),
'expected_call_order' => array( 'action2', 'action' ),
'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( 'action', 'action2' ),
'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( 'action2', 'action' ),
),
'float as string ASC' => array(
'priorities' => array( '9.5', '10.0' ),
'expected_call_order' => array( 'action', 'action2' ),
),
// Non-numeric.
'null' => array(
'priorities' => array( null, null ),
'expected_call_order' => array( 'action', 'action2' ),
),
'bool DESC' => array(
'priorities' => array( true, false ),
'expected_call_order' => array( 'action2', 'action' ),
),
'bool ASC' => array(
'priorities' => array( false, true ),
'expected_call_order' => array( 'action', 'action2' ),
),
'non-numerical string DESC' => array(
'priorities' => array( 'test1', 'test2' ),
'expected_call_order' => array( 'action', 'action2' ),
),
'non-numerical string ASC' => array(
'priorities' => array( 'test1', 'test2' ),
'expected_call_order' => array( 'action', 'action2' ),
),
'int, non-numerical string DESC' => array(
'priorities' => array( 10, 'test' ),
'expected_call_order' => array( 'action2', 'action' ),
),
'int, non-numerical string ASC' => array(
'priorities' => array( 'test', 10 ),
'expected_call_order' => array( 'action', 'action2' ),
),
'float, non-numerical string DESC' => array(
'priorities' => array( 10.0, 'test' ),
'expected_call_order' => array( 'action2', 'action' ),
),
'float, non-numerical string ASC' => array(
'priorities' => array( 'test', 10.0 ),
'expected_call_order' => array( 'action', 'action2' ),
),
);
}
/**
* @covers ::did_action
*/
public function test_did_action() {
$hook_name1 = 'action1';
$hook_name2 = 'action2';
// Do action $hook_name1 but not $hook_name2.
do_action( $hook_name1 );
$this->assertSame( 1, did_action( $hook_name1 ) );
$this->assertSame( 0, did_action( $hook_name2 ) );
// Do action $hook_name2 10 times.
$count = 10;
for ( $i = 0; $i < $count; $i++ ) {
do_action( $hook_name2 );
}
// $hook_name1's count hasn't changed, $hook_name2 should be correct.
$this->assertSame( 1, did_action( $hook_name1 ) );
$this->assertSame( $count, did_action( $hook_name2 ) );
}
/**
* @covers ::do_action
*/
public function test_all_action() {
$a = new MockAction();
$hook_name1 = __FUNCTION__ . '_1';
$hook_name2 = __FUNCTION__ . '_2';
// Add an 'all' action.
add_action( 'all', array( &$a, 'action' ) );
$this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) );
// Do some actions.
do_action( $hook_name1 );
do_action( $hook_name2 );
do_action( $hook_name1 );
do_action( $hook_name1 );
// Our action should have been called once for each tag.
$this->assertSame( 4, $a->get_call_count() );
// Only our hook was called.
$this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_hook_names() );
remove_action( 'all', array( &$a, 'action' ) );
$this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
}
/**
* @covers ::remove_action
*/
public function test_remove_all_action() {
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( 'all', array( &$a, 'action' ) );
$this->assertSame( 10, has_filter( 'all', array( &$a, 'action' ) ) );
do_action( $hook_name );
// 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 action, do it again, and make sure it's not called this time.
remove_action( 'all', array( &$a, 'action' ) );
$this->assertFalse( has_filter( 'all', array( &$a, 'action' ) ) );
do_action( $hook_name );
$this->assertSame( 1, $a->get_call_count() );
$this->assertSame( array( $hook_name ), $a->get_hook_names() );
}
/**
* @covers ::do_action_ref_array
*/
public function test_action_ref_array() {
$obj = new stdClass();
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $hook_name, array( &$a, 'action' ) );
do_action_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 11241
*
* @covers ::do_action
*/
public function test_action_keyed_array() {
$a = new MockAction();
$hook_name = __FUNCTION__;
add_action( $hook_name, array( &$a, 'action' ) );
$context = array( 'key1' => 'val1' );
do_action( $hook_name, $context );
$args = $a->get_args();
$this->assertSame( $args[0][0], $context );
$context2 = array(
'key2' => 'val2',
'key3' => 'val3',
);
do_action( $hook_name, $context2 );
$args = $a->get_args();
$this->assertSame( $args[1][0], $context2 );
}
/**
* @ticket 10493
*
* @covers ::add_action
* @covers ::has_action
* @covers ::do_action
*/
public function test_action_closure() {
$hook_name = __FUNCTION__;
$closure = static function ( $a, $b ) {
$GLOBALS[ $a ] = $b;
};
add_action( $hook_name, $closure, 10, 2 );
$this->assertSame( 10, has_action( $hook_name, $closure ) );
$context = array( 'val1', 'val2' );
do_action( $hook_name, $context[0], $context[1] );
$this->assertSame( $GLOBALS[ $context[0] ], $context[1] );
$hook_name2 = __FUNCTION__ . '_2';
$closure2 = static function () {
$GLOBALS['closure_no_args'] = true;
};
add_action( $hook_name2, $closure2 );
$this->assertSame( 10, has_action( $hook_name2, $closure2 ) );
do_action( $hook_name2 );
$this->assertTrue( $GLOBALS['closure_no_args'] );
remove_action( $hook_name, $closure );
remove_action( $hook_name2, $closure2 );
}
/**
* @ticket 23265
*
* @covers ::add_action
*/
public function test_action_callback_representations() {
$hook_name = __FUNCTION__;
$this->assertFalse( has_action( $hook_name ) );
add_action( $hook_name, array( 'Class', 'method' ) );
$this->assertSame( 10, has_action( $hook_name, array( 'Class', 'method' ) ) );
$this->assertSame( 10, has_action( $hook_name, 'Class::method' ) );
}
/**
* @covers ::remove_action
*/
public function test_action_self_removal() {
add_action( 'test_action_self_removal', array( $this, 'action_self_removal' ) );
do_action( 'test_action_self_removal' );
$this->assertSame( 1, did_action( 'test_action_self_removal' ) );
}
public function action_self_removal() {
remove_action( 'test_action_self_removal', array( $this, 'action_self_removal' ) );
}
/**
* @ticket 17817
*
* @covers ::do_action
*/
public function test_action_recursion() {
$hook_name = __FUNCTION__;
$a = new MockAction();
$b = new MockAction();
add_action( $hook_name, array( $a, 'action' ), 11, 1 );
add_action( $hook_name, array( $b, 'action' ), 13, 1 );
add_action( $hook_name, array( $this, 'action_that_causes_recursion' ), 12, 1 );
do_action( $hook_name, $hook_name );
$this->assertSame( 2, $a->get_call_count(), 'recursive actions should call all callbacks with earlier priority' );
$this->assertSame( 2, $b->get_call_count(), 'recursive actions should call callbacks with later priority' );
}
/**
* @covers ::do_action
*/
public function action_that_causes_recursion( $hook_name ) {
static $recursing = false;
if ( ! $recursing ) {
$recursing = true;
do_action( $hook_name, $hook_name );
}
$recursing = false;
}
/**
* @ticket 9968
* @ticket 17817
*
* @covers ::remove_action
* @covers ::add_action
*/
public function test_action_callback_manipulation_while_running() {
$hook_name = __FUNCTION__;
$a = new MockAction();
$b = new MockAction();
$c = new MockAction();
$d = new MockAction();
$e = new MockAction();
add_action( $hook_name, array( $a, 'action' ), 11, 2 );
add_action( $hook_name, array( $this, 'action_that_manipulates_a_running_hook' ), 12, 2 );
add_action( $hook_name, array( $b, 'action' ), 12, 2 );
do_action( $hook_name, $hook_name, array( $a, $b, $c, $d, $e ) );
do_action( $hook_name, $hook_name, array( $a, $b, $c, $d, $e ) );
$this->assertSame( 2, $a->get_call_count(), 'callbacks should run unless otherwise instructed' );
$this->assertSame( 1, $b->get_call_count(), 'callback removed by same priority callback should still get called' );
$this->assertSame( 1, $c->get_call_count(), 'callback added by same priority callback should not get called' );
$this->assertSame( 2, $d->get_call_count(), 'callback added by earlier priority callback should get called' );
$this->assertSame( 1, $e->get_call_count(), 'callback added by later priority callback should not get called' );
}
public function action_that_manipulates_a_running_hook( $hook_name, $mocks ) {
remove_action( $hook_name, array( $mocks[1], 'action' ), 12, 2 );
add_action( $hook_name, array( $mocks[2], 'action' ), 12, 2 );
add_action( $hook_name, array( $mocks[3], 'action' ), 13, 2 );
add_action( $hook_name, array( $mocks[4], 'action' ), 10, 2 );
}
/**
* @ticket 17817
*
* This specifically addresses the concern raised at
* https://core.trac.wordpress.org/ticket/17817#comment:52
*
* @covers ::remove_filter
*/
public function test_remove_anonymous_callback() {
$hook_name = __FUNCTION__;
$a = new MockAction();
add_action( $hook_name, array( $a, 'action' ), 12, 1 );
$this->assertTrue( has_action( $hook_name ) );
$hook = $GLOBALS['wp_filter'][ $hook_name ];
// From http://wordpress.stackexchange.com/a/57088/6445
foreach ( $hook as $priority => $filter ) {
foreach ( $filter as $identifier => $function ) {
if ( is_array( $function )
&& $function['function'][0] instanceof MockAction
&& 'action' === $function['function'][1]
) {
remove_filter(
$hook_name,
array( $function['function'][0], 'action' ),
$priority
);
}
}
}
$this->assertFalse( has_action( $hook_name ) );
}
/**
* Test the ArrayAccess methods of WP_Hook
*
* @ticket 17817
*
* @covers WP_Hook::offsetGet
* @covers WP_Hook::offsetSet
* @covers WP_Hook::offsetUnset
*/
public function test_array_access_of_wp_filter_global() {
global $wp_filter;
$hook_name = __FUNCTION__;
add_action( $hook_name, '__return_null', 11, 1 );
$this->assertArrayHasKey( 11, $wp_filter[ $hook_name ] );
$this->assertArrayHasKey( '__return_null', $wp_filter[ $hook_name ][11] );
unset( $wp_filter[ $hook_name ][11] );
$this->assertFalse( has_action( $hook_name, '__return_null' ) );
$wp_filter[ $hook_name ][11] = array(
'__return_null' => array(
'function' => '__return_null',
'accepted_args' => 1,
),
);
$this->assertSame( 11, has_action( $hook_name, '__return_null' ) );
}
/**
* Make sure current_action() behaves as current_filter()
*
* @ticket 14994
*
* @covers ::current_action
*/
public function test_current_action() {
global $wp_current_filter;
$wp_current_filter[] = 'first';
$wp_current_filter[] = 'second'; // Let's say a second action was invoked.
$this->assertSame( 'second', current_action() );
}
/**
* @ticket 14994
*
* @covers ::doing_filter
*/
public function test_doing_filter() {
global $wp_current_filter;
$wp_current_filter = array(); // Set to an empty array first.
$this->assertFalse( doing_filter() ); // No filter is passed in, and no filter is being processed.
$this->assertFalse( doing_filter( 'testing' ) ); // Filter is passed in but not being processed.
$wp_current_filter[] = 'testing';
$this->assertTrue( doing_filter() ); // No action is passed in, and a filter is being processed.
$this->assertTrue( doing_filter( 'testing' ) ); // Filter is passed in and is being processed.
$this->assertFalse( doing_filter( 'something_else' ) ); // Filter is passed in but not being processed.
$wp_current_filter = array();
}
/**
* @ticket 14994
*
* @covers ::doing_filter
*/
public function test_doing_action() {
global $wp_current_filter;
$wp_current_filter = array(); // Set to an empty array first.
$this->assertFalse( doing_action() ); // No action is passed in, and no filter is being processed.
$this->assertFalse( doing_action( 'testing' ) ); // Action is passed in but not being processed.
$wp_current_filter[] = 'testing';
$this->assertTrue( doing_action() ); // No action is passed in, and a filter is being processed.
$this->assertTrue( doing_action( 'testing' ) ); // Action is passed in and is being processed.
$this->assertFalse( doing_action( 'something_else' ) ); // Action is passed in but not being processed.
$wp_current_filter = array();
}
/**
* @ticket 14994
*
* @covers ::doing_filter
*/
public function test_doing_filter_real() {
$this->assertFalse( doing_filter() ); // No filter is passed in, and no filter is being processed.
$this->assertFalse( doing_filter( 'testing' ) ); // Filter is passed in but not being processed.
add_filter( 'testing', array( $this, 'apply_testing_filter' ) );
$this->assertTrue( has_action( 'testing' ) );
$this->assertSame( 10, has_action( 'testing', array( $this, 'apply_testing_filter' ) ) );
apply_filters( 'testing', '' );
// Make sure it ran.
$this->assertTrue( $this->apply_testing_filter );
$this->assertFalse( doing_filter() ); // No longer doing any filters.
$this->assertFalse( doing_filter( 'testing' ) ); // No longer doing this filter.
}
public function apply_testing_filter() {
$this->apply_testing_filter = true;
$this->assertTrue( doing_filter() );
$this->assertTrue( doing_filter( 'testing' ) );
$this->assertFalse( doing_filter( 'something_else' ) );
$this->assertFalse( doing_filter( 'testing_nested' ) );
add_filter( 'testing_nested', array( $this, 'apply_testing_nested_filter' ) );
$this->assertTrue( has_action( 'testing_nested' ) );
$this->assertSame( 10, has_action( 'testing_nested', array( $this, 'apply_testing_nested_filter' ) ) );
apply_filters( 'testing_nested', '' );
// Make sure it ran.
$this->assertTrue( $this->apply_testing_nested_filter );
$this->assertFalse( doing_filter( 'testing_nested' ) );
$this->assertFalse( doing_filter( 'testing_nested' ) );
}
public function apply_testing_nested_filter() {
$this->apply_testing_nested_filter = true;
$this->assertTrue( doing_filter() );
$this->assertTrue( doing_filter( 'testing' ) );
$this->assertTrue( doing_filter( 'testing_nested' ) );
$this->assertFalse( doing_filter( 'something_else' ) );
}
/**
* @ticket 10441
* @expectedDeprecated tests_do_action_deprecated
*
* @covers ::do_action_deprecated
*/
public function test_do_action_deprecated() {
$p = new WP_Post( (object) array( 'post_title' => 'Foo' ) );
add_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback' ) );
do_action_deprecated( 'tests_do_action_deprecated', array( $p ), '4.6.0' );
remove_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback' ) );
$this->assertSame( 'Bar', $p->post_title );
}
public static function deprecated_action_callback( $p ) {
$p->post_title = 'Bar';
}
/**
* @ticket 10441
* @expectedDeprecated tests_do_action_deprecated
*
* @covers ::do_action_deprecated
*/
public function test_do_action_deprecated_with_multiple_params() {
$p1 = new WP_Post( (object) array( 'post_title' => 'Foo1' ) );
$p2 = new WP_Post( (object) array( 'post_title' => 'Foo2' ) );
add_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback_multiple_params' ), 10, 2 );
do_action_deprecated( 'tests_do_action_deprecated', array( $p1, $p2 ), '4.6.0' );
remove_action( 'tests_do_action_deprecated', array( __CLASS__, 'deprecated_action_callback_multiple_params' ), 10, 2 );
$this->assertSame( 'Bar1', $p1->post_title );
$this->assertSame( 'Bar2', $p2->post_title );
}
public static function deprecated_action_callback_multiple_params( $p1, $p2 ) {
$p1->post_title = 'Bar1';
$p2->post_title = 'Bar2';
}
}