PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / mcp / abilities / read-resource-ability.php

read-resource-ability.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/mcp/abilities/read-resource-ability.php

121 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\Mcp\Abilities;
4
5 use Elementor\Modules\Mcp\Abilities\Utils\Prompt_Loader;
6 use Elementor\Modules\Mcp\Module as Mcp_Module;
7 use Elementor\Modules\Mcp\Registry\Ability_Registry;
8 use Elementor\Plugin;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 class Read_Resource_Ability extends Abstract_Ability {
15
16 private ?Ability_Registry $registry;
17
18 public function __construct( ?Ability_Registry $registry = null ) {
19 $this->registry = $registry;
20 }
21
22 protected function get_ability_id(): string {
23 return 'elementor/read-resource';
24 }
25
26 protected function get_definition(): Ability_Definition {
27 return new Ability_Definition(
28 __( 'Read Elementor Resource', 'elementor' ),
29 Prompt_Loader::load( 'read-resource' ),
30 'elementor',
31 [
32 'type' => 'object',
33 'properties' => [
34 'uri' => [ 'type' => 'string' ],
35 'mimeType' => [ 'type' => 'string' ],
36 'content' => [ 'type' => 'string' ],
37 ],
38 ],
39 [
40 'annotations' => [
41 'readonly' => true,
42 'idempotent' => true,
43 'destructive' => false,
44 ],
45 ],
46 fn() => current_user_can( 'edit_posts' ),
47 [
48 'type' => 'object',
49 'required' => [ 'uri' ],
50 'properties' => [
51 'uri' => [
52 'type' => 'string',
53 'description' => 'The resource URI to read. Use list-resources to discover available URIs.',
54 ],
55 ],
56 ]
57 );
58 }
59
60 public function execute( $input = [] ) {
61 $input = is_array( $input ) ? $input : [];
62 $uri = $input['uri'] ?? '';
63
64 if ( '' === $uri ) {
65 return new \WP_Error(
66 'missing_uri',
67 __( 'Resource URI is required. Use list-resources to discover available URIs.', 'elementor' ),
68 [ 'status' => 400 ]
69 );
70 }
71
72 $resource = $this->resolve_registry()->find_resource_by_uri( $uri );
73
74 if ( null === $resource ) {
75 return new \WP_Error(
76 'resource_not_found',
77 sprintf(
78 /* translators: %s: resource URI */
79 __( 'Resource not found: %s. Use list-resources to discover available URIs.', 'elementor' ),
80 $uri
81 ),
82 [ 'status' => 404 ]
83 );
84 }
85
86 if ( ! $resource->check_permission() ) {
87 return new \WP_Error(
88 'rest_forbidden',
89 __( 'Sorry, you are not allowed to perform this action.', 'elementor' ),
90 [ 'status' => \WP_Http::FORBIDDEN ]
91 );
92 }
93
94 $content = $resource->execute();
95
96 if ( is_wp_error( $content ) ) {
97 return $content;
98 }
99
100 return [
101 'uri' => $uri,
102 'mimeType' => (string) ( $resource->get_mime_type() ?? '' ),
103 'content' => is_string( $content ) ? $content : wp_json_encode( $content ),
104 ];
105 }
106
107 private function resolve_registry(): Ability_Registry {
108 if ( $this->registry instanceof Ability_Registry ) {
109 return $this->registry;
110 }
111
112 $module = Plugin::$instance->modules_manager->get_modules( 'mcp' );
113
114 $this->registry = $module instanceof Mcp_Module
115 ? $module->registry()
116 : Mcp_Module::build_core_registry();
117
118 return $this->registry;
119 }
120 }
121