*/ protected function ability(): array { return array( 'name' => 'elementor/get-element', 'args' => array( 'label' => __( 'Get Elementor Element', 'acrossai-abilities-manager' ), 'description' => __( 'Return the Elementor element at the given 7-character hex ID for a post, plus its parent-ID path from the root.', 'acrossai-abilities-manager' ), 'category' => 'acrossai-elementor', 'execute_callback' => array( $this, 'execute' ), 'permission_callback' => static function (): bool { return current_user_can( 'manage_options' ) && current_user_can( 'edit_posts' ); }, 'input_schema' => array( 'type' => 'object', 'properties' => array( 'post_id' => array( 'type' => 'integer', 'minimum' => 1 ), 'element_id' => array( 'type' => 'string' ), ), 'required' => array( 'post_id', 'element_id' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'success' => array( 'type' => 'boolean' ), 'post_id' => array( 'type' => 'integer' ), 'element_id' => array( 'type' => 'string' ), 'element' => array( 'type' => 'object' ), 'path' => array( 'type' => 'array' ), 'message' => array( 'type' => 'string' ), 'error_code' => array( 'type' => 'string' ), ), 'required' => array( 'success' ), 'additionalProperties' => false, ), 'meta' => array( 'acrossai' => array( 'tab_group' => 'elementor', 'sub_group' => 'elementor-elements', 'sub_group_label' => __( 'Elements & Widgets', 'acrossai-abilities-manager' ), ), 'show_in_rest' => true, 'mcp' => array( 'public' => false, 'type' => 'tool' ), 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ), ), ), ); } /** * @param array $input * @return array */ public function execute( array $input = array() ): array { $check = Document_Repository::assert_elementor_available(); if ( is_wp_error( $check ) ) { return $this->fail( 0, '', (string) $check->get_error_code(), (string) $check->get_error_message() ); } $post_id = absint( $input['post_id'] ?? 0 ); $element_id = isset( $input['element_id'] ) ? (string) $input['element_id'] : ''; if ( ! Document_Repository::is_valid_element_id( $element_id ) ) { return $this->fail( $post_id, $element_id, 'invalid_element_id', __( 'element_id must be a 7-character hex string.', 'acrossai-abilities-manager' ) ); } $doc = Document_Repository::load_document( $post_id, 'read' ); if ( is_wp_error( $doc ) ) { return $this->fail( $post_id, $element_id, (string) $doc->get_error_code(), (string) $doc->get_error_message() ); } $found = Document_Repository::find_element_by_id( $doc['data'], $element_id ); if ( null === $found ) { return $this->fail( $post_id, $element_id, 'element_not_found', __( 'Element not found in this post.', 'acrossai-abilities-manager' ) ); } return array( 'success' => true, 'post_id' => $post_id, 'element_id' => $element_id, 'element' => $found['element'], 'path' => $found['path'], /* translators: 1: element id, 2: post id */ 'message' => sprintf( __( 'Returned element %1$s from post #%2$d.', 'acrossai-abilities-manager' ), $element_id, $post_id ), ); } /** * @param int $post_id * @param string $element_id * @param string $code * @param string $message * @return array */ private function fail( int $post_id, string $element_id, string $code, string $message ): array { $out = array( 'success' => false, 'post_id' => $post_id, 'message' => $message, 'error_code' => $code, ); if ( '' !== $element_id ) { $out['element_id'] = $element_id; } return $out; } }