Skip to content

Commit 0e3ce6b

Browse files
committed
WP Helper functions - doing_*(): ensure parameter defaults are in line with WP Core
The underlying `HookRunningStack::has()` method already allowed for a `$hook_name` being `null`, but the wrapping functions which called the method did not. Fixed now. Includes tests. Includes fixing the documentation. Refs: * https://developer.wordpress.org/reference/functions/doing_action/ * https://developer.wordpress.org/reference/functions/doing_filter/
1 parent 75e06f3 commit 0e3ce6b

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

inc/api.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ function did($action)
273273
*
274274
* Brain Monkey version of `doing_action` will alias here.
275275
*
276-
* @param string $action
276+
* @param string|null $action
277277
* @return bool
278278
*/
279-
function doing($action)
279+
function doing($action = null)
280280
{
281281
return Container::instance()
282282
->hookRunningStack()
@@ -380,10 +380,10 @@ function applied($filter)
380380
*
381381
* Brain Monkey version of `doing_filter` will alias here.
382382
*
383-
* @param string $filter
383+
* @param string|null $filter
384384
* @return bool
385385
*/
386-
function doing($filter)
386+
function doing($filter = null)
387387
{
388388
return Container::instance()
389389
->hookRunningStack()

inc/wp-hook-functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ function remove_filter($hook_name, $callback, $priority = 10)
143143
}
144144

145145
if ( ! function_exists('doing_action')) {
146-
function doing_action($hook_name)
146+
function doing_action($hook_name = null)
147147
{
148148
return Monkey\Actions\doing($hook_name);
149149
}
150150
}
151151

152152
if ( ! function_exists('doing_filter')) {
153-
function doing_filter($hook_name)
153+
function doing_filter($hook_name = null)
154154
{
155155
return Monkey\Filters\doing($hook_name);
156156
}

src/Hook/HookRunningStack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function last()
5252
}
5353

5454
/**
55-
* @param string $hook_name
55+
* @param string|null $hook_name
5656
* @return bool
5757
*/
5858
public function has($hook_name = null)

tests/cases/unit/Api/ApplyFiltersTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ public function testNestedFiltersAndDoingFilter()
227227
Filters\expectApplied('first_level')->once()->andReturnUsing(function ($arg) {
228228

229229
static::assertTrue(current_filter() === 'first_level');
230+
static::assertTrue(doing_filter(), 'doing_filter() without hook name doesn\'t work (in first level)');
231+
static::assertTrue(doing_filter(null), 'doing_filter() with null hook name doesn\'t work (in first level)');
230232
static::assertTrue(doing_filter('first_level'));
231233
static::assertFalse(doing_filter('second_level'));
232234

@@ -237,6 +239,7 @@ public function testNestedFiltersAndDoingFilter()
237239

238240
static::assertSame('How is Monkey?', $arg);
239241
static::assertTrue(current_filter() === 'second_level');
242+
static::assertTrue(doing_filter(), 'doing_filter() without hook name doesn\'t work (in second level)');
240243
static::assertTrue(doing_filter('first_level'));
241244
static::assertTrue(doing_filter('second_level'));
242245

@@ -245,6 +248,8 @@ public function testNestedFiltersAndDoingFilter()
245248

246249

247250
static::assertSame('Monkey is great!', apply_filters('first_level', 'How is Monkey?'));
251+
static::assertFalse(doing_filter(), 'doing_filter() without hook name doesn\'t work (in outer code)');
252+
static::assertFalse(doing_filter(null), 'doing_filter() with null hook name doesn\'t work (in outer code)');
248253
static::assertFalse(doing_filter('first_level'));
249254
static::assertFalse(doing_filter('second_level'));
250255
}

tests/cases/unit/Api/DoActionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,15 @@ public function testNestedDoWithExpectationWhenHappenDoingAction()
116116
Actions\expectDone('first_level')->once()->whenHappen(function () {
117117
do_action('second_level', 'Catch me!');
118118
do_action('second_level', 'Catch me!');
119+
static::assertTrue(doing_action('first_level'));
119120
});
120121

121122
Actions\expectDone('second_level')->twice()->whenHappen(function ($arg) {
122123

123124
static::assertSame('Catch me!', $arg);
124125
static::assertTrue(current_filter() === 'second_level');
126+
static::assertTrue(doing_action(), 'doing_action() without hook name doesn\'t work (in second level)');
127+
static::assertTrue(doing_action(null), 'doing_action() with null hook name doesn\'t work (in second level)');
125128
static::assertTrue(doing_action('first_level'));
126129
static::assertTrue(doing_action('second_level'));
127130
// Checking for output will ensure above assertions have ran.
@@ -132,6 +135,7 @@ public function testNestedDoWithExpectationWhenHappenDoingAction()
132135

133136
do_action('first_level');
134137

138+
static::assertFalse(doing_action(), 'doing_action() without hook name doesn\'t work (in outer code)');
135139
static::assertFalse(doing_action('first_level'));
136140
static::assertFalse(doing_action('second_level'));
137141
}

0 commit comments

Comments
 (0)