| 1 |
<?php |
| 2 |
namespace HelloPlus; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
use HelloPlus\Includes\Module_Base; |
| 9 |
use HelloPlus\Includes\Utils; |
| 10 |
|
| 11 |
/** |
| 12 |
* Theme's main class, |
| 13 |
* responsible over initializing the modules & some general definitions. |
| 14 |
* |
| 15 |
* @package HelloPlus |
| 16 |
*/ |
| 17 |
final class Plugin { |
| 18 |
|
| 19 |
/** |
| 20 |
* @var ?Plugin |
| 21 |
*/ |
| 22 |
private static ?Plugin $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var Module_Base[] |
| 26 |
*/ |
| 27 |
private array $modules = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
private array $classes_aliases = []; |
| 33 |
|
| 34 |
/** |
| 35 |
* Throw error on object clone |
| 36 |
* |
| 37 |
* The whole idea of the singleton design pattern is that there is a single |
| 38 |
* object therefore, we don't want the object to be cloned. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function __clone() { |
| 44 |
_doing_it_wrong( |
| 45 |
__FUNCTION__, |
| 46 |
sprintf( 'Cloning instances of the singleton "%s" class is forbidden.', get_class( $this ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 47 |
'1.0.0' |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Disable un-serializing of the class |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function __wakeup() { |
| 58 |
_doing_it_wrong( |
| 59 |
__FUNCTION__, |
| 60 |
sprintf( 'Deserializing instances of the singleton "%s" class is forbidden.', get_class( $this ) ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 61 |
'1.0.0' |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
public function activate() { |
| 66 |
/** |
| 67 |
* Fires on plugin activation |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
*/ |
| 71 |
do_action( 'hello-plus/activate' ); |
| 72 |
} |
| 73 |
|
| 74 |
public function init() { |
| 75 |
/** |
| 76 |
* Fires on plugin init |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
*/ |
| 80 |
do_action( 'hello-plus/init' ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param $class_name |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function autoload( $class_name ) { |
| 89 |
if ( 0 !== strpos( $class_name, __NAMESPACE__ ) ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
$has_class_alias = isset( $this->classes_aliases[ $class_name ] ); |
| 94 |
|
| 95 |
// Backward Compatibility: Save old class name for set an alias after the new class is loaded |
| 96 |
if ( $has_class_alias ) { |
| 97 |
$class_alias_name = $this->classes_aliases[ $class_name ]; |
| 98 |
$class_to_load = $class_alias_name; |
| 99 |
} else { |
| 100 |
$class_to_load = $class_name; |
| 101 |
} |
| 102 |
|
| 103 |
if ( ! class_exists( $class_to_load ) ) { |
| 104 |
$filename = strtolower( |
| 105 |
preg_replace( |
| 106 |
[ '/^' . __NAMESPACE__ . '\\\/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ], |
| 107 |
[ '', '$1-$2', '-', DIRECTORY_SEPARATOR ], |
| 108 |
$class_to_load |
| 109 |
) |
| 110 |
); |
| 111 |
$filename = trailingslashit( HELLOPLUS_PATH ) . $filename . '.php'; |
| 112 |
|
| 113 |
if ( is_readable( $filename ) ) { |
| 114 |
include $filename; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if ( $has_class_alias ) { |
| 119 |
class_alias( $class_alias_name, $class_name ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Singleton |
| 125 |
* |
| 126 |
* @return Plugin |
| 127 |
*/ |
| 128 |
public static function instance(): Plugin { |
| 129 |
if ( is_null( self::$instance ) ) { |
| 130 |
self::$instance = new self(); |
| 131 |
} |
| 132 |
|
| 133 |
return self::$instance; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @param string $module_name |
| 138 |
* |
| 139 |
* @return ?Module_Base |
| 140 |
*/ |
| 141 |
public function get_module( string $module_name ): ?Module_Base { |
| 142 |
if ( isset( $this->modules[ $module_name ] ) ) { |
| 143 |
return $this->modules[ $module_name ]; |
| 144 |
} |
| 145 |
|
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* @param Module_Base $module |
| 151 |
* |
| 152 |
* allow child theme and 3rd party plugins to add modules |
| 153 |
* |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
public function add_module( Module_Base $module ) { |
| 157 |
$class_name = $module->get_reflection()->getName(); |
| 158 |
if ( $module::is_active() ) { |
| 159 |
$this->modules[ $class_name ] = $module::instance(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Initialize all Modules |
| 165 |
* |
| 166 |
* @return void |
| 167 |
*/ |
| 168 |
private function init_modules() { |
| 169 |
$modules_list = [ |
| 170 |
'Theme', |
| 171 |
'Admin', |
| 172 |
'Content', |
| 173 |
'TemplateParts', |
| 174 |
'Forms', |
| 175 |
]; |
| 176 |
|
| 177 |
foreach ( $modules_list as $module_name ) { |
| 178 |
$class_name = str_replace( '-', ' ', $module_name ); |
| 179 |
$class_name = str_replace( ' ', '', ucwords( $class_name ) ); |
| 180 |
$class_name = __NAMESPACE__ . '\\Modules\\' . $class_name . '\Module'; |
| 181 |
|
| 182 |
/** @var Module_Base $class_name */ |
| 183 |
if ( $class_name::is_active() && empty( $this->classes_aliases[ $module_name ] ) ) { |
| 184 |
$this->modules[ $module_name ] = $class_name::instance(); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Theme private constructor. |
| 191 |
*/ |
| 192 |
private function __construct() { |
| 193 |
static $autoloader_registered = false; |
| 194 |
|
| 195 |
if ( ! $autoloader_registered ) { |
| 196 |
$autoloader_registered = spl_autoload_register( [ $this, 'autoload' ] ); |
| 197 |
} |
| 198 |
|
| 199 |
register_activation_hook( HELLOPLUS_PLUGIN_BASE, [ $this, 'activate' ] ); |
| 200 |
|
| 201 |
add_action( 'init', [ $this, 'init' ] ); |
| 202 |
|
| 203 |
$this->init_modules(); |
| 204 |
} |
| 205 |
} |
| 206 |
|