PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / libs / wp-background-processing / classes / wp-async-request.php

wp-async-request.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/libs/wp-background-processing/classes/wp-async-request.php

208 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WP Async Request
4 *
5 * @package WP-Background-Processing
6 */
7
8 namespace AliNext_Lite\Library\BackgroundProcessing;
9
10 use WP_Error;
11
12 /**
13 * Abstract WP_Async_Request class.
14 *
15 * @abstract
16 */
17 abstract class WP_Async_Request {
18
19 /**
20 * Prefix
21 *
22 * (default value: 'wp')
23 *
24 * @var string
25 * @access protected
26 */
27 protected $prefix = 'wp';
28
29 /**
30 * Action
31 *
32 * (default value: 'async_request')
33 *
34 * @var string
35 * @access protected
36 */
37 protected $action = 'async_request';
38
39 /**
40 * Identifier
41 *
42 * @var mixed
43 * @access protected
44 */
45 protected $identifier;
46
47 /**
48 * Data
49 *
50 * (default value: array())
51 *
52 * @var array
53 * @access protected
54 */
55 protected $data = array();
56
57 /**
58 * Initiate new async request.
59 */
60 public function __construct() {
61 $this->identifier = $this->prefix . '_' . $this->action;
62
63 add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
64 add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
65 }
66
67 /**
68 * Set data used during the request.
69 *
70 * @param array $data Data.
71 *
72 * @return $this
73 */
74 public function data( $data ) {
75 $this->data = $data;
76
77 return $this;
78 }
79
80 /**
81 * Dispatch the async request.
82 *
83 * @return array|WP_Error|false HTTP Response array, WP_Error on failure, or false if not attempted.
84 */
85 public function dispatch() {
86 $url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
87 $args = $this->get_post_args();
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 if ( property_exists( $this, 'query_args' ) ) {
99 return $this->query_args;
100 }
101
102 $args = array(
103 'action' => $this->identifier,
104 'nonce' => wp_create_nonce( $this->identifier ),
105 );
106
107 /**
108 * Filters the post arguments used during an async request.
109 *
110 * @param array $url
111 */
112 return apply_filters( $this->identifier . '_query_args', $args );
113 }
114
115 /**
116 * Get query URL.
117 *
118 * @return string
119 */
120 protected function get_query_url() {
121 if ( property_exists( $this, 'query_url' ) ) {
122 return $this->query_url;
123 }
124
125 $url = admin_url( 'admin-ajax.php' );
126
127 /**
128 * Filters the post arguments used during an async request.
129 *
130 * @param string $url
131 */
132 return apply_filters( $this->identifier . '_query_url', $url );
133 }
134
135 /**
136 * Get post args.
137 *
138 * @return array
139 */
140 protected function get_post_args() {
141 if ( property_exists( $this, 'post_args' ) ) {
142 return $this->post_args;
143 }
144
145 $args = array(
146 'timeout' => 5,
147 'blocking' => false,
148 'body' => $this->data,
149 'cookies' => $_COOKIE, // Passing cookies ensures request is performed as initiating user.
150 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // Local requests, fine to pass false.
151 );
152
153 /**
154 * Filters the post arguments used during an async request.
155 *
156 * @param array $args
157 */
158 return apply_filters( $this->identifier . '_post_args', $args );
159 }
160
161 /**
162 * Maybe handle a dispatched request.
163 *
164 * Check for correct nonce and pass to handler.
165 *
166 * @return void|mixed
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 return $this->maybe_wp_die();
177 }
178
179 /**
180 * Should the process exit with wp_die?
181 *
182 * @param mixed $return What to return if filter says don't die, default is null.
183 *
184 * @return void|mixed
185 */
186 protected function maybe_wp_die( $return = null ) {
187 /**
188 * Should wp_die be used?
189 *
190 * @return bool
191 */
192 if ( apply_filters( $this->identifier . '_wp_die', true ) ) {
193 wp_die();
194 }
195
196 return $return;
197 }
198
199 /**
200 * Handle a dispatched request.
201 *
202 * Override this method to perform any actions required
203 * during the async request.
204 */
205 abstract protected function handle();
206 }
207
208