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