PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.4
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / tribe-common / src / Tribe / Service_Providers / Processes.php
pods / tribe-common / src / Tribe / Service_Providers Last commit date
Body_Classes.php 2 weeks ago Crons.php 2 weeks ago Debug_Bar.php 2 weeks ago Dialog.php 2 weeks ago PUE.php 2 weeks ago Processes.php 2 weeks ago Promoter.php 2 weeks ago Shortcodes.php 2 weeks ago Tooltip.php 2 weeks ago Widgets.php 2 weeks ago
Processes.php
279 lines
1 <?php
2
3 /**
4 * Class Tribe__Service_Providers__Processes
5 *
6 * @since 4.7.12
7 *
8 * Handles the registration and creation of our async process handlers.
9 */
10 class Tribe__Service_Providers__Processes extends tad_DI52_ServiceProvider {
11
12 /**
13 * An array of all handler actions registered in this HTTP request.
14 *
15 * This is an array cache with a request lifetime by design.
16 *
17 * @var array
18 */
19 protected $handler_actions;
20
21 /**
22 * An array of all queue actions registered in this HTTP request.
23 *
24 * This is an array cache with a request lifetime by design.
25 *
26 * @var array
27 */
28 protected $queue_actions;
29
30 /**
31 * An instance of the context abstraction layer.
32 *
33 * @var Tribe__Context
34 */
35 protected $context;
36
37 /**
38 * Hooks the filters and binds the implementations needed to handle processes.
39 */
40 public function register() {
41 $this->context = tribe( 'context' );
42
43 // If the context of this request is neither AJAX or Cron bail.
44 if ( ! ( $this->context->doing_ajax() || $this->context->doing_cron() ) ) {
45 return;
46 }
47
48 /** @var Tribe__Feature_Detection $feature_detection */
49 $feature_detection = tribe( 'feature-detection' );
50 $action = tribe_get_request_var( 'action', false );
51 $testing_for_async_support = $action === $this->get_handler_action( 'Tribe__Process__Tester' );
52
53 // Dispatch in async mode if testing for it (w/o re-checking) or if async processes are supported.
54 if ( $testing_for_async_support || $feature_detection->supports_async_process() ) {
55 $this->dispatch_async();
56
57 return;
58 }
59
60 $this->dispatch_cron();
61 }
62
63 /**
64 * Hooks the correct handler for the action.
65 *
66 * @since 4.7.12
67 *
68 * @param string $action
69 */
70 protected function hook_handler_for( $action ) {
71 if ( null === $this->handler_actions ) {
72 $handlers = [
73 'Tribe__Process__Tester',
74 'Tribe__Process__Post_Thumbnail_Setter',
75 ];
76
77 /**
78 * Filters the process handler classes the Service Provider should handle.
79 *
80 * All handlers should extend the `Tribe__Process__Handler` base class.
81 *
82 * @since 4.7.12
83 *
84 * @param array $handlers
85 */
86 $handlers = array_unique( apply_filters( 'tribe_process_handlers', $handlers ) );
87
88 $this->handler_actions = array_combine(
89 $handlers,
90 array_map( [ $this, 'get_handler_action' ], $handlers )
91 );
92 }
93
94 $array_search = array_search( $action, $this->handler_actions, true );
95
96 if ( false === $handler_class = $array_search ) {
97 return;
98 }
99
100 // the handler will handle the hooking
101 $this->container->make( $handler_class );
102 }
103
104 /**
105 * Hooks the correct queue for the action.
106 *
107 * @since 4.7.12
108 *
109 * @param string $action
110 */
111 protected function hook_queue_for( $action ) {
112 if ( null === $this->queue_actions ) {
113 $queues = [
114 'Tribe__Promise',
115 ];
116
117 /**
118 * Filters the queue processing classes the Service Provider should handle.
119 *
120 * All queues should extend the `Tribe__Process__Queue` base class.
121 *
122 * @since 4.7.12
123 *
124 * @param array $queues An array of class names, each extending the `Tribe__Process__Queue` base class.
125 */
126 $queues = array_unique( apply_filters( 'tribe_process_queues', $queues ) );
127
128 $all_queues_actions = array_combine(
129 $queues,
130 array_map( [ $this, 'get_queue_action' ], $queues )
131 );
132 }
133
134 $array_search = array_search( $action, $all_queues_actions, true );
135
136 if ( false === $queue_class = $array_search ) {
137 return;
138 }
139
140 // the queue will handle the hooking
141 $this->container->make( $queue_class );
142 }
143
144 /**
145 * Returns the action for the handler.
146 *
147 * @since 4.7.12
148 *
149 * @param string $handler_class
150 *
151 * @return string
152 */
153 protected function get_handler_action( $handler_class ) {
154 /** @var Tribe__Process__Handler handler_class */
155 return 'tribe_process_' . call_user_func( [ $handler_class, 'action' ] );
156 }
157
158 /**
159 * Returns the action for the queue.
160 *
161 * @since 4.7.12
162 *
163 * @param string $queue_class
164 *
165 * @return string
166 */
167 protected function get_queue_action( $queue_class ) {
168 /** @var Tribe__Process__Queue queue_class */
169 return 'tribe_queue_' . call_user_func( [ $queue_class, 'action' ] );
170 }
171
172 /**
173 * Dispatches the request, if in AJAX context of a valid queue processing request,
174 * to the correct handler.
175 *
176 * @since 4.7.23
177 */
178 protected function dispatch_async() {
179 if ( ! (
180 $this->context->doing_ajax()
181 && false !== $action = tribe_get_request_var( 'action', false )
182 ) ) {
183 return;
184 }
185
186 $this->hook_handler_for_action( $action );
187 }
188
189 /**
190 * Start the process handlers if in the context of a cron process and
191 * if any is registered.
192 *
193 * @since 4.7.23
194 */
195 protected function dispatch_cron() {
196 if ( ! $this->context->doing_cron() ) {
197 return;
198 }
199
200 /*
201 * Here we parse the scheduled cron events to get those scheduled by a queue
202 * or process handler.
203 */
204 $hooks = $this->get_scheduled_like( [ 'tribe_process_', 'tribe_queue_' ] );
205
206 if ( empty( $hooks ) ) {
207 return;
208 }
209
210 foreach ( $hooks as $action ) {
211 /*
212 * Building the queue or process handler for an action will make it
213 * so the handler, in its `__construct` method, will hook on the action
214 * triggered by its cron event.
215 */
216 $this->hook_handler_for_action( $action );
217 }
218 }
219
220 /**
221 * Hooks the correct queue or process handler for an action if any.
222 *
223 * @since 4.7.23
224 *
225 * @param string $action The action to hook the handler, or queue, for.
226 */
227 protected function hook_handler_for_action( $action ) {
228 if (
229 0 !== strpos( $action, 'tribe_process_' )
230 && 0 !== strpos( $action, 'tribe_queue_' )
231 ) {
232 return;
233 }
234
235 if ( 0 === strpos( $action, 'tribe_process_' ) ) {
236 $this->hook_handler_for( $action );
237 } else {
238 $this->hook_queue_for( $action );
239 }
240 }
241
242 /**
243 * Parses the `cron` array to return the hook names starting with a pattern.
244 *
245 * @since 4.7.23
246 *
247 * @param string|array $needles A pattern to look for or an array of patterns; if
248 * this is an array then a match will be an hook that
249 * matches at least one pattern.
250 *
251 * @return array An array of hook names matching the pattern.
252 */
253 protected function get_scheduled_like( $needles ) {
254 $cron = get_option( 'cron', false );
255
256 if ( empty( $cron ) ) {
257 return [];
258 }
259
260 $needles = (array) $needles;
261 $matching = [];
262
263 foreach ( $cron as $time ) {
264 if ( ! is_array( $time ) ) {
265 continue;
266 }
267 foreach ( $time as $hook => $entry ) {
268 foreach ( $needles as $needle ) {
269 if ( false !== strpos( $hook, $needle ) ) {
270 $matching[] = $hook;
271 }
272 }
273 }
274 }
275
276 return $matching;
277 }
278 }
279