wp-async-request.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WBCR\Factory_Processing_759; |
| 4 | |
| 5 | use WP_Error; |
| 6 | |
| 7 | /** |
| 8 | * WP Async Request |
| 9 | * |
| 10 | * @package WP-Background-Processing |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * Abstract WP_Async_Request class. |
| 15 | * |
| 16 | * @abstract |
| 17 | */ |
| 18 | abstract class WP_Async_Request { |
| 19 | |
| 20 | /** |
| 21 | * Prefix |
| 22 | * |
| 23 | * (default value: 'wp') |
| 24 | * |
| 25 | * @var string |
| 26 | * @access protected |
| 27 | */ |
| 28 | protected $prefix = 'wp'; |
| 29 | |
| 30 | /** |
| 31 | * Action |
| 32 | * |
| 33 | * (default value: 'async_request') |
| 34 | * |
| 35 | * @var string |
| 36 | * @access protected |
| 37 | */ |
| 38 | protected $action = 'async_request'; |
| 39 | |
| 40 | /** |
| 41 | * Identifier |
| 42 | * |
| 43 | * @var mixed |
| 44 | * @access protected |
| 45 | */ |
| 46 | protected $identifier; |
| 47 | |
| 48 | /** |
| 49 | * Data |
| 50 | * |
| 51 | * (default value: array()) |
| 52 | * |
| 53 | * @var array |
| 54 | * @access protected |
| 55 | */ |
| 56 | protected $data = []; |
| 57 | |
| 58 | /** |
| 59 | * Initiate new async request |
| 60 | */ |
| 61 | public function __construct() { |
| 62 | $this->identifier = $this->prefix . '_' . str_replace( '-', '_', $this->action ); |
| 63 | |
| 64 | add_action( 'wp_ajax_' . $this->identifier, [ $this, 'maybe_handle' ] ); |
| 65 | add_action( 'wp_ajax_nopriv_' . $this->identifier, [ $this, 'maybe_handle' ] ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Set data used during the request |
| 70 | * |
| 71 | * @param array $data Data. |
| 72 | * |
| 73 | * @return $this |
| 74 | */ |
| 75 | public function data( $data ) { |
| 76 | $this->data = $data; |
| 77 | |
| 78 | return $this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Dispatch the async request |
| 83 | * |
| 84 | * @return array|WP_Error |
| 85 | */ |
| 86 | public function dispatch() { |
| 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 | if ( property_exists( $this, 'query_args' ) ) { |
| 100 | return $this->query_args; |
| 101 | } |
| 102 | |
| 103 | $args = [ |
| 104 | 'action' => $this->identifier, |
| 105 | 'scope' => $this->scope, |
| 106 | 'nonce' => wp_create_nonce( $this->identifier ), |
| 107 | ]; |
| 108 | |
| 109 | /** |
| 110 | * Filters the post arguments used during an async request. |
| 111 | * |
| 112 | * @param array $url |
| 113 | */ |
| 114 | return apply_filters( $this->identifier . '_query_args', $args ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get query URL |
| 119 | * |
| 120 | * @return string |
| 121 | */ |
| 122 | protected function get_query_url() { |
| 123 | if ( property_exists( $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 | if ( property_exists( $this, 'post_args' ) ) { |
| 144 | return $this->post_args; |
| 145 | } |
| 146 | |
| 147 | $args = [ |
| 148 | 'timeout' => 0.01, |
| 149 | 'blocking' => false, |
| 150 | 'body' => $this->data, |
| 151 | 'cookies' => $_COOKIE, |
| 152 | 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
| 153 | ]; |
| 154 | |
| 155 | /** |
| 156 | * Filters the post arguments used during an async request. |
| 157 | * |
| 158 | * @param array $args |
| 159 | */ |
| 160 | return apply_filters( $this->identifier . '_post_args', $args ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Maybe handle |
| 165 | * |
| 166 | * Check for correct nonce and pass to handler. |
| 167 | */ |
| 168 | public function maybe_handle() { |
| 169 | // Don't lock up other requests while processing |
| 170 | session_write_close(); |
| 171 | |
| 172 | check_ajax_referer( $this->identifier, 'nonce' ); |
| 173 | |
| 174 | $this->handle(); |
| 175 | |
| 176 | wp_die(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Handle |
| 181 | * |
| 182 | * Override this method to perform any actions required |
| 183 | * during the async request. |
| 184 | */ |
| 185 | abstract protected function handle(); |
| 186 | } |
| 187 |