Handler.php
141 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Ajax; |
| 6 | |
| 7 | use AC\Registerable; |
| 8 | use LogicException; |
| 9 | |
| 10 | class Handler implements Registerable |
| 11 | { |
| 12 | public const NONCE_ACTION = 'ac-ajax'; |
| 13 | |
| 14 | protected array $params = []; |
| 15 | |
| 16 | /** |
| 17 | * @var callable |
| 18 | */ |
| 19 | protected $callback; |
| 20 | |
| 21 | protected bool $wp_ajax; |
| 22 | |
| 23 | protected int $priority = 10; |
| 24 | |
| 25 | public function __construct(?bool $wp_ajax = null) |
| 26 | { |
| 27 | $this->wp_ajax = $wp_ajax === null; |
| 28 | |
| 29 | $this->set_nonce(); |
| 30 | } |
| 31 | |
| 32 | public function register(): void |
| 33 | { |
| 34 | if (! $this->get_action()) { |
| 35 | throw new LogicException('Action parameter is missing.'); |
| 36 | } |
| 37 | |
| 38 | if (! $this->get_callback()) { |
| 39 | throw new LogicException('Callback is missing.'); |
| 40 | } |
| 41 | |
| 42 | add_action($this->get_action(), $this->get_callback(), $this->priority); |
| 43 | } |
| 44 | |
| 45 | public function deregister(): void |
| 46 | { |
| 47 | remove_action($this->get_action(), $this->get_callback(), $this->priority); |
| 48 | } |
| 49 | |
| 50 | public function get_action(): string |
| 51 | { |
| 52 | $action = $this->get_param('action'); |
| 53 | |
| 54 | if ($this->wp_ajax) { |
| 55 | $action = 'wp_ajax_' . $action; |
| 56 | } |
| 57 | |
| 58 | return $action; |
| 59 | } |
| 60 | |
| 61 | public function set_action(string $action): self |
| 62 | { |
| 63 | $this->params['action'] = $action; |
| 64 | |
| 65 | return $this; |
| 66 | } |
| 67 | |
| 68 | public function set_priority(int $priority): self |
| 69 | { |
| 70 | $this->priority = $priority; |
| 71 | |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | public function get_priority(): ?int |
| 76 | { |
| 77 | return $this->priority; |
| 78 | } |
| 79 | |
| 80 | public function set_callback(callable $callback): self |
| 81 | { |
| 82 | $this->callback = $callback; |
| 83 | |
| 84 | return $this; |
| 85 | } |
| 86 | |
| 87 | public function get_callback(): callable |
| 88 | { |
| 89 | return $this->callback; |
| 90 | } |
| 91 | |
| 92 | public function set_nonce(?string $nonce = null): void |
| 93 | { |
| 94 | if (null === $nonce) { |
| 95 | $nonce = wp_create_nonce(self::NONCE_ACTION); |
| 96 | } |
| 97 | |
| 98 | $this->params['_ajax_nonce'] = $nonce; |
| 99 | } |
| 100 | |
| 101 | public function verify_request(?string $action = null): void |
| 102 | { |
| 103 | if (null === $action) { |
| 104 | $action = self::NONCE_ACTION; |
| 105 | } |
| 106 | |
| 107 | check_ajax_referer($action); |
| 108 | } |
| 109 | |
| 110 | public function get_params(): array |
| 111 | { |
| 112 | return $this->params; |
| 113 | } |
| 114 | |
| 115 | public function get_param(string $key) |
| 116 | { |
| 117 | if (! array_key_exists($key, $this->params)) { |
| 118 | return null; |
| 119 | } |
| 120 | |
| 121 | return $this->params[$key]; |
| 122 | } |
| 123 | |
| 124 | public function set_param(string $key, $value): void |
| 125 | { |
| 126 | switch ($key) { |
| 127 | case 'action': |
| 128 | $this->set_action($value); |
| 129 | |
| 130 | break; |
| 131 | case 'nonce': |
| 132 | $this->set_nonce($value); |
| 133 | |
| 134 | break; |
| 135 | default: |
| 136 | $this->params[$key] = $value; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | } |
| 141 |