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