PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / elementor / wp-one-package / src / Connect / Facade.php

Facade.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.4, at vendor/elementor/wp-one-package/src/Connect/Facade.php

329 lines 7.3 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 plugin_slug or throw exception if not found
162 *
163 * @param string $plugin_slug The plugin slug
164 * @return Facade The facade instance
165 * @throws \RuntimeException If instance not found
166 */
167 public static function get_or_fail( string $plugin_slug ): Facade {
168 if ( ! isset( self::$instances[ $plugin_slug ] ) ) {
169 throw new \RuntimeException(
170 "Facade instance '{$plugin_slug}' not found. Did you forget to call Facade::make() first?"
171 );
172 }
173
174 return self::get( $plugin_slug );
175 }
176
177 /**
178 * Check if a Facade instance exists for the given plugin_slug
179 *
180 * @param string $plugin_slug The plugin slug
181 * @return bool
182 */
183 public static function has( string $plugin_slug ): bool {
184 return isset( self::$instances[ $plugin_slug ] );
185 }
186
187 /**
188 * Get all registered app names
189 *
190 * @return array<string>
191 */
192 public static function registered(): array {
193 return array_keys( self::$instances );
194 }
195
196 /**
197 * Remove a Facade instance from the registry
198 *
199 * @param string $plugin_slug The plugin slug
200 * @return bool True if removed, false if not found
201 */
202 public static function forget( string $plugin_slug ): bool {
203 if ( isset( self::$instances[ $plugin_slug ] ) ) {
204 unset( self::$instances[ $plugin_slug ] );
205 return true;
206 }
207
208 return false;
209 }
210
211 /**
212 * Create Data, Utils, Service, and Logger instances
213 *
214 * @return void
215 */
216 private function create_services(): void {
217 // Create Data instance
218 $this->data = new Data( $this );
219
220 // Create Utils instance
221 $this->utils = new Utils( $this );
222
223 // Create Service instance
224 $this->service = new Service( $this );
225
226 // Create HomeUrl instance
227 $this->home_url = new HomeUrl( $this );
228
229 // Create Logger instance with app name
230 $this->logger = new Logger( $this->config['app_name'] );
231 }
232
233 /**
234 * Initialize routes and components
235 * Called automatically by constructor
236 *
237 * @return void
238 */
239 private function init(): void {
240 $this->init_routes();
241 $this->init_components();
242 }
243
244 /**
245 * Get Data instance
246 *
247 * @return Data
248 */
249 public function data(): Data {
250 return $this->data;
251 }
252
253 /**
254 * Get Utils instance
255 *
256 * @return Utils
257 */
258 public function utils(): Utils {
259 return $this->utils;
260 }
261
262 /**
263 * Get Service instance
264 *
265 * @return Service
266 */
267 public function service(): Service {
268 return $this->service;
269 }
270
271 /**
272 * Get HomeUrl instance
273 *
274 * @return HomeUrl
275 */
276 public function home_url(): HomeUrl {
277 return $this->home_url;
278 }
279
280 /**
281 * Get Logger instance
282 *
283 * @return Logger
284 */
285 public function logger(): Logger {
286 return $this->logger;
287 }
288
289 /**
290 * Get configuration
291 * @param string|null $key
292 * @return array|string
293 */
294 public function get_config( ?string $key = null ) {
295 if ( $key ) {
296 return $this->config[ $key ];
297 }
298 return $this->config;
299 }
300
301 /**
302 * Get the app name for this instance
303 *
304 * @return string
305 */
306 public function get_app_name(): string {
307 return $this->config['app_name'];
308 }
309
310 /**
311 * Initialize REST API routes
312 *
313 * @return void
314 */
315 private function init_routes(): void {
316 new \ElementorOne\Connect\Controllers\Authorization( $this );
317 }
318
319 /**
320 * Initialize components
321 *
322 * @return void
323 */
324 private function init_components(): void {
325 new \ElementorOne\Connect\Components\Handler( $this );
326 new \ElementorOne\Connect\Components\License( $this );
327 }
328 }
329