| 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 Update_Settings_Ability extends Abstract_Ability { |
| 13 |
|
| 14 |
protected function get_ability_id(): string { |
| 15 |
return 'elementor/update-page-settings'; |
| 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 |
__( 'Update Elementor Page Settings', 'elementor' ), |
| 25 |
__( 'Updates Elementor document-level settings for a post (for example page layout, title visibility, or custom page settings). Pass only the keys you want to change. Ask the user for the URL or post ID. Use get-page-structure when you also need the element tree. Requires permission to edit the target post.', 'elementor' ), |
| 26 |
'elementor', |
| 27 |
[ |
| 28 |
'type' => 'object', |
| 29 |
'properties' => [ |
| 30 |
'success' => [ 'type' => 'boolean' ], |
| 31 |
'post_id' => [ 'type' => 'integer' ], |
| 32 |
'preview_url' => Document_Mutation_Links::preview_schema_property(), |
| 33 |
'llm_instructions' => Document_Mutation_Links::llm_instructions_schema_property(), |
| 34 |
], |
| 35 |
], |
| 36 |
[ |
| 37 |
'annotations' => [ |
| 38 |
'readonly' => false, |
| 39 |
'idempotent' => false, |
| 40 |
'destructive' => true, |
| 41 |
], |
| 42 |
], |
| 43 |
function () { |
| 44 |
return current_user_can( 'edit_posts' ); |
| 45 |
}, |
| 46 |
[ |
| 47 |
'type' => 'object', |
| 48 |
'required' => [ 'post_id', 'settings' ], |
| 49 |
'properties' => [ |
| 50 |
'post_id' => [ |
| 51 |
'type' => 'integer', |
| 52 |
'description' => 'WordPress post ID of the Elementor document.', |
| 53 |
], |
| 54 |
'settings' => [ |
| 55 |
'type' => 'object', |
| 56 |
'description' => 'Partial document settings object; merged into existing settings. Schema enforcement is delegated to document->save().', |
| 57 |
'additionalProperties' => true, |
| 58 |
], |
| 59 |
], |
| 60 |
] |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
public function execute( $input = [] ) { |
| 65 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 66 |
$settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : null; |
| 67 |
|
| 68 |
if ( ! $post_id ) { |
| 69 |
return new \WP_Error( 'invalid_post_id', __( 'A valid post_id is required.', 'elementor' ), [ 'status' => \WP_Http::BAD_REQUEST ] ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( null === $settings ) { |
| 73 |
return new \WP_Error( 'invalid_settings', __( 'The settings object is required.', 'elementor' ), [ 'status' => \WP_Http::BAD_REQUEST ] ); |
| 74 |
} |
| 75 |
|
| 76 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 77 |
|
| 78 |
if ( ! $document ) { |
| 79 |
return new \WP_Error( 'document_not_found', __( 'Document not found.', 'elementor' ), [ 'status' => \WP_Http::NOT_FOUND ] ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! $document->is_editable_by_current_user() ) { |
| 83 |
return new \WP_Error( 'rest_cannot_edit', __( 'Sorry, you are not allowed to edit this document.', 'elementor' ), [ 'status' => \WP_Http::FORBIDDEN ] ); |
| 84 |
} |
| 85 |
|
| 86 |
$saved = $document->save( [ 'settings' => $settings ] ); |
| 87 |
|
| 88 |
if ( ! $saved ) { |
| 89 |
return new \WP_Error( 'save_failed', __( 'Could not save document settings.', 'elementor' ), [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] ); |
| 90 |
} |
| 91 |
|
| 92 |
return [ |
| 93 |
'success' => true, |
| 94 |
'post_id' => $post_id, |
| 95 |
] + Document_Mutation_Links::for_document( $document ); |
| 96 |
} |
| 97 |
} |
| 98 |
|