PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.2
Elementor Website Builder – more than just a page builder v3.1.2
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 / includes / db.php

db.php in Elementor Website Builder – more than just a page builder 3.1.2, at includes/db.php

562 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Base\Document;
5 use Elementor\Core\DynamicTags\Manager;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 /**
12 * Elementor database.
13 *
14 * Elementor database handler class is responsible for communicating with the
15 * DB, save and retrieve Elementor data and meta data.
16 *
17 * @since 1.0.0
18 */
19 class DB {
20
21 /**
22 * Current DB version of the editor.
23 */
24 const DB_VERSION = '0.4';
25
26 /**
27 * Post publish status.
28 *
29 * @deprecated 3.1.0 Use `Document::STATUS_PUBLISH` instead
30 */
31 const STATUS_PUBLISH = Document::STATUS_PUBLISH;
32
33 /**
34 * Post draft status.
35 *
36 * @deprecated 3.1.0 Use `Document::STATUS_DRAFT` instead
37 */
38 const STATUS_DRAFT = Document::STATUS_DRAFT;
39
40 /**
41 * Post private status.
42 *
43 * @deprecated 3.1.0 Use `Document::STATUS_PRIVATE` instead
44 */
45 const STATUS_PRIVATE = Document::STATUS_PRIVATE;
46
47 /**
48 * Post autosave status.
49 *
50 * @deprecated 3.1.0 Use `Document::STATUS_AUTOSAVE` instead
51 */
52 const STATUS_AUTOSAVE = Document::STATUS_AUTOSAVE;
53
54 /**
55 * Post pending status.
56 *
57 * @deprecated 3.1.0 Use `Document::STATUS_PENDING` instead
58 */
59 const STATUS_PENDING = Document::STATUS_PENDING;
60
61 /**
62 * Switched post data.
63 *
64 * Holds the switched post data.
65 *
66 * @since 1.5.0
67 * @access protected
68 *
69 * @var array Switched post data. Default is an empty array.
70 */
71 protected $switched_post_data = [];
72
73 /**
74 * Switched data.
75 *
76 * Holds the switched data.
77 *
78 * @since 2.0.0
79 * @access protected
80 *
81 * @var array Switched data. Default is an empty array.
82 */
83 protected $switched_data = [];
84
85 /**
86 * Get builder.
87 *
88 * Retrieve editor data from the database.
89 *
90 * @since 1.0.0
91 * @deprecated 3.1.0 Use `Plugin::$instance->documents->get( $post_id )->get_elements_raw_data( null, true )` OR `Plugin::$instance->documents->get_doc_or_auto_save( $post_id )->get_elements_raw_data( null, true )` instead
92 * @access public
93 *
94 * @param int $post_id Post ID.
95 * @param string $status Optional. Post status. Default is `publish`.
96 *
97 * @return array Editor data.
98 */
99 public function get_builder( $post_id, $status = Document::STATUS_PUBLISH ) {
100 Plugin::$instance->modules_manager
101 ->get_modules( 'dev-tools' )
102 ->deprecation
103 ->deprecated_function(
104 __METHOD__,
105 '3.1.0',
106 '`Plugin::$instance->documents->get( $post_id )->get_elements_raw_data( null, true )` OR `Plugin::$instance->documents->get_doc_or_auto_save( $post_id )->get_elements_raw_data( null, true )`'
107 );
108
109 if ( Document::STATUS_DRAFT === $status ) {
110 $document = Plugin::$instance->documents->get_doc_or_auto_save( $post_id );
111 } else {
112 $document = Plugin::$instance->documents->get( $post_id );
113 }
114
115 if ( $document ) {
116 $editor_data = $document->get_elements_raw_data( null, true );
117 } else {
118 $editor_data = [];
119 }
120
121 return $editor_data;
122 }
123
124 /**
125 * Get JSON meta.
126 *
127 * Retrieve post meta data, and return the JSON decoded data.
128 *
129 * @since 1.0.0
130 * @access protected
131 *
132 * @param int $post_id Post ID.
133 * @param string $key The meta key to retrieve.
134 *
135 * @return array Decoded JSON data from post meta.
136 */
137 protected function _get_json_meta( $post_id, $key ) {
138 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0' );
139
140 $meta = get_post_meta( $post_id, $key, true );
141
142 if ( is_string( $meta ) && ! empty( $meta ) ) {
143 $meta = json_decode( $meta, true );
144 }
145
146 if ( empty( $meta ) ) {
147 $meta = [];
148 }
149
150 return $meta;
151 }
152
153 /**
154 * Is using Elementor.
155 *
156 * Set whether the page is using Elementor or not.
157 *
158 * @since 1.5.0
159 * @deprecated 3.1.0 Use `Plugin::$instance->documents->get( $post_id )->set_is_build_with_elementor( $is_elementor )` instead
160 * @access public
161 *
162 * @param int $post_id Post ID.
163 * @param bool $is_elementor Optional. Whether the page is elementor page.
164 * Default is true.
165 */
166 public function set_is_elementor_page( $post_id, $is_elementor = true ) {
167 Plugin::$instance->modules_manager
168 ->get_modules( 'dev-tools' )
169 ->deprecation
170 ->deprecated_function(
171 __METHOD__,
172 '3.1.0',
173 'Plugin::$instance->documents->get( $post_id )->set_is_build_with_elementor( $is_elementor )'
174 );
175
176 $document = Plugin::$instance->documents->get( $post_id );
177
178 if ( ! $document ) {
179 return;
180 }
181
182 $document->set_is_built_with_elementor( $is_elementor );
183 }
184
185 /**
186 * Render element plain content.
187 *
188 * When saving data in the editor, this method renders recursively the plain
189 * content containing only the content and the HTML. No CSS data.
190 *
191 * @since 2.0.0
192 * @access private
193 *
194 * @param array $element_data Element data.
195 */
196 private function render_element_plain_content( $element_data ) {
197 if ( 'widget' === $element_data['elType'] ) {
198 /** @var Widget_Base $widget */
199 $widget = Plugin::$instance->elements_manager->create_element_instance( $element_data );
200
201 if ( $widget ) {
202 $widget->render_plain_content();
203 }
204 }
205
206 if ( ! empty( $element_data['elements'] ) ) {
207 foreach ( $element_data['elements'] as $element ) {
208 $this->render_element_plain_content( $element );
209 }
210 }
211 }
212
213 /**
214 * Save plain text.
215 *
216 * Retrieves the raw content, removes all kind of unwanted HTML tags and saves
217 * the content as the `post_content` field in the database.
218 *
219 * @since 1.9.0
220 * @access public
221 *
222 * @param int $post_id Post ID.
223 */
224 public function save_plain_text( $post_id ) {
225 // Switch $dynamic_tags to parsing mode = remove.
226 $dynamic_tags = Plugin::$instance->dynamic_tags;
227 $parsing_mode = $dynamic_tags->get_parsing_mode();
228 $dynamic_tags->set_parsing_mode( Manager::MODE_REMOVE );
229
230 $plain_text = $this->get_plain_text( $post_id );
231
232 wp_update_post(
233 [
234 'ID' => $post_id,
235 'post_content' => $plain_text,
236 ]
237 );
238
239 // Restore parsing mode.
240 $dynamic_tags->set_parsing_mode( $parsing_mode );
241 }
242
243 /**
244 * Iterate data.
245 *
246 * Accept any type of Elementor data and a callback function. The callback
247 * function runs recursively for each element and his child elements.
248 *
249 * @since 1.0.0
250 * @access public
251 *
252 * @param array $data_container Any type of elementor data.
253 * @param callable $callback A function to iterate data by.
254 * @param array $args Array of args pointers for passing parameters in & out of the callback
255 *
256 * @return mixed Iterated data.
257 */
258 public function iterate_data( $data_container, $callback, $args = [] ) {
259 if ( isset( $data_container['elType'] ) ) {
260 if ( ! empty( $data_container['elements'] ) ) {
261 $data_container['elements'] = $this->iterate_data( $data_container['elements'], $callback, $args );
262 }
263
264 return call_user_func( $callback, $data_container, $args );
265 }
266
267 foreach ( $data_container as $element_key => $element_value ) {
268 $element_data = $this->iterate_data( $data_container[ $element_key ], $callback, $args );
269
270 if ( null === $element_data ) {
271 continue;
272 }
273
274 $data_container[ $element_key ] = $element_data;
275 }
276
277 return $data_container;
278 }
279
280 /**
281 * Safely copy Elementor meta.
282 *
283 * Make sure the original page was built with Elementor and the post is not
284 * auto-save. Only then copy elementor meta from one post to another using
285 * `copy_elementor_meta()`.
286 *
287 * @since 1.9.2
288 * @access public
289 *
290 * @param int $from_post_id Original post ID.
291 * @param int $to_post_id Target post ID.
292 */
293 public function safe_copy_elementor_meta( $from_post_id, $to_post_id ) {
294 // It's from WP-Admin & not from Elementor.
295 if ( ! did_action( 'elementor/db/before_save' ) ) {
296
297 if ( ! Plugin::$instance->db->is_built_with_elementor( $from_post_id ) ) {
298 return;
299 }
300
301 // It's an exited Elementor auto-save
302 if ( get_post_meta( $to_post_id, '_elementor_data', true ) ) {
303 return;
304 }
305 }
306
307 $this->copy_elementor_meta( $from_post_id, $to_post_id );
308 }
309
310 /**
311 * Copy Elementor meta.
312 *
313 * Duplicate the data from one post to another.
314 *
315 * Consider using `safe_copy_elementor_meta()` method instead.
316 *
317 * @since 1.1.0
318 * @access public
319 *
320 * @param int $from_post_id Original post ID.
321 * @param int $to_post_id Target post ID.
322 */
323 public function copy_elementor_meta( $from_post_id, $to_post_id ) {
324 $from_post_meta = get_post_meta( $from_post_id );
325 $core_meta = [
326 '_wp_page_template',
327 '_thumbnail_id',
328 ];
329
330 foreach ( $from_post_meta as $meta_key => $values ) {
331 // Copy only meta with the `_elementor` prefix
332 if ( 0 === strpos( $meta_key, '_elementor' ) || in_array( $meta_key, $core_meta, true ) ) {
333 $value = $values[0];
334
335 // The elementor JSON needs slashes before saving
336 if ( '_elementor_data' === $meta_key ) {
337 $value = wp_slash( $value );
338 } else {
339 $value = maybe_unserialize( $value );
340 }
341
342 // Don't use `update_post_meta` that can't handle `revision` post type
343 update_metadata( 'post', $to_post_id, $meta_key, $value );
344 }
345 }
346 }
347
348 /**
349 * Is built with Elementor.
350 *
351 * Check whether the post was built with Elementor.
352 *
353 * @since 1.0.10
354 * @access public
355 *
356 * @param int $post_id Post ID.
357 *
358 * @return bool Whether the post was built with Elementor.
359 */
360 public function is_built_with_elementor( $post_id ) {
361 return ! ! get_post_meta( $post_id, '_elementor_edit_mode', true );
362 }
363
364 /**
365 * Switch to post.
366 *
367 * Change the global WordPress post to the requested post.
368 *
369 * @since 1.5.0
370 * @access public
371 *
372 * @param int $post_id Post ID to switch to.
373 */
374 public function switch_to_post( $post_id ) {
375 $post_id = absint( $post_id );
376 // If is already switched, or is the same post, return.
377 if ( get_the_ID() === $post_id ) {
378 $this->switched_post_data[] = false;
379 return;
380 }
381
382 $this->switched_post_data[] = [
383 'switched_id' => $post_id,
384 'original_id' => get_the_ID(), // Note, it can be false if the global isn't set
385 ];
386
387 $GLOBALS['post'] = get_post( $post_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
388
389 setup_postdata( $GLOBALS['post'] );
390 }
391
392 /**
393 * Restore current post.
394 *
395 * Rollback to the previous global post, rolling back from `DB::switch_to_post()`.
396 *
397 * @since 1.5.0
398 * @access public
399 */
400 public function restore_current_post() {
401 $data = array_pop( $this->switched_post_data );
402
403 // If not switched, return.
404 if ( ! $data ) {
405 return;
406 }
407
408 // It was switched from an empty global post, restore this state and unset the global post
409 if ( false === $data['original_id'] ) {
410 unset( $GLOBALS['post'] );
411 return;
412 }
413
414 $GLOBALS['post'] = get_post( $data['original_id'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
415
416 setup_postdata( $GLOBALS['post'] );
417 }
418
419
420 /**
421 * Switch to query.
422 *
423 * Change the WordPress query to a new query with the requested
424 * query variables.
425 *
426 * @since 2.0.0
427 * @access public
428 *
429 * @param array $query_vars New query variables.
430 * @param bool $force_global_post
431 */
432 public function switch_to_query( $query_vars, $force_global_post = false ) {
433 global $wp_query;
434 $current_query_vars = $wp_query->query;
435
436 // If is already switched, or is the same query, return.
437 if ( $current_query_vars === $query_vars ) {
438 $this->switched_data[] = false;
439 return;
440 }
441
442 $new_query = new \WP_Query( $query_vars );
443
444 $switched_data = [
445 'switched' => $new_query,
446 'original' => $wp_query,
447 ];
448
449 if ( ! empty( $GLOBALS['post'] ) ) {
450 $switched_data['post'] = $GLOBALS['post'];
451 }
452
453 $this->switched_data[] = $switched_data;
454
455 $wp_query = $new_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
456
457 // Ensure the global post is set only if needed
458 unset( $GLOBALS['post'] );
459
460 if ( isset( $new_query->posts[0] ) ) {
461 if ( $force_global_post || $new_query->is_singular() ) {
462 $GLOBALS['post'] = $new_query->posts[0]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
463 setup_postdata( $GLOBALS['post'] );
464 }
465 }
466
467 if ( $new_query->is_author() ) {
468 $GLOBALS['authordata'] = get_userdata( $new_query->get( 'author' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
469 }
470 }
471
472 /**
473 * Restore current query.
474 *
475 * Rollback to the previous query, rolling back from `DB::switch_to_query()`.
476 *
477 * @since 2.0.0
478 * @access public
479 */
480 public function restore_current_query() {
481 $data = array_pop( $this->switched_data );
482
483 // If not switched, return.
484 if ( ! $data ) {
485 return;
486 }
487
488 global $wp_query;
489
490 $wp_query = $data['original']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
491
492 // Ensure the global post/authordata is set only if needed.
493 unset( $GLOBALS['post'] );
494 unset( $GLOBALS['authordata'] );
495
496 if ( ! empty( $data['post'] ) ) {
497 $GLOBALS['post'] = $data['post']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
498 setup_postdata( $GLOBALS['post'] );
499 }
500
501 if ( $wp_query->is_author() ) {
502 $GLOBALS['authordata'] = get_userdata( $wp_query->get( 'author' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
503 }
504 }
505
506 /**
507 * Get plain text.
508 *
509 * Retrieve the post plain text.
510 *
511 * @since 1.9.0
512 * @access public
513 *
514 * @param int $post_id Post ID.
515 *
516 * @return string Post plain text.
517 */
518 public function get_plain_text( $post_id ) {
519 $document = Plugin::$instance->documents->get( $post_id );
520 $data = $document ? $document->get_elements_data() : [];
521
522 return $this->get_plain_text_from_data( $data );
523 }
524
525 /**
526 * Get plain text from data.
527 *
528 * Retrieve the post plain text from any given Elementor data.
529 *
530 * @since 1.9.2
531 * @access public
532 *
533 * @param array $data Post ID.
534 *
535 * @return string Post plain text.
536 */
537 public function get_plain_text_from_data( $data ) {
538 ob_start();
539 if ( $data ) {
540 foreach ( $data as $element_data ) {
541 $this->render_element_plain_content( $element_data );
542 }
543 }
544
545 $plain_text = ob_get_clean();
546
547 // Remove unnecessary tags.
548 $plain_text = preg_replace( '/<\/?div[^>]*\>/i', '', $plain_text );
549 $plain_text = preg_replace( '/<\/?span[^>]*\>/i', '', $plain_text );
550 $plain_text = preg_replace( '#<script(.*?)>(.*?)</script>#is', '', $plain_text );
551 $plain_text = preg_replace( '/<i [^>]*><\\/i[^>]*>/', '', $plain_text );
552 $plain_text = preg_replace( '/ class=".*?"/', '', $plain_text );
553
554 // Remove empty lines.
555 $plain_text = preg_replace( '/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', "\n", $plain_text );
556
557 $plain_text = trim( $plain_text );
558
559 return $plain_text;
560 }
561 }
562