-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-event-manager.php
135 lines (125 loc) · 4.81 KB
/
class-event-manager.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
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
<?php
namespace WP_Rocket\Event_Management;
/**
* The event manager manages events using the WordPress plugin API.
*
* @since 3.1
* @author Carl Alexander <contact@carlalexander.ca>
*/
class Event_Manager {
/**
* Adds a callback to a specific hook of the WordPress plugin API.
*
* @uses add_filter()
*
* @param string $hook_name Name of the hook.
* @param callable $callback Callback function.
* @param int $priority Priority.
* @param int $accepted_args Number of arguments.
*/
public function add_callback( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
add_filter( $hook_name, $callback, $priority, $accepted_args );
}
/**
* Add an event subscriber.
*
* The event manager registers all the hooks that the given subscriber
* wants to register with the WordPress Plugin API.
*
* @param Subscriber_Interface $subscriber Subscriber_Interface implementation.
*/
public function add_subscriber( Subscriber_Interface $subscriber ) {
if ( $subscriber instanceof Event_Manager_Aware_Subscriber_Interface ) {
$subscriber->set_event_manager( $this );
}
$events = $subscriber->get_subscribed_events();
if ( empty( $events ) ) {
return;
}
foreach ( $subscriber->get_subscribed_events() as $hook_name => $parameters ) {
$this->add_subscriber_callback( $subscriber, $hook_name, $parameters );
}
}
/**
* Checks the WordPress plugin API to see if the given hook has
* the given callback. The priority of the callback will be returned
* or false. If no callback is given will return true or false if
* there's any callbacks registered to the hook.
*
* @uses has_filter()
*
* @param string $hook_name Hook name.
* @param mixed $callback Callback.
*
* @return bool|int
*/
public function has_callback( $hook_name, $callback = false ) {
return has_filter( $hook_name, $callback );
}
/**
* Removes the given callback from the given hook. The WordPress plugin API only
* removes the hook if the callback and priority match a registered hook.
*
* @uses remove_filter()
*
* @param string $hook_name Hook name.
* @param callable $callback Callback.
* @param int $priority Priority.
*
* @return bool
*/
public function remove_callback( $hook_name, $callback, $priority = 10 ) {
return remove_filter( $hook_name, $callback, $priority );
}
/**
* Remove an event subscriber.
*
* The event manager removes all the hooks that the given subscriber
* wants to register with the WordPress Plugin API.
*
* @param Subscriber_Interface $subscriber Subscriber_Interface implementation.
*/
public function remove_subscriber( Subscriber_Interface $subscriber ) {
foreach ( $subscriber->get_subscribed_events() as $hook_name => $parameters ) {
$this->remove_subscriber_callback( $subscriber, $hook_name, $parameters );
}
}
/**
* Adds the given subscriber's callback to a specific hook
* of the WordPress plugin API.
*
* @param Subscriber_Interface $subscriber Subscriber_Interface implementation.
* @param string $hook_name Hook name.
* @param mixed $parameters Parameters, can be a string, an array or a multidimensional array.
*/
private function add_subscriber_callback( Subscriber_Interface $subscriber, $hook_name, $parameters ) {
if ( is_string( $parameters ) ) {
$this->add_callback( $hook_name, [ $subscriber, $parameters ] );
} elseif ( is_array( $parameters ) && count( $parameters ) !== count( $parameters, COUNT_RECURSIVE ) ) {
foreach ( $parameters as $parameter ) {
$this->add_subscriber_callback( $subscriber, $hook_name, $parameter );
}
} elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
$this->add_callback( $hook_name, [ $subscriber, $parameters[0] ], isset( $parameters[1] ) ? $parameters[1] : 10, isset( $parameters[2] ) ? $parameters[2] : 1 );
}
}
/**
* Removes the given subscriber's callback to a specific hook
* of the WordPress plugin API.
*
* @param Subscriber_Interface $subscriber Subscriber_Interface implementation.
* @param string $hook_name Hook name.
* @param mixed $parameters Parameters, can be a string, an array or a multidimensional array.
*/
private function remove_subscriber_callback( Subscriber_Interface $subscriber, $hook_name, $parameters ) {
if ( is_string( $parameters ) ) {
$this->remove_callback( $hook_name, [ $subscriber, $parameters ] );
} elseif ( is_array( $parameters ) && count( $parameters ) !== count( $parameters, COUNT_RECURSIVE ) ) {
foreach ( $parameters as $parameter ) {
$this->remove_subscriber_callback( $subscriber, $hook_name, $parameter );
}
} elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
$this->remove_callback( $hook_name, [ $subscriber, $parameters[0] ], isset( $parameters[1] ) ? $parameters[1] : 10 );
}
}
}