| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageSeoWP\Async; |
| 4 |
|
| 5 |
$base_url = (defined('DOCKER_ENV') ? 'http://host.docker.internal' : 'http://localhost'); |
| 6 |
/** |
| 7 |
* WP Async Request. |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Abstract WPAsyncRequest class. |
| 12 |
* |
| 13 |
* @abstract |
| 14 |
*/ |
| 15 |
abstract class WPAsyncRequest |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Prefix. |
| 19 |
* |
| 20 |
* (default value: 'wp') |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $prefix = 'wp'; |
| 25 |
/** |
| 26 |
* Action. |
| 27 |
* |
| 28 |
* (default value: 'async_request') |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $action = 'async_request'; |
| 33 |
/** |
| 34 |
* Identifier. |
| 35 |
* |
| 36 |
* @var mixed |
| 37 |
*/ |
| 38 |
protected $identifier; |
| 39 |
protected $query_args; |
| 40 |
protected $query_url; |
| 41 |
protected $post_args; |
| 42 |
/** |
| 43 |
* Data. |
| 44 |
* |
| 45 |
* (default value: array()) |
| 46 |
* |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
protected $data = []; |
| 50 |
|
| 51 |
/** |
| 52 |
* Initiate new async request. |
| 53 |
*/ |
| 54 |
public function __construct() |
| 55 |
{ |
| 56 |
$this->identifier = $this->prefix . '_' . $this->action; |
| 57 |
add_action('wp_ajax_' . $this->identifier, [$this, 'maybe_handle']); |
| 58 |
add_action('wp_ajax_nopriv_' . $this->identifier, [$this, 'maybe_handle']); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Set data used during the request. |
| 63 |
* |
| 64 |
* @param array $data Data. |
| 65 |
* |
| 66 |
* @return $this |
| 67 |
*/ |
| 68 |
public function data($data) |
| 69 |
{ |
| 70 |
$this->data = $data; |
| 71 |
|
| 72 |
return $this; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Dispatch the async request. |
| 77 |
* |
| 78 |
* @return array|WP_Error |
| 79 |
*/ |
| 80 |
public function dispatch() |
| 81 |
{ |
| 82 |
$url = add_query_arg($this->get_query_args(), $this->get_query_url()); |
| 83 |
$args = $this->get_post_args(); |
| 84 |
|
| 85 |
if (function_exists('getenv_docker')) { |
| 86 |
$url = str_replace('localhost', 'host.docker.internal', $url); |
| 87 |
} |
| 88 |
|
| 89 |
return wp_remote_post(esc_url_raw($url), $args); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get query args. |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
protected function get_query_args() |
| 98 |
{ |
| 99 |
if (property_exists($this, 'query_args') && !empty($this->query_args)) { |
| 100 |
return $this->query_args; |
| 101 |
} |
| 102 |
|
| 103 |
$args = [ |
| 104 |
'action' => $this->identifier, |
| 105 |
'nonce' => wp_create_nonce($this->identifier), |
| 106 |
]; |
| 107 |
|
| 108 |
/** |
| 109 |
* Filters the post arguments used during an async request. |
| 110 |
* |
| 111 |
* @param array $url |
| 112 |
*/ |
| 113 |
return apply_filters($this->identifier . '_query_args', $args); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get query URL. |
| 118 |
* |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
protected function get_query_url() |
| 122 |
{ |
| 123 |
if (property_exists($this, 'query_url') && !empty($this->query_url)) { |
| 124 |
return $this->query_url; |
| 125 |
} |
| 126 |
|
| 127 |
$url = admin_url('admin-ajax.php'); |
| 128 |
|
| 129 |
/** |
| 130 |
* Filters the post arguments used during an async request. |
| 131 |
* |
| 132 |
* @param string $url |
| 133 |
*/ |
| 134 |
return apply_filters($this->identifier . '_query_url', $url); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get post args. |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
protected function get_post_args() |
| 143 |
{ |
| 144 |
if (property_exists($this, 'post_args') && !empty($this->post_args)) { |
| 145 |
return $this->post_args; |
| 146 |
} |
| 147 |
|
| 148 |
$args = array( |
| 149 |
'timeout' => 5, |
| 150 |
'blocking' => false, |
| 151 |
'body' => $this->data, |
| 152 |
'cookies' => $_COOKIE, // Passing cookies ensures request is performed as initiating user. |
| 153 |
'sslverify' => apply_filters('https_local_ssl_verify', false), // Local requests, fine to pass false. |
| 154 |
); |
| 155 |
|
| 156 |
/** |
| 157 |
* Filters the post arguments used during an async request. |
| 158 |
* |
| 159 |
* @param array $args |
| 160 |
*/ |
| 161 |
return apply_filters($this->identifier . '_post_args', $args); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Maybe handle a dispatched request. |
| 167 |
* |
| 168 |
* Check for correct nonce and pass to handler. |
| 169 |
* |
| 170 |
* @return void|mixed |
| 171 |
*/ |
| 172 |
public function maybe_handle() |
| 173 |
{ |
| 174 |
// Don't lock up other requests while processing. |
| 175 |
session_write_close(); |
| 176 |
|
| 177 |
check_ajax_referer($this->identifier, 'nonce'); |
| 178 |
|
| 179 |
$this->handle(); |
| 180 |
|
| 181 |
return $this->maybe_wp_die(); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Should the process exit with wp_die? |
| 186 |
* |
| 187 |
* @param mixed $return What to return if filter says don't die, default is null. |
| 188 |
* |
| 189 |
* @return void|mixed |
| 190 |
*/ |
| 191 |
protected function maybe_wp_die($return = null) |
| 192 |
{ |
| 193 |
/** |
| 194 |
* Should wp_die be used? |
| 195 |
* |
| 196 |
* @return bool |
| 197 |
*/ |
| 198 |
if (apply_filters($this->identifier . '_wp_die', true)) { |
| 199 |
wp_die(); |
| 200 |
} |
| 201 |
|
| 202 |
return $return; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Handle. |
| 207 |
* |
| 208 |
* Override this method to perform any actions required |
| 209 |
* during the async request. |
| 210 |
*/ |
| 211 |
abstract protected function handle(); |
| 212 |
} |
| 213 |
|