PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / trunk
Secure Custom Fields vtrunk
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / ajax / class-acf-ajax.php
secure-custom-fields / includes / ajax Last commit date
class-acf-ajax-check-screen.php 1 month ago class-acf-ajax-local-json-diff.php 1 year ago class-acf-ajax-query-users.php 3 months ago class-acf-ajax-query.php 4 months ago class-acf-ajax-upgrade.php 1 month ago class-acf-ajax-user-setting.php 1 year ago class-acf-ajax.php 1 year ago index.php 1 year ago
class-acf-ajax.php
227 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'ACF_Ajax' ) ) :
8
9 class ACF_Ajax {
10
11 /** @var string The AJAX action name. */
12 var $action = '';
13
14 /** @var array The $_REQUEST data. */
15 var $request;
16
17 /** @var boolean Prevents access for non-logged in users. */
18 var $public = false;
19
20 /**
21 * __construct
22 *
23 * Sets up the class functionality.
24 *
25 * @date 31/7/18
26 * @since ACF 5.7.2
27 *
28 * @return void
29 */
30 function __construct() {
31 $this->initialize();
32 $this->add_actions();
33 }
34
35 /**
36 * has
37 *
38 * Returns true if the request has data for the given key.
39 *
40 * @date 31/7/18
41 * @since ACF 5.7.2
42 *
43 * @param string $key The data key.
44 * @return boolean
45 */
46 function has( $key = '' ) {
47 return isset( $this->request[ $key ] );
48 }
49
50 /**
51 * get
52 *
53 * Returns request data for the given key.
54 *
55 * @date 31/7/18
56 * @since ACF 5.7.2
57 *
58 * @param string $key The data key.
59 * @return mixed
60 */
61 function get( $key = '' ) {
62 return isset( $this->request[ $key ] ) ? $this->request[ $key ] : null;
63 }
64
65 /**
66 * Sets request data for the given key.
67 *
68 * @date 31/7/18
69 * @since ACF 5.7.2
70 *
71 * @param string $key The data key.
72 * @param mixed $value The data value.
73 * @return ACF_Ajax
74 */
75 function set( $key = '', $value = null ) {
76 $this->request[ $key ] = $value;
77 return $this;
78 }
79
80 /**
81 * initialize
82 *
83 * Allows easy access to modifying properties without changing constructor.
84 *
85 * @date 31/7/18
86 * @since ACF 5.7.2
87 *
88 * @return void
89 */
90 function initialize() {
91 /* do nothing */
92 }
93
94 /**
95 * add_actions
96 *
97 * Adds the ajax actions for this response.
98 *
99 * @date 31/7/18
100 * @since ACF 5.7.2
101 *
102 * @return void
103 */
104 function add_actions() {
105
106 // add action for logged-in users
107 add_action( "wp_ajax_{$this->action}", array( $this, 'request' ) );
108
109 // add action for non logged-in users
110 if ( $this->public ) {
111 add_action( "wp_ajax_nopriv_{$this->action}", array( $this, 'request' ) );
112 }
113 }
114
115 /**
116 * request
117 *
118 * Callback for ajax action. Sets up properties and calls the get_response() function.
119 *
120 * @date 1/8/18
121 * @since ACF 5.7.2
122 *
123 * @return void
124 */
125 function request() {
126
127 // Store data for has() and get() functions.
128 $this->request = wp_unslash( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified below in verify_request().
129
130 // Verify request and handle error.
131 $error = $this->verify_request( $this->request );
132 if ( is_wp_error( $error ) ) {
133 $this->send( $error );
134 }
135
136 // Send response.
137 $this->send( $this->get_response( $this->request ) );
138 }
139
140 /**
141 * Verifies the request.
142 *
143 * @date 9/3/20
144 * @since ACF 5.8.8
145 *
146 * @param array $request The request args.
147 * @return (bool|WP_Error) True on success, WP_Error on fail.
148 */
149 function verify_request( $request ) {
150
151 // Verify nonce.
152 if ( ! acf_verify_ajax() ) {
153 return new WP_Error( 'acf_invalid_nonce', __( 'Invalid nonce.', 'secure-custom-fields' ), array( 'status' => 404 ) );
154 }
155 return true;
156 }
157
158 /**
159 * get_response
160 *
161 * Returns the response data to sent back.
162 *
163 * @date 31/7/18
164 * @since ACF 5.7.2
165 *
166 * @param array $request The request args.
167 * @return mixed The response data or WP_Error.
168 */
169 function get_response( $request ) {
170 return true;
171 }
172
173 /**
174 * send
175 *
176 * Sends back JSON based on the $response as either success or failure.
177 *
178 * @date 31/7/18
179 * @since ACF 5.7.2
180 *
181 * @param mixed $response The response to send back.
182 * @return void
183 */
184 function send( $response ) {
185
186 // Return error.
187 if ( is_wp_error( $response ) ) {
188 $this->send_error( $response );
189
190 // Return success.
191 } else {
192 wp_send_json( $response );
193 }
194 }
195
196 /**
197 * Sends a JSON response for the given WP_Error object.
198 *
199 * @date 8/3/20
200 * @since ACF 5.8.8
201 *
202 * @param WP_Error error The error object.
203 * @return void
204 */
205 function send_error( $error ) {
206
207 // Get error status
208 $error_data = $error->get_error_data();
209 if ( is_array( $error_data ) && isset( $error_data['status'] ) ) {
210 $status_code = $error_data['status'];
211 } else {
212 $status_code = 500;
213 }
214
215 wp_send_json(
216 array(
217 'code' => $error->get_error_code(),
218 'message' => $error->get_error_message(),
219 'data' => $error->get_error_data(),
220 ),
221 $status_code
222 );
223 }
224 }
225
226 endif; // class_exists check
227