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