PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.1
Elementor Website Builder – more than just a page builder v4.3.1
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 4.1.0-beta2 4.1.0-dev2 4.0.8 All 454 releases
elementor / vendor / elementor / wp-one-package / src / Connect / Facade.php

Facade.php in Elementor Website Builder – more than just a page builder 4.3.1, at vendor/elementor/wp-one-package/src/Connect/Facade.php

346 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ElementorOne\Connect;
4
5 use ElementorOne\Connect\Classes\Data;
6 use ElementorOne\Connect\Classes\Utils;
7 use ElementorOne\Connect\Classes\HomeUrl;
8 use ElementorOne\Connect\Classes\Service;
9 use ElementorOne\Logger;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly
13 }
14
15 /**
16 * Class Facade
17 *
18 * Facade for the Connect subsystem.
19 * Provides a simplified interface to Data, Utils, and Service instances.
20 * Manages multiple instances indexed by plugin_slug for easy retrieval.
21 */
22 class Facade {
23 /**
24 * @var array<string, Facade> Registry of facade instances by plugin_slug
25 */
26 private static array $instances = [];
27
28 /**
29 * @var array Configuration for this facade instance
30 */
31 private array $config;
32
33 /**
34 * @var Data Data instance
35 */
36 private Data $data;
37
38 /**
39 * @var Utils Utils instance
40 */
41 private Utils $utils;
42
43 /**
44 * @var Service Service instance
45 */
46 private Service $service;
47
48 /**
49 * @var HomeUrl HomeUrl instance
50 */
51 private HomeUrl $home_url;
52
53 /**
54 * @var Logger Logger instance
55 */
56 private Logger $logger;
57
58 /**
59 * Private constructor - use make() to create instances
60 *
61 * @param array $config Configuration array with required keys:
62 * - app_name: App name
63 * - app_prefix: App prefix for options
64 * - app_rest_namespace: REST namespace
65 * - base_url: Base URL for API
66 * - admin_page: Admin page slug
67 * - app_type: App type
68 * - plugin_slug: Plugin slug
69 * - scopes: OAuth scopes
70 * - state_nonce: State nonce name
71 * - connect_mode: Connect mode - 'site' or 'user'
72 */
73 private function __construct( array $config ) {
74 $this->config = $config;
75 $this->create_services();
76 $this->init();
77 }
78
79 /**
80 * Create or retrieve a Facade instance by plugin_slug
81 * If an instance with the same plugin_slug exists, it will be returned
82 * Otherwise, a new instance is created and registered
83 *
84 * @param array $config Configuration array (must include all required keys)
85 * @return Facade
86 * @throws \InvalidArgumentException If required config keys are missing
87 */
88 public static function make( array $config ): Facade {
89 // Validate configuration
90 self::validate_config( $config );
91
92 $plugin_slug = $config['plugin_slug'];
93
94 // Return existing instance if already created
95 if ( isset( self::$instances[ $plugin_slug ] ) ) {
96 return self::get( $plugin_slug );
97 }
98
99 // Create new instance and register it
100 $instance = new self( $config );
101 self::$instances[ $plugin_slug ] = $instance;
102
103 return $instance;
104 }
105
106 /**
107 * Validate configuration array has all required keys
108 *
109 * @param array $config Configuration to validate
110 * @return void
111 * @throws \InvalidArgumentException If required keys are missing
112 */
113 private static function validate_config( array $config ): void {
114 $required_keys = [
115 'app_name',
116 'app_prefix',
117 'app_rest_namespace',
118 'base_url',
119 'admin_page',
120 'app_type',
121 'plugin_slug',
122 'scopes',
123 'state_nonce',
124 'connect_mode',
125 ];
126
127 $missing_keys = [];
128
129 foreach ( $required_keys as $key ) {
130 if ( ! isset( $config[ $key ] ) ) {
131 $missing_keys[] = $key;
132 }
133 }
134
135 if ( ! empty( $missing_keys ) ) {
136 throw new \InvalidArgumentException(
137 'Configuration is missing required keys: ' . implode( ', ', $missing_keys )
138 );
139 }
140
141 // Validate connect_mode value
142 if ( ! in_array( $config['connect_mode'], [ 'site', 'user' ], true ) ) {
143 throw new \InvalidArgumentException(
144 'Configuration key "connect_mode" must be either "site" or "user", got: ' . $config['connect_mode']
145 );
146 }
147 }
148
149 /**
150 * Get a Facade instance by plugin_slug
151 *
152 * @param string $plugin_slug The plugin slug
153 * @return Facade|null The facade instance or null if not found
154 */
155 public static function get( string $plugin_slug ): ?Facade {
156 $instance = self::$instances[ $plugin_slug ] ?? null;
157 return apply_filters( 'elementor_one/get_connect_instance', $instance, $plugin_slug, self::$instances );
158 }
159
160 /**
161 * Get a Facade instance by admin page
162 *
163 * @param string $plugin_page The admin page slug
164 * @return Facade|null The facade instance or null if not found
165 */
166 public static function get_by_plugin_page( string $plugin_page ): ?Facade {
167 foreach ( self::$instances as $instance ) {
168 $admin_page = $instance->get_config( 'admin_page' );
169 $normalized_plugin_page = explode( 'page=', $admin_page, 2 )[1] ?? $admin_page;
170 if ( $normalized_plugin_page === $plugin_page ) {
171 return $instance;
172 }
173 }
174 return null;
175 }
176
177 /**
178 * Get a Facade instance by plugin_slug or throw exception if not found
179 *
180 * @param string $plugin_slug The plugin slug
181 * @return Facade The facade instance
182 * @throws \RuntimeException If instance not found
183 */
184 public static function get_or_fail( string $plugin_slug ): Facade {
185 if ( ! isset( self::$instances[ $plugin_slug ] ) ) {
186 throw new \RuntimeException(
187 "Facade instance '{$plugin_slug}' not found. Did you forget to call Facade::make() first?"
188 );
189 }
190
191 return self::get( $plugin_slug );
192 }
193
194 /**
195 * Check if a Facade instance exists for the given plugin_slug
196 *
197 * @param string $plugin_slug The plugin slug
198 * @return bool
199 */
200 public static function has( string $plugin_slug ): bool {
201 return isset( self::$instances[ $plugin_slug ] );
202 }
203
204 /**
205 * Get all registered app names
206 *
207 * @return array<string>
208 */
209 public static function registered(): array {
210 return array_keys( self::$instances );
211 }
212
213 /**
214 * Remove a Facade instance from the registry
215 *
216 * @param string $plugin_slug The plugin slug
217 * @return bool True if removed, false if not found
218 */
219 public static function forget( string $plugin_slug ): bool {
220 if ( isset( self::$instances[ $plugin_slug ] ) ) {
221 unset( self::$instances[ $plugin_slug ] );
222 return true;
223 }
224
225 return false;
226 }
227
228 /**
229 * Create Data, Utils, Service, and Logger instances
230 *
231 * @return void
232 */
233 private function create_services(): void {
234 // Create Data instance
235 $this->data = new Data( $this );
236
237 // Create Utils instance
238 $this->utils = new Utils( $this );
239
240 // Create Service instance
241 $this->service = new Service( $this );
242
243 // Create HomeUrl instance
244 $this->home_url = new HomeUrl( $this );
245
246 // Create Logger instance with app name
247 $this->logger = new Logger( $this->config['app_name'] );
248 }
249
250 /**
251 * Initialize routes and components
252 * Called automatically by constructor
253 *
254 * @return void
255 */
256 private function init(): void {
257 $this->init_routes();
258 $this->init_components();
259 }
260
261 /**
262 * Get Data instance
263 *
264 * @return Data
265 */
266 public function data(): Data {
267 return $this->data;
268 }
269
270 /**
271 * Get Utils instance
272 *
273 * @return Utils
274 */
275 public function utils(): Utils {
276 return $this->utils;
277 }
278
279 /**
280 * Get Service instance
281 *
282 * @return Service
283 */
284 public function service(): Service {
285 return $this->service;
286 }
287
288 /**
289 * Get HomeUrl instance
290 *
291 * @return HomeUrl
292 */
293 public function home_url(): HomeUrl {
294 return $this->home_url;
295 }
296
297 /**
298 * Get Logger instance
299 *
300 * @return Logger
301 */
302 public function logger(): Logger {
303 return $this->logger;
304 }
305
306 /**
307 * Get configuration
308 * @param string|null $key
309 * @return array|string
310 */
311 public function get_config( ?string $key = null ) {
312 if ( $key ) {
313 return $this->config[ $key ];
314 }
315 return $this->config;
316 }
317
318 /**
319 * Get the app name for this instance
320 *
321 * @return string
322 */
323 public function get_app_name(): string {
324 return $this->config['app_name'];
325 }
326
327 /**
328 * Initialize REST API routes
329 *
330 * @return void
331 */
332 private function init_routes(): void {
333 new \ElementorOne\Connect\Controllers\Authorization( $this );
334 }
335
336 /**
337 * Initialize components
338 *
339 * @return void
340 */
341 private function init_components(): void {
342 new \ElementorOne\Connect\Components\Handler( $this );
343 new \ElementorOne\Connect\Components\License( $this );
344 }
345 }
346