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

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