PluginProbe
Elementor Website Builder – more than just a page builder / 3.5.6
Elementor Website Builder – more than just a page builder v3.5.6
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 +41 -140 4.2.43.5.6 View file →
@@ -9,9 +9,9 @@
9 9 use Elementor\TemplateLibrary\Source_Local;
10 10 use Elementor\Utils;
11 11
12 12 if ( ! defined( 'ABSPATH' ) ) {
13 - exit; // Exit if accessed directly.
13 + exit; // Exit if accessed directly
14 14 }
15 15
16 16 /**
17 17 * Elementor documents manager.
@@ -87,9 +87,8 @@
87 87 add_filter( 'post_row_actions', [ $this, 'filter_post_row_actions' ], 11, 2 );
88 88 add_filter( 'page_row_actions', [ $this, 'filter_post_row_actions' ], 11, 2 );
89 89 add_filter( 'user_has_cap', [ $this, 'remove_user_edit_cap' ], 10, 3 );
90 90 add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] );
91 - add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
92 91 }
93 92
94 93 /**
95 94 * Register ajax actions.
@@ -136,18 +135,18 @@
136 135 *
137 136 * @since 2.0.0
138 137 * @access public
139 138 *
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.
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.
143 142 *
144 143 * @return Documents_Manager The updated document manager instance.
145 144 */
146 - public function register_document_type( $type, $class_name ) {
147 - $this->types[ $type ] = $class_name;
145 + public function register_document_type( $type, $class ) {
146 + $this->types[ $type ] = $class;
148 147
149 - $cpt = $class_name::get_property( 'cpt' );
148 + $cpt = $class::get_property( 'cpt' );
150 149
151 150 if ( $cpt ) {
152 151 foreach ( $cpt as $post_type ) {
153 152 $this->cpt[ $post_type ] = $type;
@@ -153,9 +152,9 @@
153 152 $this->cpt[ $post_type ] = $type;
154 153 }
155 154 }
156 155
157 - if ( $class_name::get_property( 'register_type' ) ) {
156 + if ( $class::get_property( 'register_type' ) ) {
158 157 Source_Local::add_template_type( $type );
159 158 }
160 159
161 160 return $this;
@@ -194,11 +193,28 @@
194 193 */
195 194 $post_id = apply_filters( 'elementor/documents/get/post_id', $post_id );
196 195
197 196 if ( ! $from_cache || ! isset( $this->documents[ $post_id ] ) ) {
198 - $doc_type = $this->get_doc_type_by_id( $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 +
199 216 $doc_type_class = $this->get_document_type( $doc_type );
200 -
201 217 $this->documents[ $post_id ] = new $doc_type_class( [
202 218 'post_id' => $post_id,
203 219 ] );
204 220 }
@@ -206,41 +222,8 @@
206 222 return $this->documents[ $post_id ];
207 223 }
208 224
209 225 /**
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 226 * Get document or autosave.
244 227 *
245 228 * Retrieve either the document or the autosave.
246 229 *
@@ -273,13 +256,9 @@
273 256 *
274 257 * @return false|Document The document if it exist, False otherwise.
275 258 */
276 259 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() ) {
260 + if ( is_preview() || Plugin::$instance->preview->is_preview_mode() ) {
282 261 $document = $this->get_doc_or_auto_save( $post_id, get_current_user_id() );
283 262 } else {
284 263 $document = $this->get( $post_id );
285 264 }
@@ -322,10 +301,10 @@
322 301 *
323 302 * @since 2.0.0
324 303 * @access public
325 304 *
326 - * @param array $args Optional. An array of key => value arguments to match against
327 - * the properties. Default is empty array.
305 + * @param array $args Optional. An array of key => value arguments to match against
306 + * the properties. Default is empty array.
328 307 * @param string $operator Optional. The logical operation to perform. 'or' means only one
329 308 * element from the array needs to match; 'and' means all elements
330 309 * must match; 'not' means no elements may match. Default 'and'.
331 310 *
@@ -478,9 +457,9 @@
478 457 * Filter Post Row Actions.
479 458 *
480 459 * Let the Document to filter the array of row action links on the Posts list table.
481 460 *
482 - * @param array $actions
461 + * @param array $actions
483 462 * @param \WP_Post $post
484 463 *
485 464 * @return array
486 465 */
@@ -501,9 +480,9 @@
501 480 *
502 481 * @since 2.0.0
503 482 * @access public
504 483 *
505 - * @param array $request Post ID.
484 + * @param $request Post ID.
506 485 *
507 486 * @throws \Exception If current user don't have permissions to edit the post or the post is not using Elementor.
508 487 *
509 488 * @return array The document data after saving.
@@ -546,16 +525,13 @@
546 525 ];
547 526
548 527 $document->save( $data );
549 528
550 - $post = $document->get_post();
551 - $main_post = $document->get_main_post();
552 -
553 529 // Refresh after save.
554 - $document = $this->get( $post->ID, false );
530 + $document = $this->get( $document->get_post()->ID, false );
555 531
556 532 $return_data = [
557 - 'status' => $post->post_status,
533 + 'status' => $document->get_post()->post_status,
558 534 'config' => [
559 535 'document' => [
560 536 'last_edited' => $document->get_last_edited(),
561 537 'urls' => [
@@ -564,17 +540,8 @@
564 540 ],
565 541 ],
566 542 ];
567 543
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 544 /**
578 545 * Returned documents ajax saved data.
579 546 *
580 547 * Filters the ajax data returned when saving the post on the builder.
@@ -593,18 +560,17 @@
593 560 * Ajax discard changes.
594 561 *
595 562 * Load the document data from an autosave, deleting unsaved changes.
596 563 *
597 - * @param array $request
564 + * @since 2.0.0
565 + * @access public
598 566 *
567 + * @param $request
568 + *
599 569 * @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 570 */
605 571 public function ajax_discard_changes( $request ) {
606 - $document = $this->get_with_permissions( $request['editor_post_id'] );
572 + $document = $this->get( $request['editor_post_id'] );
607 573
608 574 $autosave = $document->get_autosave();
609 575
610 576 if ( $autosave ) {
@@ -623,9 +589,9 @@
623 589
624 590 $document = $this->get_doc_or_auto_save( $post_id );
625 591
626 592 if ( ! $document ) {
627 - throw new \Exception( 'Not found.' );
593 + throw new \Exception( 'Not Found.' );
628 594 }
629 595
630 596 if ( ! $document->is_editable_by_current_user() ) {
631 597 throw new \Exception( 'Access denied.' );
@@ -731,9 +697,9 @@
731 697 * Get create new post URL.
732 698 *
733 699 * Retrieve a custom URL for creating a new post/page using Elementor.
734 700 *
735 - * @param string $post_type Optional. Post type slug. Default is 'page'.
701 + * @param string $post_type Optional. Post type slug. Default is 'page'.
736 702 * @param string|null $template_type Optional. Query arg 'template_type'. Default is null.
737 703 *
738 704 * @return string A URL for creating new post using Elementor.
739 705 */
@@ -751,71 +717,6 @@
751 717
752 718 $new_post_url = add_query_arg( '_wpnonce', wp_create_nonce( 'elementor_action_new_post' ), $new_post_url );
753 719
754 720 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 721 }
821 722 }