PluginProbe
Datafeedr API / 1.0.125
Datafeedr API v1.0.125
1.4.3 1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 All 182 releases
datafeedr-api / Wuwei / Framework / Event / Manager.php

Manager.php in Datafeedr API 1.0.125, at Wuwei/Framework/Event/Manager.php

191 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace Datafeedr\Api\Wuwei\Event;
2
3 /**
4 * Class Manager
5 *
6 * The event manager manages events using the WordPress plugin API.
7 *
8 * @link https://carlalexander.ca/design-system-wordpress-event-management/
9 *
10 * @since 2.0.0
11 */
12 class Manager {
13
14 /**
15 * Adds a callback to a specific hook of the WordPress plugin API.
16 *
17 * @since 2.0.0
18 *
19 * @uses add_filter()
20 *
21 * @param string $hook_name
22 * @param callable $callback
23 * @param int $priority
24 * @param int $accepted_args
25 */
26 public function add_callback( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
27 add_filter( $hook_name, $callback, $priority, $accepted_args );
28 }
29
30 /**
31 * Add an event subscriber.
32 *
33 * The event manager registers all the hooks that the given subscriber
34 * wants to register with the WordPress Plugin API.
35 *
36 * @since 2.0.0
37 *
38 * @param Subscriber_Interface $subscriber
39 */
40 public function add_subscriber( Subscriber_Interface $subscriber ) {
41 foreach ( $subscriber->get_subscribed_events() as $hook_name => $parameters ) {
42 $this->add_subscriber_callback( $subscriber, $hook_name, $parameters );
43 }
44 }
45
46 /**
47 * Executes all the callbacks registered with the given hook.
48 *
49 * @since 2.0.0
50 *
51 * @see do_action()
52 *
53 * @return mixed Returns the return value of the callback which for do_action is void.
54 */
55 public function execute() {
56 return call_user_func_array( 'do_action', func_get_args() );
57 }
58
59 /**
60 * Filters the given value by applying all the changes from the callbacks
61 * registered with the given hook. Returns the filtered value.
62 *
63 * @since 2.0.0
64 *
65 * @see apply_filters()
66 *
67 * @return mixed Returns the filtered value.
68 */
69 public function filter() {
70 return call_user_func_array( 'apply_filters', func_get_args() );
71 }
72
73 /**
74 * Get the name of the hook that WordPress plugin API is executing. Returns
75 * false if it isn't executing a hook.
76 *
77 * @since 2.0.0
78 *
79 * @see current_filter()
80 *
81 * @return string|bool
82 */
83 public function get_current_hook() {
84 return current_filter();
85 }
86
87 /**
88 * Checks the WordPress plugin API to see if the given hook has
89 * the given callback. The priority of the callback will be returned
90 * or false. If no callback is given will return true or false if
91 * there's any callbacks registered to the hook.
92 *
93 * @since 2.0.0
94 *
95 * @see has_filter()
96 *
97 * @param string $hook_name
98 * @param mixed $callback
99 *
100 * @return bool|int
101 */
102 public function has_callback( $hook_name, $callback = false ) {
103 return has_filter( $hook_name, $callback );
104 }
105
106 /**
107 * Removes the given callback from the given hook. The WordPress plugin API only
108 * removes the hook if the callback and priority match a registered hook.
109 *
110 * @since 2.0.0
111 *
112 * @see remove_filter()
113 *
114 * @param string $hook_name
115 * @param callable $callback
116 * @param int $priority
117 *
118 * @return bool
119 */
120 public function remove_callback( $hook_name, $callback, $priority = 10 ) {
121 return remove_filter( $hook_name, $callback, $priority );
122 }
123
124 /**
125 * Remove an event subscriber.
126 *
127 * The event manager removes all the hooks that the given subscriber
128 * wants to register with the WordPress Plugin API.
129 *
130 * @since 2.0.0
131 *
132 * @param Subscriber_Interface $subscriber
133 */
134 public function remove_subscriber( Subscriber_Interface $subscriber ) {
135 foreach ( $subscriber->get_subscribed_events() as $hook_name => $parameters ) {
136 $this->remove_subscriber_callback( $subscriber, $hook_name, $parameters );
137 }
138 }
139
140 /**
141 * Adds the given subscriber's callback to a specific hook
142 * of the WordPress plugin API.
143 *
144 * @since 2.0.0
145 *
146 * @param Subscriber_Interface $subscriber
147 * @param string $hook_name
148 * @param mixed $parameters
149 */
150 private function add_subscriber_callback( Subscriber_Interface $subscriber, $hook_name, $parameters ) {
151 if ( is_string( $parameters ) ) {
152 $this->add_callback( $hook_name, array( $subscriber, $parameters ) );
153 } elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
154 $this->add_callback(
155 $hook_name,
156 array(
157 $subscriber,
158 $parameters[0]
159 ),
160 isset( $parameters[1] ) ? $parameters[1] : 10,
161 isset( $parameters[2] ) ? $parameters[2] : 1
162 );
163 }
164 }
165
166 /**
167 * Removes the given subscriber's callback to a specific hook
168 * of the WordPress plugin API.
169 *
170 * @since 2.0.0
171 *
172 * @param Subscriber_Interface $subscriber
173 * @param string $hook_name
174 * @param mixed $parameters
175 */
176 private function remove_subscriber_callback( Subscriber_Interface $subscriber, $hook_name, $parameters ) {
177 if ( is_string( $parameters ) ) {
178 $this->remove_callback( $hook_name, array( $subscriber, $parameters ) );
179 } elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
180 $this->remove_callback(
181 $hook_name,
182 array(
183 $subscriber,
184 $parameters[0]
185 ),
186 isset( $parameters[1] ) ? $parameters[1] : 10
187 );
188 }
189 }
190 }
191