PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Ajax / Handler.php
codepress-admin-columns / classes / Ajax Last commit date
Handler.php 2 days ago NullHandler.php 2 days ago
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