namespace ImageOptimization\Classes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
* An abstract class providing the properties and methods needed to
* manage and handle modules in inheriting classes.
abstract class Module_Base {
* Module class reflection.
* Holds the information about a class.
private $reflection = null;
* Holds the module registered routes.
* Holds the module components.
private $components = [];
* Holds the module instance.
protected static $_instances = [];
* Retrieve the module name.
* @return string Module name.
abstract public function get_name();
* Ensures only one instance of the module class is loaded or can be loaded.
* @return Module_Base An instance of the class.
public static function instance() {
$class_name = static::class_name();
if ( empty( static::$_instances[ $class_name ] ) ) {
static::$_instances[ $class_name ] = new static(); // @codeCoverageIgnore
return static::$_instances[ $class_name ];
public static function is_active() {
* Retrieve the name of the class.
public static function class_name() {
return get_called_class();
* Disable class cloning and throw an error on object clone.
* The whole idea of the singleton design pattern is that there is a single
* object. Therefore, we don't want the object to be cloned.
public function __clone() {
// Cloning instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'image-optimization' ), '1.0.0' ); // @codeCoverageIgnore
* Disable unserializing of the class.
public function __wakeup() {
// Unserializing instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, esc_html__( 'Something went wrong.', 'image-optimization' ), '1.0.0' ); // @codeCoverageIgnore
public function get_reflection() {
if ( null === $this->reflection ) {
$this->reflection = new \ReflectionClass( $this );
} catch ( \ReflectionException $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( $e->getMessage() );
return $this->reflection;
* Add new component to the current module.
* @param string $id Component ID.
* @param mixed $instance An instance of the component.
public function add_component( string $id, $instance ) {
$this->components[ $id ] = $instance;
* Add new route to the current module.
* @param string $id Route ID.
* @param mixed $instance An instance of the route.
public function add_route( string $id, $instance ) {
$this->routes[ $id ] = $instance;
public function get_components(): array {
return $this->components;
* Retrieve the module component.
* @param string $id Component ID.
* @return mixed An instance of the component, or `false` if the component
public function get_component( string $id ) {
if ( isset( $this->components[ $id ] ) ) {
return $this->components[ $id ];
* Retrieve the namespace of the class
public static function namespace_name() {
$class_name = static::class_name();
return substr( $class_name, 0, strrpos( $class_name, '\\' ) );
* @param string $file_name
* @param string $file_extension
* @param string $relative_url Optional. Default is null.
final protected function get_assets_url( $file_name, $file_extension, $relative_url = null ): string {
static $is_test_mode = null;
if ( null === $is_test_mode ) {
$is_test_mode = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
$relative_url = $this->get_assets_relative_url();
$url = $this->get_assets_base_url() . $relative_url . $file_name;
return $url . '.' . $file_extension;
* @param string $file_name
* @param string $relative_url Optional. Default is null.
* @param string $add_min_suffix Optional. Default is 'default'.
final protected function get_js_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default' ): string {
return $this->get_assets_url( $file_name, 'js', $relative_url, $add_min_suffix );
* @param string $file_name
* @param string $relative_url Optional. Default is null.
* @param string $add_min_suffix Optional. Default is 'default'.
* @param bool $add_direction_suffix Optional. Default is `false`
final protected function get_css_assets_url( $file_name, $relative_url = null, $add_min_suffix = 'default', $add_direction_suffix = false ): string {
static $direction_suffix = null;
if ( ! $direction_suffix ) {
$direction_suffix = is_rtl() ? '-rtl' : '';
if ( $add_direction_suffix ) {
$file_name .= $direction_suffix;
return $this->get_assets_url( $file_name, 'css', $relative_url, $add_min_suffix );
protected function get_assets_base_url(): string {
return IMAGE_OPTIMIZATION_URL;
* Get assets relative url
protected function get_assets_relative_url(): string {
public static function routes_list() : array {
public static function component_list() : array {
* Adds an array of components.
* Assumes namespace structure contains `\Components\`
public function register_components() {
$namespace = static::namespace_name();
$components_ids = static::component_list();
foreach ( $components_ids as $component_id ) {
$class_name = $namespace . '\\Components\\' . $component_id;
$this->add_component( $component_id, new $class_name() );
* Adds an array of routes.
* Assumes namespace structure contains `\Rest\`
public function register_routes() {
$namespace = static::namespace_name();
$routes_ids = static::routes_list();
foreach ( $routes_ids as $route_id ) {
$class_name = $namespace . '\\Rest\\' . $route_id;
$this->add_route( $route_id, new $class_name() );