PluginProbe
Elementor Website Builder – more than just a page builder / 3.0.4
Elementor Website Builder – more than just a page builder v3.0.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
← All changes | core/documents-manager.php +63 -188 4.2.33.0.4 View file →
@@ -4,14 +4,15 @@
4 4 use Elementor\Core\Base\Document;
5 5 use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
6 6 use Elementor\Core\DocumentTypes\Page;
7 7 use Elementor\Core\DocumentTypes\Post;
8 +use Elementor\DB;
8 9 use Elementor\Plugin;
9 10 use Elementor\TemplateLibrary\Source_Local;
10 11 use Elementor\Utils;
11 12
12 13 if ( ! defined( 'ABSPATH' ) ) {
13 - exit; // Exit if accessed directly.
14 + exit; // Exit if accessed directly
14 15 }
15 16
16 17 /**
17 18 * Elementor documents manager.
@@ -87,9 +88,8 @@
87 88 add_filter( 'post_row_actions', [ $this, 'filter_post_row_actions' ], 11, 2 );
88 89 add_filter( 'page_row_actions', [ $this, 'filter_post_row_actions' ], 11, 2 );
89 90 add_filter( 'user_has_cap', [ $this, 'remove_user_edit_cap' ], 10, 3 );
90 91 add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] );
91 - add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
92 92 }
93 93
94 94 /**
95 95 * Register ajax actions.
@@ -136,18 +136,18 @@
136 136 *
137 137 * @since 2.0.0
138 138 * @access public
139 139 *
140 - * @param string $type Document type name.
141 - * @param string $class_name The name of the class that registers the document type.
142 - * Full name with the namespace.
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 143 *
144 144 * @return Documents_Manager The updated document manager instance.
145 145 */
146 - public function register_document_type( $type, $class_name ) {
147 - $this->types[ $type ] = $class_name;
146 + public function register_document_type( $type, $class ) {
147 + $this->types[ $type ] = $class;
148 148
149 - $cpt = $class_name::get_property( 'cpt' );
149 + $cpt = $class::get_property( 'cpt' );
150 150
151 151 if ( $cpt ) {
152 152 foreach ( $cpt as $post_type ) {
153 153 $this->cpt[ $post_type ] = $type;
@@ -153,9 +153,9 @@
153 153 $this->cpt[ $post_type ] = $type;
154 154 }
155 155 }
156 156
157 - if ( $class_name::get_property( 'register_type' ) ) {
157 + if ( $class::get_property( 'register_type' ) ) {
158 158 Source_Local::add_template_type( $type );
159 159 }
160 160
161 161 return $this;
@@ -182,23 +182,31 @@
182 182 if ( ! $post_id || ! get_post( $post_id ) ) {
183 183 return false;
184 184 }
185 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 186 $post_id = apply_filters( 'elementor/documents/get/post_id', $post_id );
196 187
197 188 if ( ! $from_cache || ! isset( $this->documents[ $post_id ] ) ) {
198 - $doc_type = $this->get_doc_type_by_id( $post_id );
189 +
190 + if ( wp_is_post_autosave( $post_id ) ) {
191 + $post_type = get_post_type( wp_get_post_parent_id( $post_id ) );
192 + } else {
193 + $post_type = get_post_type( $post_id );
194 + }
195 +
196 + $doc_type = 'post';
197 +
198 + if ( isset( $this->cpt[ $post_type ] ) ) {
199 + $doc_type = $this->cpt[ $post_type ];
200 + }
201 +
202 + $meta_type = get_post_meta( $post_id, Document::TYPE_META_KEY, true );
203 +
204 + if ( $meta_type && isset( $this->types[ $meta_type ] ) ) {
205 + $doc_type = $meta_type;
206 + }
207 +
199 208 $doc_type_class = $this->get_document_type( $doc_type );
200 -
201 209 $this->documents[ $post_id ] = new $doc_type_class( [
202 210 'post_id' => $post_id,
203 211 ] );
204 212 }
@@ -206,41 +214,8 @@
206 214 return $this->documents[ $post_id ];
207 215 }
208 216
209 217 /**
210 - * Retrieve a document after checking it exist and allowed to edit.
211 - *
212 - * @param string $id
213 - * @return Document
214 - * @throws \Exception If the document is not found or the current user is not allowed to edit it.
215 - * @since 3.13.0
216 - */
217 - public function get_with_permissions( $id ): Document {
218 - $document = $this->get( $id );
219 -
220 - if ( ! $document ) {
221 - throw new \Exception( 'Not found.' );
222 - }
223 -
224 - if ( ! $document->is_editable_by_current_user() ) {
225 - throw new \Exception( 'Access denied.' );
226 - }
227 -
228 - return $document;
229 - }
230 -
231 - /**
232 - * A `void` version for `get_with_permissions`.
233 - *
234 - * @param string $id
235 - * @return void
236 - * @throws \Exception If the document is not found or the current user is not allowed to edit it.
237 - */
238 - public function check_permissions( $id ) {
239 - $this->get_with_permissions( $id );
240 - }
241 -
242 - /**
243 218 * Get document or autosave.
244 219 *
245 220 * Retrieve either the document or the autosave.
246 221 *
@@ -273,13 +248,9 @@
273 248 *
274 249 * @return false|Document The document if it exist, False otherwise.
275 250 */
276 251 public function get_doc_for_frontend( $post_id ) {
277 - $preview_id = (int) Utils::get_super_global_value( $_GET, 'preview_id' );
278 - $is_preview = is_preview();
279 - $is_nonce_verify = wp_verify_nonce( Utils::get_super_global_value( $_GET, 'preview_nonce' ), 'post_preview_' . $preview_id );
280 -
281 - if ( ( $is_preview && $is_nonce_verify ) || Plugin::$instance->preview->is_preview_mode() ) {
252 + if ( is_preview() || Plugin::$instance->preview->is_preview_mode() ) {
282 253 $document = $this->get_doc_or_auto_save( $post_id, get_current_user_id() );
283 254 } else {
284 255 $document = $this->get( $post_id );
285 256 }
@@ -322,10 +293,10 @@
322 293 *
323 294 * @since 2.0.0
324 295 * @access public
325 296 *
326 - * @param array $args Optional. An array of key => value arguments to match against
327 - * the properties. Default is empty array.
297 + * @param array $args Optional. An array of key => value arguments to match against
298 + * the properties. Default is empty array.
328 299 * @param string $operator Optional. The logical operation to perform. 'or' means only one
329 300 * element from the array needs to match; 'and' means all elements
330 301 * must match; 'not' means no elements may match. Default 'and'.
331 302 *
@@ -380,12 +351,12 @@
380 351 return new \WP_Error( 500, sprintf( 'Type %s does not exist.', $type ) );
381 352 }
382 353
383 354 if ( empty( $post_data['post_title'] ) ) {
384 - $post_data['post_title'] = esc_html__( 'Elementor', 'elementor' );
355 + $post_data['post_title'] = __( 'Elementor', 'elementor' );
385 356 if ( 'post' !== $type ) {
386 357 $post_data['post_title'] = sprintf(
387 - /* translators: %s: Document title. */
358 + /* translators: %s: Document title */
388 359 __( 'Elementor %s', 'elementor' ),
389 360 call_user_func( [ $class, 'get_title' ] )
390 361 );
391 362 }
@@ -398,14 +369,8 @@
398 369 $meta_data[ Document::TYPE_META_KEY ] = $type;
399 370
400 371 $post_data['meta_input'] = $meta_data;
401 372
402 - $post_types = $class::get_property( 'cpt' );
403 -
404 - if ( ! empty( $post_types[0] ) && empty( $post_data['post_type'] ) ) {
405 - $post_data['post_type'] = $post_types[0];
406 - }
407 -
408 373 $post_id = wp_insert_post( $post_data );
409 374
410 375 if ( ! empty( $update_title ) ) {
411 376 $post_data['ID'] = $post_id;
@@ -478,9 +443,9 @@
478 443 * Filter Post Row Actions.
479 444 *
480 445 * Let the Document to filter the array of row action links on the Posts list table.
481 446 *
482 - * @param array $actions
447 + * @param array $actions
483 448 * @param \WP_Post $post
484 449 *
485 450 * @return array
486 451 */
@@ -501,9 +466,9 @@
501 466 *
502 467 * @since 2.0.0
503 468 * @access public
504 469 *
505 - * @param array $request Post ID.
470 + * @param $request Post ID.
506 471 *
507 472 * @throws \Exception If current user don't have permissions to edit the post or the post is not using Elementor.
508 473 *
509 474 * @return array The document data after saving.
@@ -519,18 +484,18 @@
519 484
520 485 // Set the post as global post.
521 486 Plugin::$instance->db->switch_to_post( $document->get_post()->ID );
522 487
523 - $status = Document::STATUS_DRAFT;
488 + $status = DB::STATUS_DRAFT;
524 489
525 - if ( isset( $request['status'] ) && in_array( $request['status'], [ Document::STATUS_PUBLISH, Document::STATUS_PRIVATE, Document::STATUS_PENDING, Document::STATUS_AUTOSAVE ], true ) ) {
490 + if ( isset( $request['status'] ) && in_array( $request['status'], [ DB::STATUS_PUBLISH, DB::STATUS_PRIVATE, DB::STATUS_PENDING, DB::STATUS_AUTOSAVE ], true ) ) {
526 491 $status = $request['status'];
527 492 }
528 493
529 - if ( Document::STATUS_AUTOSAVE === $status ) {
494 + if ( DB::STATUS_AUTOSAVE === $status ) {
530 495 // If the post is a draft - save the `autosave` to the original draft.
531 496 // Allow a revision only if the original post is already published.
532 - if ( in_array( $document->get_post()->post_status, [ Document::STATUS_PUBLISH, Document::STATUS_PRIVATE ], true ) ) {
497 + if ( in_array( $document->get_post()->post_status, [ DB::STATUS_PUBLISH, DB::STATUS_PRIVATE ], true ) ) {
533 498 $document = $document->get_autosave( 0, true );
534 499 }
535 500 }
536 501
@@ -546,16 +511,13 @@
546 511 ];
547 512
548 513 $document->save( $data );
549 514
550 - $post = $document->get_post();
551 - $main_post = $document->get_main_post();
552 -
553 515 // Refresh after save.
554 - $document = $this->get( $post->ID, false );
516 + $document = $this->get( $document->get_post()->ID, false );
555 517
556 518 $return_data = [
557 - 'status' => $post->post_status,
519 + 'status' => $document->get_post()->post_status,
558 520 'config' => [
559 521 'document' => [
560 522 'last_edited' => $document->get_last_edited(),
561 523 'urls' => [
@@ -564,17 +526,8 @@
564 526 ],
565 527 ],
566 528 ];
567 529
568 - $post_status_object = get_post_status_object( $main_post->post_status );
569 -
570 - if ( $post_status_object ) {
571 - $return_data['config']['document']['status'] = [
572 - 'value' => $post_status_object->name,
573 - 'label' => $post_status_object->label,
574 - ];
575 - }
576 -
577 530 /**
578 531 * Returned documents ajax saved data.
579 532 *
580 533 * Filters the ajax data returned when saving the post on the builder.
@@ -593,18 +546,17 @@
593 546 * Ajax discard changes.
594 547 *
595 548 * Load the document data from an autosave, deleting unsaved changes.
596 549 *
597 - * @param array $request
550 + * @since 2.0.0
551 + * @access public
598 552 *
553 + * @param $request
554 + *
599 555 * @return bool True if changes discarded, False otherwise.
600 - * @throws \Exception If current user don't have permissions to edit the post or the post is not using Elementor.
601 - *
602 - * @since 2.0.0
603 - * @access public
604 556 */
605 557 public function ajax_discard_changes( $request ) {
606 - $document = $this->get_with_permissions( $request['editor_post_id'] );
558 + $document = $this->get( $request['editor_post_id'] );
607 559
608 560 $autosave = $document->get_autosave();
609 561
610 562 if ( $autosave ) {
@@ -623,9 +575,9 @@
623 575
624 576 $document = $this->get_doc_or_auto_save( $post_id );
625 577
626 578 if ( ! $document ) {
627 - throw new \Exception( 'Not found.' );
579 + throw new \Exception( 'Not Found.' );
628 580 }
629 581
630 582 if ( ! $document->is_editable_by_current_user() ) {
631 583 throw new \Exception( 'Access denied.' );
@@ -636,9 +588,9 @@
636 588
637 589 $this->switch_to_document( $document );
638 590
639 591 // Change mode to Builder
640 - $document->set_is_built_with_elementor( true );
592 + Plugin::$instance->db->set_is_elementor_page( $post_id );
641 593
642 594 $doc_config = $document->get_config();
643 595
644 596 return $doc_config;
@@ -701,8 +653,23 @@
701 653 public function get_current() {
702 654 return $this->current_doc;
703 655 }
704 656
657 + /**
658 + * Get groups.
659 + *
660 + * @since 2.0.0
661 + * @deprecated 2.4.0
662 + * @access public
663 + *
664 + * @return array
665 + */
666 + public function get_groups() {
667 + _deprecated_function( __METHOD__, '2.4.0' );
668 +
669 + return [];
670 + }
671 +
705 672 public function localize_settings( $settings ) {
706 673 $translations = [];
707 674
708 675 foreach ( $this->get_document_types() as $type => $class ) {
@@ -724,98 +691,6 @@
724 691 * @param Documents_Manager $this The document manager instance.
725 692 */
726 693 do_action( 'elementor/documents/register', $this );
727 694 }
728 - }
729 -
730 - /**
731 - * Get create new post URL.
732 - *
733 - * Retrieve a custom URL for creating a new post/page using Elementor.
734 - *
735 - * @param string $post_type Optional. Post type slug. Default is 'page'.
736 - * @param string|null $template_type Optional. Query arg 'template_type'. Default is null.
737 - *
738 - * @return string A URL for creating new post using Elementor.
739 - */
740 - public static function get_create_new_post_url( $post_type = 'page', $template_type = null ) {
741 - $query_args = [
742 - 'action' => 'elementor_new_post',
743 - 'post_type' => $post_type,
744 - ];
745 -
746 - if ( $template_type ) {
747 - $query_args['template_type'] = $template_type;
748 - }
749 -
750 - $new_post_url = add_query_arg( $query_args, admin_url( 'edit.php' ) );
751 -
752 - $new_post_url = add_query_arg( '_wpnonce', wp_create_nonce( 'elementor_action_new_post' ), $new_post_url );
753 -
754 - return $new_post_url;
755 - }
756 -
757 - private function get_doc_type_by_id( $post_id ) {
758 - // Auto-save inherits from the original post.
759 - if ( wp_is_post_autosave( $post_id ) ) {
760 - $post_id = wp_get_post_parent_id( $post_id );
761 - }
762 -
763 - // Content built with Elementor.
764 - $template_type = get_post_meta( $post_id, Document::TYPE_META_KEY, true );
765 -
766 - if ( $template_type && isset( $this->types[ $template_type ] ) ) {
767 - return $template_type;
768 - }
769 -
770 - // Elementor installation on a site with existing content (which doesn't contain Elementor's meta).
771 - $post_type = get_post_type( $post_id );
772 -
773 - return $this->cpt[ $post_type ] ?? 'post';
774 - }
775 -
776 - public function register_rest_routes() {
777 - register_rest_route('elementor/v1/documents', '/(?P<id>\d+)/media/import', [
778 - 'methods' => \WP_REST_Server::CREATABLE,
779 - 'callback' => function( $request ) {
780 - $post_id = $request->get_param( 'id' );
781 -
782 - try {
783 - $document = $this->get_with_permissions( $post_id );
784 -
785 - $elements_data = $document->get_elements_data();
786 -
787 - $import_data = $document->get_import_data( [
788 - 'content' => $elements_data,
789 - ] );
790 -
791 - $document->save( [
792 - 'elements' => $import_data['content'],
793 - ] );
794 -
795 - return new \WP_REST_Response( [
796 - 'success' => true,
797 - 'document_saved' => true,
798 - ], 200 );
799 -
800 - } catch ( \Exception $e ) {
801 - return new \WP_Error(
802 - 'elementor_import_error',
803 - $e->getMessage(),
804 - [ 'status' => 500 ]
805 - );
806 - }
807 - },
808 - 'permission_callback' => function() {
809 - return current_user_can( 'manage_options' );
810 - },
811 - 'args' => [
812 - 'id' => [
813 - 'required' => true,
814 - 'validate_callback' => function( $param ) {
815 - return is_numeric( $param );
816 - },
817 - ],
818 - ],
819 - ]);
820 695 }
821 696 }