PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.6.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.6.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Foundation / Concerns / FoundationTrait.php

FoundationTrait.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.6.0, at vendor/wpfluent/framework/src/WPFluent/Foundation/Concerns/FoundationTrait.php

521 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Foundation\Concerns;
4
5 use FluentCommunity\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 ($handler instanceof \Closure) {
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 $parts = array_filter(explode('\\', $handler));
420
421 return count($parts) > 1;
422 }
423
424 /**
425 * Resolve the namespace for a controller
426 * @param string $handler
427 * @return mixed
428 */
429 public function getControllerNamespace($handler)
430 {
431 if ($this->hasNamespace($handler)) {
432 return '';
433 }
434
435 return $this->controllerNamespace;
436 }
437
438 /**
439 * Resolve the namespace for a policy
440 * @param string $handler
441 * @return mixed
442 */
443 public function getPolicyNamespace($handler)
444 {
445 if ($this->hasNamespace($handler)) {
446 return '';
447 }
448
449 return $this->policyNamespace;
450 }
451
452 /**
453 * Make an instance by the container
454 * @param string $class
455 * @return mixed
456 */
457 public function makeInstance($class)
458 {
459 if ($this->hasNamespace($class)) {
460 $instance = $this->make($class);
461 } else {
462 $instance = $this->make($this->handlerNamespace . '\\' . $class);
463 }
464
465 return $instance;
466 }
467
468 /**
469 * Retrieve the base url
470 * @param string $url
471 * @return string
472 */
473 public function url($url = '')
474 {
475 return $this->baseUrl.ltrim($url, '/');
476 }
477
478 /**
479 * Get the current authenticated user.
480 *
481 * Returns a WPUserProxy instance if the 'init' action has fired,
482 * otherwise logs a warning in debug mode and returns null.
483 *
484 * @return mixed
485 */
486 public function user()
487 {
488 if (!did_action('init')) {
489 if ($this->isDebugOn()) {
490 error_log("User not available—'init' action hasn't fired.");
491 }
492
493 return new class {
494 public function __call($method, $args) {}
495 };
496 }
497
498 $wpUser = wp_get_current_user();
499
500 if (!$wpUser->ID) {
501 return new class {
502 public function __call($method, $args) {}
503 };
504 }
505
506 $userModel = $this->__namespace__ . '\App\Models\User';
507
508 return class_exists($userModel)
509 ? ($userModel::find($wpUser->ID) ?? new WPUserProxy($wpUser))
510 : new WPUserProxy($wpUser);
511 }
512
513 public function enableApplicationPassword()
514 {
515 add_filter(
516 'wp_is_application_passwords_available',
517 fn() => !str_starts_with($this->env(), 'prod')
518 );
519 }
520 }
521