<?php
namespace Elementor\Modules\Mcp\Abilities;
use Elementor\Modules\History\Revisions_Manager;
use Elementor\Modules\Mcp\Abilities\Utils\Document_Mutation_Links;
use Elementor\Modules\Mcp\Abilities\Utils\Document_Revision_Snapshots;
use Elementor\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Publish_Document_Ability extends Abstract_Ability {
const PROMOTED_POST_FIELDS = [ Update_Settings_Ability::TITLE_SETTING, Update_Settings_Ability::EXCERPT_SETTING ];
protected function get_ability_id(): string {
return 'elementor/publish-document';
}
public function is_exposed_via_proxy(): bool {
return false;
}
protected function get_definition(): Ability_Definition {
return new Ability_Definition(
__( 'Publish Elementor Document', 'elementor' ),
__( '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, build-composition or update-page-settings 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' ),
'elementor',
[
'type' => 'object',
'properties' => [
'post_id' => [ 'type' => 'integer' ],
'status' => [ 'type' => 'string' ],
'previous_status' => [ 'type' => 'string' ],
'preview_url' => Document_Mutation_Links::preview_schema_property(),
'llm_instructions' => Document_Mutation_Links::llm_instructions_schema_property(),
],
],
[
'annotations' => [
'readonly' => false,
'idempotent' => true,
'destructive' => true,
],
'mutates_page' => true,
],
function () {
return current_user_can( 'edit_posts' );
},
[
'type' => 'object',
'required' => [ 'post_id' ],
'properties' => [
'post_id' => [
'type' => 'integer',
'description' => 'WordPress post ID of the Elementor document to publish.',
],
],
]
);
}
public function execute( $input = [] ) {
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0;
if ( ! $post_id ) {
return new \WP_Error(
'invalid_post_id',
__( 'A valid post_id is required.', 'elementor' ),
[ 'status' => \WP_Http::BAD_REQUEST ]
);
}
if ( wp_is_post_revision( $post_id ) ) {
return new \WP_Error(
'invalid_post_id',
__( 'Revision IDs cannot be published. Pass the parent document ID.', 'elementor' ),
[ 'status' => \WP_Http::BAD_REQUEST ]
);
}
$document = Plugin::$instance->documents->get( $post_id );
if ( ! $document ) {
return new \WP_Error(
'document_not_found',
__( 'Document not found.', 'elementor' ),
[ 'status' => \WP_Http::NOT_FOUND ]
);
}
$main_id = $document->get_main_id();
if ( ! current_user_can( 'publish_post', $main_id ) ) {
return new \WP_Error(
'rest_cannot_publish',
__( 'Sorry, you are not allowed to publish this document.', 'elementor' ),
[ 'status' => \WP_Http::FORBIDDEN ]
);
}
$previous_status = get_post_status( $main_id );
$this->promote_pending_autosave( $main_id );
if ( 'publish' !== $previous_status ) {
$updated = wp_update_post(
[
'ID' => $main_id,
'post_status' => 'publish',
],
true
);
if ( is_wp_error( $updated ) ) {
return $updated;
}
}
return $this->build_success_response( $document, get_post_status( $main_id ), $previous_status );
}
/**
* Promote the current user's pending autosave (created by MCP element writes on live posts)
* onto the main document, then discard the autosave slot — mirroring what the editor's own
* publish flow does in `Page\Manager::save`.
*
* Reuses `Revisions_Manager::restore_revision` so ALL `_elementor*` meta (data, page settings,
* etc.) is copied consistently, the element cache is invalidated and Post CSS is regenerated.
* That copy skips keys the autosave lacks, so the restorable meta (page settings, page template)
* is synced first, which removes live values a staged restore cleared. The copy also skips post
* fields, so the title, excerpt and featured image that update-page-settings staged are promoted
* separately, and only those, so live changes made since the autosave was created are kept. Running before the
* `wp_update_post` status transition means listeners on `transition_post_status` (Post CSS,
* CDN purges, sitemaps, etc.) see the promoted content on the very same request.
*/
private function promote_pending_autosave( int $main_id ): void {
$autosave_post = wp_get_post_autosave( $main_id, get_current_user_id() );
if ( ! $autosave_post ) {
return;
}
Document_Revision_Snapshots::restore_meta( $autosave_post->ID, $main_id );
$this->promote_staged_post_fields( $autosave_post, $main_id );
Revisions_Manager::restore_revision( $main_id, $autosave_post->ID );
wp_delete_post_revision( $autosave_post->ID );
}
private function promote_staged_post_fields( \WP_Post $autosave_post, int $main_id ): void {
$staged_fields = (array) get_metadata( 'post', $autosave_post->ID, Update_Settings_Ability::STAGED_POST_FIELDS_META_KEY, true );
if ( in_array( Update_Settings_Ability::FEATURED_IMAGE_SETTING, $staged_fields, true ) ) {
$this->promote_featured_image( $autosave_post->ID, $main_id );
}
$main_post = get_post( $main_id );
$changed_fields = [];
foreach ( self::PROMOTED_POST_FIELDS as $field ) {
if ( in_array( $field, $staged_fields, true ) && $autosave_post->$field !== $main_post->$field ) {
$changed_fields[ $field ] = $autosave_post->$field;
}
}
if ( ! $changed_fields ) {
return;
}
wp_update_post( wp_slash( [ 'ID' => $main_id ] + $changed_fields ) );
}
private function promote_featured_image( int $autosave_id, int $main_id ): void {
$attachment_id = get_metadata( 'post', $autosave_id, Update_Settings_Ability::FEATURED_IMAGE_META_KEY, true );
if ( $attachment_id ) {
update_metadata( 'post', $main_id, Update_Settings_Ability::FEATURED_IMAGE_META_KEY, $attachment_id );
return;
}
delete_metadata( 'post', $main_id, Update_Settings_Ability::FEATURED_IMAGE_META_KEY );
}
private function build_success_response( $document, string $status, string $previous_status ): array {
return [
'post_id' => (int) $document->get_main_id(),
'status' => $status,
'previous_status' => $previous_status,
] + Document_Mutation_Links::for_document(
$document,
__( 'Document published.', 'elementor' )
);
}
}