PluginProbe
WP Console – WordPress PHP Console powered by PsySH / trunk
WP Console – WordPress PHP Console powered by PsySH vtrunk
trunk 2.4.0 2.4.1 2.5.0 2.5.1 2.6.0
wp-console / includes / WPConsole.php

WPConsole.php in WP Console – WordPress PHP Console powered by PsySH trunk, at includes/WPConsole.php

247 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPConsole;
4
5 use WPConsole\Traits\Singleton;
6
7 /**
8 * Main plugin class
9 *
10 * @since 1.0.0
11 */
12 final class WPConsole {
13
14 use Singleton;
15
16 /**
17 * Plugin version.
18 *
19 * @since 1.0.0
20 *
21 * @var string
22 */
23 public $version = '2.6.0';
24
25 /**
26 * Minimum PHP version required
27 *
28 * @since 1.0.0
29 *
30 * @var string
31 */
32 public $min_php = '5.6.0';
33
34 /**
35 * Admin notice messages
36 *
37 * @since 1.0.0
38 *
39 * @var array
40 */
41 private $admin_notices = [];
42
43 /**
44 * Contains chainable class instance
45 *
46 * Can be called via wp_console()->controller_name->feature.
47 *
48 * @since 1.0.0
49 *
50 * @var object
51 */
52 private $controllers = null;
53
54 /**
55 * Contains REST controller class instances
56 *
57 * Can be called via wp_console()->controller_name->feature.
58 *
59 * @since 1.0.0
60 *
61 * @var object
62 */
63 public $rest_controllers = null;
64
65 /**
66 * Bootstrap the plugin
67 *
68 * @since 1.0.0
69 *
70 * @return void
71 */
72 private function boot() {
73 if ( ! $this->met_requirements() ) {
74 add_action( 'admin_notices', [ $this, 'admin_notices' ] );
75 return;
76 }
77
78 add_action( 'plugins_loaded', [ $this, 'init_plugin' ] );
79 }
80
81 /**
82 * Getter to call the controller classes
83 *
84 * @since 1.0.0
85 *
86 * @param string $prop
87 *
88 * @return mixed
89 */
90 public function __get( $prop ) {
91 if ( isset( $this->controllers->$prop ) ) {
92 return $this->controllers->$prop;
93 }
94 }
95
96 /**
97 * Validate plugin requirements
98 *
99 * @since 1.0.0
100 *
101 * @return bool
102 */
103 private function met_requirements() {
104 if ( version_compare( PHP_VERSION, $this->min_php, '<' ) ) {
105 $this->admin_notices['unmet_php_version'] = sprintf(
106 '<strong>%s</strong> requires PHP version <strong>%s</strong> but you are using version <strong>%s</strong>',
107 'WP Console',
108 $this->min_php,
109 PHP_VERSION
110 );
111
112 return false;
113 }
114
115 return true;
116 }
117
118 /**
119 * Show admin notices
120 *
121 * @since 1.0.0
122 *
123 * @return void
124 */
125 public function admin_notices() {
126 foreach ( $this->admin_notices as $notice ) {
127 printf( '<div class="error"><p>' . $notice . '</p></div>' );
128 }
129 }
130
131 /**
132 * Initialize plugin logics
133 *
134 * @since 1.0.0
135 *
136 * @return void
137 */
138 public function init_plugin() {
139 /**
140 * Fires right before plugin loads its logics
141 *
142 * @since 1.0.0
143 */
144 do_action( 'wp_console_before_init' );
145
146 $this->define_constants();
147 $this->load_core();
148 $this->load_controllers();
149
150 add_action( 'init', [ $this, 'load_plugin_textdomain' ] );
151 add_action( 'rest_api_init', [ $this, 'load_rest_controllers' ] );
152
153 /**
154 * Fires after plugin finished loding its logics
155 *
156 * @since 1.0.0
157 */
158 do_action( 'wp_console_init' );
159 }
160
161 /**
162 * Define plugin constants
163 *
164 * @since 1.0.0
165 *
166 * @return void
167 */
168 private function define_constants() {
169 define( 'WP_CONSOLE_VERSION', $this->version );
170 define( 'WP_CONSOLE_INCLUDES', WP_CONSOLE_ABSPATH . '/includes' );
171 define( 'WP_CONSOLE_URL', plugins_url( '', WP_CONSOLE_FILE ) );
172 define( 'WP_CONSOLE_ASSETS', WP_CONSOLE_URL . '/assets' );
173 define( 'WP_CONSOLE_VIEWS', WP_CONSOLE_ABSPATH . '/views' );
174 }
175
176 /**
177 * Load plugin core
178 *
179 * @since 1.0.0
180 *
181 * @return void
182 */
183 private function load_core() {
184 new \WPConsole\Hooks();
185 new \WPConsole\Scripts();
186 new \WPConsole\AdminBar();
187 new \WPConsole\Core\Console\Console();
188 new \WPConsole\Core\DebugLog\DebugLog();
189 new \WPConsole\Core\UserSettings\UserSettings();
190
191 /**
192 * Fires after finished loading the plugin core
193 *
194 * @since 1.0.0
195 */
196 do_action( 'wp_console_core_loaded' );
197 }
198
199 /**
200 * Load plugin controllers
201 *
202 * These controllers are chainable and could be called
203 * like wp_console()->controller_name->feature.
204 *
205 * @since 1.0.0
206 *
207 * @return void
208 */
209 private function load_controllers() {
210 /**
211 * Add chainable controllers
212 *
213 * @since 1.0.0
214 *
215 * @var array
216 */
217 $this->controllers = apply_filters( 'wp_console_controllers', (object) [] );
218 }
219
220 /**
221 * Load plugin textdomain
222 *
223 * @since 1.0.0
224 *
225 * @return void
226 */
227 public function load_plugin_textdomain() {
228 load_plugin_textdomain( 'wp-console', false, dirname( plugin_basename( WP_CONSOLE_FILE ) ) . '/languages/' );
229 }
230
231 /**
232 * Load the REST API controllers
233 *
234 * @since 1.0.0
235 *
236 * @return void
237 */
238 public function load_rest_controllers() {
239 /**
240 * WP Console REST API controllers
241 *
242 * @since 1.0.0
243 */
244 $this->rest_controllers = apply_filters( 'wp_console_rest_controllers', (object) [] );
245 }
246 }
247