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
270 lines (218 loc) · 7.7 KB
/
filters.php
File metadata and controls
270 lines (218 loc) · 7.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
<?php
/**
* Test apply_filters() and related functions
*
* @group hooks
*/
class Tests_Filters extends WP_UnitTestCase {
function test_simple_filter() {
$a = new MockAction();
$tag = rand_str();
$val = rand_str();
add_filter($tag, array($a, 'filter'));
$this->assertEquals($val, apply_filters($tag, $val));
// only one event occurred for the hook, with empty args
$this->assertEquals(1, $a->get_call_count());
// only our hook was called
$this->assertEquals(array($tag), $a->get_tags());
$argsvar = $a->get_args();
$args = array_pop( $argsvar );
$this->assertEquals(array($val), $args);
}
function test_remove_filter() {
$a = new MockAction();
$tag = rand_str();
$val = rand_str();
add_filter($tag, array($a, 'filter'));
$this->assertEquals($val, apply_filters($tag, $val));
// make sure our hook was called correctly
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals(array($tag), $a->get_tags());
// now remove the filter, do it again, and make sure it's not called this time
remove_filter($tag, array($a, 'filter'));
$this->assertEquals($val, apply_filters($tag, $val));
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals(array($tag), $a->get_tags());
}
function test_has_filter() {
$tag = rand_str();
$func = rand_str();
$this->assertFalse( has_filter($tag, $func) );
$this->assertFalse( has_filter($tag) );
add_filter($tag, $func);
$this->assertEquals( 10, has_filter($tag, $func) );
$this->assertTrue( has_filter($tag) );
remove_filter($tag, $func);
$this->assertFalse( has_filter($tag, $func) );
$this->assertFalse( has_filter($tag) );
}
// one tag with multiple filters
function test_multiple_filters() {
$a1 = new MockAction();
$a2 = new MockAction();
$tag = rand_str();
$val = rand_str();
// add both filters to the hook
add_filter($tag, array($a1, 'filter'));
add_filter($tag, array($a2, 'filter'));
$this->assertEquals($val, apply_filters($tag, $val));
// both filters called once each
$this->assertEquals(1, $a1->get_call_count());
$this->assertEquals(1, $a2->get_call_count());
}
function test_filter_args_1() {
$a = new MockAction();
$tag = rand_str();
$val = rand_str();
$arg1 = rand_str();
add_filter($tag, array($a, 'filter'), 10, 2);
// call the filter with a single argument
$this->assertEquals($val, apply_filters($tag, $val, $arg1));
$this->assertEquals(1, $a->get_call_count());
$argsvar = $a->get_args();
$this->assertEquals( array( $val, $arg1 ), array_pop( $argsvar ) );
}
function test_filter_args_2() {
$a1 = new MockAction();
$a2 = new MockAction();
$tag = rand_str();
$val = rand_str();
$arg1 = rand_str();
$arg2 = rand_str();
// a1 accepts two arguments, a2 doesn't
add_filter($tag, array($a1, 'filter'), 10, 3);
add_filter($tag, array($a2, 'filter'));
// call the filter with two arguments
$this->assertEquals($val, apply_filters($tag, $val, $arg1, $arg2));
// a1 should be called with both args
$this->assertEquals(1, $a1->get_call_count());
$argsvar1 = $a1->get_args();
$this->assertEquals( array( $val, $arg1, $arg2 ), array_pop( $argsvar1 ) );
// a2 should be called with one only
$this->assertEquals(1, $a2->get_call_count());
$argsvar2 = $a2->get_args();
$this->assertEquals( array( $val ), array_pop( $argsvar2 ) );
}
function test_filter_priority() {
$a = new MockAction();
$tag = rand_str();
$val = rand_str();
// make two filters with different priorities
add_filter($tag, array($a, 'filter'), 10);
add_filter($tag, array($a, 'filter2'), 9);
$this->assertEquals($val, apply_filters($tag, $val));
// there should be two events, one per filter
$this->assertEquals(2, $a->get_call_count());
$expected = array (
// filter2 is called first because it has priority 9
array (
'filter' => 'filter2',
'tag' => $tag,
'args' => array($val)
),
// filter 1 is called second
array (
'filter' => 'filter',
'tag' => $tag,
'args' => array($val)
),
);
$this->assertEquals($expected, $a->get_events());
}
function test_all_filter() {
$a = new MockAction();
$tag1 = rand_str();
$tag2 = rand_str();
$val = rand_str();
// add an 'all' filter
add_filter('all', array($a, 'filterall'));
// do some filters
$this->assertEquals($val, apply_filters($tag1, $val));
$this->assertEquals($val, apply_filters($tag2, $val));
$this->assertEquals($val, apply_filters($tag1, $val));
$this->assertEquals($val, apply_filters($tag1, $val));
// our filter should have been called once for each apply_filters call
$this->assertEquals(4, $a->get_call_count());
// the right hooks should have been called in order
$this->assertEquals(array($tag1, $tag2, $tag1, $tag1), $a->get_tags());
remove_filter('all', array($a, 'filterall'));
$this->assertFalse( has_filter('all', array($a, 'filterall')) );
}
function test_remove_all_filter() {
$a = new MockAction();
$tag = rand_str();
$val = rand_str();
add_filter('all', array($a, 'filterall'));
$this->assertTrue( has_filter('all') );
$this->assertEquals( 10, has_filter('all', array($a, 'filterall')) );
$this->assertEquals($val, apply_filters($tag, $val));
// make sure our hook was called correctly
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals(array($tag), $a->get_tags());
// 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->assertEquals($val, apply_filters($tag, $val));
// call cound should remain at 1
$this->assertEquals(1, $a->get_call_count());
$this->assertEquals(array($tag), $a->get_tags());
}
/**
* @ticket 9886
*/
function test_filter_ref_array() {
$obj = new stdClass();
$a = new MockAction();
$tag = rand_str();
add_action($tag, array($a, 'filter'));
apply_filters_ref_array($tag, array(&$obj));
$args = $a->get_args();
$this->assertSame($args[0][0], $obj);
// just in case we don't trust assertSame
$obj->foo = true;
$this->assertFalse( empty($args[0][0]->foo) );
}
/**
* @ticket 12723
*/
function test_filter_ref_array_result() {
$obj = new stdClass();
$a = new MockAction();
$b = new MockAction();
$tag = rand_str();
add_action($tag, array($a, 'filter_append'), 10, 2);
add_action($tag, array($b, 'filter_append'), 10, 2);
$result = apply_filters_ref_array($tag, array('string', &$obj));
$this->assertEquals($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->assertFalse( empty($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->assertFalse( empty($args[0][1]->foo) );
}
function _self_removal($tag) {
remove_action( $tag, array($this, '_self_removal'), 10, 1 );
return $tag;
}
/**
* @ticket 21169
*/
function test_filter_removal_during_filter() {
$tag = rand_str();
$a = new MockAction();
$b = new MockAction();
add_action( $tag, array($a, 'filter_append'), 11, 1 );
add_action( $tag, array($b, 'filter_append'), 12, 1 );
add_action( $tag, array($this, '_self_removal'), 10, 1 );
$result = apply_filters($tag, $tag);
$this->assertEquals( 1, $a->get_call_count(), 'priority 11 filters should run after priority 10 empties itself' );
$this->assertEquals( 1, $b->get_call_count(), 'priority 12 filters should run after priority 10 empties itself and priority 11 runs' );
$this->assertEquals( $result, $tag . '_append_append', 'priority 11 and 12 filters should run after priority 10 empties itself' );
}
}