PluginProbe
Elementor Website Builder – more than just a page builder / 3.27.5
Elementor Website Builder – more than just a page builder v3.27.5
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.27.5, at core/documents-manager.php

825 lines 20.1 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 add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
92 }
93
94 /**
95 * Register ajax actions.
96 *
97 * Process ajax action handles when saving data and discarding changes.
98 *
99 * Fired by `elementor/ajax/register_actions` action.
100 *
101 * @since 2.0.0
102 * @access public
103 *
104 * @param Ajax $ajax_manager An instance of the ajax manager.
105 */
106 public function register_ajax_actions( $ajax_manager ) {
107 $ajax_manager->register_ajax_action( 'save_builder', [ $this, 'ajax_save' ] );
108 $ajax_manager->register_ajax_action( 'discard_changes', [ $this, 'ajax_discard_changes' ] );
109 $ajax_manager->register_ajax_action( 'get_document_config', [ $this, 'ajax_get_document_config' ] );
110 }
111
112 /**
113 * Register default types.
114 *
115 * Registers the default document types.
116 *
117 * @since 2.0.0
118 * @access public
119 */
120 public function register_default_types() {
121 $default_types = [
122 'post' => Post::get_class_full_name(), // BC.
123 'wp-post' => Post::get_class_full_name(),
124 'wp-page' => Page::get_class_full_name(),
125 ];
126
127 foreach ( $default_types as $type => $class ) {
128 $this->register_document_type( $type, $class );
129 }
130 }
131
132 /**
133 * Register document type.
134 *
135 * Registers a single document.
136 *
137 * @since 2.0.0
138 * @access public
139 *
140 * @param string $type Document type name.
141 * @param string $class The name of the class that registers the document type.
142 * Full name with the namespace.
143 *
144 * @return Documents_Manager The updated document manager instance.
145 */
146 public function register_document_type( $type, $class ) {
147 $this->types[ $type ] = $class;
148
149 $cpt = $class::get_property( 'cpt' );
150
151 if ( $cpt ) {
152 foreach ( $cpt as $post_type ) {
153 $this->cpt[ $post_type ] = $type;
154 }
155 }
156
157 if ( $class::get_property( 'register_type' ) ) {
158 Source_Local::add_template_type( $type );
159 }
160
161 return $this;
162 }
163
164 /**
165 * Get document.
166 *
167 * Retrieve the document data based on a post ID.
168 *
169 * @since 2.0.0
170 * @access public
171 *
172 * @param int $post_id Post ID.
173 * @param bool $from_cache Optional. Whether to retrieve cached data. Default is true.
174 *
175 * @return false|Document Document data or false if post ID was not entered.
176 */
177 public function get( $post_id, $from_cache = true ) {
178 $this->register_types();
179
180 $post_id = absint( $post_id );
181
182 if ( ! $post_id || ! get_post( $post_id ) ) {
183 return false;
184 }
185
186 /**
187 * Retrieve document post ID.
188 *
189 * Filters the document post ID.
190 *
191 * @since 2.0.7
192 *
193 * @param int $post_id The post ID of the document.
194 */
195 $post_id = apply_filters( 'elementor/documents/get/post_id', $post_id );
196
197 if ( ! $from_cache || ! isset( $this->documents[ $post_id ] ) ) {
198 $doc_type = $this->get_doc_type_by_id( $post_id );
199 $doc_type_class = $this->get_document_type( $doc_type );
200
201 $this->documents[ $post_id ] = new $doc_type_class( [
202 'post_id' => $post_id,
203 ] );
204 }
205
206 return $this->documents[ $post_id ];
207 }
208
209 /**
210 * Retrieve a document after checking it exist and allowed to edit.
211 *
212 * @since 3.13.0
213 *
214 * @param int $post_id The post ID of the document.
215 *
216 * @return Document
217 * @throws \Exception
218 */
219 public function get_with_permissions( $id ): Document {
220 $document = $this->get( $id );
221
222 if ( ! $document ) {
223 throw new \Exception( 'Not found.' );
224 }
225
226 if ( ! $document->is_editable_by_current_user() ) {
227 throw new \Exception( 'Access denied.' );
228 }
229
230 return $document;
231 }
232
233 /**
234 * A `void` version for `get_with_permissions`.
235 *
236 * @param $id
237 * @return void
238 * @throws \Exception
239 */
240 public function check_permissions( $id ) {
241 $this->get_with_permissions( $id );
242 }
243
244 /**
245 * Get document or autosave.
246 *
247 * Retrieve either the document or the autosave.
248 *
249 * @since 2.0.0
250 * @access public
251 *
252 * @param int $id Optional. Post ID. Default is `0`.
253 * @param int $user_id Optional. User ID. Default is `0`.
254 *
255 * @return false|Document The document if it exist, False otherwise.
256 */
257 public function get_doc_or_auto_save( $id, $user_id = 0 ) {
258 $document = $this->get( $id );
259 if ( $document && $document->get_autosave_id( $user_id ) ) {
260 $document = $document->get_autosave( $user_id );
261 }
262
263 return $document;
264 }
265
266 /**
267 * Get document for frontend.
268 *
269 * Retrieve the document for frontend use.
270 *
271 * @since 2.0.0
272 * @access public
273 *
274 * @param int $post_id Optional. Post ID. Default is `0`.
275 *
276 * @return false|Document The document if it exist, False otherwise.
277 */
278 public function get_doc_for_frontend( $post_id ) {
279 $preview_id = (int) Utils::get_super_global_value( $_GET, 'preview_id' );
280 $is_preview = is_preview() && $post_id === $preview_id;
281 $is_nonce_verify = wp_verify_nonce( Utils::get_super_global_value( $_GET, 'preview_nonce' ), 'post_preview_' . $preview_id );
282
283 if ( ( $is_preview && $is_nonce_verify ) || Plugin::$instance->preview->is_preview_mode() ) {
284 $document = $this->get_doc_or_auto_save( $post_id, get_current_user_id() );
285 } else {
286 $document = $this->get( $post_id );
287 }
288
289 return $document;
290 }
291
292 /**
293 * Get document type.
294 *
295 * Retrieve the type of any given document.
296 *
297 * @since 2.0.0
298 * @access public
299 *
300 * @param string $type
301 *
302 * @param string $fallback
303 *
304 * @return Document|bool The type of the document.
305 */
306 public function get_document_type( $type, $fallback = 'post' ) {
307 $types = $this->get_document_types();
308
309 if ( isset( $types[ $type ] ) ) {
310 return $types[ $type ];
311 }
312
313 if ( isset( $types[ $fallback ] ) ) {
314 return $types[ $fallback ];
315 }
316
317 return false;
318 }
319
320 /**
321 * Get document types.
322 *
323 * Retrieve the all the registered document types.
324 *
325 * @since 2.0.0
326 * @access public
327 *
328 * @param array $args Optional. An array of key => value arguments to match against
329 * the properties. Default is empty array.
330 * @param string $operator Optional. The logical operation to perform. 'or' means only one
331 * element from the array needs to match; 'and' means all elements
332 * must match; 'not' means no elements may match. Default 'and'.
333 *
334 * @return Document[] All the registered document types.
335 */
336 public function get_document_types( $args = [], $operator = 'and' ) {
337 $this->register_types();
338
339 if ( ! empty( $args ) ) {
340 $types_properties = $this->get_types_properties();
341
342 $filtered = wp_filter_object_list( $types_properties, $args, $operator );
343
344 return array_intersect_key( $this->types, $filtered );
345 }
346
347 return $this->types;
348 }
349
350 /**
351 * Get document types with their properties.
352 *
353 * @return array A list of properties arrays indexed by the type.
354 */
355 public function get_types_properties() {
356 $types_properties = [];
357
358 foreach ( $this->get_document_types() as $type => $class ) {
359 $types_properties[ $type ] = $class::get_properties();
360 }
361 return $types_properties;
362 }
363
364 /**
365 * Create a document.
366 *
367 * Create a new document using any given parameters.
368 *
369 * @since 2.0.0
370 * @access public
371 *
372 * @param string $type Document type.
373 * @param array $post_data An array containing the post data.
374 * @param array $meta_data An array containing the post meta data.
375 *
376 * @return Document The type of the document.
377 */
378 public function create( $type, $post_data = [], $meta_data = [] ) {
379 $class = $this->get_document_type( $type, false );
380
381 if ( ! $class ) {
382 return new \WP_Error( 500, sprintf( 'Type %s does not exist.', $type ) );
383 }
384
385 if ( empty( $post_data['post_title'] ) ) {
386 $post_data['post_title'] = esc_html__( 'Elementor', 'elementor' );
387 if ( 'post' !== $type ) {
388 $post_data['post_title'] = sprintf(
389 /* translators: %s: Document title. */
390 __( 'Elementor %s', 'elementor' ),
391 call_user_func( [ $class, 'get_title' ] )
392 );
393 }
394 $update_title = true;
395 }
396
397 $meta_data['_elementor_edit_mode'] = 'builder';
398
399 // Save the type as-is for plugins that hooked at `wp_insert_post`.
400 $meta_data[ Document::TYPE_META_KEY ] = $type;
401
402 $post_data['meta_input'] = $meta_data;
403
404 $post_types = $class::get_property( 'cpt' );
405
406 if ( ! empty( $post_types[0] ) && empty( $post_data['post_type'] ) ) {
407 $post_data['post_type'] = $post_types[0];
408 }
409
410 $post_id = wp_insert_post( $post_data );
411
412 if ( ! empty( $update_title ) ) {
413 $post_data['ID'] = $post_id;
414 $post_data['post_title'] .= ' #' . $post_id;
415
416 // The meta doesn't need update.
417 unset( $post_data['meta_input'] );
418
419 wp_update_post( $post_data );
420 }
421
422 /** @var Document $document */
423 $document = new $class( [
424 'post_id' => $post_id,
425 ] );
426
427 // Let the $document to re-save the template type by his way + version.
428 $document->save( [] );
429
430 return $document;
431 }
432
433 /**
434 * Remove user edit capabilities if document is not editable.
435 *
436 * Filters the user capabilities to disable editing in admin.
437 *
438 * @param array $allcaps An array of all the user's capabilities.
439 * @param array $caps Actual capabilities for meta capability.
440 * @param array $args Optional parameters passed to has_cap(), typically object ID.
441 *
442 * @return array
443 */
444 public function remove_user_edit_cap( $allcaps, $caps, $args ) {
445 global $pagenow;
446
447 if ( ! in_array( $pagenow, [ 'post.php', 'edit.php' ], true ) ) {
448 return $allcaps;
449 }
450
451 // Don't touch not existing or not allowed caps.
452 if ( empty( $caps[0] ) || empty( $allcaps[ $caps[0] ] ) ) {
453 return $allcaps;
454 }
455
456 $capability = $args[0];
457
458 if ( 'edit_post' !== $capability ) {
459 return $allcaps;
460 }
461
462 if ( empty( $args[2] ) ) {
463 return $allcaps;
464 }
465
466 $post_id = $args[2];
467
468 $document = Plugin::$instance->documents->get( $post_id );
469
470 if ( ! $document ) {
471 return $allcaps;
472 }
473
474 $allcaps[ $caps[0] ] = $document::get_property( 'is_editable' );
475
476 return $allcaps;
477 }
478
479 /**
480 * Filter Post Row Actions.
481 *
482 * Let the Document to filter the array of row action links on the Posts list table.
483 *
484 * @param array $actions
485 * @param \WP_Post $post
486 *
487 * @return array
488 */
489 public function filter_post_row_actions( $actions, $post ) {
490 $document = $this->get( $post->ID );
491
492 if ( $document ) {
493 $actions = $document->filter_admin_row_actions( $actions );
494 }
495
496 return $actions;
497 }
498
499 /**
500 * Save document data using ajax.
501 *
502 * Save the document on the builder using ajax, when saving the changes, and refresh the editor.
503 *
504 * @since 2.0.0
505 * @access public
506 *
507 * @param $request Post ID.
508 *
509 * @throws \Exception If current user don't have permissions to edit the post or the post is not using Elementor.
510 *
511 * @return array The document data after saving.
512 */
513 public function ajax_save( $request ) {
514 $document = $this->get( $request['editor_post_id'] );
515
516 if ( ! $document->is_built_with_elementor() || ! $document->is_editable_by_current_user() ) {
517 throw new \Exception( 'Access denied.' );
518 }
519
520 $this->switch_to_document( $document );
521
522 // Set the post as global post.
523 Plugin::$instance->db->switch_to_post( $document->get_post()->ID );
524
525 $status = Document::STATUS_DRAFT;
526
527 if ( isset( $request['status'] ) && in_array( $request['status'], [ Document::STATUS_PUBLISH, Document::STATUS_PRIVATE, Document::STATUS_PENDING, Document::STATUS_AUTOSAVE ], true ) ) {
528 $status = $request['status'];
529 }
530
531 if ( Document::STATUS_AUTOSAVE === $status ) {
532 // If the post is a draft - save the `autosave` to the original draft.
533 // Allow a revision only if the original post is already published.
534 if ( in_array( $document->get_post()->post_status, [ Document::STATUS_PUBLISH, Document::STATUS_PRIVATE ], true ) ) {
535 $document = $document->get_autosave( 0, true );
536 }
537 }
538
539 // Set default page template because the footer-saver doesn't send default values,
540 // But if the template was changed from canvas to default - it needed to save.
541 if ( Utils::is_cpt_custom_templates_supported() && ! isset( $request['settings']['template'] ) ) {
542 $request['settings']['template'] = 'default';
543 }
544
545 $data = [
546 'elements' => $request['elements'],
547 'settings' => $request['settings'],
548 ];
549
550 $document->save( $data );
551
552 $post = $document->get_post();
553 $main_post = $document->get_main_post();
554
555 // Refresh after save.
556 $document = $this->get( $post->ID, false );
557
558 $return_data = [
559 'status' => $post->post_status,
560 'config' => [
561 'document' => [
562 'last_edited' => $document->get_last_edited(),
563 'urls' => [
564 'wp_preview' => $document->get_wp_preview_url(),
565 ],
566 ],
567 ],
568 ];
569
570 $post_status_object = get_post_status_object( $main_post->post_status );
571
572 if ( $post_status_object ) {
573 $return_data['config']['document']['status'] = [
574 'value' => $post_status_object->name,
575 'label' => $post_status_object->label,
576 ];
577 }
578
579 /**
580 * Returned documents ajax saved data.
581 *
582 * Filters the ajax data returned when saving the post on the builder.
583 *
584 * @since 2.0.0
585 *
586 * @param array $return_data The returned data.
587 * @param Document $document The document instance.
588 */
589 $return_data = apply_filters( 'elementor/documents/ajax_save/return_data', $return_data, $document );
590
591 return $return_data;
592 }
593
594 /**
595 * Ajax discard changes.
596 *
597 * Load the document data from an autosave, deleting unsaved changes.
598 *
599 * @param $request
600 *
601 * @return bool True if changes discarded, False otherwise.
602 * @throws \Exception
603 *
604 * @since 2.0.0
605 * @access public
606 *
607 */
608 public function ajax_discard_changes( $request ) {
609 $document = $this->get_with_permissions( $request['editor_post_id'] );
610
611 $autosave = $document->get_autosave();
612
613 if ( $autosave ) {
614 $success = $autosave->delete();
615 } else {
616 $success = true;
617 }
618
619 return $success;
620 }
621
622 public function ajax_get_document_config( $request ) {
623 $post_id = absint( $request['id'] );
624
625 Plugin::$instance->editor->set_post_id( $post_id );
626
627 $document = $this->get_doc_or_auto_save( $post_id );
628
629 if ( ! $document ) {
630 throw new \Exception( 'Not found.' );
631 }
632
633 if ( ! $document->is_editable_by_current_user() ) {
634 throw new \Exception( 'Access denied.' );
635 }
636
637 // Set the global data like $post, $authordata and etc
638 Plugin::$instance->db->switch_to_post( $post_id );
639
640 $this->switch_to_document( $document );
641
642 // Change mode to Builder
643 $document->set_is_built_with_elementor( true );
644
645 $doc_config = $document->get_config();
646
647 return $doc_config;
648 }
649
650 /**
651 * Switch to document.
652 *
653 * Change the document to any new given document type.
654 *
655 * @since 2.0.0
656 * @access public
657 *
658 * @param Document $document The document to switch to.
659 */
660 public function switch_to_document( $document ) {
661 // If is already switched, or is the same post, return.
662 if ( $this->current_doc === $document ) {
663 $this->switched_data[] = false;
664 return;
665 }
666
667 $this->switched_data[] = [
668 'switched_doc' => $document,
669 'original_doc' => $this->current_doc, // Note, it can be null if the global isn't set
670 ];
671
672 $this->current_doc = $document;
673 }
674
675 /**
676 * Restore document.
677 *
678 * Rollback to the original document.
679 *
680 * @since 2.0.0
681 * @access public
682 */
683 public function restore_document() {
684 $data = array_pop( $this->switched_data );
685
686 // If not switched, return.
687 if ( ! $data ) {
688 return;
689 }
690
691 $this->current_doc = $data['original_doc'];
692 }
693
694 /**
695 * Get current document.
696 *
697 * Retrieve the current document.
698 *
699 * @since 2.0.0
700 * @access public
701 *
702 * @return Document The current document.
703 */
704 public function get_current() {
705 return $this->current_doc;
706 }
707
708 public function localize_settings( $settings ) {
709 $translations = [];
710
711 foreach ( $this->get_document_types() as $type => $class ) {
712 $translations[ $type ] = $class::get_title();
713 }
714
715 return array_replace_recursive( $settings, [
716 'i18n' => $translations,
717 ] );
718 }
719
720 private function register_types() {
721 if ( ! did_action( 'elementor/documents/register' ) ) {
722 /**
723 * Register Elementor documents.
724 *
725 * @since 2.0.0
726 *
727 * @param Documents_Manager $this The document manager instance.
728 */
729 do_action( 'elementor/documents/register', $this );
730 }
731 }
732
733 /**
734 * Get create new post URL.
735 *
736 * Retrieve a custom URL for creating a new post/page using Elementor.
737 *
738 * @param string $post_type Optional. Post type slug. Default is 'page'.
739 * @param string|null $template_type Optional. Query arg 'template_type'. Default is null.
740 *
741 * @return string A URL for creating new post using Elementor.
742 */
743 public static function get_create_new_post_url( $post_type = 'page', $template_type = null ) {
744 $query_args = [
745 'action' => 'elementor_new_post',
746 'post_type' => $post_type,
747 ];
748
749 if ( $template_type ) {
750 $query_args['template_type'] = $template_type;
751 }
752
753 $new_post_url = add_query_arg( $query_args, admin_url( 'edit.php' ) );
754
755 $new_post_url = add_query_arg( '_wpnonce', wp_create_nonce( 'elementor_action_new_post' ), $new_post_url );
756
757 return $new_post_url;
758 }
759
760 private function get_doc_type_by_id( $post_id ) {
761 // Auto-save inherits from the original post.
762 if ( wp_is_post_autosave( $post_id ) ) {
763 $post_id = wp_get_post_parent_id( $post_id );
764 }
765
766 // Content built with Elementor.
767 $template_type = get_post_meta( $post_id, Document::TYPE_META_KEY, true );
768
769 if ( $template_type && isset( $this->types[ $template_type ] ) ) {
770 return $template_type;
771 }
772
773 // Elementor installation on a site with existing content (which doesn't contain Elementor's meta).
774 $post_type = get_post_type( $post_id );
775
776 return $this->cpt[ $post_type ] ?? 'post';
777 }
778
779 public function register_rest_routes() {
780 register_rest_route('elementor/v1/documents', '/(?P<id>\d+)/media/import', [
781 'methods' => \WP_REST_Server::CREATABLE,
782 'callback' => function( $request ) {
783 $post_id = $request->get_param( 'id' );
784
785 try {
786 $document = $this->get_with_permissions( $post_id );
787
788 $elements_data = $document->get_elements_data();
789
790 $import_data = $document->get_import_data( [
791 'content' => $elements_data,
792 ] );
793
794 $document->save( [
795 'elements' => $import_data['content'],
796 ] );
797
798 return new \WP_REST_Response( [
799 'success' => true,
800 'document_saved' => true,
801 ], 200 );
802
803 } catch ( \Exception $e ) {
804 return new \WP_Error(
805 'elementor_import_error',
806 $e->getMessage(),
807 [ 'status' => 500 ]
808 );
809 }
810 },
811 'permission_callback' => function() {
812 return current_user_can( 'manage_options' );
813 },
814 'args' => [
815 'id' => [
816 'required' => true,
817 'validate_callback' => function( $param ) {
818 return is_numeric( $param );
819 },
820 ],
821 ],
822 ]);
823 }
824 }
825