-
Notifications
You must be signed in to change notification settings - Fork 3
/
one-time-callbacks.php
63 lines (58 loc) · 2.45 KB
/
one-time-callbacks.php
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
<?php
/**
* Plugin Name: One-time Callbacks
* Plugin URI: https://github.com/stevegrunwell/one-time-callbacks
* Description: Enable WordPress actions and filter callbacks to be called exactly once.
* Author: Steve Grunwell
* Author URI: https://stevegrunwell.com
* Version: 1.0.0
*
* @package SteveGrunwell\OneTimeCallbacks
*/
if ( ! function_exists( 'add_action_once' ) ) :
/**
* Register an action to run exactly one time.
*
* The arguments match that of add_action(), but this function will also register a second
* callback designed to remove the first immediately after it runs.
*
* @param string $hook The action name.
* @param callable $callback The callback function.
* @param int $priority Optional. The priority at which the callback should be executed.
* Default is 10.
* @param int $args Optional. The number of arguments expected by the callback function.
* Default is 1.
* @return bool Like add_action(), this function always returns true.
*/
function add_action_once( $hook, $callback, $priority = 10, $args = 1 ) {
$singular = function () use ( $hook, $callback, $priority, $args, &$singular ) {
call_user_func_array( $callback, func_get_args() );
remove_action( $hook, $singular, $priority, $args );
};
return add_action( $hook, $singular, $priority, $args );
}
endif;
if ( ! function_exists( 'add_filter_once' ) ) :
/**
* Register a filter to run exactly one time.
*
* The arguments match that of add_filter(), but this function will also register a second
* callback designed to remove the first immediately after it runs.
*
* @param string $hook The action name.
* @param callable $callback The callback function.
* @param int $priority Optional. The priority at which the callback should be executed.
* Default is 10.
* @param int $args Optional. The number of arguments expected by the callback.
* Default is 1.
* @return bool Like add_filter(), this function always returns true.
*/
function add_filter_once( $hook, $callback, $priority = 10, $args = 1 ) {
$singular = function () use ( $hook, $callback, $priority, $args, &$singular ) {
$value = call_user_func_array( $callback, func_get_args() );
remove_filter( $hook, $singular, $priority, $args );
return $value;
};
return add_filter( $hook, $singular, $priority, $args );
}
endif;