PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.4
Elementor Website Builder – more than just a page builder v4.3.4
4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 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 All 457 releases
elementor / vendor / elementor / elementor-mcp-composer / src / Admin / Page.php

Page.php in Elementor Website Builder – more than just a page builder 4.3.4, at vendor/elementor/elementor-mcp-composer/src/Admin/Page.php

185 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\MCP\Composer\Admin;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 /**
10 * Passive MCP admin page definition.
11 *
12 * Exposes slug, titles, capability, icon, and render callback for consumers
13 * to mount into any admin menu system. Does not register itself.
14 */
15 class Page {
16
17 const SLUG = 'elementor-mcp';
18 const SCRIPT_HANDLE = 'elementor-mcp-page';
19
20 /**
21 * @var Page|null
22 */
23 private static $instance = null;
24
25 public static function instance( $plugin_uri = '' ): self {
26 if ( null === self::$instance ) {
27 self::$instance = new self();
28 }
29
30 return self::$instance;
31 }
32
33 /**
34 * @return string
35 */
36 public function get_slug(): string {
37 return self::SLUG;
38 }
39
40 /**
41 * @return string
42 */
43 public function get_page_title(): string {
44 return \esc_html__( 'Elementor MCP', 'elementor' );
45 }
46
47 /**
48 * @return string
49 */
50 public function get_label(): string {
51 return \esc_html__( 'Elementor MCP', 'elementor' );
52 }
53
54 /**
55 * @return string
56 */
57 public function get_capability(): string {
58 return 'manage_options';
59 }
60
61 /**
62 * @return string
63 */
64 public function get_icon(): string {
65 return 'eicon-code';
66 }
67
68 /**
69 * Enqueue assets for the admin page.
70 *
71 * @return void
72 */
73 public function enqueue_assets(): void {
74 $asset_file = ELEMENTOR_MCP_COMPOSER_ASSETS_PATH . 'mcp-page.asset.php';
75
76 if ( ! file_exists( $asset_file ) ) {
77 return;
78 }
79
80 $asset_data = require $asset_file;
81 $version = $asset_data['version'] ?? '1.0.0';
82 $deps = $asset_data['dependencies'] ?? [];
83 $base_url = ELEMENTOR_MCP_COMPOSER_ASSETS_URL;
84
85 wp_enqueue_style(
86 self::SCRIPT_HANDLE,
87 $base_url . 'mcp-page.css',
88 [],
89 $version
90 );
91
92 wp_enqueue_script(
93 self::SCRIPT_HANDLE,
94 $base_url . 'mcp-page.js',
95 $deps,
96 $version,
97 true
98 );
99 }
100
101 /**
102 * Render the admin page markup.
103 *
104 * @return void
105 */
106 public function render(): void {
107 if ( ! $this->current_user_can_access_page() ) {
108 $this->deny_page_access();
109 }
110
111 if ( ! $this->has_built_assets() ) {
112 $this->render_missing_assets_notice();
113 return;
114 }
115
116 $this->enqueue_assets();
117 \wp_localize_script( self::SCRIPT_HANDLE, 'elementorMcpData', $this->get_page_data() );
118
119 $this->render_page();
120 }
121
122 private function current_user_can_access_page(): bool {
123 if ( ! function_exists( 'current_user_can' ) ) {
124 return false;
125 }
126
127 return (bool) call_user_func( 'current_user_can', $this->get_capability() );
128 }
129
130 private function deny_page_access(): void {
131 $message = \esc_html__( 'Sorry, you are not allowed to access this page.', 'elementor' );
132
133 if ( function_exists( 'wp_die' ) ) {
134 call_user_func( 'wp_die', $message, 403 );
135 return;
136 }
137
138 exit;
139 }
140
141 private function render_page(): void {
142 ?>
143 <div class="wrap">
144 <div id="elementor-mcp-root"></div>
145 </div>
146 <?php
147 }
148
149 private function has_built_assets(): bool {
150 return file_exists( ELEMENTOR_MCP_COMPOSER_ASSETS_PATH . 'mcp-page.asset.php' );
151 }
152
153 private function render_missing_assets_notice(): void {
154 ?>
155 <div class="notice notice-error">
156 <p><?php echo esc_html__( 'MCP admin assets not built. Run: npm run build', 'elementor' ); ?></p>
157 </div>
158 <?php
159 }
160
161 /**
162 * @return array<string, mixed>
163 */
164 private function get_page_data(): array {
165 $version = defined( 'ELEMENTOR_MCP_COMPOSER_VERSION' ) ? ELEMENTOR_MCP_COMPOSER_VERSION : '1.0.0';
166 $fastlane_mode = defined( 'ELEMENTOR_MCP_ONBOARDING_FASTLANE' ) && ELEMENTOR_MCP_ONBOARDING_FASTLANE;
167
168 return [
169 'endpoint' => function_exists( 'rest_url' ) ? rest_url( 'elementor-mcp-composer/v' . $version ) : '',
170 'serverName' => 'Elementor MCP',
171 'version' => $version,
172 'nonce' => wp_create_nonce( 'wp_rest' ),
173 'fastlaneMode' => $fastlane_mode,
174 'settings' => [
175 'mcp' => array_merge(
176 McpSettingsController::get_settings_data(),
177 CredentialsController::get_application_password_ui_flags()
178 ),
179 'consent' => ConsentController::get_consent(),
180 'test' => TestController::get_test_completed(),
181 ],
182 ];
183 }
184 }
185