class-loader.php
7.68 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* Class to boot up plugin.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\Core
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\Core;
// If this file is called directly, abort.
defined( 'WPINC' ) || die;
use DirectoryIterator;
use mysql_xdevapi\Exception;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
use function file_exists;
use function is_dir;
use function is_null;
use function str_replace;
use function strtolower;
use function trailingslashit;
/**
* Class WPMUDEV_BLC
*
* @package WPMUDEV_BLC\Core
*/
final class Loader extends Base {
/**
* Scripts to be registered for frontend.
*
* @since 2.0.0
*
* @var array $scripts Front js scripts to be enqueued.
*/
public static $scripts = array();
/**
* Scripts to be registered for backend.
*
* @since 2.0.0
*
* @var array $admin_scripts Admin js scripts to be enqueued.
*/
public static $admin_scripts = array();
/**
* Styles to be registered for frontend.
*
* @since 2.0.0
*
* @var array $style Front end styles to be enqueued.
*/
public static $styles = array();
/**
* Styles to be registered for backend.
*
* @since 2.0.0
*
* @var array $style Admin styles to be enqueued.
*/
public static $admin_styles = array();
/**
* Settings helper class instance.
*
* @since 2.0.0
* @var object
*
*/
public $settings;
/**
* Minimum supported php version.
*
* @since 2.0.0
* @var float
*
*/
public $php_version = '5.6';
/**
* Minimum WordPress version.
*
* @since 2.0.0
* @var float
*
*/
public $wp_version = '5.2';
/**
* Initialize functionality of the plugin.
*
* This is where we kick-start the plugin by defining
* everything required and register all hooks.
*
* @since 2.0.0
* @access protected
* @return void
*/
protected function __construct() {
if ( ! $this->can_boot() ) {
return;
}
$this->init();
}
/**
* Main condition that checks if plugin parts should conitnue loading.
*
* @return bool
*/
private function can_boot() {
/**
* Checks
* - PHP version
* - WP Version
* If not then return.
*/
global $wp_version;
return (
version_compare( PHP_VERSION, $this->php_version, '>' ) &&
version_compare( $wp_version, $this->wp_version, '>' )
);
}
/**
* Register all the actions and filters.
*
* @since 2.0.0
* @access private
* @return void
*/
private function init() {
// Initialize the core files and the app files.
// Core files are the base files that the app classes can rely on.
// Not all core files need to be initiated.
$this->init_app();
/*
* Setup plugin scripts
*/
add_action( 'wp_enqueue_scripts', array( $this, 'handle_front_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'handle_admin_scripts' ) );
/**
* Action hook to trigger after initializing all core actions.
*
* @since 2.0.0
*/
do_action( 'wpmudev_blc_after_core_init' );
}
/**
* Load all App modules.
*
* @since 2.0.0
* @return void
*/
public function init_app() {
/*
* Load plugin components. (Admin pages, Shortcodes, Rest Endpoints etc).
* Structures that build the plugins features and ui.
*/
$this->load_components(
apply_filters(
'wpmudev_blc_load_components',
array(
'Action_Links',
'Admin_Pages',
'Admin_Notices',
'Admin_Modals',
'Rest_Endpoints',
'Emails',
'Webhooks',
'Virtual_Posts',
'Scheduled_Events',
'Hub_Endpoints',
'Options',
)
)
);
}
/**
* Loads components.
*
* @since 2.0.0
*
* @param array $components An array of components (root folder names).
*/
private function load_components( $components = array() ) {
if ( ! empty( $components ) ) {
array_map(
array( $this, 'load_component' ),
$components
);
}
}
/**
* Registers and enqueues plugin scripts and styles for backend
*
* @since 2.0.0
*/
public function handle_admin_scripts() {
if ( ! empty( self::$admin_scripts ) ) {
$this->handle_scripts( self::$admin_scripts );
}
if ( ! empty( self::$admin_styles ) ) {
$this->handle_styles( self::$admin_styles );
}
}
/**
* Registers and enqueues plugin styles for frontend
*
* @since 2.0.0
*
* @param array $scripts An array with all scripts to be enqueued.
*
*/
public function handle_scripts( $scripts = array() ) {
if ( ! empty( $scripts ) ) {
foreach ( $scripts as $handle => $script ) {
$src = $script['src'] ?? '';
$deps = $script['deps'] ?? array();
$ver = $script['ver'] ?? WPMUDEV_BLC_SCIPTS_VERSION;
$in_footer = $script['in_footer'] ?? false;
$has_translation = $script['translate'] ?? false;
wp_register_script( $handle, $src, $deps, $ver, $in_footer );
if ( isset( $script['localize'] ) ) {
foreach ( $script['localize'] as $object_name => $localize_array ) {
wp_localize_script( $handle, $object_name, $localize_array );
}
}
wp_enqueue_script( $handle );
if ( $has_translation ) {
wp_set_script_translations(
$handle,
'broken-link-checker',
plugin_dir_path( __FILE__ ) . 'languages'
);
}
}
}
}
/**
* Registers and enqueues plugin css files.
*
* @since 2.0.0
*
* @param array $styles An array with all styles to be enqueued.
*
*/
public function handle_styles( $styles = array() ) {
if ( ! empty( $styles ) ) {
foreach ( $styles as $handle => $style ) {
$src = $style['src'] ?? '';
$deps = $style['deps'] ?? array();
$ver = $style['ver'] ?? WPMUDEV_BLC_SCIPTS_VERSION;
$media = $style['media'] ?? 'all';
wp_register_style( $handle, $src, $deps, $ver, $media );
wp_enqueue_style( $handle );
}
}
}
/**
* Registers and enqueues plugin scripts and styles for backend
*
* @since 2.0.0
*/
public function handle_front_scripts() {
if ( ! empty( self::$scripts ) ) {
$this->handle_scripts( self::$scripts );
}
if ( ! empty( self::$styles ) ) {
$this->handle_styles( self::$styles );
}
}
/**
* Loads component's controller.
*
* @since 2.0.0
*
* @param string $component The component name which is the folder name that contains the component files (mvc etc).
* @param string $namespace The namespace where the component belongs to. Default is App which derives from the `plugin_path/app` main folder.
*/
private function load_component( $component = null, $namespace = 'App' ) {
if ( ! is_null( $component ) ) {
$component_path_part = str_replace( '_', '-', $component );
$component_path = trailingslashit( WPMUDEV_BLC_DIR ) . strtolower( trailingslashit( $namespace ) . trailingslashit( $component_path_part ) );
if ( is_dir( $component_path ) ) {
$component_dir = new DirectoryIterator( $component_path );
foreach ( $component_dir as $fileinfo ) {
if ( $fileinfo->isDir() && ! $fileinfo->isDot() ) {
$component_item_dir = $fileinfo->getFilename();
$component_item = str_replace( '-', '_', $component_item_dir );
if ( file_exists( trailingslashit( $component_path ) . trailingslashit( $component_item_dir ) . 'class-controller.php' ) ) {
$component_item = "WPMUDEV_BLC\\{$namespace}\\{$component}\\{$component_item}\\Controller";
try {
if ( method_exists( $component_item::instance(), 'init' ) ) {
$component_item::instance()->init();
} else {
throw new \Exception( 'Method init() is missing from class ' . get_class( $component_item::instance() ) );
}
} catch ( \Exception $e ) {
error_log( $e->getMessage() );
}
}
}
}
}
}
}
}