@@ -14,157 +14,157 @@ |
||
| 14 | 14 | |
| 15 | 15 | if ( ! class_exists( 'WP_Async_Request' ) ) { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Abstract WP_Async_Request class. |
|
| 19 | - * |
|
| 20 | - * @abstract |
|
| 21 | - */ |
|
| 22 | - abstract class WP_Async_Request { |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Prefix |
|
| 26 | - * |
|
| 27 | - * (default value: 'wp') |
|
| 28 | - * |
|
| 29 | - * @var string |
|
| 30 | - * @access protected |
|
| 31 | - */ |
|
| 32 | - protected $prefix = 'wp'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Action |
|
| 36 | - * |
|
| 37 | - * (default value: 'async_request') |
|
| 38 | - * |
|
| 39 | - * @var string |
|
| 40 | - * @access protected |
|
| 41 | - */ |
|
| 42 | - protected $action = 'async_request'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Identifier |
|
| 46 | - * |
|
| 47 | - * @var mixed |
|
| 48 | - * @access protected |
|
| 49 | - */ |
|
| 50 | - protected $identifier; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Data |
|
| 54 | - * |
|
| 55 | - * (default value: array()) |
|
| 56 | - * |
|
| 57 | - * @var array |
|
| 58 | - * @access protected |
|
| 59 | - */ |
|
| 60 | - protected $data = array(); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Initiate new async request |
|
| 64 | - */ |
|
| 65 | - public function __construct() { |
|
| 66 | - $this->identifier = $this->prefix . '_' . $this->action; |
|
| 67 | - |
|
| 68 | - add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 69 | - add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Set data used during the request |
|
| 74 | - * |
|
| 75 | - * @param array $data Data. |
|
| 76 | - * |
|
| 77 | - * @return $this |
|
| 78 | - */ |
|
| 79 | - public function data( $data ) { |
|
| 80 | - $this->data = $data; |
|
| 81 | - |
|
| 82 | - return $this; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Dispatch the async request |
|
| 87 | - * |
|
| 88 | - * @return array|WP_Error |
|
| 89 | - */ |
|
| 90 | - public function dispatch() { |
|
| 91 | - $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
|
| 92 | - $args = $this->get_post_args(); |
|
| 93 | - |
|
| 94 | - return wp_remote_post( esc_url_raw( $url ), $args ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Get query args |
|
| 99 | - * |
|
| 100 | - * @return array |
|
| 101 | - */ |
|
| 102 | - protected function get_query_args() { |
|
| 103 | - if ( property_exists( $this, 'query_args' ) ) { |
|
| 104 | - return $this->query_args; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return array( |
|
| 108 | - 'action' => $this->identifier, |
|
| 109 | - 'nonce' => wp_create_nonce( $this->identifier ), |
|
| 110 | - ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get query URL |
|
| 115 | - * |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - protected function get_query_url() { |
|
| 119 | - if ( property_exists( $this, 'query_url' ) ) { |
|
| 120 | - return $this->query_url; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return admin_url( 'admin-ajax.php' ); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Get post args |
|
| 128 | - * |
|
| 129 | - * @return array |
|
| 130 | - */ |
|
| 131 | - protected function get_post_args() { |
|
| 132 | - if ( property_exists( $this, 'post_args' ) ) { |
|
| 133 | - return $this->post_args; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return array( |
|
| 137 | - 'timeout' => 0.01, |
|
| 138 | - 'blocking' => false, |
|
| 139 | - 'body' => $this->data, |
|
| 140 | - 'cookies' => $_COOKIE, |
|
| 141 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Maybe handle |
|
| 147 | - * |
|
| 148 | - * Check for correct nonce and pass to handler. |
|
| 149 | - */ |
|
| 150 | - public function maybe_handle() { |
|
| 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 | - } |
|
| 17 | + /** |
|
| 18 | + * Abstract WP_Async_Request class. |
|
| 19 | + * |
|
| 20 | + * @abstract |
|
| 21 | + */ |
|
| 22 | + abstract class WP_Async_Request { |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Prefix |
|
| 26 | + * |
|
| 27 | + * (default value: 'wp') |
|
| 28 | + * |
|
| 29 | + * @var string |
|
| 30 | + * @access protected |
|
| 31 | + */ |
|
| 32 | + protected $prefix = 'wp'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Action |
|
| 36 | + * |
|
| 37 | + * (default value: 'async_request') |
|
| 38 | + * |
|
| 39 | + * @var string |
|
| 40 | + * @access protected |
|
| 41 | + */ |
|
| 42 | + protected $action = 'async_request'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Identifier |
|
| 46 | + * |
|
| 47 | + * @var mixed |
|
| 48 | + * @access protected |
|
| 49 | + */ |
|
| 50 | + protected $identifier; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Data |
|
| 54 | + * |
|
| 55 | + * (default value: array()) |
|
| 56 | + * |
|
| 57 | + * @var array |
|
| 58 | + * @access protected |
|
| 59 | + */ |
|
| 60 | + protected $data = array(); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Initiate new async request |
|
| 64 | + */ |
|
| 65 | + public function __construct() { |
|
| 66 | + $this->identifier = $this->prefix . '_' . $this->action; |
|
| 67 | + |
|
| 68 | + add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 69 | + add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Set data used during the request |
|
| 74 | + * |
|
| 75 | + * @param array $data Data. |
|
| 76 | + * |
|
| 77 | + * @return $this |
|
| 78 | + */ |
|
| 79 | + public function data( $data ) { |
|
| 80 | + $this->data = $data; |
|
| 81 | + |
|
| 82 | + return $this; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Dispatch the async request |
|
| 87 | + * |
|
| 88 | + * @return array|WP_Error |
|
| 89 | + */ |
|
| 90 | + public function dispatch() { |
|
| 91 | + $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
|
| 92 | + $args = $this->get_post_args(); |
|
| 93 | + |
|
| 94 | + return wp_remote_post( esc_url_raw( $url ), $args ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Get query args |
|
| 99 | + * |
|
| 100 | + * @return array |
|
| 101 | + */ |
|
| 102 | + protected function get_query_args() { |
|
| 103 | + if ( property_exists( $this, 'query_args' ) ) { |
|
| 104 | + return $this->query_args; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return array( |
|
| 108 | + 'action' => $this->identifier, |
|
| 109 | + 'nonce' => wp_create_nonce( $this->identifier ), |
|
| 110 | + ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get query URL |
|
| 115 | + * |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + protected function get_query_url() { |
|
| 119 | + if ( property_exists( $this, 'query_url' ) ) { |
|
| 120 | + return $this->query_url; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return admin_url( 'admin-ajax.php' ); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Get post args |
|
| 128 | + * |
|
| 129 | + * @return array |
|
| 130 | + */ |
|
| 131 | + protected function get_post_args() { |
|
| 132 | + if ( property_exists( $this, 'post_args' ) ) { |
|
| 133 | + return $this->post_args; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return array( |
|
| 137 | + 'timeout' => 0.01, |
|
| 138 | + 'blocking' => false, |
|
| 139 | + 'body' => $this->data, |
|
| 140 | + 'cookies' => $_COOKIE, |
|
| 141 | + 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Maybe handle |
|
| 147 | + * |
|
| 148 | + * Check for correct nonce and pass to handler. |
|
| 149 | + */ |
|
| 150 | + public function maybe_handle() { |
|
| 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 | 170 | } |