| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Http; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use BadMethodCallException; |
| 7 |
use InvalidArgumentException; |
| 8 |
use FluentBooking\Framework\Foundation\App; |
| 9 |
|
| 10 |
/** |
| 11 |
* Register AJAX handlers using HTTP-like method names. |
| 12 |
* |
| 13 |
* $action should start with config.app.hook_prefix, |
| 14 |
* e.g. `wpfluent_my_action`. |
| 15 |
* |
| 16 |
* The $handler can be a callable or "Class@method" string. |
| 17 |
* These dynamic methods delegate to the register() method. |
| 18 |
* |
| 19 |
* @method $this get(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 20 |
* @method $this post(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 21 |
* @method $this put(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 22 |
* @method $this patch(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 23 |
* @method $this delete(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 24 |
* |
| 25 |
* @method static get(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 26 |
* @method static post(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 27 |
* @method static put(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 28 |
* @method static patch(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 29 |
* @method static delete(string $action, callable|string $handler, int|string|array $priority = 10, string $scope = "both") |
| 30 |
* |
| 31 |
* Usage Examples: |
| 32 |
* |
| 33 |
* Ajax::post('action', 'MyController@handle'); // priority 10, admin & public |
| 34 |
* Ajax::post('action', 'MyController@handle', 5); // priority 5, admin & public |
| 35 |
* Ajax::post('action', 'MyController@handle', 'admin'); // priority 10, admin |
| 36 |
* Ajax::post('action', 'MyController@handle', 5, 'public'); // priority 5, public |
| 37 |
* |
| 38 |
* Ajax::post('action', 'MyController@handle', [ |
| 39 |
* 'priority' => 5, |
| 40 |
* 'scope' => 'public' |
| 41 |
* ]); |
| 42 |
*/ |
| 43 |
|
| 44 |
class Ajax |
| 45 |
{ |
| 46 |
/** |
| 47 |
* @var \FluentBooking\Framework\Foundation\Application |
| 48 |
*/ |
| 49 |
protected $app = null; |
| 50 |
|
| 51 |
/** |
| 52 |
* $passthru allowed methods |
| 53 |
* @var array |
| 54 |
*/ |
| 55 |
protected $passthru = [ |
| 56 |
'get', |
| 57 |
'post', |
| 58 |
'put', |
| 59 |
'patch', |
| 60 |
'delete', |
| 61 |
]; |
| 62 |
|
| 63 |
/** |
| 64 |
* Consruct the Instance. |
| 65 |
* |
| 66 |
* @param \FluentBooking\Framework\Foundation\Application $app |
| 67 |
*/ |
| 68 |
public function __construct($app = null) |
| 69 |
{ |
| 70 |
$this->app = $app ?? App::getInstance(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Alternative constructor. |
| 75 |
* |
| 76 |
* @return $this |
| 77 |
*/ |
| 78 |
public static function getInstance() |
| 79 |
{ |
| 80 |
return new static; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Register the ajax hook using appropriate method. |
| 85 |
* |
| 86 |
* @param string $method |
| 87 |
* @param string $action |
| 88 |
* @param callable|string $handler |
| 89 |
* @param integer $priority |
| 90 |
* @param string $scope |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function register( |
| 94 |
$method, |
| 95 |
$action, |
| 96 |
$handler, |
| 97 |
$priority = 10, |
| 98 |
$scope = 'both' |
| 99 |
) { |
| 100 |
if (!in_array($method, $this->passthru)) { |
| 101 |
throw new BadMethodCallException( |
| 102 |
"Ajax::{$method}() is not supported." |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
// If 3rd argument is an array |
| 107 |
if (is_array($priority)) { |
| 108 |
$scope = $priority['scope'] ?? 'both'; |
| 109 |
$priority = $priority['priority'] ?? 10; |
| 110 |
} |
| 111 |
// If scope is passed as the 3rd argument |
| 112 |
elseif (is_string($priority) && !is_numeric($priority)) { |
| 113 |
[$scope, $priority] = [$priority, $scope]; |
| 114 |
} |
| 115 |
|
| 116 |
if (!in_array($scope, ['admin', 'public', 'both'], true)) { |
| 117 |
throw new InvalidArgumentException( |
| 118 |
"Invalid scope '{$scope}' provided." |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
$callback = function () use ($method, $handler) { |
| 123 |
try { |
| 124 |
wp_send_json_success( |
| 125 |
$this->handle($method, $handler) |
| 126 |
); |
| 127 |
} catch (Exception $e) { |
| 128 |
wp_send_json_error($e->getMessage()); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
if ($scope === 'both') { |
| 133 |
$registrationMethod = 'addAjaxActions'; |
| 134 |
} elseif ($scope === 'admin') { |
| 135 |
$registrationMethod = 'addAdminAjaxAction'; |
| 136 |
} elseif ($scope === 'public') { |
| 137 |
$registrationMethod = 'addPublicAjaxAction'; |
| 138 |
} |
| 139 |
|
| 140 |
$this->app->$registrationMethod( |
| 141 |
$action, $callback, $priority |
| 142 |
); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Handle an AJAX request. |
| 147 |
* |
| 148 |
* @param string $method |
| 149 |
* @param mixed $handler |
| 150 |
* @return mixed |
| 151 |
*/ |
| 152 |
public function handle($method, $handler) |
| 153 |
{ |
| 154 |
if ($_SERVER['REQUEST_METHOD'] !== strtoupper($method)) { |
| 155 |
throw new BadMethodCallException( |
| 156 |
"Invalid request method." |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
check_ajax_referer($this->app->config->get('hook_prefix')); |
| 161 |
|
| 162 |
return $this->app->call( |
| 163 |
$this->app->parseHookHandler($handler) |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Overload magic method. |
| 169 |
* |
| 170 |
* @param string $method |
| 171 |
* @param array $args |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public function __call($method, $args) |
| 175 |
{ |
| 176 |
$this->register($method, ...$args); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Overload static magic method. |
| 181 |
* |
| 182 |
* @param string $method |
| 183 |
* @param array $args |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public static function __callStatic($method, $args) |
| 187 |
{ |
| 188 |
return (new static)->$method(...$args); |
| 189 |
} |
| 190 |
} |
| 191 |
|