| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Mcp\Abilities; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Get_Structure_Ability extends Abstract_Ability { |
| 12 |
|
| 13 |
protected function get_ability_id(): string { |
| 14 |
return 'elementor/get-page-structure'; |
| 15 |
} |
| 16 |
|
| 17 |
protected function get_definition(): Ability_Definition { |
| 18 |
return new Ability_Definition( |
| 19 |
__( 'Get Elementor Page Structure', 'elementor' ), |
| 20 |
__( 'Returns the Elementor element tree (widgets, containers, and nested content) for a single post or page ID. Use after list-pages when you need the live JSON structure to reason about layout, widget types, or to plan edits. Only works for posts that were saved with Elementor.', 'elementor' ), |
| 21 |
'elementor', |
| 22 |
[ |
| 23 |
'type' => 'object', |
| 24 |
'properties' => [ |
| 25 |
'elements' => [ |
| 26 |
'type' => 'array', |
| 27 |
'description' => 'Root-level Elementor elements for the document.', |
| 28 |
], |
| 29 |
], |
| 30 |
], |
| 31 |
[ |
| 32 |
'annotations' => [ |
| 33 |
'readonly' => true, |
| 34 |
'idempotent' => true, |
| 35 |
'destructive' => false, |
| 36 |
], |
| 37 |
], |
| 38 |
function () { |
| 39 |
return current_user_can( 'edit_posts' ); |
| 40 |
}, |
| 41 |
[ |
| 42 |
'type' => 'object', |
| 43 |
'required' => [ 'post_id' ], |
| 44 |
'properties' => [ |
| 45 |
'post_id' => [ |
| 46 |
'type' => 'integer', |
| 47 |
'description' => 'WordPress post ID of the Elementor document.', |
| 48 |
], |
| 49 |
], |
| 50 |
] |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
public function execute( $input = [] ) { |
| 55 |
$post_id = $this->resolve_post_id( $input ); |
| 56 |
if ( is_wp_error( $post_id ) ) { |
| 57 |
return $post_id; |
| 58 |
} |
| 59 |
|
| 60 |
$document = $this->get_editable_document( $post_id ); |
| 61 |
if ( is_wp_error( $document ) ) { |
| 62 |
return $document; |
| 63 |
} |
| 64 |
|
| 65 |
$elements = $document->get_elements_data(); |
| 66 |
|
| 67 |
return [ |
| 68 |
'elements' => is_array( $elements ) ? $elements : [], |
| 69 |
]; |
| 70 |
} |
| 71 |
|
| 72 |
private function resolve_post_id( $input ) { |
| 73 |
$post_id = isset( $input['post_id'] ) ? absint( $input['post_id'] ) : 0; |
| 74 |
|
| 75 |
if ( ! $post_id ) { |
| 76 |
return new \WP_Error( |
| 77 |
'invalid_post_id', |
| 78 |
__( 'A valid post_id is required.', 'elementor' ), |
| 79 |
[ 'status' => \WP_Http::BAD_REQUEST ] |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 84 |
return new \WP_Error( |
| 85 |
'rest_cannot_view', |
| 86 |
__( 'Sorry, you are not allowed to access this document.', 'elementor' ), |
| 87 |
[ 'status' => \WP_Http::FORBIDDEN ] |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
return $post_id; |
| 92 |
} |
| 93 |
|
| 94 |
private function get_editable_document( int $post_id ) { |
| 95 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 96 |
|
| 97 |
if ( ! $document ) { |
| 98 |
return new \WP_Error( |
| 99 |
'document_not_found', |
| 100 |
__( 'Document not found.', 'elementor' ), |
| 101 |
[ 'status' => \WP_Http::NOT_FOUND ] |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! $document->is_built_with_elementor() ) { |
| 106 |
return new \WP_Error( |
| 107 |
'not_elementor', |
| 108 |
__( 'This post is not built with Elementor.', 'elementor' ), |
| 109 |
[ 'status' => \WP_Http::BAD_REQUEST ] |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! $document->is_editable_by_current_user() ) { |
| 114 |
return new \WP_Error( |
| 115 |
'rest_cannot_view', |
| 116 |
__( 'Sorry, you are not allowed to edit this document.', 'elementor' ), |
| 117 |
[ 'status' => \WP_Http::FORBIDDEN ] |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
return $document; |
| 122 |
} |
| 123 |
} |
| 124 |
|