PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.0
Elementor Website Builder – more than just a page builder v3.8.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 / documents-manager.php

documents-manager.php in Elementor Website Builder – more than just a page builder 3.8.0, at core/documents-manager.php

723 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Core;
3
4 use Elementor\Core\Base\Document;
5 use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
6 use Elementor\Core\DocumentTypes\Page;
7 use Elementor\Core\DocumentTypes\Post;
8 use Elementor\Plugin;
9 use Elementor\TemplateLibrary\Source_Local;
10 use Elementor\Utils;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly
14 }
15
16 /**
17 * Elementor documents manager.
18 *
19 * Elementor documents manager handler class is responsible for registering and
20 * managing Elementor documents.
21 *
22 * @since 2.0.0
23 */
24 class Documents_Manager {
25
26 /**
27 * Registered types.
28 *
29 * Holds the list of all the registered types.
30 *
31 * @since 2.0.0
32 * @access protected
33 *
34 * @var Document[]
35 */
36 protected $types = [];
37
38 /**
39 * Registered documents.
40 *
41 * Holds the list of all the registered documents.
42 *
43 * @since 2.0.0
44 * @access protected
45 *
46 * @var Document[]
47 */
48 protected $documents = [];
49
50 /**
51 * Current document.
52 *
53 * Holds the current document.
54 *
55 * @since 2.0.0
56 * @access protected
57 *
58 * @var Document
59 */
60 protected $current_doc;
61
62 /**
63 * Switched data.
64 *
65 * Holds the current document when changing to the requested post.
66 *
67 * @since 2.0.0
68 * @access protected
69 *
70 * @var array
71 */
72 protected $switched_data = [];
73
74 protected $cpt = [];
75
76 /**
77 * Documents manager constructor.
78 *
79 * Initializing the Elementor documents manager.
80 *
81 * @since 2.0.0
82 * @access public
83 */
84 public function __construct() {
85 add_action( 'elementor/documents/register', [ $this, 'register_default_types' ], 0 );
86 add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] );
87 add_filter( 'post_row_actions', [ $this, 'filter_post_row_actions' ], 11, 2 );
88 add_filter( 'page_row_actions', [ $this, 'filter_post_row_actions' ], 11, 2 );
89 add_filter( 'user_has_cap', [ $this, 'remove_user_edit_cap' ], 10, 3 );
90 add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] );
91 }
92
93 /**
94 * Register ajax actions.
95 *
96 * Process ajax action handles when saving data and discarding changes.
97 *
98 * Fired by `elementor/ajax/register_actions` action.
99 *
100 * @since 2.0.0
101 * @access public
102 *
103 * @param Ajax $ajax_manager An instance of the ajax manager.
104 */
105 public function register_ajax_actions( $ajax_manager ) {
106 $ajax_manager->register_ajax_action( 'save_builder', [ $this, 'ajax_save' ] );
107 $ajax_manager->register_ajax_action( 'discard_changes', [ $this, 'ajax_discard_changes' ] );
108 $ajax_manager->register_ajax_action( 'get_document_config', [ $this, 'ajax_get_document_config' ] );
109 }
110
111 /**
112 * Register default types.
113 *
114 * Registers the default document types.
115 *
116 * @since 2.0.0
117 * @access public
118 */
119 public function register_default_types() {
120 $default_types = [
121 'post' => Post::get_class_full_name(), // BC.
122 'wp-post' => Post::get_class_full_name(),
123 'wp-page' => Page::get_class_full_name(),
124 ];
125
126 foreach ( $default_types as $type => $class ) {
127 $this->register_document_type( $type, $class );
128 }
129 }
130
131 /**
132 * Register document type.
133 *
134 * Registers a single document.
135 *
136 * @since 2.0.0
137 * @access public
138 *
139 * @param string $type Document type name.
140 * @param string $class The name of the class that registers the document type.
141 * Full name with the namespace.
142 *
143 * @return Documents_Manager The updated document manager instance.
144 */
145 public function register_document_type( $type, $class ) {
146 $this->types[ $type ] = $class;
147
148 $cpt = $class::get_property( 'cpt' );
149
150 if ( $cpt ) {
151 foreach ( $cpt as $post_type ) {
152 $this->cpt[ $post_type ] = $type;
153 }
154 }
155
156 if ( $class::get_property( 'register_type' ) ) {
157 Source_Local::add_template_type( $type );
158 }
159
160 return $this;
161 }
162
163 /**
164 * Get document.
165 *
166 * Retrieve the document data based on a post ID.
167 *
168 * @since 2.0.0
169 * @access public
170 *
171 * @param int $post_id Post ID.
172 * @param bool $from_cache Optional. Whether to retrieve cached data. Default is true.
173 *
174 * @return false|Document Document data or false if post ID was not entered.
175 */
176 public function get( $post_id, $from_cache = true ) {
177 $this->register_types();
178
179 $post_id = absint( $post_id );
180
181 if ( ! $post_id || ! get_post( $post_id ) ) {
182 return false;
183 }
184
185 /**
186 * Retrieve document post ID.
187 *
188 * Filters the document post ID.
189 *
190 * @since 2.0.7
191 *
192 * @param int $post_id The post ID of the document.
193 */
194 $post_id = apply_filters( 'elementor/documents/get/post_id', $post_id );
195
196 if ( ! $from_cache || ! isset( $this->documents[ $post_id ] ) ) {
197
198 if ( wp_is_post_autosave( $post_id ) ) {
199 $post_type = get_post_type( wp_get_post_parent_id( $post_id ) );
200 } else {
201 $post_type = get_post_type( $post_id );
202 }
203
204 $doc_type = 'post';
205
206 if ( isset( $this->cpt[ $post_type ] ) ) {
207 $doc_type = $this->cpt[ $post_type ];
208 }
209
210 $meta_type = get_post_meta( $post_id, Document::TYPE_META_KEY, true );
211
212 if ( $meta_type && isset( $this->types[ $meta_type ] ) ) {
213 $doc_type = $meta_type;
214 }
215
216 $doc_type_class = $this->get_document_type( $doc_type );
217 $this->documents[ $post_id ] = new $doc_type_class( [
218 'post_id' => $post_id,
219 ] );
220 }
221
222 return $this->documents[ $post_id ];
223 }
224
225 /**
226 * Get document or autosave.
227 *
228 * Retrieve either the document or the autosave.
229 *
230 * @since 2.0.0
231 * @access public
232 *
233 * @param int $id Optional. Post ID. Default is `0`.
234 * @param int $user_id Optional. User ID. Default is `0`.
235 *
236 * @return false|Document The document if it exist, False otherwise.
237 */
238 public function get_doc_or_auto_save( $id, $user_id = 0 ) {
239 $document = $this->get( $id );
240 if ( $document && $document->get_autosave_id( $user_id ) ) {
241 $document = $document->get_autosave( $user_id );
242 }
243
244 return $document;
245 }
246
247 /**
248 * Get document for frontend.
249 *
250 * Retrieve the document for frontend use.
251 *
252 * @since 2.0.0
253 * @access public
254 *
255 * @param int $post_id Optional. Post ID. Default is `0`.
256 *
257 * @return false|Document The document if it exist, False otherwise.
258 */
259 public function get_doc_for_frontend( $post_id ) {
260 if ( is_preview() || Plugin::$instance->preview->is_preview_mode() ) {
261 $document = $this->get_doc_or_auto_save( $post_id, get_current_user_id() );
262 } else {
263 $document = $this->get( $post_id );
264 }
265
266 return $document;
267 }
268
269 /**
270 * Get document type.
271 *
272 * Retrieve the type of any given document.
273 *
274 * @since 2.0.0
275 * @access public
276 *
277 * @param string $type
278 *
279 * @param string $fallback
280 *
281 * @return Document|bool The type of the document.
282 */
283 public function get_document_type( $type, $fallback = 'post' ) {
284 $types = $this->get_document_types();
285
286 if ( isset( $types[ $type ] ) ) {
287 return $types[ $type ];
288 }
289
290 if ( isset( $types[ $fallback ] ) ) {
291 return $types[ $fallback ];
292 }
293
294 return false;
295 }
296
297 /**
298 * Get document types.
299 *
300 * Retrieve the all the registered document types.
301 *
302 * @since 2.0.0
303 * @access public
304 *
305 * @param array $args Optional. An array of key => value arguments to match against
306 * the properties. Default is empty array.
307 * @param string $operator Optional. The logical operation to perform. 'or' means only one
308 * element from the array needs to match; 'and' means all elements
309 * must match; 'not' means no elements may match. Default 'and'.
310 *
311 * @return Document[] All the registered document types.
312 */
313 public function get_document_types( $args = [], $operator = 'and' ) {
314 $this->register_types();
315
316 if ( ! empty( $args ) ) {
317 $types_properties = $this->get_types_properties();
318
319 $filtered = wp_filter_object_list( $types_properties, $args, $operator );
320
321 return array_intersect_key( $this->types, $filtered );
322 }
323
324 return $this->types;
325 }
326
327 /**
328 * Get document types with their properties.
329 *
330 * @return array A list of properties arrays indexed by the type.
331 */
332 public function get_types_properties() {
333 $types_properties = [];
334
335 foreach ( $this->get_document_types() as $type => $class ) {
336 $types_properties[ $type ] = $class::get_properties();
337 }
338 return $types_properties;
339 }
340
341 /**
342 * Create a document.
343 *
344 * Create a new document using any given parameters.
345 *
346 * @since 2.0.0
347 * @access public
348 *
349 * @param string $type Document type.
350 * @param array $post_data An array containing the post data.
351 * @param array $meta_data An array containing the post meta data.
352 *
353 * @return Document The type of the document.
354 */
355 public function create( $type, $post_data = [], $meta_data = [] ) {
356 $class = $this->get_document_type( $type, false );
357
358 if ( ! $class ) {
359 return new \WP_Error( 500, sprintf( 'Type %s does not exist.', $type ) );
360 }
361
362 if ( empty( $post_data['post_title'] ) ) {
363 $post_data['post_title'] = esc_html__( 'Elementor', 'elementor' );
364 if ( 'post' !== $type ) {
365 $post_data['post_title'] = sprintf(
366 /* translators: %s: Document title. */
367 __( 'Elementor %s', 'elementor' ),
368 call_user_func( [ $class, 'get_title' ] )
369 );
370 }
371 $update_title = true;
372 }
373
374 $meta_data['_elementor_edit_mode'] = 'builder';
375
376 // Save the type as-is for plugins that hooked at `wp_insert_post`.
377 $meta_data[ Document::TYPE_META_KEY ] = $type;
378
379 $post_data['meta_input'] = $meta_data;
380
381 $post_types = $class::get_property( 'cpt' );
382
383 if ( ! empty( $post_types[0] ) && empty( $post_data['post_type'] ) ) {
384 $post_data['post_type'] = $post_types[0];
385 }
386
387 $post_id = wp_insert_post( $post_data );
388
389 if ( ! empty( $update_title ) ) {
390 $post_data['ID'] = $post_id;
391 $post_data['post_title'] .= ' #' . $post_id;
392
393 // The meta doesn't need update.
394 unset( $post_data['meta_input'] );
395
396 wp_update_post( $post_data );
397 }
398
399 /** @var Document $document */
400 $document = new $class( [
401 'post_id' => $post_id,
402 ] );
403
404 // Let the $document to re-save the template type by his way + version.
405 $document->save( [] );
406
407 return $document;
408 }
409
410 /**
411 * Remove user edit capabilities if document is not editable.
412 *
413 * Filters the user capabilities to disable editing in admin.
414 *
415 * @param array $allcaps An array of all the user's capabilities.
416 * @param array $caps Actual capabilities for meta capability.
417 * @param array $args Optional parameters passed to has_cap(), typically object ID.
418 *
419 * @return array
420 */
421 public function remove_user_edit_cap( $allcaps, $caps, $args ) {
422 global $pagenow;
423
424 if ( ! in_array( $pagenow, [ 'post.php', 'edit.php' ], true ) ) {
425 return $allcaps;
426 }
427
428 // Don't touch not existing or not allowed caps.
429 if ( empty( $caps[0] ) || empty( $allcaps[ $caps[0] ] ) ) {
430 return $allcaps;
431 }
432
433 $capability = $args[0];
434
435 if ( 'edit_post' !== $capability ) {
436 return $allcaps;
437 }
438
439 if ( empty( $args[2] ) ) {
440 return $allcaps;
441 }
442
443 $post_id = $args[2];
444
445 $document = Plugin::$instance->documents->get( $post_id );
446
447 if ( ! $document ) {
448 return $allcaps;
449 }
450
451 $allcaps[ $caps[0] ] = $document::get_property( 'is_editable' );
452
453 return $allcaps;
454 }
455
456 /**
457 * Filter Post Row Actions.
458 *
459 * Let the Document to filter the array of row action links on the Posts list table.
460 *
461 * @param array $actions
462 * @param \WP_Post $post
463 *
464 * @return array
465 */
466 public function filter_post_row_actions( $actions, $post ) {
467 $document = $this->get( $post->ID );
468
469 if ( $document ) {
470 $actions = $document->filter_admin_row_actions( $actions );
471 }
472
473 return $actions;
474 }
475
476 /**
477 * Save document data using ajax.
478 *
479 * Save the document on the builder using ajax, when saving the changes, and refresh the editor.
480 *
481 * @since 2.0.0
482 * @access public
483 *
484 * @param $request Post ID.
485 *
486 * @throws \Exception If current user don't have permissions to edit the post or the post is not using Elementor.
487 *
488 * @return array The document data after saving.
489 */
490 public function ajax_save( $request ) {
491 $document = $this->get( $request['editor_post_id'] );
492
493 if ( ! $document->is_built_with_elementor() || ! $document->is_editable_by_current_user() ) {
494 throw new \Exception( 'Access denied.' );
495 }
496
497 $this->switch_to_document( $document );
498
499 // Set the post as global post.
500 Plugin::$instance->db->switch_to_post( $document->get_post()->ID );
501
502 $status = Document::STATUS_DRAFT;
503
504 if ( isset( $request['status'] ) && in_array( $request['status'], [ Document::STATUS_PUBLISH, Document::STATUS_PRIVATE, Document::STATUS_PENDING, Document::STATUS_AUTOSAVE ], true ) ) {
505 $status = $request['status'];
506 }
507
508 if ( Document::STATUS_AUTOSAVE === $status ) {
509 // If the post is a draft - save the `autosave` to the original draft.
510 // Allow a revision only if the original post is already published.
511 if ( in_array( $document->get_post()->post_status, [ Document::STATUS_PUBLISH, Document::STATUS_PRIVATE ], true ) ) {
512 $document = $document->get_autosave( 0, true );
513 }
514 }
515
516 // Set default page template because the footer-saver doesn't send default values,
517 // But if the template was changed from canvas to default - it needed to save.
518 if ( Utils::is_cpt_custom_templates_supported() && ! isset( $request['settings']['template'] ) ) {
519 $request['settings']['template'] = 'default';
520 }
521
522 $data = [
523 'elements' => $request['elements'],
524 'settings' => $request['settings'],
525 ];
526
527 $document->save( $data );
528
529 // Refresh after save.
530 $document = $this->get( $document->get_post()->ID, false );
531
532 $return_data = [
533 'status' => $document->get_post()->post_status,
534 'config' => [
535 'document' => [
536 'last_edited' => $document->get_last_edited(),
537 'urls' => [
538 'wp_preview' => $document->get_wp_preview_url(),
539 ],
540 ],
541 ],
542 ];
543
544 /**
545 * Returned documents ajax saved data.
546 *
547 * Filters the ajax data returned when saving the post on the builder.
548 *
549 * @since 2.0.0
550 *
551 * @param array $return_data The returned data.
552 * @param Document $document The document instance.
553 */
554 $return_data = apply_filters( 'elementor/documents/ajax_save/return_data', $return_data, $document );
555
556 return $return_data;
557 }
558
559 /**
560 * Ajax discard changes.
561 *
562 * Load the document data from an autosave, deleting unsaved changes.
563 *
564 * @since 2.0.0
565 * @access public
566 *
567 * @param $request
568 *
569 * @return bool True if changes discarded, False otherwise.
570 */
571 public function ajax_discard_changes( $request ) {
572 $document = $this->get( $request['editor_post_id'] );
573
574 $autosave = $document->get_autosave();
575
576 if ( $autosave ) {
577 $success = $autosave->delete();
578 } else {
579 $success = true;
580 }
581
582 return $success;
583 }
584
585 public function ajax_get_document_config( $request ) {
586 $post_id = absint( $request['id'] );
587
588 Plugin::$instance->editor->set_post_id( $post_id );
589
590 $document = $this->get_doc_or_auto_save( $post_id );
591
592 if ( ! $document ) {
593 throw new \Exception( 'Not Found.' );
594 }
595
596 if ( ! $document->is_editable_by_current_user() ) {
597 throw new \Exception( 'Access denied.' );
598 }
599
600 // Set the global data like $post, $authordata and etc
601 Plugin::$instance->db->switch_to_post( $post_id );
602
603 $this->switch_to_document( $document );
604
605 // Change mode to Builder
606 $document->set_is_built_with_elementor( true );
607
608 $doc_config = $document->get_config();
609
610 return $doc_config;
611 }
612
613 /**
614 * Switch to document.
615 *
616 * Change the document to any new given document type.
617 *
618 * @since 2.0.0
619 * @access public
620 *
621 * @param Document $document The document to switch to.
622 */
623 public function switch_to_document( $document ) {
624 // If is already switched, or is the same post, return.
625 if ( $this->current_doc === $document ) {
626 $this->switched_data[] = false;
627 return;
628 }
629
630 $this->switched_data[] = [
631 'switched_doc' => $document,
632 'original_doc' => $this->current_doc, // Note, it can be null if the global isn't set
633 ];
634
635 $this->current_doc = $document;
636 }
637
638 /**
639 * Restore document.
640 *
641 * Rollback to the original document.
642 *
643 * @since 2.0.0
644 * @access public
645 */
646 public function restore_document() {
647 $data = array_pop( $this->switched_data );
648
649 // If not switched, return.
650 if ( ! $data ) {
651 return;
652 }
653
654 $this->current_doc = $data['original_doc'];
655 }
656
657 /**
658 * Get current document.
659 *
660 * Retrieve the current document.
661 *
662 * @since 2.0.0
663 * @access public
664 *
665 * @return Document The current document.
666 */
667 public function get_current() {
668 return $this->current_doc;
669 }
670
671 public function localize_settings( $settings ) {
672 $translations = [];
673
674 foreach ( $this->get_document_types() as $type => $class ) {
675 $translations[ $type ] = $class::get_title();
676 }
677
678 return array_replace_recursive( $settings, [
679 'i18n' => $translations,
680 ] );
681 }
682
683 private function register_types() {
684 if ( ! did_action( 'elementor/documents/register' ) ) {
685 /**
686 * Register Elementor documents.
687 *
688 * @since 2.0.0
689 *
690 * @param Documents_Manager $this The document manager instance.
691 */
692 do_action( 'elementor/documents/register', $this );
693 }
694 }
695
696 /**
697 * Get create new post URL.
698 *
699 * Retrieve a custom URL for creating a new post/page using Elementor.
700 *
701 * @param string $post_type Optional. Post type slug. Default is 'page'.
702 * @param string|null $template_type Optional. Query arg 'template_type'. Default is null.
703 *
704 * @return string A URL for creating new post using Elementor.
705 */
706 public static function get_create_new_post_url( $post_type = 'page', $template_type = null ) {
707 $query_args = [
708 'action' => 'elementor_new_post',
709 'post_type' => $post_type,
710 ];
711
712 if ( $template_type ) {
713 $query_args['template_type'] = $template_type;
714 }
715
716 $new_post_url = add_query_arg( $query_args, admin_url( 'edit.php' ) );
717
718 $new_post_url = add_query_arg( '_wpnonce', wp_create_nonce( 'elementor_action_new_post' ), $new_post_url );
719
720 return $new_post_url;
721 }
722 }
723