| @@ -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; |
| @@ -208,12 +207,14 @@ | ||
| 208 | 207 | |
| 209 | 208 | /** |
| 210 | 209 | * Retrieve a document after checking it exist and allowed to edit. |
| 211 | 210 | * |
| 212 | - * @param string $id | |
| 211 | + * @since 3.13.0 | |
| 212 | + * | |
| 213 | + * @param int $post_id The post ID of the document. | |
| 214 | + * | |
| 213 | 215 | * @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 | + * @throws \Exception | |
| 216 | 217 | */ |
| 217 | 218 | public function get_with_permissions( $id ): Document { |
| 218 | 219 | $document = $this->get( $id ); |
| 219 | 220 | |
| @@ -230,11 +231,11 @@ | ||
| 230 | 231 | |
| 231 | 232 | /** |
| 232 | 233 | * A `void` version for `get_with_permissions`. |
| 233 | 234 | * |
| 234 | - * @param string $id | |
| 235 | + * @param $id | |
| 235 | 236 | * @return void |
| 236 | - * @throws \Exception If the document is not found or the current user is not allowed to edit it. | |
| 237 | + * @throws \Exception | |
| 237 | 238 | */ |
| 238 | 239 | public function check_permissions( $id ) { |
| 239 | 240 | $this->get_with_permissions( $id ); |
| 240 | 241 | } |
| @@ -274,9 +275,9 @@ | ||
| 274 | 275 | * @return false|Document The document if it exist, False otherwise. |
| 275 | 276 | */ |
| 276 | 277 | public function get_doc_for_frontend( $post_id ) { |
| 277 | 278 | $preview_id = (int) Utils::get_super_global_value( $_GET, 'preview_id' ); |
| 278 | - $is_preview = is_preview(); | |
| 279 | + $is_preview = is_preview() && $post_id === $preview_id; | |
| 279 | 280 | $is_nonce_verify = wp_verify_nonce( Utils::get_super_global_value( $_GET, 'preview_nonce' ), 'post_preview_' . $preview_id ); |
| 280 | 281 | |
| 281 | 282 | if ( ( $is_preview && $is_nonce_verify ) || Plugin::$instance->preview->is_preview_mode() ) { |
| 282 | 283 | $document = $this->get_doc_or_auto_save( $post_id, get_current_user_id() ); |
| @@ -322,10 +323,10 @@ | ||
| 322 | 323 | * |
| 323 | 324 | * @since 2.0.0 |
| 324 | 325 | * @access public |
| 325 | 326 | * |
| 326 | - * @param array $args Optional. An array of key => value arguments to match against | |
| 327 | - * the properties. Default is empty array. | |
| 327 | + * @param array $args Optional. An array of key => value arguments to match against | |
| 328 | + * the properties. Default is empty array. | |
| 328 | 329 | * @param string $operator Optional. The logical operation to perform. 'or' means only one |
| 329 | 330 | * element from the array needs to match; 'and' means all elements |
| 330 | 331 | * must match; 'not' means no elements may match. Default 'and'. |
| 331 | 332 | * |
| @@ -478,9 +479,9 @@ | ||
| 478 | 479 | * Filter Post Row Actions. |
| 479 | 480 | * |
| 480 | 481 | * Let the Document to filter the array of row action links on the Posts list table. |
| 481 | 482 | * |
| 482 | - * @param array $actions | |
| 483 | + * @param array $actions | |
| 483 | 484 | * @param \WP_Post $post |
| 484 | 485 | * |
| 485 | 486 | * @return array |
| 486 | 487 | */ |
| @@ -501,9 +502,9 @@ | ||
| 501 | 502 | * |
| 502 | 503 | * @since 2.0.0 |
| 503 | 504 | * @access public |
| 504 | 505 | * |
| 505 | - * @param array $request Post ID. | |
| 506 | + * @param $request Post ID. | |
| 506 | 507 | * |
| 507 | 508 | * @throws \Exception If current user don't have permissions to edit the post or the post is not using Elementor. |
| 508 | 509 | * |
| 509 | 510 | * @return array The document data after saving. |
| @@ -593,15 +594,16 @@ | ||
| 593 | 594 | * Ajax discard changes. |
| 594 | 595 | * |
| 595 | 596 | * Load the document data from an autosave, deleting unsaved changes. |
| 596 | 597 | * |
| 597 | - * @param array $request | |
| 598 | + * @param $request | |
| 598 | 599 | * |
| 599 | 600 | * @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 | + * @throws \Exception | |
| 601 | 602 | * |
| 602 | 603 | * @since 2.0.0 |
| 603 | 604 | * @access public |
| 605 | + * | |
| 604 | 606 | */ |
| 605 | 607 | public function ajax_discard_changes( $request ) { |
| 606 | 608 | $document = $this->get_with_permissions( $request['editor_post_id'] ); |
| 607 | 609 | |
| @@ -731,9 +733,9 @@ | ||
| 731 | 733 | * Get create new post URL. |
| 732 | 734 | * |
| 733 | 735 | * Retrieve a custom URL for creating a new post/page using Elementor. |
| 734 | 736 | * |
| 735 | - * @param string $post_type Optional. Post type slug. Default is 'page'. | |
| 737 | + * @param string $post_type Optional. Post type slug. Default is 'page'. | |
| 736 | 738 | * @param string|null $template_type Optional. Query arg 'template_type'. Default is null. |
| 737 | 739 | * |
| 738 | 740 | * @return string A URL for creating new post using Elementor. |
| 739 | 741 | */ |
| @@ -770,52 +772,6 @@ | ||
| 770 | 772 | // Elementor installation on a site with existing content (which doesn't contain Elementor's meta). |
| 771 | 773 | $post_type = get_post_type( $post_id ); |
| 772 | 774 | |
| 773 | 775 | 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 | 776 | } |
| 821 | 777 | } |