PluginProbe
Elementor Website Builder – more than just a page builder / 3.1.0-dev2
Elementor Website Builder – more than just a page builder v3.1.0-dev2
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.0-dev2, at includes/db.php

581 lines 14.6 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 $meta = get_post_meta( $post_id, $key, true );
139
140 if ( is_string( $meta ) && ! empty( $meta ) ) {
141 $meta = json_decode( $meta, true );
142 }
143
144 if ( empty( $meta ) ) {
145 $meta = [];
146 }
147
148 return $meta;
149 }
150
151 /**
152 * Is using Elementor.
153 *
154 * Set whether the page is using Elementor or not.
155 *
156 * @since 1.5.0
157 * @deprecated 3.1.0 Use `Plugin::$instance->documents->get( $post_id )->set_is_build_with_elementor( $is_elementor )` instead
158 * @access public
159 *
160 * @param int $post_id Post ID.
161 * @param bool $is_elementor Optional. Whether the page is elementor page.
162 * Default is true.
163 */
164 public function set_is_elementor_page( $post_id, $is_elementor = true ) {
165 Plugin::$instance->modules_manager
166 ->get_modules( 'dev-tools' )
167 ->deprecation
168 ->deprecated_function(
169 __METHOD__,
170 '3.1.0',
171 'Plugin::$instance->documents->get( $post_id )->set_is_build_with_elementor( $is_elementor )'
172 );
173
174 $document = Plugin::$instance->documents->get( $post_id );
175
176 if ( ! $document ) {
177 return;
178 }
179
180 $document->set_is_built_with_elementor( $is_elementor );
181 }
182
183 /**
184 * Render element plain content.
185 *
186 * When saving data in the editor, this method renders recursively the plain
187 * content containing only the content and the HTML. No CSS data.
188 *
189 * @since 2.0.0
190 * @access private
191 *
192 * @param array $element_data Element data.
193 */
194 private function render_element_plain_content( $element_data ) {
195 if ( 'widget' === $element_data['elType'] ) {
196 /** @var Widget_Base $widget */
197 $widget = Plugin::$instance->elements_manager->create_element_instance( $element_data );
198
199 if ( $widget ) {
200 $widget->render_plain_content();
201 }
202 }
203
204 if ( ! empty( $element_data['elements'] ) ) {
205 foreach ( $element_data['elements'] as $element ) {
206 $this->render_element_plain_content( $element );
207 }
208 }
209 }
210
211 /**
212 * Save plain text.
213 *
214 * Retrieves the raw content, removes all kind of unwanted HTML tags and saves
215 * the content as the `post_content` field in the database.
216 *
217 * @since 1.9.0
218 * @access public
219 *
220 * @param int $post_id Post ID.
221 */
222 public function save_plain_text( $post_id ) {
223 // Switch $dynamic_tags to parsing mode = remove.
224 $dynamic_tags = Plugin::$instance->dynamic_tags;
225 $parsing_mode = $dynamic_tags->get_parsing_mode();
226 $dynamic_tags->set_parsing_mode( Manager::MODE_REMOVE );
227
228 $plain_text = $this->get_plain_text( $post_id );
229
230 wp_update_post(
231 [
232 'ID' => $post_id,
233 'post_content' => $plain_text,
234 ]
235 );
236
237 // Restore parsing mode.
238 $dynamic_tags->set_parsing_mode( $parsing_mode );
239 }
240
241 /**
242 * Iterate data.
243 *
244 * Accept any type of Elementor data and a callback function. The callback
245 * function runs recursively for each element and his child elements.
246 *
247 * @since 1.0.0
248 * @access public
249 *
250 * @param array $data_container Any type of elementor data.
251 * @param callable $callback A function to iterate data by.
252 * @param array $args Array of args pointers for passing parameters in & out of the callback
253 *
254 * @return mixed Iterated data.
255 */
256 public function iterate_data( $data_container, $callback, $args = [] ) {
257 if ( isset( $data_container['elType'] ) ) {
258 if ( ! empty( $data_container['elements'] ) ) {
259 $data_container['elements'] = $this->iterate_data( $data_container['elements'], $callback, $args );
260 }
261
262 return call_user_func( $callback, $data_container, $args );
263 }
264
265 foreach ( $data_container as $element_key => $element_value ) {
266 $element_data = $this->iterate_data( $data_container[ $element_key ], $callback, $args );
267
268 if ( null === $element_data ) {
269 continue;
270 }
271
272 $data_container[ $element_key ] = $element_data;
273 }
274
275 return $data_container;
276 }
277
278 /**
279 * Safely copy Elementor meta.
280 *
281 * Make sure the original page was built with Elementor and the post is not
282 * auto-save. Only then copy elementor meta from one post to another using
283 * `copy_elementor_meta()`.
284 *
285 * @since 1.9.2
286 * @access public
287 *
288 * @param int $from_post_id Original post ID.
289 * @param int $to_post_id Target post ID.
290 */
291 public function safe_copy_elementor_meta( $from_post_id, $to_post_id ) {
292 // It's from WP-Admin & not from Elementor.
293 if ( ! did_action( 'elementor/db/before_save' ) ) {
294
295 if ( ! Plugin::$instance->db->is_built_with_elementor( $from_post_id ) ) {
296 return;
297 }
298
299 // It's an exited Elementor auto-save
300 if ( get_post_meta( $to_post_id, '_elementor_data', true ) ) {
301 return;
302 }
303 }
304
305 $this->copy_elementor_meta( $from_post_id, $to_post_id );
306 }
307
308 /**
309 * Copy Elementor meta.
310 *
311 * Duplicate the data from one post to another.
312 *
313 * Consider using `safe_copy_elementor_meta()` method instead.
314 *
315 * @since 1.1.0
316 * @access public
317 *
318 * @param int $from_post_id Original post ID.
319 * @param int $to_post_id Target post ID.
320 */
321 public function copy_elementor_meta( $from_post_id, $to_post_id ) {
322 $from_post_meta = get_post_meta( $from_post_id );
323 $core_meta = [
324 '_wp_page_template',
325 '_thumbnail_id',
326 ];
327
328 foreach ( $from_post_meta as $meta_key => $values ) {
329 // Copy only meta with the `_elementor` prefix
330 if ( 0 === strpos( $meta_key, '_elementor' ) || in_array( $meta_key, $core_meta, true ) ) {
331 $value = $values[0];
332
333 // The elementor JSON needs slashes before saving
334 if ( '_elementor_data' === $meta_key ) {
335 $value = wp_slash( $value );
336 } else {
337 $value = maybe_unserialize( $value );
338 }
339
340 // Don't use `update_post_meta` that can't handle `revision` post type
341 update_metadata( 'post', $to_post_id, $meta_key, $value );
342 }
343 }
344 }
345
346 /**
347 * Is built with Elementor.
348 *
349 * Check whether the post was built with Elementor.
350 *
351 * @since 1.0.10
352 * @access public
353 *
354 * @param int $post_id Post ID.
355 *
356 * @return bool Whether the post was built with Elementor.
357 */
358 public function is_built_with_elementor( $post_id ) {
359 return ! ! get_post_meta( $post_id, '_elementor_edit_mode', true );
360 }
361
362 /**
363 * Is Elementor Landing Page.
364 *
365 * Check whether the post is an Elementor Landing Page.
366 *
367 * @since 3.1.0
368 * @access public
369 *
370 * @param \WP_Post $post Post Object
371 *
372 * @return bool Whether the post was built with Elementor.
373 */
374 public function is_elementor_landing_page( $post ) {
375 // If the post is not a page, save a call to the DB.
376 if ( 'page' !== $post->post_type ) {
377 return false;
378 }
379
380 return 'landing-page' === get_post_meta( $post->ID, '_elementor_template_type', true );
381 }
382
383 /**
384 * Switch to post.
385 *
386 * Change the global WordPress post to the requested post.
387 *
388 * @since 1.5.0
389 * @access public
390 *
391 * @param int $post_id Post ID to switch to.
392 */
393 public function switch_to_post( $post_id ) {
394 $post_id = absint( $post_id );
395 // If is already switched, or is the same post, return.
396 if ( get_the_ID() === $post_id ) {
397 $this->switched_post_data[] = false;
398 return;
399 }
400
401 $this->switched_post_data[] = [
402 'switched_id' => $post_id,
403 'original_id' => get_the_ID(), // Note, it can be false if the global isn't set
404 ];
405
406 $GLOBALS['post'] = get_post( $post_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
407
408 setup_postdata( $GLOBALS['post'] );
409 }
410
411 /**
412 * Restore current post.
413 *
414 * Rollback to the previous global post, rolling back from `DB::switch_to_post()`.
415 *
416 * @since 1.5.0
417 * @access public
418 */
419 public function restore_current_post() {
420 $data = array_pop( $this->switched_post_data );
421
422 // If not switched, return.
423 if ( ! $data ) {
424 return;
425 }
426
427 // It was switched from an empty global post, restore this state and unset the global post
428 if ( false === $data['original_id'] ) {
429 unset( $GLOBALS['post'] );
430 return;
431 }
432
433 $GLOBALS['post'] = get_post( $data['original_id'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
434
435 setup_postdata( $GLOBALS['post'] );
436 }
437
438
439 /**
440 * Switch to query.
441 *
442 * Change the WordPress query to a new query with the requested
443 * query variables.
444 *
445 * @since 2.0.0
446 * @access public
447 *
448 * @param array $query_vars New query variables.
449 * @param bool $force_global_post
450 */
451 public function switch_to_query( $query_vars, $force_global_post = false ) {
452 global $wp_query;
453 $current_query_vars = $wp_query->query;
454
455 // If is already switched, or is the same query, return.
456 if ( $current_query_vars === $query_vars ) {
457 $this->switched_data[] = false;
458 return;
459 }
460
461 $new_query = new \WP_Query( $query_vars );
462
463 $switched_data = [
464 'switched' => $new_query,
465 'original' => $wp_query,
466 ];
467
468 if ( ! empty( $GLOBALS['post'] ) ) {
469 $switched_data['post'] = $GLOBALS['post'];
470 }
471
472 $this->switched_data[] = $switched_data;
473
474 $wp_query = $new_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
475
476 // Ensure the global post is set only if needed
477 unset( $GLOBALS['post'] );
478
479 if ( isset( $new_query->posts[0] ) ) {
480 if ( $force_global_post || $new_query->is_singular() ) {
481 $GLOBALS['post'] = $new_query->posts[0]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
482 setup_postdata( $GLOBALS['post'] );
483 }
484 }
485
486 if ( $new_query->is_author() ) {
487 $GLOBALS['authordata'] = get_userdata( $new_query->get( 'author' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
488 }
489 }
490
491 /**
492 * Restore current query.
493 *
494 * Rollback to the previous query, rolling back from `DB::switch_to_query()`.
495 *
496 * @since 2.0.0
497 * @access public
498 */
499 public function restore_current_query() {
500 $data = array_pop( $this->switched_data );
501
502 // If not switched, return.
503 if ( ! $data ) {
504 return;
505 }
506
507 global $wp_query;
508
509 $wp_query = $data['original']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
510
511 // Ensure the global post/authordata is set only if needed.
512 unset( $GLOBALS['post'] );
513 unset( $GLOBALS['authordata'] );
514
515 if ( ! empty( $data['post'] ) ) {
516 $GLOBALS['post'] = $data['post']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
517 setup_postdata( $GLOBALS['post'] );
518 }
519
520 if ( $wp_query->is_author() ) {
521 $GLOBALS['authordata'] = get_userdata( $wp_query->get( 'author' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
522 }
523 }
524
525 /**
526 * Get plain text.
527 *
528 * Retrieve the post plain text.
529 *
530 * @since 1.9.0
531 * @access public
532 *
533 * @param int $post_id Post ID.
534 *
535 * @return string Post plain text.
536 */
537 public function get_plain_text( $post_id ) {
538 $document = Plugin::$instance->documents->get( $post_id );
539 $data = $document ? $document->get_elements_data() : [];
540
541 return $this->get_plain_text_from_data( $data );
542 }
543
544 /**
545 * Get plain text from data.
546 *
547 * Retrieve the post plain text from any given Elementor data.
548 *
549 * @since 1.9.2
550 * @access public
551 *
552 * @param array $data Post ID.
553 *
554 * @return string Post plain text.
555 */
556 public function get_plain_text_from_data( $data ) {
557 ob_start();
558 if ( $data ) {
559 foreach ( $data as $element_data ) {
560 $this->render_element_plain_content( $element_data );
561 }
562 }
563
564 $plain_text = ob_get_clean();
565
566 // Remove unnecessary tags.
567 $plain_text = preg_replace( '/<\/?div[^>]*\>/i', '', $plain_text );
568 $plain_text = preg_replace( '/<\/?span[^>]*\>/i', '', $plain_text );
569 $plain_text = preg_replace( '#<script(.*?)>(.*?)</script>#is', '', $plain_text );
570 $plain_text = preg_replace( '/<i [^>]*><\\/i[^>]*>/', '', $plain_text );
571 $plain_text = preg_replace( '/ class=".*?"/', '', $plain_text );
572
573 // Remove empty lines.
574 $plain_text = preg_replace( '/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', "\n", $plain_text );
575
576 $plain_text = trim( $plain_text );
577
578 return $plain_text;
579 }
580 }
581