| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\Framework\Http; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use BadMethodCallException; |
| 7 |
use InvalidArgumentException; |
| 8 |
use FluentBoards\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( |
| 20 |
* string $action, |
| 21 |
* callable|string $handler, |
| 22 |
* int|string|array $priority = 10, |
| 23 |
* string $scope = "both" |
| 24 |
* ) |
| 25 |
* |
| 26 |
* @method $this post( |
| 27 |
* string $action, |
| 28 |
* callable|string $handler, |
| 29 |
* int|string|array $priority = 10, |
| 30 |
* string $scope = "both" |
| 31 |
* ) |
| 32 |
* |
| 33 |
* @method $this put( |
| 34 |
* string $action, |
| 35 |
* callable|string $handler, |
| 36 |
* int|string|array $priority = 10, |
| 37 |
* string $scope = "both" |
| 38 |
* ) |
| 39 |
* |
| 40 |
* @method $this patch( |
| 41 |
* string $action, |
| 42 |
* callable|string $handler, |
| 43 |
* int|string|array $priority = 10, |
| 44 |
* string $scope = "both" |
| 45 |
* ) |
| 46 |
* |
| 47 |
* @method $this delete( |
| 48 |
* string $action, |
| 49 |
* callable|string $handler, |
| 50 |
* int|string|array $priority = 10, |
| 51 |
* string $scope = "both" |
| 52 |
* ) |
| 53 |
* |
| 54 |
* @method static get( |
| 55 |
* string $action, |
| 56 |
* callable|string $handler, |
| 57 |
* int|string|array $priority = 10, |
| 58 |
* string $scope = "both" |
| 59 |
* ) |
| 60 |
* |
| 61 |
* @method static post( |
| 62 |
* string $action, |
| 63 |
* callable|string $handler, |
| 64 |
* int|string|array $priority = 10, |
| 65 |
* string $scope = "both" |
| 66 |
* ) |
| 67 |
* |
| 68 |
* @method static put( |
| 69 |
* string $action, |
| 70 |
* callable|string $handler, |
| 71 |
* int|string|array $priority = 10, |
| 72 |
* string $scope = "both" |
| 73 |
* ) |
| 74 |
* |
| 75 |
* @method static patch( |
| 76 |
* string $action, |
| 77 |
* callable|string $handler, |
| 78 |
* int|string|array $priority = 10, |
| 79 |
* string $scope = "both" |
| 80 |
* ) |
| 81 |
* |
| 82 |
* @method static delete( |
| 83 |
* string $action, |
| 84 |
* callable|string $handler, |
| 85 |
* int|string|array $priority = 10, |
| 86 |
* string $scope = "both" |
| 87 |
* ) |
| 88 |
* |
| 89 |
* Usage Examples: |
| 90 |
* |
| 91 |
* Ajax::post('action', 'MyController@handle'); // priority 10, admin & public |
| 92 |
* Ajax::post('action', 'MyController@handle', 5); // priority 5, admin & public |
| 93 |
* Ajax::post('action', 'MyController@handle', 'admin'); // priority 10, admin |
| 94 |
* Ajax::post('action', 'MyController@handle', 5, 'public'); // priority 5, public |
| 95 |
* |
| 96 |
* Ajax::post('action', 'MyController@handle', [ |
| 97 |
* 'priority' => 5, |
| 98 |
* 'scope' => 'public' |
| 99 |
* ]); |
| 100 |
*/ |
| 101 |
|
| 102 |
class Ajax |
| 103 |
{ |
| 104 |
/** |
| 105 |
* $app FluentBoards\Framework\Foundation\Application |
| 106 |
* @var null |
| 107 |
*/ |
| 108 |
protected $app = null; |
| 109 |
|
| 110 |
/** |
| 111 |
* $passthru allowed methods |
| 112 |
* @var array |
| 113 |
*/ |
| 114 |
protected $passthru = [ |
| 115 |
'get', |
| 116 |
'post', |
| 117 |
'put', |
| 118 |
'patch', |
| 119 |
'delete', |
| 120 |
]; |
| 121 |
|
| 122 |
/** |
| 123 |
* Consruct the Instance. |
| 124 |
* |
| 125 |
* @param FluentBoards\Framework\Foundation\Application $app |
| 126 |
*/ |
| 127 |
public function __construct($app = null) |
| 128 |
{ |
| 129 |
$this->app = $app ?? App::getInstance(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Alternative constructor. |
| 134 |
* |
| 135 |
* @return $this |
| 136 |
*/ |
| 137 |
public static function getInstance() |
| 138 |
{ |
| 139 |
return new static; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Register the ajax hook using appropriate method. |
| 144 |
* |
| 145 |
* @param string $method |
| 146 |
* @param string $action |
| 147 |
* @param callable|string $handler |
| 148 |
* @param integer $priority |
| 149 |
* @param string $scope |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public function register( |
| 153 |
$method, |
| 154 |
$action, |
| 155 |
$handler, |
| 156 |
$priority = 10, |
| 157 |
$scope = 'both' |
| 158 |
) { |
| 159 |
if (!in_array($method, $this->passthru)) { |
| 160 |
throw new BadMethodCallException( |
| 161 |
"Ajax::{$method}() is not supported." |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
// If 3rd argument is an array |
| 166 |
if (is_array($priority)) { |
| 167 |
$scope = $priority['scope'] ?? 'both'; |
| 168 |
$priority = $priority['priority'] ?? 10; |
| 169 |
} |
| 170 |
// If scope is passed as the 3rd argument |
| 171 |
elseif (is_string($priority) && !is_numeric($priority)) { |
| 172 |
[$scope, $priority] = [$priority, $scope]; |
| 173 |
} |
| 174 |
|
| 175 |
if (!in_array($scope, ['admin', 'public', 'both'], true)) { |
| 176 |
throw new InvalidArgumentException( |
| 177 |
"Invalid scope '{$scope}' provided." |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
$callback = function () use ($method, $action, $handler) { |
| 182 |
try { |
| 183 |
wp_send_json_success( |
| 184 |
$this->handle($method, $action, $handler) |
| 185 |
); |
| 186 |
} catch (Exception $e) { |
| 187 |
wp_send_json_error($e->getMessage()); |
| 188 |
} |
| 189 |
}; |
| 190 |
|
| 191 |
if ($scope === 'both') { |
| 192 |
$registrationMethod = 'addAjaxActions'; |
| 193 |
} elseif ($scope === 'admin') { |
| 194 |
$registrationMethod = 'addAdminAjaxAction'; |
| 195 |
} elseif ($scope === 'public') { |
| 196 |
$registrationMethod = 'addPublicAjaxAction'; |
| 197 |
} |
| 198 |
|
| 199 |
$this->app->$registrationMethod( |
| 200 |
$action, $callback, $priority |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
public function handle($method, $action, $handler) |
| 205 |
{ |
| 206 |
if ($_SERVER['REQUEST_METHOD'] !== strtoupper($method)) { |
| 207 |
throw new BadMethodCallException( |
| 208 |
"Invalid request method." |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
check_ajax_referer($action); |
| 213 |
|
| 214 |
return $this->app->call( |
| 215 |
$this->app->parseHookHandler($handler) |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
public function __call($method, $args) |
| 220 |
{ |
| 221 |
return $this->register($method, ...$args); |
| 222 |
} |
| 223 |
|
| 224 |
public static function __callStatic($method, $args) |
| 225 |
{ |
| 226 |
return (new static)->$method(...$args); |
| 227 |
} |
| 228 |
} |
| 229 |
|