| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\WordPressLibraries; |
| 4 |
|
| 5 |
/** |
| 6 |
* This is a fork of WP_Async_Request that adds GiveWP namespaces to prevent conflicts with other plugins. |
| 7 |
* |
| 8 |
* IMPORTANT: Developers, please be aware that the usage of WPAsyncRequest and WPBackgroundProcess is discouraged as they are included only for legacy purposes. |
| 9 |
* Instead, it is strongly recommended to use Action Scheduler for any asynchronous processing needs. |
| 10 |
* Action Scheduler is available, provides a more efficient solution, and is the preferred choice for new development. |
| 11 |
* |
| 12 |
* @since 2.32.0 |
| 13 |
*/ |
| 14 |
abstract class WPAsyncRequest |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* Prefix |
| 19 |
* |
| 20 |
* (default value: 'wp') |
| 21 |
* |
| 22 |
* @var string |
| 23 |
* @access protected |
| 24 |
*/ |
| 25 |
protected $prefix = 'wp'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Action |
| 29 |
* |
| 30 |
* (default value: 'async_request') |
| 31 |
* |
| 32 |
* @var string |
| 33 |
* @access protected |
| 34 |
*/ |
| 35 |
protected $action = 'async_request'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Identifier |
| 39 |
* |
| 40 |
* @var mixed |
| 41 |
* @access protected |
| 42 |
*/ |
| 43 |
protected $identifier; |
| 44 |
|
| 45 |
/** |
| 46 |
* Data |
| 47 |
* |
| 48 |
* (default value: array()) |
| 49 |
* |
| 50 |
* @var array |
| 51 |
* @access protected |
| 52 |
*/ |
| 53 |
protected $data = []; |
| 54 |
|
| 55 |
/** |
| 56 |
* Initiate new async request |
| 57 |
*/ |
| 58 |
public function __construct() |
| 59 |
{ |
| 60 |
$this->identifier = $this->prefix . '_' . $this->action; |
| 61 |
|
| 62 |
add_action('wp_ajax_' . $this->identifier, [$this, 'maybe_handle']); |
| 63 |
add_action('wp_ajax_nopriv_' . $this->identifier, [$this, 'maybe_handle']); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Set data used during the request |
| 68 |
* |
| 69 |
* @param array $data Data. |
| 70 |
* |
| 71 |
* @return $this |
| 72 |
*/ |
| 73 |
public function data($data) |
| 74 |
{ |
| 75 |
$this->data = $data; |
| 76 |
|
| 77 |
return $this; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Dispatch the async request |
| 82 |
* |
| 83 |
* @return array|WP_Error |
| 84 |
*/ |
| 85 |
public function dispatch() |
| 86 |
{ |
| 87 |
$url = add_query_arg($this->get_query_args(), $this->get_query_url()); |
| 88 |
$args = $this->get_post_args(); |
| 89 |
|
| 90 |
return wp_remote_post(esc_url_raw($url), $args); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get query args |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
protected function get_query_args() |
| 99 |
{ |
| 100 |
if (property_exists($this, 'query_args')) { |
| 101 |
return $this->query_args; |
| 102 |
} |
| 103 |
|
| 104 |
return [ |
| 105 |
'action' => $this->identifier, |
| 106 |
'nonce' => wp_create_nonce($this->identifier), |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get query URL |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
protected function get_query_url() |
| 116 |
{ |
| 117 |
if (property_exists($this, 'query_url')) { |
| 118 |
return $this->query_url; |
| 119 |
} |
| 120 |
|
| 121 |
return admin_url('admin-ajax.php'); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get post args |
| 126 |
* |
| 127 |
* @return array |
| 128 |
*/ |
| 129 |
protected function get_post_args() |
| 130 |
{ |
| 131 |
if (property_exists($this, 'post_args')) { |
| 132 |
return $this->post_args; |
| 133 |
} |
| 134 |
|
| 135 |
return [ |
| 136 |
'timeout' => 0.01, |
| 137 |
'blocking' => false, |
| 138 |
'body' => $this->data, |
| 139 |
'cookies' => $_COOKIE, |
| 140 |
'sslverify' => apply_filters('https_local_ssl_verify', false), |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Maybe handle |
| 146 |
* |
| 147 |
* Check for correct nonce and pass to handler. |
| 148 |
*/ |
| 149 |
public function maybe_handle() |
| 150 |
{ |
| 151 |
// Don't lock up other requests while processing |
| 152 |
session_write_close(); |
| 153 |
|
| 154 |
check_ajax_referer($this->identifier, 'nonce'); |
| 155 |
|
| 156 |
$this->handle(); |
| 157 |
|
| 158 |
wp_die(); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Handle |
| 163 |
* |
| 164 |
* Override this method to perform any actions required |
| 165 |
* during the async request. |
| 166 |
*/ |
| 167 |
abstract protected function handle(); |
| 168 |
|
| 169 |
} |
| 170 |
|