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

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