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