PluginProbe
Solace Extra / trunk
Solace Extra vtrunk
1.7.1 1.7.0 1.6.2 1.6.1 1.6.0 1.5.3 trunk 1.0.8 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.7 1.1.8 1.1.9 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.5.0 1.5.1 All 26 releases
solace-extra / admin / classes / wp-async-request.php

wp-async-request.php in Solace Extra trunk, at admin/classes/wp-async-request.php

210 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5 /**
6 * WP Async Request
7 *
8 * @package WP-Background-Processing
9 */
10
11 // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- WordPress importer uses core/legacy hook names for compatibility.
12
13 /**
14 * Abstract WP_Async_Request class.
15 *
16 * @abstract
17 */
18 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Vendored WP-Background-Processing library class.
19 abstract class XWP_Async_Request {
20
21 /**
22 * Prefix
23 *
24 * (default value: 'wp')
25 *
26 * @var string
27 * @access protected
28 */
29 protected $prefix = 'wp';
30
31 /**
32 * Action
33 *
34 * (default value: 'async_request')
35 *
36 * @var string
37 * @access protected
38 */
39 protected $action = 'async_request';
40
41 /**
42 * Identifier
43 *
44 * @var mixed
45 * @access protected
46 */
47 protected $identifier;
48
49 /**
50 * Data
51 *
52 * (default value: array())
53 *
54 * @var array
55 * @access protected
56 */
57 protected $data = array();
58
59 /**
60 * Initiate new async request.
61 */
62 public function __construct() {
63 $this->identifier = $this->prefix . '_' . $this->action;
64
65 add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) );
66 add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) );
67 }
68
69 /**
70 * Set data used during the request.
71 *
72 * @param array $data Data.
73 *
74 * @return $this
75 */
76 public function data( $data ) {
77 $this->data = $data;
78
79 return $this;
80 }
81
82 /**
83 * Dispatch the async request.
84 *
85 * @return array|WP_Error|false HTTP Response array, WP_Error on failure, or false if not attempted.
86 */
87 public function dispatch() {
88 $url = add_query_arg( $this->get_query_args(), $this->get_query_url() );
89 $args = $this->get_post_args();
90
91 return wp_remote_post( esc_url_raw( $url ), $args );
92 }
93
94 /**
95 * Get query args.
96 *
97 * @return array
98 */
99 protected function get_query_args() {
100 if ( property_exists( $this, 'query_args' ) ) {
101 return $this->query_args;
102 }
103
104 $args = array(
105 'action' => $this->identifier,
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 = array(
148 'timeout' => 5,
149 'blocking' => false,
150 'body' => $this->data,
151 'cookies' => $_COOKIE, // Passing cookies ensures request is performed as initiating user.
152 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WordPress core filter
153 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // Local requests, fine to pass false.
154 );
155
156 /**
157 * Filters the post arguments used during an async request.
158 *
159 * @param array $args
160 */
161 return apply_filters( $this->identifier . '_post_args', $args );
162 }
163
164 /**
165 * Maybe handle a dispatched request.
166 *
167 * Check for correct nonce and pass to handler.
168 *
169 * @return void|mixed
170 */
171 public function maybe_handle() {
172 // Don't lock up other requests while processing.
173 session_write_close();
174
175 check_ajax_referer( $this->identifier, 'nonce' );
176
177 $this->handle();
178
179 return $this->maybe_wp_die();
180 }
181
182 /**
183 * Should the process exit with wp_die?
184 *
185 * @param mixed $return What to return if filter says don't die, default is null.
186 *
187 * @return void|mixed
188 */
189 protected function maybe_wp_die( $return = null ) {
190 /**
191 * Should wp_die be used?
192 *
193 * @return bool
194 */
195 if ( apply_filters( $this->identifier . '_wp_die', true ) ) {
196 wp_die();
197 }
198
199 return $return;
200 }
201
202 /**
203 * Handle a dispatched request.
204 *
205 * Override this method to perform any actions required
206 * during the async request.
207 */
208 abstract protected function handle();
209 }
210