PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.91.6
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.91.6
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Foundation / Concerns / FoundationTrait.php

FoundationTrait.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.91.6, at vendor/wpfluent/framework/src/WPFluent/Foundation/Concerns/FoundationTrait.php

497 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Foundation\Concerns;
4
5 use FluentBoards\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 null
357 */
358 public function addShortcode($action, $handler)
359 {
360 return add_shortcode(
361 $action, $this->parseHookHandler($handler)
362 );
363 }
364
365 /**
366 * Execute a shortcode
367 * @param mixed $content
368 * @param boolean $ignore_html
369 * @return mixed
370 */
371 public function doShortcode($content, $ignore_html = false)
372 {
373 return do_shortcode($content, $ignore_html);
374 }
375
376 /**
377 * Parse a hookm handler
378 * @param mixed $handler
379 * @return mixed
380 */
381 public function parseHookHandler($handler)
382 {
383 if (is_string($handler)) {
384
385 if (function_exists($handler)) return $handler;
386
387 if (count($array = preg_split('/::|@/', $handler)) < 2) {
388 $array[] = 'handle';
389 }
390
391 list($class, $method) = $array;
392
393 $class = $this->makeInstance($class);
394
395 return is_callable($class) ? $class : [$class, $method];
396
397 } else if (is_array($handler)) {
398 list($class, $method) = $handler;
399 if (is_string($class)) {
400 $class = $this->makeInstance($class);
401 }
402 return [$class, $method];
403 }
404
405 return $handler;
406 }
407
408 /**
409 * Chdeck if handler has fqn
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 \FluentBoards\Framework\Http\Request\WPUserProxy|null
485 */
486 public function user()
487 {
488 if (did_action('init')) {
489 return new WPUserProxy(wp_get_current_user());
490 }
491
492 if ($this->isDebugOn()) {
493 error_log("User not available—'init' action hasn't fired yet.");
494 }
495 }
496 }
497