PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.5.6
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.5.6
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.5.6, at includes/Admin/BackgroundProcess/WP_Async_Request.php

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