|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Main plugin controller class for SimpliBlocks. |
| 3 | + * Plugin bootstrap class for SimpliBlocks. |
4 | 4 | * |
5 | | - * This class bootstraps the plugin, loads dependencies, sets up internationalization, |
6 | | - * and initializes core services including the plugin updater. |
| 5 | + * This class is responsible for initializing the plugin by defining constants |
| 6 | + * and loading core services. It does not maintain runtime state and therefore |
| 7 | + * does not implement the singleton pattern. |
7 | 8 | * |
8 | 9 | * @package ZIORWebDev\SimpliBlocks |
9 | 10 | */ |
10 | 11 |
|
11 | 12 | namespace ZIORWebDev\SimpliBlocks; |
12 | 13 |
|
13 | | -use ZIORWebDev\WordPressBlocks; |
| 14 | +use ZIORWebDev\WordPressBlocks\Loader as BlocksLoader; |
14 | 15 |
|
15 | 16 | // Exit if accessed directly. |
16 | 17 | if ( ! defined( 'ABSPATH' ) ) { |
17 | | - exit(); |
| 18 | + exit; |
18 | 19 | } |
19 | 20 |
|
20 | 21 | /** |
21 | | - * The core plugin class for SimpliBlocks. |
| 22 | + * Main plugin bootstrap class. |
22 | 23 | * |
23 | | - * Responsible for defining core constants, loading dependencies, setting up localization, |
24 | | - * and initializing the plugin loader and updater. |
| 24 | + * Handles plugin initialization, constant definition, and service bootstrapping. |
| 25 | + * All methods are static to avoid unnecessary global instances. |
25 | 26 | * |
26 | | - * Implements the singleton pattern to ensure only one instance is used. |
27 | | - * |
28 | | - * @package ZIORWebDev\SimpliBlocks. |
| 27 | + * @since 1.0.0 |
29 | 28 | */ |
30 | 29 | final class Plugin { |
31 | 30 |
|
32 | | - /** |
33 | | - * Path to the main plugin file. |
34 | | - * |
35 | | - * @var string |
36 | | - */ |
37 | | - protected $plugin_file; |
38 | | - |
39 | | - /** |
40 | | - * Name of the plugin. |
41 | | - * |
42 | | - * @var string |
43 | | - */ |
44 | | - protected $plugin_name = ''; |
45 | | - |
46 | | - /** |
47 | | - * Singleton instance of the Plugin class. |
48 | | - * |
49 | | - * @var Plugin |
50 | | - */ |
51 | | - protected static $instance; |
52 | | - |
53 | | - /** |
54 | | - * Current version of the plugin. |
55 | | - * |
56 | | - * @var string |
57 | | - */ |
58 | | - protected $plugin_version = ''; |
59 | | - |
60 | | - /** |
61 | | - * Class constructor. |
62 | | - */ |
63 | | - private function __construct( string $plugin_file ) { |
64 | | - $this->plugin_file = $plugin_file; |
65 | | - } |
66 | | - |
67 | 31 | /** |
68 | 32 | * Initialize the plugin. |
69 | 33 | * |
70 | | - * Sets up constants, includes required files, and initializes the plugin updater. |
| 34 | + * Defines core constants and boots required services. |
| 35 | + * This method should be called once from the main plugin file. |
71 | 36 | * |
72 | 37 | * @since 1.0.0 |
| 38 | + * |
| 39 | + * @param string $plugin_file Absolute path to the main plugin file. |
73 | 40 | * @return void |
74 | 41 | */ |
75 | | - private function init(): void { |
76 | | - $this->setup_constants(); |
77 | | - $this->include_classes(); |
78 | | - |
79 | | - /** |
80 | | - * Load WordPress Blocks library. |
81 | | - */ |
82 | | - WordPressBlocks\Load::get_instance(); |
| 42 | + public static function init( string $plugin_file ): void { |
| 43 | + self::define_constants( $plugin_file ); |
| 44 | + self::init_services(); |
83 | 45 | } |
84 | 46 |
|
85 | 47 | /** |
86 | | - * Include required plugin files. |
| 48 | + * Define plugin constants. |
87 | 49 | * |
88 | | - * Loads configuration, core functions, loader class, and the updater helper. |
| 50 | + * Constants are only defined once to prevent redefinition errors. |
| 51 | + * These constants are used throughout the plugin. |
89 | 52 | * |
90 | 53 | * @since 1.0.0 |
| 54 | + * |
| 55 | + * @param string $plugin_file Absolute path to the main plugin file. |
91 | 56 | * @return void |
92 | 57 | */ |
93 | | - private function include_classes(): void { |
94 | | - require_once ZIOR_SIMPLIBLOCKS_PLUGIN_DIR . 'vendor/autoload.php'; |
| 58 | + private static function define_constants( string $plugin_file ): void { |
| 59 | + define( 'ZIOR_SIMPLIBLOCKS_PLUGIN_FILE', $plugin_file ); |
| 60 | + define( 'ZIOR_SIMPLIBLOCKS_PLUGIN_DIR', plugin_dir_path( $plugin_file ) ); |
| 61 | + define( 'ZIOR_SIMPLIBLOCKS_PLUGIN_URL', plugin_dir_url( $plugin_file ) ); |
95 | 62 | } |
96 | 63 |
|
97 | 64 | /** |
98 | | - * Defines plugin constants used throughout the plugin. |
| 65 | + * Boot core plugin services. |
| 66 | + * |
| 67 | + * Responsible for initializing external libraries, loaders, |
| 68 | + * and other core services required by the plugin. |
99 | 69 | * |
100 | 70 | * @since 1.0.0 |
| 71 | + * |
101 | 72 | * @return void |
102 | 73 | */ |
103 | | - private function setup_constants(): void { |
104 | | - define( 'ZIOR_SIMPLIBLOCKS_PLUGIN_DIR', plugin_dir_path( $this->plugin_file ) ); |
105 | | - define( 'ZIOR_SIMPLIBLOCKS_PLUGIN_URL', plugin_dir_url( $this->plugin_file ) ); |
106 | | - define( 'ZIOR_SIMPLIBLOCKS_PLUGIN_FILE', $this->plugin_file ); |
107 | | - define( 'ZIOR_SIMPLIBLOCKS_PLUGIN_VERSION', $this->plugin_version ); |
| 74 | + private static function init_services(): void { |
| 75 | + ( new BlocksLoader() )->init(); |
108 | 76 | } |
109 | 77 |
|
110 | 78 | /** |
111 | | - * Get the singleton instance of the Plugin class. |
| 79 | + * Plugin activation callback. |
112 | 80 | * |
113 | | - * @param string $plugin_file |
114 | | - * @return Plugin |
115 | | - * @since 1.0.0 |
116 | | - */ |
117 | | - public static function get_instance( string $plugin_file ): Plugin { |
118 | | - if ( is_null( self::$instance ) ) { |
119 | | - self::$instance = new self( $plugin_file ); |
120 | | - self::$instance->init(); |
121 | | - } |
122 | | - |
123 | | - return self::$instance; |
124 | | - } |
125 | | - |
126 | | - /** |
127 | | - * Load the plugin textdomain. |
| 81 | + * Fires a custom action that other components can hook into |
| 82 | + * to perform activation-related tasks. |
128 | 83 | * |
129 | | - * @return void |
130 | 84 | * @since 1.0.0 |
131 | | - */ |
132 | | - public function load_textdomain(): void { |
133 | | - load_plugin_textdomain( |
134 | | - 'zior-simpliblocks', |
135 | | - false, |
136 | | - ZIOR_SIMPLIBLOCKS_PLUGIN_DIR . '/languages/', |
137 | | - ); |
138 | | - } |
139 | | - |
140 | | - /** |
141 | | - * Activate the plugin. |
142 | 85 | * |
143 | 86 | * @return void |
144 | | - * @since 1.0.0 |
145 | 87 | */ |
146 | | - public function activate_plugin(): void { |
| 88 | + public static function activate_plugin(): void { |
| 89 | + /** |
| 90 | + * Fires when the SimpliBlocks plugin is activated. |
| 91 | + * |
| 92 | + * @since 1.0.0 |
| 93 | + */ |
147 | 94 | do_action( 'zior_simpliblocks_activation' ); |
148 | 95 | } |
149 | 96 |
|
150 | 97 | /** |
151 | | - * Deactivate the plugin. |
| 98 | + * Plugin deactivation callback. |
| 99 | + * |
| 100 | + * Fires a custom action that other components can hook into |
| 101 | + * to perform deactivation-related cleanup. |
152 | 102 | * |
153 | | - * @return void |
154 | 103 | * @since 1.0.0 |
| 104 | + * |
| 105 | + * @return void |
155 | 106 | */ |
156 | | - public function deactivate_plugin(): void { |
| 107 | + public static function deactivate_plugin(): void { |
| 108 | + /** |
| 109 | + * Fires when the SimpliBlocks plugin is deactivated. |
| 110 | + * |
| 111 | + * @since 1.0.0 |
| 112 | + */ |
157 | 113 | do_action( 'zior_simpliblocks_deactivation' ); |
158 | 114 | } |
159 | 115 | } |
0 commit comments