PluginProbe
Hello Plus / 1.7.7
Hello Plus v1.7.7
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 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.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / plugin.php

plugin.php in Hello Plus 1.7.7, at plugin.php

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