PluginProbe ʕ •ᴥ•ʔ
Image Optimizer – Optimize Images and Convert to WebP or AVIF / 1.7.1
Image Optimizer – Optimize Images and Convert to WebP or AVIF v1.7.1
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 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3
image-optimization / classes / module-base.php
image-optimization / classes Last commit date
async-operation 1 year ago client 4 months ago exceptions 1 year ago file-system 2 years ago image 4 months ago migration 1 year ago rest 1 year ago basic-enum.php 2 years ago file-utils.php 2 years ago locale.php 2 years ago logger.php 4 months ago module-base.php 2 years ago route.php 2 years ago utils.php 4 months ago
module-base.php
336 lines
1 <?php
2
3 namespace ImageOptimization\Classes;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 /**
10 * Module Base.
11 *
12 * An abstract class providing the properties and methods needed to
13 * manage and handle modules in inheriting classes.
14 *
15 * @abstract
16 */
17 abstract class Module_Base {
18
19 /**
20 * Module class reflection.
21 *
22 * Holds the information about a class.
23 * @access private
24 *
25 * @var \ReflectionClass
26 */
27 private $reflection = null;
28
29 /**
30 * Module routes.
31 *
32 * Holds the module registered routes.
33 * @access public
34 *
35 * @var array
36 */
37 public $routes = [];
38
39 /**
40 * Module components.
41 *
42 * Holds the module components.
43 * @access private
44 *
45 * @var array
46 */
47 private $components = [];
48
49 /**
50 * Module instance.
51 *
52 * Holds the module instance.
53 * @access protected
54 *
55 * @var Module_Base[]
56 */
57 protected static $_instances = [];
58
59 /**
60 * Get module name.
61 *
62 * Retrieve the module name.
63 * @access public
64 * @abstract
65 *
66 * @return string Module name.
67 */
68 abstract public function get_name();
69
70 /**
71 * Instance.
72 *
73 * Ensures only one instance of the module class is loaded or can be loaded.
74 * @access public
75 * @static
76 *
77 * @return Module_Base An instance of the class.
78 */
79 public static function instance() {
80 $class_name = static::class_name();
81
82 if ( empty( static::$_instances[ $class_name ] ) ) {
83 static::$_instances[ $class_name ] = new static(); // @codeCoverageIgnore
84 }
85
86 return static::$_instances[ $class_name ];
87 }
88
89 /**
90 * is_active
91 * @access public
92 * @static
93 * @return bool
94 */
95 public static function is_active() {
96 return true;
97 }
98
99 /**
100 * Class name.
101 *
102 * Retrieve the name of the class.
103 * @access public
104 * @static
105 */
106 public static function class_name() {
107 return get_called_class();
108 }
109
110 /**
111 * Clone.
112 *
113 * Disable class cloning and throw an error on object clone.
114 *
115 * The whole idea of the singleton design pattern is that there is a single
116 * object. Therefore, we don't want the object to be cloned.
117 *
118 * @access public
119 */
120 public function __clone() {
121 // Cloning instances of the class is forbidden
122 _doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'image-optimization' ), '1.0.0' ); // @codeCoverageIgnore
123 }
124
125 /**
126 * Wakeup.
127 *
128 * Disable unserializing of the class.
129 * @access public
130 */
131 public function __wakeup() {
132 // Unserializing instances of the class is forbidden
133 _doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'image-optimization' ), '1.0.0' ); // @codeCoverageIgnore
134 }
135
136 /**
137 * @access public
138 */
139 public function get_reflection() {
140 if ( null === $this->reflection ) {
141 try {
142 $this->reflection = new \ReflectionClass( $this );
143 } catch ( \ReflectionException $e ) {
144 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
145 error_log( $e->getMessage() );
146 }
147 }
148 }
149
150 return $this->reflection;
151 }
152
153 /**
154 * Add module component.
155 *
156 * Add new component to the current module.
157 * @access public
158 *
159 * @param string $id Component ID.
160 * @param mixed $instance An instance of the component.
161 */
162 public function add_component( string $id, $instance ) {
163 $this->components[ $id ] = $instance;
164 }
165
166 /**
167 * Add module route.
168 *
169 * Add new route to the current module.
170 * @access public
171 *
172 * @param string $id Route ID.
173 * @param mixed $instance An instance of the route.
174 */
175 public function add_route( string $id, $instance ) {
176 $this->routes[ $id ] = $instance;
177 }
178
179 /**
180 * @access public
181 * @return string[]
182 */
183 public function get_components(): array {
184 return $this->components;
185 }
186
187 /**
188 * Get module component.
189 *
190 * Retrieve the module component.
191 * @access public
192 *
193 * @param string $id Component ID.
194 *
195 * @return mixed An instance of the component, or `false` if the component
196 * doesn't exist.
197 * @codeCoverageIgnore
198 */
199 public function get_component( string $id ) {
200 if ( isset( $this->components[ $id ] ) ) {
201 return $this->components[ $id ];
202 }
203
204 return false;
205 }
206
207 /**
208 * Retrieve the namespace of the class
209 *
210 * @access public
211 * @static
212 */
213 public static function namespace_name() {
214 $class_name = static::class_name();
215 return substr( $class_name, 0, strrpos( $class_name, '\\' ) );
216 }
217
218
219 /**
220 * Get assets url.
221 *
222 * @param string $file_name
223 * @param string $file_extension
224 * @param string $relative_url Optional. Default is null.
225 *
226 * @return string
227 */
228 final protected function get_assets_url( $file_name, $file_extension, $relative_url = null ): string {
229 static $is_test_mode = null;
230
231 if ( null === $is_test_mode ) {
232 $is_test_mode = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
233 }
234
235 if ( ! $relative_url ) {
236 $relative_url = $this->get_assets_relative_url();
237 }
238
239 $url = $this->get_assets_base_url() . $relative_url . $file_name;
240
241 return $url . '.' . $file_extension;
242 }
243
244 /**
245 * Get js assets url
246 *
247 * @param string $file_name
248 * @param string $relative_url Optional. Default is null.
249 * @param string $add_min_suffix Optional. Default is 'default'.
250 *
251 * @return string
252 */
253 final protected function get_js_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default' ): string {
254 return $this->get_assets_url( $file_name, 'js', $relative_url, $add_min_suffix );
255 }
256
257 /**
258 * Get css assets url
259 *
260 * @param string $file_name
261 * @param string $relative_url Optional. Default is null.
262 * @param string $add_min_suffix Optional. Default is 'default'.
263 * @param bool $add_direction_suffix Optional. Default is `false`
264 *
265 * @return string
266 */
267 final protected function get_css_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default', $add_direction_suffix = false ): string {
268 static $direction_suffix = null;
269
270 if ( ! $direction_suffix ) {
271 $direction_suffix = is_rtl() ? '-rtl' : '';
272 }
273
274 if ( $add_direction_suffix ) {
275 $file_name .= $direction_suffix;
276 }
277
278 return $this->get_assets_url( $file_name, 'css', $relative_url, $add_min_suffix );
279 }
280
281 /**
282 * Get assets base url
283 *
284 * @return string
285 */
286 protected function get_assets_base_url(): string {
287 return IMAGE_OPTIMIZATION_URL;
288 }
289
290 /**
291 * Get assets relative url
292 *
293 * @return string
294 */
295 protected function get_assets_relative_url(): string {
296 return 'assets/build/';
297 }
298
299 public static function routes_list() : array {
300 return [];
301 }
302
303 public static function component_list() : array {
304 return [];
305 }
306
307 /**
308 * Adds an array of components.
309 * Assumes namespace structure contains `\Components\`
310 */
311 public function register_components() {
312 $namespace = static::namespace_name();
313 $components_ids = static::component_list();
314
315 foreach ( $components_ids as $component_id ) {
316 $class_name = $namespace . '\\Components\\' . $component_id;
317 $this->add_component( $component_id, new $class_name() );
318 }
319 }
320
321 /**
322 * Adds an array of routes.
323 * Assumes namespace structure contains `\Rest\`
324 */
325 public function register_routes() {
326 $namespace = static::namespace_name();
327 $routes_ids = static::routes_list();
328
329 foreach ( $routes_ids as $route_id ) {
330 $class_name = $namespace . '\\Rest\\' . $route_id;
331 $this->add_route( $route_id, new $class_name() );
332 }
333 }
334 }
335
336