PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.3.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.3.0
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / vendor / wpfluent / framework / src / WPFluent / Foundation / Concerns / FoundationTrait.php

FoundationTrait.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.3.0, at vendor/wpfluent/framework/src/WPFluent/Foundation/Concerns/FoundationTrait.php

525 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\Framework\Foundation\Concerns;
4
5 use FluentBooking\Framework\Http\Request\WPUserProxy;
6
7 trait FoundationTrait
8 {
9 use AjaxTrait, HooksRemovalTrait;
10
11 /**
12 * Check if wp debug mode is on.
13 *
14 * @return boolean
15 */
16 public function isDebugOn()
17 {
18 return defined('WP_DEBUG') && WP_DEBUG;
19 }
20
21 /**
22 * Check whether multi-site or not.
23 *
24 * @return boolean
25 */
26 public function isMultiSite()
27 {
28 return function_exists('is_multisite') && is_multisite();
29 }
30
31 /**
32 * Determine the environment.
33 *
34 * @return string
35 */
36 public function env()
37 {
38 if (php_sapi_name() === 'cli' && isset($_ENV['WPF_ENV'])) {
39 $env = $_ENV['WPF_ENV'];
40 } else {
41 $env = $this->isDebugOn() ? 'dev' : '';
42 }
43
44 return $env ?: $this->config->get('app.env', 'prod');
45 }
46
47 /**
48 * Get current namespace for the plugin.
49 *
50 * @return string
51 */
52 public function getCurrentNamespace()
53 {
54 return $this->getComposer('extra.wpfluent.namespace.current');
55 }
56
57 /**
58 * Make the custom hook name for the plugin.
59 * @param string $prefix
60 * @param string $hook
61 * @return string
62 */
63 public function hook($prefix, $hook)
64 {
65 return $prefix . $hook;
66 }
67
68 /**
69 * Parse the handler for the rest request
70 * @param string|\Closure $handler
71 * @param string $ns
72 * @return mixed
73 */
74 public function parseRestHandler($handler, $ns = '')
75 {
76 if (is_object($handler) && is_callable($handler)) {
77 return $handler;
78 }
79
80 if (is_array($handler)) {
81
82 if (is_object($handler[0])) {
83 return $handler;
84 }
85
86 if (is_string($handler[0])) {
87 $handler = $handler[0] . '@' . $handler[1];
88 }
89 }
90
91 if ($ns && str_contains($handler, $ns)) {
92 if (strpos($handler, $ns) !== false) {
93 $handler = trim(str_replace($ns, '', $handler), '\\');
94 }
95 }
96
97 $handler = $ns ? $ns . '\\' . $handler : $handler;
98
99 return $this->getControllerNamespace($handler) . '\\' . $handler;
100 }
101
102 /**
103 * Parse the policy handler
104 * @param string|array $handler
105 * @return mixed
106 */
107 public function parsePolicyHandler($handler)
108 {
109 if (is_string($handler)) {
110
111 if (function_exists($handler)) return $handler;
112
113 $handler = ltrim($handler, '\\');
114
115 $handler = $this->getPolicyNamespace($handler) . '\\' . $handler;
116
117 if (is_string($handler) && strpos($handler, '@') !== false) {
118
119 list($class, $method) = explode('@', $handler);
120
121 if (!method_exists($class, $method)) {
122 $method = 'verifyRequest';
123 }
124
125 $instance = $this->make($class);
126
127 $handler = [$instance, $method];
128 }
129
130 } else if (is_array($handler)) {
131 list($class, $method) = $handler;
132
133 if (is_string($class)) {
134 $handler = $this->getPolicyNamespace($handler) . '\\' . $class . '::' . $method;
135 }
136 }
137
138 return $handler;
139 }
140
141 /**
142 * Register an action hook
143 * @param string $action
144 * @param mixed $handler
145 * @param integer $priority
146 * @param integer $numOfArgs
147 */
148 public function addAction($action, $handler, $priority = 10, $numOfArgs = 1)
149 {
150 return add_action(
151 $action,
152 $this->parseHookHandler($handler),
153 $priority,
154 $numOfArgs
155 );
156 }
157
158 /**
159 * Register a custom action hook
160 * @param string $action
161 * @param mixed $handler
162 * @param integer $priority
163 * @param integer $numOfArgs
164 */
165 public function addCustomAction($action, $handler, $priority = 10, $numOfArgs = 1)
166 {
167 $prefix = $this->config->get('app.hook_prefix');
168
169 return $this->addAction(
170 $this->hook($prefix, $action), $handler, $priority, $numOfArgs
171 );
172 }
173
174 /**
175 * Dispatch an action
176 * @return null
177 */
178 public function doAction()
179 {
180 return call_user_func_array('do_action', func_get_args());
181 }
182
183 /**
184 * Dispatch a custom action
185 * @return null
186 */
187 public function doCustomAction()
188 {
189 $args = func_get_args();
190
191 $prefix = $this->config->get('app.hook_prefix');
192
193 $args[0] = $this->hook($prefix, $args[0]);
194
195 return call_user_func_array('do_action', $args);
196 }
197
198 /**
199 * Register a filter hook
200 * @param string $action
201 * @param mixed $handler
202 * @param integer $priority
203 * @param integer $numOfArgs
204 */
205 public function addFilter($action, $handler, $priority = 10, $numOfArgs = 1)
206 {
207 return add_filter(
208 $action,
209 $this->parseHookHandler($handler),
210 $priority,
211 $numOfArgs
212 );
213 }
214
215 /**
216 * Register a custom filter hook
217 * @param string $action
218 * @param mixed $handler
219 * @param integer $priority
220 * @param integer $numOfArgs
221 */
222 public function addCustomFilter($action, $handler, $priority = 10, $numOfArgs = 1)
223 {
224 $prefix = $this->config->get('app.hook_prefix');
225
226 return $this->addFilter(
227 $this->hook($prefix, $action), $handler, $priority, $numOfArgs
228 );
229 }
230
231 /**
232 * Dispatc a filter
233 * @return mixed
234 */
235 public function applyFilters()
236 {
237 return call_user_func_array('apply_filters', func_get_args());
238 }
239
240 /**
241 * Dispatch a custom filter
242 * @return mixed
243 */
244 public function applyCustomFilters()
245 {
246 $args = func_get_args();
247 $prefix = $this->config->get('app.hook_prefix');
248 $args[0] = $this->hook($prefix, $args[0]);
249
250 return call_user_func_array('apply_filters', $args);
251 }
252
253 /**
254 * Checks if any action has been fired.
255 *
256 * @param string $action
257 * @return int Number of times the action was fired
258 */
259 public function didAction($action)
260 {
261 return did_action($action);
262 }
263
264 /**
265 * Checks if any action has been registered.
266 *
267 * @param string $action
268 * @param mixed $callback
269 * @return bool
270 */
271 public function hasAction($action, $callback = false)
272 {
273 return has_action($action, $callback);
274 }
275
276 /**
277 * Checks if any custom action has been fired.
278 *
279 * @param string $action
280 * @return int Number of times the action was fired
281 */
282 public function didCustomAction($action)
283 {
284 return did_action(
285 $this->config->get('app.hook_prefix') . $action
286 );
287 }
288
289 /**
290 * Checks if any custom action has been registered.
291 *
292 * @param string $action
293 * @param mixed $callback
294 * @return bool
295 */
296 public function hasCustomAction($action, $callback = false)
297 {
298 $prefix = $this->config->get('app.hook_prefix');
299
300 return has_action($prefix.$action, $callback);
301 }
302
303 /**
304 * Checks if any action has been fired.
305 *
306 * @param string $action
307 * @return int Number of times the action was fired
308 */
309 public function didFilter($action)
310 {
311 return did_filter($action);
312 }
313
314 /**
315 * Checks if any action has been registered.
316 *
317 * @param string $action
318 * @param mixed $callback
319 * @return bool
320 */
321 public function hasFilter($action, $callback = false)
322 {
323 return has_filter($action, $callback);
324 }
325
326 /**
327 * Checks if any custom action has been fired.
328 *
329 * @param string $action
330 * @return int Number of times the action was fired
331 */
332 public function didCustomFilter($action)
333 {
334 return did_filter(
335 $this->config->get('app.hook_prefix') . $action
336 );
337 }
338
339 /**
340 * Checks if any custom action has been registered.
341 *
342 * @param string $action
343 * @param mixed $callback
344 * @return bool
345 */
346 public function hasCustomFilter($action, $callback = false)
347 {
348 $prefix = $this->config->get('app.hook_prefix');
349
350 return has_filter($prefix.$action, $callback);
351 }
352
353 /**
354 * Register a short code
355 * @param string $action
356 * @param string|\Closure $handler
357 * @return void
358 */
359 public function addShortcode($action, $handler)
360 {
361 add_shortcode($action, $this->parseHookHandler($handler));
362 }
363
364 /**
365 * Execute a shortcode
366 * @param mixed $content
367 * @param boolean $ignore_html
368 * @return mixed
369 */
370 public function doShortcode($content, $ignore_html = false)
371 {
372 return do_shortcode($content, $ignore_html);
373 }
374
375 /**
376 * Parse a hookm handler
377 * @param mixed $handler
378 * @return mixed
379 */
380 public function parseHookHandler($handler)
381 {
382 if (is_string($handler)) {
383
384 if (function_exists($handler)) return $handler;
385
386 if (count($array = preg_split('/::|@/', $handler)) < 2) {
387 $array[] = 'handle';
388 }
389
390 list($class, $method) = $array;
391
392 $class = $this->makeInstance($class);
393
394 return is_callable($class) ? $class : [$class, $method];
395
396 } else if (is_array($handler)) {
397 list($class, $method) = $handler;
398 if (is_string($class)) {
399 $class = $this->makeInstance($class);
400 }
401 return [$class, $method];
402 }
403
404 return $handler;
405 }
406
407 /**
408 * Chdeck if handler has FQCN.
409 *
410 * @param string|\Closure $handler
411 * @return boolean
412 */
413 public function hasNamespace($handler)
414 {
415 if ($handler instanceof \Closure) {
416 return false;
417 };
418
419 if (is_object($handler)) {
420 return false;
421 }
422
423 $parts = array_filter(explode('\\', $handler));
424
425 return count($parts) > 1;
426 }
427
428 /**
429 * Resolve the namespace for a controller
430 * @param string $handler
431 * @return mixed
432 */
433 public function getControllerNamespace($handler)
434 {
435 if ($this->hasNamespace($handler)) {
436 return '';
437 }
438
439 return $this->controllerNamespace;
440 }
441
442 /**
443 * Resolve the namespace for a policy
444 * @param string $handler
445 * @return mixed
446 */
447 public function getPolicyNamespace($handler)
448 {
449 if ($this->hasNamespace($handler)) {
450 return '';
451 }
452
453 return $this->policyNamespace;
454 }
455
456 /**
457 * Make an instance by the container
458 * @param string $class
459 * @return mixed
460 */
461 public function makeInstance($class)
462 {
463 if ($this->hasNamespace($class)) {
464 $instance = $this->make($class);
465 } else {
466 $instance = $this->make($this->handlerNamespace . '\\' . $class);
467 }
468
469 return $instance;
470 }
471
472 /**
473 * Retrieve the base url
474 * @param string $url
475 * @return string
476 */
477 public function url($url = '')
478 {
479 return $this->baseUrl.ltrim($url, '/');
480 }
481
482 /**
483 * Get the current authenticated user.
484 *
485 * Returns a WPUserProxy instance if the 'init' action has fired,
486 * otherwise logs a warning in debug mode and returns null.
487 *
488 * @return mixed
489 */
490 public function user()
491 {
492 if (!did_action('init')) {
493 if ($this->isDebugOn()) {
494 error_log("User not available—'init' action hasn't fired.");
495 }
496
497 return new class {
498 public function __call($method, $args) {}
499 };
500 }
501
502 $wpUser = wp_get_current_user();
503
504 if (!$wpUser->ID) {
505 return new class {
506 public function __call($method, $args) {}
507 };
508 }
509
510 $userModel = $this->__namespace__ . '\App\Models\User';
511
512 return class_exists($userModel)
513 ? ($userModel::find($wpUser->ID) ?? new WPUserProxy($wpUser))
514 : new WPUserProxy($wpUser);
515 }
516
517 public function enableApplicationPassword()
518 {
519 add_filter(
520 'wp_is_application_passwords_available',
521 fn() => !str_starts_with($this->env(), 'prod')
522 );
523 }
524 }
525