← All changes
|
vendor/wpfluent/framework/src/WPFluent/Http/Ajax.php
+38
-76
1.10.0
→
2.5.0
View file →
| @@ -15,77 +15,19 @@ | ||
| 15 | 15 | * |
| 16 | 16 | * The $handler can be a callable or "Class@method" string. |
| 17 | 17 | * These dynamic methods delegate to the register() method. |
| 18 | 18 | * |
| 19 | - * @method $this get( | |
| 20 | - * string $action, | |
| 21 | - * callable|string $handler, | |
| 22 | - * int|string|array $priority = 10, | |
| 23 | - * string $scope = "both" | |
| 24 | - * ) | |
| 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") | |
| 25 | 24 | * |
| 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 | - * ) | |
| 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") | |
| 88 | 30 | * |
| 89 | 31 | * Usage Examples: |
| 90 | 32 | * |
| 91 | 33 | * Ajax::post('action', 'MyController@handle'); // priority 10, admin & public |
| @@ -101,10 +43,9 @@ | ||
| 101 | 43 | |
| 102 | 44 | class Ajax |
| 103 | 45 | { |
| 104 | 46 | /** |
| 105 | - * $app FluentBooking\Framework\Foundation\Application | |
| 106 | - * @var null | |
| 47 | + * @var \FluentBooking\Framework\Foundation\Application | |
| 107 | 48 | */ |
| 108 | 49 | protected $app = null; |
| 109 | 50 | |
| 110 | 51 | /** |
| @@ -121,9 +62,9 @@ | ||
| 121 | 62 | |
| 122 | 63 | /** |
| 123 | 64 | * Consruct the Instance. |
| 124 | 65 | * |
| 125 | - * @param FluentBooking\Framework\Foundation\Application $app | |
| 66 | + * @param \FluentBooking\Framework\Foundation\Application $app | |
| 126 | 67 | */ |
| 127 | 68 | public function __construct($app = null) |
| 128 | 69 | { |
| 129 | 70 | $this->app = $app ?? App::getInstance(); |
| @@ -177,12 +118,12 @@ | ||
| 177 | 118 | "Invalid scope '{$scope}' provided." |
| 178 | 119 | ); |
| 179 | 120 | } |
| 180 | 121 | |
| 181 | - $callback = function () use ($method, $action, $handler) { | |
| 122 | + $callback = function () use ($method, $handler) { | |
| 182 | 123 | try { |
| 183 | 124 | wp_send_json_success( |
| 184 | - $this->handle($method, $action, $handler) | |
| 125 | + $this->handle($method, $handler) | |
| 185 | 126 | ); |
| 186 | 127 | } catch (Exception $e) { |
| 187 | 128 | wp_send_json_error($e->getMessage()); |
| 188 | 129 | } |
| @@ -200,9 +141,16 @@ | ||
| 200 | 141 | $action, $callback, $priority |
| 201 | 142 | ); |
| 202 | 143 | } |
| 203 | 144 | |
| 204 | - public function handle($method, $action, $handler) | |
| 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) | |
| 205 | 153 | { |
| 206 | 154 | if ($_SERVER['REQUEST_METHOD'] !== strtoupper($method)) { |
| 207 | 155 | throw new BadMethodCallException( |
| 208 | 156 | "Invalid request method." |
| @@ -208,9 +156,9 @@ | ||
| 208 | 156 | "Invalid request method." |
| 209 | 157 | ); |
| 210 | 158 | } |
| 211 | 159 | |
| 212 | - check_ajax_referer($action); | |
| 160 | + check_ajax_referer($this->app->config->get('hook_prefix')); | |
| 213 | 161 | |
| 214 | 162 | return $this->app->call( |
| 215 | 163 | $this->app->parseHookHandler($handler) |
| 216 | 164 | ); |
| @@ -215,13 +163,27 @@ | ||
| 215 | 163 | $this->app->parseHookHandler($handler) |
| 216 | 164 | ); |
| 217 | 165 | } |
| 218 | 166 | |
| 167 | + /** | |
| 168 | + * Overload magic method. | |
| 169 | + * | |
| 170 | + * @param string $method | |
| 171 | + * @param array $args | |
| 172 | + * @return void | |
| 173 | + */ | |
| 219 | 174 | public function __call($method, $args) |
| 220 | 175 | { |
| 221 | - return $this->register($method, ...$args); | |
| 176 | + $this->register($method, ...$args); | |
| 222 | 177 | } |
| 223 | 178 | |
| 179 | + /** | |
| 180 | + * Overload static magic method. | |
| 181 | + * | |
| 182 | + * @param string $method | |
| 183 | + * @param array $args | |
| 184 | + * @return void | |
| 185 | + */ | |
| 224 | 186 | public static function __callStatic($method, $args) |
| 225 | 187 | { |
| 226 | 188 | return (new static)->$method(...$args); |
| 227 | 189 | } |