Handler.php
212 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Ajax; |
| 4 | |
| 5 | use AC\Registrable; |
| 6 | use LogicException; |
| 7 | |
| 8 | class Handler implements Registrable { |
| 9 | |
| 10 | const NONCE_ACTION = 'ac-ajax'; |
| 11 | |
| 12 | /** |
| 13 | * @var array |
| 14 | */ |
| 15 | protected $params; |
| 16 | |
| 17 | /** |
| 18 | * @var string|array |
| 19 | */ |
| 20 | protected $callback; |
| 21 | |
| 22 | /** |
| 23 | * @var bool |
| 24 | */ |
| 25 | protected $wp_ajax; |
| 26 | |
| 27 | /** |
| 28 | * @var int |
| 29 | */ |
| 30 | protected $priority = 10; |
| 31 | |
| 32 | /** |
| 33 | * @param bool|null $wp_ajax Using the WP Ajax endpoint or custom |
| 34 | */ |
| 35 | public function __construct( $wp_ajax = null ) { |
| 36 | $this->wp_ajax = $wp_ajax === null; |
| 37 | |
| 38 | $this->set_nonce(); |
| 39 | } |
| 40 | |
| 41 | public function register() { |
| 42 | if ( ! $this->get_action() ) { |
| 43 | throw new LogicException( 'Action parameter is missing.' ); |
| 44 | } |
| 45 | |
| 46 | if ( ! $this->get_callback() ) { |
| 47 | throw new LogicException( 'Callback is missing.' ); |
| 48 | } |
| 49 | |
| 50 | add_action( $this->get_action(), $this->get_callback(), $this->priority ); |
| 51 | } |
| 52 | |
| 53 | public function deregister() { |
| 54 | remove_action( $this->get_action(), $this->get_callback(), $this->priority ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @return string|null |
| 59 | */ |
| 60 | public function get_action() { |
| 61 | $action = $this->get_param( 'action' ); |
| 62 | |
| 63 | if ( $this->wp_ajax ) { |
| 64 | $action = 'wp_ajax_' . $action; |
| 65 | } |
| 66 | |
| 67 | return $action; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param string $action |
| 72 | * |
| 73 | * @return $this |
| 74 | */ |
| 75 | public function set_action( $action ) { |
| 76 | $this->params['action'] = $action; |
| 77 | |
| 78 | return $this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @param int $priority |
| 83 | * |
| 84 | * @return Handler |
| 85 | */ |
| 86 | public function set_priority( $priority ) { |
| 87 | if ( ! is_int( $priority ) ) { |
| 88 | throw new LogicException( 'Priority can only be of type integer.' ); |
| 89 | } |
| 90 | |
| 91 | $this->priority = $priority; |
| 92 | |
| 93 | return $this; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return int |
| 98 | */ |
| 99 | public function get_priority() { |
| 100 | return $this->priority; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param string|array $callback |
| 105 | * |
| 106 | * @return $this |
| 107 | */ |
| 108 | public function set_callback( $callback ) { |
| 109 | $this->callback = $callback; |
| 110 | |
| 111 | return $this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return array|string |
| 116 | */ |
| 117 | public function get_callback() { |
| 118 | return $this->callback; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param null|string $nonce |
| 123 | * |
| 124 | * @return $this |
| 125 | */ |
| 126 | public function set_nonce( $nonce = null ) { |
| 127 | if ( null === $nonce ) { |
| 128 | $nonce = wp_create_nonce( self::NONCE_ACTION ); |
| 129 | } |
| 130 | |
| 131 | $this->params['_ajax_nonce'] = $nonce; |
| 132 | |
| 133 | return $this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @return $this |
| 138 | */ |
| 139 | public function unset_nonce() { |
| 140 | unset( $this->params['_ajax_nonce'] ); |
| 141 | |
| 142 | return $this; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @param string $action |
| 147 | */ |
| 148 | public function verify_request( $action = null ) { |
| 149 | if ( null === $action ) { |
| 150 | $action = self::NONCE_ACTION; |
| 151 | } |
| 152 | |
| 153 | check_ajax_referer( $action ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return array |
| 158 | */ |
| 159 | public function get_params() { |
| 160 | return $this->params; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * @param $key |
| 165 | * |
| 166 | * @return mixed|null |
| 167 | */ |
| 168 | public function get_param( $key ) { |
| 169 | if ( ! array_key_exists( $key, $this->params ) ) { |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | return $this->params[ $key ]; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * @param array $params |
| 178 | * |
| 179 | * @return $this |
| 180 | */ |
| 181 | public function set_params( array $params ) { |
| 182 | foreach ( $params as $key => $value ) { |
| 183 | $this->set_param( $key, $value ); |
| 184 | } |
| 185 | |
| 186 | return $this; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param string $key |
| 191 | * @param mixed $value |
| 192 | * |
| 193 | * @return $this |
| 194 | */ |
| 195 | public function set_param( $key, $value ) { |
| 196 | switch ( $key ) { |
| 197 | case 'action': |
| 198 | $this->set_action( $value ); |
| 199 | |
| 200 | break; |
| 201 | case 'nonce': |
| 202 | $this->set_nonce( $value ); |
| 203 | |
| 204 | break; |
| 205 | default: |
| 206 | $this->params[ $key ] = $value; |
| 207 | } |
| 208 | |
| 209 | return $this; |
| 210 | } |
| 211 | |
| 212 | } |