PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.2.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.2.1
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Background / AsyncRequest.php
AsyncRequest.php
198 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace SureCart\Background;
3
4 /**
5 * Abstract WP_Async_Request class.
6 *
7 * @abstract
8 */
9 abstract class AsyncRequest {
10 /**
11 * Prefix
12 *
13 * (default value: 'wp')
14 *
15 * @var string
16 * @access protected
17 */
18 protected $prefix = 'wp';
19
20 /**
21 * Action
22 *
23 * (default value: 'async_request')
24 *
25 * @var string
26 * @access protected
27 */
28 protected $action = 'async_request';
29
30 /**
31 * Identifier
32 *
33 * @var mixed
34 * @access protected
35 */
36 protected $identifier;
37
38 /**
39 * Data
40 *
41 * (default value: array())
42 *
43 * @var array
44 * @access protected
45 */
46 protected $data = array();
47
48 /**
49 * Initiate new async request.
50 */
51 public function __construct() {
52 $this->identifier = $this->prefix . '_' . $this->action;
53
54 add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
55 add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
56 }
57
58 /**
59 * Set data used during the request.
60 *
61 * @param array $data Data.
62 *
63 * @return $this
64 */
65 public function data( $data ) {
66 $this->data = $data;
67
68 return $this;
69 }
70
71 /**
72 * Dispatch the async request.
73 *
74 * @return array|WP_Error|false HTTP Response array, WP_Error on failure, or false if not attempted.
75 */
76 public function dispatch() {
77 $url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
78 $args = $this->get_post_args();
79
80 return wp_remote_post( esc_url_raw( $url ), $args );
81 }
82
83 /**
84 * Get query args.
85 *
86 * @return array
87 */
88 protected function get_query_args() {
89 if ( property_exists( $this, 'query_args' ) ) {
90 return $this->query_args;
91 }
92
93 $args = array(
94 'action' => $this->identifier,
95 'nonce' => \wp_create_nonce( $this->identifier ),
96 );
97
98 /**
99 * Filters the post arguments used during an async request.
100 *
101 * @param array $url
102 */
103 return apply_filters( $this->identifier . '_query_args', $args );
104 }
105
106 /**
107 * Get query URL.
108 *
109 * @return string
110 */
111 protected function get_query_url() {
112 if ( property_exists( $this, 'query_url' ) ) {
113 return $this->query_url;
114 }
115
116 $url = admin_url( 'admin-ajax.php' );
117
118 /**
119 * Filters the post arguments used during an async request.
120 *
121 * @param string $url
122 */
123 return apply_filters( $this->identifier . '_query_url', $url );
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 $args = array(
137 'timeout' => 5,
138 'blocking' => false,
139 'body' => $this->data,
140 'cookies' => $_COOKIE, // Passing cookies ensures request is performed as initiating user.
141 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // Local requests, fine to pass false.
142 );
143
144 /**
145 * Filters the post arguments used during an async request.
146 *
147 * @param array $args
148 */
149 return apply_filters( $this->identifier . '_post_args', $args );
150 }
151
152 /**
153 * Maybe handle a dispatched request.
154 *
155 * Check for correct nonce and pass to handler.
156 *
157 * @return void|mixed
158 */
159 public function maybe_handle() {
160 // Don't lock up other requests while processing.
161 session_write_close();
162
163 check_ajax_referer( $this->identifier, 'nonce' );
164
165 $this->handle();
166
167 return $this->maybe_wp_die();
168 }
169
170 /**
171 * Should the process exit with wp_die?
172 *
173 * @param mixed $return What to return if filter says don't die, default is null.
174 *
175 * @return void|mixed
176 */
177 protected function maybe_wp_die( $return = null ) {
178 /**
179 * Should wp_die be used?
180 *
181 * @return bool
182 */
183 if ( apply_filters( $this->identifier . '_wp_die', true ) ) {
184 wp_die();
185 }
186
187 return $return;
188 }
189
190 /**
191 * Handle a dispatched request.
192 *
193 * Override this method to perform any actions required
194 * during the async request.
195 */
196 abstract protected function handle();
197 }
198