| @@ -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; |
| @@ -182,23 +181,31 @@ | ||
| 182 | 181 | if ( ! $post_id || ! get_post( $post_id ) ) { |
| 183 | 182 | return false; |
| 184 | 183 | } |
| 185 | 184 | |
| 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 | 185 | $post_id = apply_filters( 'elementor/documents/get/post_id', $post_id ); |
| 196 | 186 | |
| 197 | 187 | if ( ! $from_cache || ! isset( $this->documents[ $post_id ] ) ) { |
| 198 | - $doc_type = $this->get_doc_type_by_id( $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 | + | |
| 199 | 207 | $doc_type_class = $this->get_document_type( $doc_type ); |
| 200 | - | |
| 201 | 208 | $this->documents[ $post_id ] = new $doc_type_class( [ |
| 202 | 209 | 'post_id' => $post_id, |
| 203 | 210 | ] ); |
| 204 | 211 | } |
| @@ -206,41 +213,8 @@ | ||
| 206 | 213 | return $this->documents[ $post_id ]; |
| 207 | 214 | } |
| 208 | 215 | |
| 209 | 216 | /** |
| 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 | 217 | * Get document or autosave. |
| 244 | 218 | * |
| 245 | 219 | * Retrieve either the document or the autosave. |
| 246 | 220 | * |
| @@ -273,13 +247,9 @@ | ||
| 273 | 247 | * |
| 274 | 248 | * @return false|Document The document if it exist, False otherwise. |
| 275 | 249 | */ |
| 276 | 250 | 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() ) { | |
| 251 | + if ( is_preview() || Plugin::$instance->preview->is_preview_mode() ) { | |
| 282 | 252 | $document = $this->get_doc_or_auto_save( $post_id, get_current_user_id() ); |
| 283 | 253 | } else { |
| 284 | 254 | $document = $this->get( $post_id ); |
| 285 | 255 | } |
| @@ -322,10 +292,10 @@ | ||
| 322 | 292 | * |
| 323 | 293 | * @since 2.0.0 |
| 324 | 294 | * @access public |
| 325 | 295 | * |
| 326 | - * @param array $args Optional. An array of key => value arguments to match against | |
| 327 | - * the properties. Default is empty array. | |
| 296 | + * @param array $args Optional. An array of key => value arguments to match against | |
| 297 | + * the properties. Default is empty array. | |
| 328 | 298 | * @param string $operator Optional. The logical operation to perform. 'or' means only one |
| 329 | 299 | * element from the array needs to match; 'and' means all elements |
| 330 | 300 | * must match; 'not' means no elements may match. Default 'and'. |
| 331 | 301 | * |
| @@ -380,12 +350,12 @@ | ||
| 380 | 350 | return new \WP_Error( 500, sprintf( 'Type %s does not exist.', $type ) ); |
| 381 | 351 | } |
| 382 | 352 | |
| 383 | 353 | if ( empty( $post_data['post_title'] ) ) { |
| 384 | - $post_data['post_title'] = esc_html__( 'Elementor', 'elementor' ); | |
| 354 | + $post_data['post_title'] = __( 'Elementor', 'elementor' ); | |
| 385 | 355 | if ( 'post' !== $type ) { |
| 386 | 356 | $post_data['post_title'] = sprintf( |
| 387 | - /* translators: %s: Document title. */ | |
| 357 | + /* translators: %s: Document title */ | |
| 388 | 358 | __( 'Elementor %s', 'elementor' ), |
| 389 | 359 | call_user_func( [ $class, 'get_title' ] ) |
| 390 | 360 | ); |
| 391 | 361 | } |
| @@ -398,14 +368,8 @@ | ||
| 398 | 368 | $meta_data[ Document::TYPE_META_KEY ] = $type; |
| 399 | 369 | |
| 400 | 370 | $post_data['meta_input'] = $meta_data; |
| 401 | 371 | |
| 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 | 372 | $post_id = wp_insert_post( $post_data ); |
| 409 | 373 | |
| 410 | 374 | if ( ! empty( $update_title ) ) { |
| 411 | 375 | $post_data['ID'] = $post_id; |
| @@ -478,9 +442,9 @@ | ||
| 478 | 442 | * Filter Post Row Actions. |
| 479 | 443 | * |
| 480 | 444 | * Let the Document to filter the array of row action links on the Posts list table. |
| 481 | 445 | * |
| 482 | - * @param array $actions | |
| 446 | + * @param array $actions | |
| 483 | 447 | * @param \WP_Post $post |
| 484 | 448 | * |
| 485 | 449 | * @return array |
| 486 | 450 | */ |
| @@ -501,9 +465,9 @@ | ||
| 501 | 465 | * |
| 502 | 466 | * @since 2.0.0 |
| 503 | 467 | * @access public |
| 504 | 468 | * |
| 505 | - * @param array $request Post ID. | |
| 469 | + * @param $request Post ID. | |
| 506 | 470 | * |
| 507 | 471 | * @throws \Exception If current user don't have permissions to edit the post or the post is not using Elementor. |
| 508 | 472 | * |
| 509 | 473 | * @return array The document data after saving. |
| @@ -546,16 +510,13 @@ | ||
| 546 | 510 | ]; |
| 547 | 511 | |
| 548 | 512 | $document->save( $data ); |
| 549 | 513 | |
| 550 | - $post = $document->get_post(); | |
| 551 | - $main_post = $document->get_main_post(); | |
| 552 | - | |
| 553 | 514 | // Refresh after save. |
| 554 | - $document = $this->get( $post->ID, false ); | |
| 515 | + $document = $this->get( $document->get_post()->ID, false ); | |
| 555 | 516 | |
| 556 | 517 | $return_data = [ |
| 557 | - 'status' => $post->post_status, | |
| 518 | + 'status' => $document->get_post()->post_status, | |
| 558 | 519 | 'config' => [ |
| 559 | 520 | 'document' => [ |
| 560 | 521 | 'last_edited' => $document->get_last_edited(), |
| 561 | 522 | 'urls' => [ |
| @@ -564,17 +525,8 @@ | ||
| 564 | 525 | ], |
| 565 | 526 | ], |
| 566 | 527 | ]; |
| 567 | 528 | |
| 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 | 529 | /** |
| 578 | 530 | * Returned documents ajax saved data. |
| 579 | 531 | * |
| 580 | 532 | * Filters the ajax data returned when saving the post on the builder. |
| @@ -593,18 +545,17 @@ | ||
| 593 | 545 | * Ajax discard changes. |
| 594 | 546 | * |
| 595 | 547 | * Load the document data from an autosave, deleting unsaved changes. |
| 596 | 548 | * |
| 597 | - * @param array $request | |
| 549 | + * @since 2.0.0 | |
| 550 | + * @access public | |
| 598 | 551 | * |
| 552 | + * @param $request | |
| 553 | + * | |
| 599 | 554 | * @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 | 555 | */ |
| 605 | 556 | public function ajax_discard_changes( $request ) { |
| 606 | - $document = $this->get_with_permissions( $request['editor_post_id'] ); | |
| 557 | + $document = $this->get( $request['editor_post_id'] ); | |
| 607 | 558 | |
| 608 | 559 | $autosave = $document->get_autosave(); |
| 609 | 560 | |
| 610 | 561 | if ( $autosave ) { |
| @@ -623,9 +574,9 @@ | ||
| 623 | 574 | |
| 624 | 575 | $document = $this->get_doc_or_auto_save( $post_id ); |
| 625 | 576 | |
| 626 | 577 | if ( ! $document ) { |
| 627 | - throw new \Exception( 'Not found.' ); | |
| 578 | + throw new \Exception( 'Not Found.' ); | |
| 628 | 579 | } |
| 629 | 580 | |
| 630 | 581 | if ( ! $document->is_editable_by_current_user() ) { |
| 631 | 582 | throw new \Exception( 'Access denied.' ); |
| @@ -701,8 +652,23 @@ | ||
| 701 | 652 | public function get_current() { |
| 702 | 653 | return $this->current_doc; |
| 703 | 654 | } |
| 704 | 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 | + | |
| 705 | 671 | public function localize_settings( $settings ) { |
| 706 | 672 | $translations = []; |
| 707 | 673 | |
| 708 | 674 | foreach ( $this->get_document_types() as $type => $class ) { |
| @@ -724,98 +690,6 @@ | ||
| 724 | 690 | * @param Documents_Manager $this The document manager instance. |
| 725 | 691 | */ |
| 726 | 692 | do_action( 'elementor/documents/register', $this ); |
| 727 | 693 | } |
| 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 | 694 | } |
| 821 | 695 | } |