← All changes
|
modules/mcp/abilities/publish-document-ability.php
+36
-13
4.3.0-beta1
→
4.3.1
View file →
| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Mcp\Abilities; |
| 4 | 4 | |
| 5 | +use Elementor\Modules\History\Revisions_Manager; | |
| 5 | 6 | use Elementor\Modules\Mcp\Abilities\Utils\Document_Mutation_Links; |
| 6 | 7 | use Elementor\Plugin; |
| 7 | 8 | |
| 8 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -21,9 +22,9 @@ | ||
| 21 | 22 | |
| 22 | 23 | protected function get_definition(): Ability_Definition { |
| 23 | 24 | return new Ability_Definition( |
| 24 | 25 | __( 'Publish Elementor Document', 'elementor' ), |
| 25 | - __( 'Transitions an Elementor document (page, post, theme template, popup) to `publish` so it appears on the front end. Call this AFTER all element edits are complete, because subsequent edits may downgrade the document back to draft. Idempotent: publishing an already-published document is a no-op success.', 'elementor' ), | |
| 26 | + __( 'Transitions an Elementor document (page, post, theme template, popup) to `publish` so it appears on the front end. Call this AFTER all element edits are complete. Edits via manage-elements or build-composition on published pages are staged in an autosave; calling this promotes the staged autosave to the live document. When there are no pending edits on an already-published document, the call is a no-op success.', 'elementor' ), | |
| 26 | 27 | 'elementor', |
| 27 | 28 | [ |
| 28 | 29 | 'type' => 'object', |
| 29 | 30 | 'properties' => [ |
| @@ -97,25 +98,47 @@ | ||
| 97 | 98 | } |
| 98 | 99 | |
| 99 | 100 | $previous_status = get_post_status( $main_id ); |
| 100 | 101 | |
| 101 | - if ( 'publish' === $previous_status ) { | |
| 102 | - return $this->build_success_response( $document, $previous_status, $previous_status ); | |
| 102 | + $this->promote_pending_autosave( $main_id ); | |
| 103 | + | |
| 104 | + if ( 'publish' !== $previous_status ) { | |
| 105 | + $updated = wp_update_post( | |
| 106 | + [ | |
| 107 | + 'ID' => $main_id, | |
| 108 | + 'post_status' => 'publish', | |
| 109 | + ], | |
| 110 | + true | |
| 111 | + ); | |
| 112 | + | |
| 113 | + if ( is_wp_error( $updated ) ) { | |
| 114 | + return $updated; | |
| 115 | + } | |
| 103 | 116 | } |
| 104 | 117 | |
| 105 | - $updated = wp_update_post( | |
| 106 | - [ | |
| 107 | - 'ID' => $main_id, | |
| 108 | - 'post_status' => 'publish', | |
| 109 | - ], | |
| 110 | - true | |
| 111 | - ); | |
| 118 | + return $this->build_success_response( $document, get_post_status( $main_id ), $previous_status ); | |
| 119 | + } | |
| 112 | 120 | |
| 113 | - if ( is_wp_error( $updated ) ) { | |
| 114 | - return $updated; | |
| 121 | + /** | |
| 122 | + * Promote the current user's pending autosave (created by MCP element writes on live posts) | |
| 123 | + * onto the main document, then discard the autosave slot — mirroring what the editor's own | |
| 124 | + * publish flow does in `Page\Manager::save`. | |
| 125 | + * | |
| 126 | + * Reuses `Revisions_Manager::restore_revision` so ALL `_elementor*` meta (data, page settings, | |
| 127 | + * CSS cache, etc.) is copied consistently and Post CSS is regenerated. Running before the | |
| 128 | + * `wp_update_post` status transition means listeners on `transition_post_status` (Post CSS, | |
| 129 | + * CDN purges, sitemaps, etc.) see the promoted content on the very same request. | |
| 130 | + */ | |
| 131 | + private function promote_pending_autosave( int $main_id ): void { | |
| 132 | + $autosave_post = wp_get_post_autosave( $main_id, get_current_user_id() ); | |
| 133 | + | |
| 134 | + if ( ! $autosave_post ) { | |
| 135 | + return; | |
| 115 | 136 | } |
| 116 | 137 | |
| 117 | - return $this->build_success_response( $document, get_post_status( $main_id ), $previous_status ); | |
| 138 | + Revisions_Manager::restore_revision( $main_id, $autosave_post->ID ); | |
| 139 | + | |
| 140 | + wp_delete_post_revision( $autosave_post->ID ); | |
| 118 | 141 | } |
| 119 | 142 | |
| 120 | 143 | private function build_success_response( $document, string $status, string $previous_status ): array { |
| 121 | 144 | return [ |