PluginProbe
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe / trunk
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe vtrunk
4.17.3 trunk 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 4.10.0 4.11.1 4.12.2 4.14.1 4.14.2 4.14.3 4.15.0 4.16.0 All 59 releases
stripe / src / EventManagement / EventManager.php

EventManager.php in Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe trunk, at src/EventManagement/EventManager.php

215 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Event management: Event manager
4 *
5 * @package SimplePay
6 * @subpackage Core
7 * @copyright Copyright (c) 2022, Sandhills Development, LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 4.4.0
10 */
11
12 namespace SimplePay\Core\EventManagement;
13
14 /**
15 * EventManager class.
16 *
17 * @since 4.4.0
18 */
19 class EventManager {
20
21 /**
22 * Adds an event subscriber.
23 *
24 * @since 4.4.0
25 *
26 * @param SubscriberInterface $subscriber SubscriberInterface implementation.
27 * @return void
28 */
29 public function add_subscriber( SubscriberInterface $subscriber ) {
30 $events = $subscriber->get_subscribed_events();
31
32 if ( empty( $events ) ) {
33 return;
34 }
35
36 foreach ( $events as $hook_name => $parameters ) {
37 $this->add_subscriber_callback(
38 $subscriber,
39 $hook_name,
40 $parameters
41 );
42 }
43 }
44
45 /**
46 * Removes an event subscriber.
47 *
48 * @since 4.4.0
49 *
50 * @param SubscriberInterface $subscriber SubscriberInterface implementation.
51 * @return void
52 */
53 public function remove_subscriber( SubscriberInterface $subscriber ) {
54 $events = $subscriber->get_subscribed_events();
55
56 if ( empty( $events ) ) {
57 return;
58 }
59
60 foreach ( $events as $hook_name => $parameters ) {
61 $this->remove_subscriber_callback(
62 $subscriber,
63 $hook_name,
64 $parameters
65 );
66 }
67 }
68
69 /**
70 * Adds a callback to a specific hook of the WordPress plugin API.
71 *
72 * @since 4.4.0
73 *
74 * @param string $hook_name Name of the hook.
75 * @param callable $callback Callback function.
76 * @param int $priority Optional. Callback priority. Default 10.
77 * @param int $accepted_args Optional. Number of arguments the callback accepts. Default 1.
78 * @return void
79 */
80 public function add_callback(
81 $hook_name,
82 $callback,
83 $priority = 10,
84 $accepted_args = 1
85 ) {
86 add_filter( $hook_name, $callback, $priority, $accepted_args );
87 }
88
89 /**
90 * Checks the WordPress plugin API to see if the given hook has the given callback.
91 *
92 * @since 4.4.0
93 *
94 * @param string $hook_name Hook name.
95 * @param callable|false $callback Optional. Callback function.
96 * @return int|bool If callback is omitted, returns boolean for whether the hook has anything registered.
97 * When checking a specific function, the priority of that hook is returned, or false
98 * if the function is not attached.
99 */
100 public function has_callback( $hook_name, $callback = false ) {
101 return has_filter( $hook_name, $callback );
102 }
103
104 /**
105 * Removes the given callback from the given hook.
106 *
107 * @since 4.4.0
108 *
109 * @param string $hook_name Hook name.
110 * @param callable $callback Callback.
111 * @param int $priority Optional. Callback priority. Default 10.
112 * @return bool Whether the function existed before it was removed.
113 */
114 public function remove_callback( $hook_name, $callback, $priority = 10 ) {
115 return remove_filter( $hook_name, $callback, $priority );
116 }
117
118 /**
119 * Adds the given subscriber's callback to a specific hook.
120 *
121 * @since 4.4.0
122 *
123 * @param SubscriberInterface $subscriber Subscriber_Interface implementation.
124 * @param string $hook_name Hook name.
125 * @param mixed $parameters Event parameters. Accepts a string, array, or a multidimensional array.
126 * @return void
127 */
128 private function add_subscriber_callback(
129 SubscriberInterface $subscriber,
130 $hook_name,
131 $parameters
132 ) {
133 if ( is_string( $parameters ) ) {
134 $callback = array( $subscriber, $parameters );
135
136 if ( is_callable( $callback ) ) {
137 $this->add_callback( $hook_name, $callback );
138 }
139 } elseif (
140 is_array( $parameters ) &&
141 count( $parameters ) !== count( $parameters, COUNT_RECURSIVE )
142 ) {
143 foreach ( $parameters as $parameter ) {
144 $this->add_subscriber_callback(
145 $subscriber,
146 $hook_name,
147 $parameter
148 );
149 }
150 } elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
151 $callback = array( $subscriber, $parameters[0] );
152
153 if ( is_callable( $callback ) ) {
154 $this->add_callback(
155 $hook_name,
156 $callback,
157 isset( $parameters[1] )
158 ? $parameters[1]
159 : 10,
160 isset( $parameters[2] )
161 ? $parameters[2]
162 : 1
163 );
164 }
165 }
166 }
167
168 /**
169 * Removes the given subscriber's callback to a specific hook.
170 *
171 * @since 4.4.0
172 *
173 * @param SubscriberInterface $subscriber SubscriberInterface implementation.
174 * @param string $hook_name Hook name.
175 * @param mixed $parameters Event parameters. Accepts a string, array, or a multidimensional array.
176 * @return void
177 */
178 private function remove_subscriber_callback(
179 SubscriberInterface $subscriber,
180 $hook_name,
181 $parameters
182 ) {
183 if ( is_string( $parameters ) ) {
184 $callback = array( $subscriber, $parameters );
185
186 if ( is_callable( $callback ) ) {
187 $this->remove_callback( $hook_name, $callback );
188 }
189 } elseif (
190 is_array( $parameters ) &&
191 count( $parameters ) !== count( $parameters, COUNT_RECURSIVE )
192 ) {
193 foreach ( $parameters as $parameter ) {
194 $this->remove_subscriber_callback(
195 $subscriber,
196 $hook_name,
197 $parameter
198 );
199 }
200 } elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
201 $callback = array( $subscriber, $parameters[0] );
202
203 if ( is_callable( $callback ) ) {
204 $this->remove_callback(
205 $hook_name,
206 $callback,
207 isset( $parameters[1] )
208 ? $parameters[1]
209 : 10
210 );
211 }
212 }
213 }
214 }
215