5, * 'scope' => 'public' * ]); */ class Ajax { /** * @var \FluentForm\Framework\Foundation\Application */ protected $app = null; /** * $passthru allowed methods * @var array */ protected $passthru = [ 'get', 'post', 'put', 'patch', 'delete', ]; /** * Consruct the Instance. * * @param \FluentForm\Framework\Foundation\Application $app */ public function __construct($app = null) { $this->app = $app ?? App::getInstance(); } /** * Alternative constructor. * * @return $this */ public static function getInstance() { return new static; } /** * Register the ajax hook using appropriate method. * * @param string $method * @param string $action * @param callable|string $handler * @param integer $priority * @param string $scope * @return void */ public function register( $method, $action, $handler, $priority = 10, $scope = 'both' ) { if (!in_array($method, $this->passthru)) { throw new BadMethodCallException( "Ajax::{$method}() is not supported." ); } // If 3rd argument is an array if (is_array($priority)) { $scope = $priority['scope'] ?? 'both'; $priority = $priority['priority'] ?? 10; } // If scope is passed as the 3rd argument elseif (is_string($priority) && !is_numeric($priority)) { [$scope, $priority] = [$priority, $scope]; } if (!in_array($scope, ['admin', 'public', 'both'], true)) { throw new InvalidArgumentException( "Invalid scope '{$scope}' provided." ); } $callback = function () use ($method, $handler) { try { wp_send_json_success( $this->handle($method, $handler) ); } catch (Exception $e) { wp_send_json_error($e->getMessage()); } }; if ($scope === 'both') { $registrationMethod = 'addAjaxActions'; } elseif ($scope === 'admin') { $registrationMethod = 'addAdminAjaxAction'; } elseif ($scope === 'public') { $registrationMethod = 'addPublicAjaxAction'; } $this->app->$registrationMethod( $action, $callback, $priority ); } /** * Handle an AJAX request. * * @param string $method * @param mixed $handler * @return mixed */ public function handle($method, $handler) { if ($_SERVER['REQUEST_METHOD'] !== strtoupper($method)) { throw new BadMethodCallException( "Invalid request method." ); } check_ajax_referer($this->app->config->get('hook_prefix')); return $this->app->call( $this->app->parseHookHandler($handler) ); } /** * Overload magic method. * * @param string $method * @param array $args * @return void */ public function __call($method, $args) { $this->register($method, ...$args); } /** * Overload static magic method. * * @param string $method * @param array $args * @return void */ public static function __callStatic($method, $args) { return (new static)->$method(...$args); } }