| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities; |
| 4 |
|
| 5 |
use Elementor\Modules\Mcp\Abilities\Utils\Document_Mutation_Links; |
| 6 |
use Elementor\Plugin; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class Publish_Document_Ability extends Abstract_Ability { |
| 13 |
|
| 14 |
protected function get_ability_id(): string { |
| 15 |
return 'elementor/publish-document'; |
| 16 |
} |
| 17 |
|
| 18 |
public function is_exposed_via_proxy(): bool { |
| 19 |
return false; |
| 20 |
} |
| 21 |
|
| 22 |
protected function get_definition(): Ability_Definition { |
| 23 |
return new Ability_Definition( |
| 24 |
__( '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. Edits via manage-elements or build-composition on published pages are staged in an autosave and do not go live until you publish. Idempotent: publishing an already-published document is a no-op success.', 'elementor' ), |
| 26 |
'elementor', |
| 27 |
[ |
| 28 |
'type' => 'object', |
| 29 |
'properties' => [ |
| 30 |
'post_id' => [ 'type' => 'integer' ], |
| 31 |
'status' => [ 'type' => 'string' ], |
| 32 |
'previous_status' => [ 'type' => 'string' ], |
| 33 |
'preview_url' => Document_Mutation_Links::preview_schema_property(), |
| 34 |
'llm_instructions' => Document_Mutation_Links::llm_instructions_schema_property(), |
| 35 |
], |
| 36 |
], |
| 37 |
[ |
| 38 |
'annotations' => [ |
| 39 |
'readonly' => false, |
| 40 |
'idempotent' => true, |
| 41 |
'destructive' => true, |
| 42 |
], |
| 43 |
], |
| 44 |
function () { |
| 45 |
return current_user_can( 'edit_posts' ); |
| 46 |
}, |
| 47 |
[ |
| 48 |
'type' => 'object', |
| 49 |
'required' => [ 'post_id' ], |
| 50 |
'properties' => [ |
| 51 |
'post_id' => [ |
| 52 |
'type' => 'integer', |
| 53 |
'description' => 'WordPress post ID of the Elementor document to publish.', |
| 54 |
], |
| 55 |
], |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function execute( $input = [] ) { |
| 61 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 62 |
|
| 63 |
if ( ! $post_id ) { |
| 64 |
return new \WP_Error( |
| 65 |
'invalid_post_id', |
| 66 |
__( 'A valid post_id is required.', 'elementor' ), |
| 67 |
[ 'status' => \WP_Http::BAD_REQUEST ] |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
if ( wp_is_post_revision( $post_id ) ) { |
| 72 |
return new \WP_Error( |
| 73 |
'invalid_post_id', |
| 74 |
__( 'Revision IDs cannot be published. Pass the parent document ID.', 'elementor' ), |
| 75 |
[ 'status' => \WP_Http::BAD_REQUEST ] |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 80 |
|
| 81 |
if ( ! $document ) { |
| 82 |
return new \WP_Error( |
| 83 |
'document_not_found', |
| 84 |
__( 'Document not found.', 'elementor' ), |
| 85 |
[ 'status' => \WP_Http::NOT_FOUND ] |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
$main_id = $document->get_main_id(); |
| 90 |
|
| 91 |
if ( ! current_user_can( 'publish_post', $main_id ) ) { |
| 92 |
return new \WP_Error( |
| 93 |
'rest_cannot_publish', |
| 94 |
__( 'Sorry, you are not allowed to publish this document.', 'elementor' ), |
| 95 |
[ 'status' => \WP_Http::FORBIDDEN ] |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
$previous_status = get_post_status( $main_id ); |
| 100 |
|
| 101 |
if ( 'publish' === $previous_status ) { |
| 102 |
return $this->build_success_response( $document, $previous_status, $previous_status ); |
| 103 |
} |
| 104 |
|
| 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 |
} |
| 116 |
|
| 117 |
return $this->build_success_response( $document, get_post_status( $main_id ), $previous_status ); |
| 118 |
} |
| 119 |
|
| 120 |
private function build_success_response( $document, string $status, string $previous_status ): array { |
| 121 |
return [ |
| 122 |
'post_id' => (int) $document->get_main_id(), |
| 123 |
'status' => $status, |
| 124 |
'previous_status' => $previous_status, |
| 125 |
] + Document_Mutation_Links::for_document( |
| 126 |
$document, |
| 127 |
__( 'Document published.', 'elementor' ) |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|