PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / classes / Dependencies / wp-media / event-manager / EventManager.php

EventManager.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at inc/classes/Dependencies/wp-media/event-manager/EventManager.php

136 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\EventManagement;
3
4 /**
5 * The event manager manages events using the WordPress plugin API.
6 *
7 * @since 3.1
8 * @author Carl Alexander <[email protected]>
9 */
10 class EventManager {
11 /**
12 * Adds a callback to a specific hook of the WordPress plugin API.
13 *
14 * @uses add_filter()
15 *
16 * @param string $hook_name Name of the hook.
17 * @param callable $callback Callback function.
18 * @param int $priority Priority.
19 * @param int $accepted_args Number of arguments.
20 */
21 public function add_callback( $hook_name, $callback, $priority = 10, $accepted_args = 1 ) {
22 add_filter( $hook_name, $callback, $priority, $accepted_args );
23 }
24
25 /**
26 * Add an event subscriber.
27 *
28 * The event manager registers all the hooks that the given subscriber
29 * wants to register with the WordPress Plugin API.
30 *
31 * @param SubscriberInterface $subscriber SubscriberInterface implementation.
32 */
33 public function add_subscriber( SubscriberInterface $subscriber ) {
34 if ( $subscriber instanceof EventManagerAwareSubscriberInterface ) {
35 $subscriber->set_event_manager( $this );
36 }
37
38 $events = $subscriber->get_subscribed_events();
39
40 if ( empty( $events ) ) {
41 return;
42 }
43
44 foreach ( $subscriber->get_subscribed_events() as $hook_name => $parameters ) {
45 $this->add_subscriber_callback( $subscriber, $hook_name, $parameters );
46 }
47 }
48
49 /**
50 * Checks the WordPress plugin API to see if the given hook has
51 * the given callback. The priority of the callback will be returned
52 * or false. If no callback is given will return true or false if
53 * there's any callbacks registered to the hook.
54 *
55 * @uses has_filter()
56 *
57 * @param string $hook_name Hook name.
58 * @param mixed $callback Callback.
59 *
60 * @return bool|int
61 */
62 public function has_callback( $hook_name, $callback = false ) {
63 return has_filter( $hook_name, $callback );
64 }
65
66 /**
67 * Removes the given callback from the given hook. The WordPress plugin API only
68 * removes the hook if the callback and priority match a registered hook.
69 *
70 * @uses remove_filter()
71 *
72 * @param string $hook_name Hook name.
73 * @param callable $callback Callback.
74 * @param int $priority Priority.
75 *
76 * @return bool
77 */
78 public function remove_callback( $hook_name, $callback, $priority = 10 ) {
79 return remove_filter( $hook_name, $callback, $priority );
80 }
81
82 /**
83 * Remove an event subscriber.
84 *
85 * The event manager removes all the hooks that the given subscriber
86 * wants to register with the WordPress Plugin API.
87 *
88 * @param SubscriberInterface $subscriber SubscriberInterface implementation.
89 */
90 public function remove_subscriber( SubscriberInterface $subscriber ) {
91 foreach ( $subscriber->get_subscribed_events() as $hook_name => $parameters ) {
92 $this->remove_subscriber_callback( $subscriber, $hook_name, $parameters );
93 }
94 }
95
96 /**
97 * Adds the given subscriber's callback to a specific hook
98 * of the WordPress plugin API.
99 *
100 * @param SubscriberInterface $subscriber SubscriberInterface implementation.
101 * @param string $hook_name Hook name.
102 * @param mixed $parameters Parameters, can be a string, an array or a multidimensional array.
103 */
104 private function add_subscriber_callback( SubscriberInterface $subscriber, $hook_name, $parameters ) {
105 if ( is_string( $parameters ) ) {
106 $this->add_callback( $hook_name, [ $subscriber, $parameters ] );
107 } elseif ( is_array( $parameters ) && count( $parameters ) !== count( $parameters, COUNT_RECURSIVE ) ) {
108 foreach ( $parameters as $parameter ) {
109 $this->add_subscriber_callback( $subscriber, $hook_name, $parameter );
110 }
111 } elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
112 $this->add_callback( $hook_name, [ $subscriber, $parameters[0] ], isset( $parameters[1] ) ? $parameters[1] : 10, isset( $parameters[2] ) ? $parameters[2] : 1 );
113 }
114 }
115
116 /**
117 * Removes the given subscriber's callback to a specific hook
118 * of the WordPress plugin API.
119 *
120 * @param SubscriberInterface $subscriber SubscriberInterface implementation.
121 * @param string $hook_name Hook name.
122 * @param mixed $parameters Parameters, can be a string, an array or a multidimensional array.
123 */
124 private function remove_subscriber_callback( SubscriberInterface $subscriber, $hook_name, $parameters ) {
125 if ( is_string( $parameters ) ) {
126 $this->remove_callback( $hook_name, [ $subscriber, $parameters ] );
127 } elseif ( is_array( $parameters ) && count( $parameters ) !== count( $parameters, COUNT_RECURSIVE ) ) {
128 foreach ( $parameters as $parameter ) {
129 $this->remove_subscriber_callback( $subscriber, $hook_name, $parameter );
130 }
131 } elseif ( is_array( $parameters ) && isset( $parameters[0] ) ) {
132 $this->remove_callback( $hook_name, [ $subscriber, $parameters[0] ], isset( $parameters[1] ) ? $parameters[1] : 10 );
133 }
134 }
135 }
136