PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0
Elementor Website Builder – more than just a page builder v4.2.0
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / common / modules / ajax / module.php

module.php in Elementor Website Builder – more than just a page builder 4.2.0, at core/common/modules/ajax/module.php

317 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core\Common\Modules\Ajax;
3
4 use Elementor\Core\Base\Module as BaseModule;
5 use Elementor\Core\Utils\Exceptions;
6 use Elementor\Plugin;
7 use Elementor\Utils;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 /**
14 * Elementor ajax manager.
15 *
16 * Elementor ajax manager handler class is responsible for handling Elementor
17 * ajax requests, ajax responses and registering actions applied on them.
18 *
19 * @since 2.0.0
20 */
21 class Module extends BaseModule {
22
23 const NONCE_KEY = 'elementor_ajax';
24
25 /**
26 * Ajax actions.
27 *
28 * Holds all the register ajax action.
29 *
30 * @since 2.0.0
31 * @access private
32 *
33 * @var array
34 */
35 private $ajax_actions = [];
36
37 /**
38 * Ajax requests.
39 *
40 * Holds all the register ajax requests.
41 *
42 * @since 2.0.0
43 * @access private
44 *
45 * @var array
46 */
47 private $requests = [];
48
49 /**
50 * Ajax response data.
51 *
52 * Holds all the response data for all the ajax requests.
53 *
54 * @since 2.0.0
55 * @access private
56 *
57 * @var array
58 */
59 private $response_data = [];
60
61 /**
62 * Current ajax action ID.
63 *
64 * Holds all the ID for the current ajax action.
65 *
66 * @since 2.0.0
67 * @access private
68 *
69 * @var string|null
70 */
71 private $current_action_id = null;
72
73 /**
74 * Ajax manager constructor.
75 *
76 * Initializing Elementor ajax manager.
77 *
78 * @since 2.0.0
79 * @access public
80 */
81 public function __construct() {
82 add_action( 'wp_ajax_elementor_ajax', [ $this, 'handle_ajax_request' ] );
83 }
84
85 /**
86 * Get module name.
87 *
88 * Retrieve the module name.
89 *
90 * @since 1.7.0
91 * @access public
92 *
93 * @return string Module name.
94 */
95 public function get_name() {
96 return 'ajax';
97 }
98
99 /**
100 * Register ajax action.
101 *
102 * Add new actions for a specific ajax request and the callback function to
103 * be handle the response.
104 *
105 * @since 2.0.0
106 * @access public
107 *
108 * @param string $tag Ajax request name/tag.
109 * @param callable $callback The callback function.
110 */
111 public function register_ajax_action( $tag, $callback ) {
112 if ( ! did_action( 'elementor/ajax/register_actions' ) ) {
113 _doing_it_wrong( __METHOD__, esc_html( sprintf( 'Use `%s` hook to register ajax action.', 'elementor/ajax/register_actions' ) ), '2.0.0' );
114 }
115
116 $this->ajax_actions[ $tag ] = compact( 'tag', 'callback' );
117 }
118
119 /**
120 * Handle ajax request.
121 *
122 * Verify ajax nonce, and run all the registered actions for this request.
123 *
124 * Fired by `wp_ajax_elementor_ajax` action.
125 *
126 * @since 2.0.0
127 * @access public
128 */
129 public function handle_ajax_request() {
130 if ( ! $this->verify_request_nonce() ) {
131 $this->add_response_data( false, esc_html__( 'Token Expired.', 'elementor' ) )
132 ->send_error( Exceptions::UNAUTHORIZED );
133 }
134
135 $editor_post_id = 0;
136
137 if ( ! empty( $_REQUEST['editor_post_id'] ) ) {
138 $editor_post_id = absint( $_REQUEST['editor_post_id'] );
139
140 Plugin::$instance->db->switch_to_post( $editor_post_id );
141 }
142
143 /**
144 * Register ajax actions.
145 *
146 * Fires when an ajax request is received and verified.
147 *
148 * Used to register new ajax action handles.
149 *
150 * @since 2.0.0
151 *
152 * @param self $this An instance of ajax manager.
153 */
154 do_action( 'elementor/ajax/register_actions', $this );
155
156 if ( ! empty( $_REQUEST['actions'] ) ) {
157 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, each action should sanitize its own data.
158 $this->requests = json_decode( wp_unslash( $_REQUEST['actions'] ), true );
159 }
160
161 foreach ( $this->requests as $id => $action_data ) {
162 $this->current_action_id = $id;
163
164 if ( ! isset( $this->ajax_actions[ $action_data['action'] ] ) ) {
165 $this->add_response_data( false, esc_html__( 'Action not found.', 'elementor' ), Exceptions::BAD_REQUEST );
166
167 continue;
168 }
169
170 if ( $editor_post_id ) {
171 $action_data['data']['editor_post_id'] = $editor_post_id;
172 }
173
174 try {
175 $data = $action_data['data'] ?? [];
176 $results = call_user_func( $this->ajax_actions[ $action_data['action'] ]['callback'], $data, $this );
177
178 if ( false === $results ) {
179 $this->add_response_data( false );
180 } else {
181 $this->add_response_data( true, $results );
182 }
183 } catch ( \Exception $e ) {
184 $this->add_response_data( false, $e->getMessage(), $e->getCode() );
185 }
186 }
187
188 $this->current_action_id = null;
189
190 $this->send_success();
191 }
192
193 /**
194 * Get current action data.
195 *
196 * Retrieve the data for the current ajax request.
197 *
198 * @since 2.0.1
199 * @access public
200 *
201 * @return bool|mixed Ajax request data if action exist, False otherwise.
202 */
203 public function get_current_action_data() {
204 if ( ! $this->current_action_id ) {
205 return false;
206 }
207
208 return $this->requests[ $this->current_action_id ];
209 }
210
211 /**
212 * Create nonce.
213 *
214 * Creates a cryptographic token to
215 * give the user an access to Elementor ajax actions.
216 *
217 * @since 2.3.0
218 * @access public
219 *
220 * @return string The nonce token.
221 */
222 public function create_nonce() {
223 return wp_create_nonce( self::NONCE_KEY );
224 }
225
226 /**
227 * Verify request nonce.
228 *
229 * Whether the request nonce verified or not.
230 *
231 * @since 2.3.0
232 * @access public
233 *
234 * @return bool True if request nonce verified, False otherwise.
235 */
236 public function verify_request_nonce() {
237 return wp_verify_nonce( Utils::get_super_global_value( $_REQUEST, '_nonce' ), self::NONCE_KEY );
238 }
239
240 protected function get_init_settings() {
241 return [
242 'url' => admin_url( 'admin-ajax.php' ),
243 'nonce' => $this->create_nonce(),
244 ];
245 }
246
247 /**
248 * Ajax success response.
249 *
250 * Send a JSON response data back to the ajax request, indicating success.
251 *
252 * @since 2.0.0
253 * @access protected
254 */
255 private function send_success() {
256 $response = [
257 'success' => true,
258 'data' => [
259 'responses' => $this->response_data,
260 ],
261 ];
262
263 $json = wp_json_encode( $response );
264
265 while ( ob_get_status() ) {
266 ob_end_clean();
267 }
268
269 header( 'Content-Type: application/json; charset=UTF-8' );
270 echo $json; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
271
272 wp_die( '', '', [ 'response' => null ] );
273 }
274
275 /**
276 * Ajax failure response.
277 *
278 * Send a JSON response data back to the ajax request, indicating failure.
279 *
280 * @since 2.0.0
281 * @access protected
282 *
283 * @param null $code
284 */
285 private function send_error( $code = null ) {
286 wp_send_json_error( [
287 'responses' => $this->response_data,
288 ], $code );
289 }
290
291 /**
292 * Add response data.
293 *
294 * Add new response data to the array of all the ajax requests.
295 *
296 * @since 2.0.0
297 * @access protected
298 *
299 * @param bool $success True if the requests returned successfully, False
300 * otherwise.
301 * @param mixed $data Optional. Response data. Default is null.
302 *
303 * @param int $code Optional. Response code. Default is 200.
304 *
305 * @return Module An instance of ajax manager.
306 */
307 private function add_response_data( $success, $data = null, $code = 200 ) {
308 $this->response_data[ $this->current_action_id ] = [
309 'success' => $success,
310 'code' => $code,
311 'data' => $data,
312 ];
313
314 return $this;
315 }
316 }
317