PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Admin / BackgroundProcess / WP_Async_Request.php

WP_Async_Request.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.2.0, at includes/Admin/BackgroundProcess/WP_Async_Request.php

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