controllers
6 months ago
external
6 months ago
interfaces
3 years ago
models
1 year ago
traits
1 year ago
utils
1 year ago
views
3 years ago
class-activation.php
3 years ago
class-deactivation.php
3 years ago
class-loader.php
6 months ago
class-loader.php
391 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class to boot up plugin. |
| 4 | * |
| 5 | * @link https://wordpress.org/plugins/broken-link-checker/ |
| 6 | * @since 2.0.0 |
| 7 | * |
| 8 | * @author WPMUDEV (https://wpmudev.com) |
| 9 | * @package WPMUDEV_BLC\Core |
| 10 | * |
| 11 | * @copyright (c) 2022, Incsub (http://incsub.com) |
| 12 | */ |
| 13 | |
| 14 | namespace WPMUDEV_BLC\Core; |
| 15 | |
| 16 | // If this file is called directly, abort. |
| 17 | defined( 'WPINC' ) || die; |
| 18 | |
| 19 | use DirectoryIterator; |
| 20 | use mysql_xdevapi\Exception; |
| 21 | use WPMUDEV_BLC\Core\Utils\Abstracts\Base; |
| 22 | use function file_exists; |
| 23 | use function is_dir; |
| 24 | use function is_null; |
| 25 | use function str_replace; |
| 26 | use function strtolower; |
| 27 | use function trailingslashit; |
| 28 | |
| 29 | /** |
| 30 | * Class WPMUDEV_BLC |
| 31 | * |
| 32 | * @package WPMUDEV_BLC\Core |
| 33 | */ |
| 34 | final class Loader extends Base { |
| 35 | |
| 36 | /** |
| 37 | * Scripts to be registered for frontend. |
| 38 | * |
| 39 | * @since 2.0.0 |
| 40 | * |
| 41 | * @var array $scripts Front js scripts to be enqueued. |
| 42 | */ |
| 43 | public static $scripts = array(); |
| 44 | |
| 45 | /** |
| 46 | * Scripts to be registered for backend. |
| 47 | * |
| 48 | * @since 2.0.0 |
| 49 | * |
| 50 | * @var array $admin_scripts Admin js scripts to be enqueued. |
| 51 | */ |
| 52 | public static $admin_scripts = array(); |
| 53 | |
| 54 | /** |
| 55 | * Styles to be registered for frontend. |
| 56 | * |
| 57 | * @since 2.0.0 |
| 58 | * |
| 59 | * @var array $style Front end styles to be enqueued. |
| 60 | */ |
| 61 | public static $styles = array(); |
| 62 | |
| 63 | /** |
| 64 | * Styles to be registered for backend. |
| 65 | * |
| 66 | * @since 2.0.0 |
| 67 | * |
| 68 | * @var array $style Admin styles to be enqueued. |
| 69 | */ |
| 70 | public static $admin_styles = array(); |
| 71 | /** |
| 72 | * Settings helper class instance. |
| 73 | * |
| 74 | * @since 2.0.0 |
| 75 | * @var object |
| 76 | */ |
| 77 | public $settings; |
| 78 | |
| 79 | /** |
| 80 | * Minimum supported php version. |
| 81 | * |
| 82 | * @since 2.0.0 |
| 83 | * @var float |
| 84 | */ |
| 85 | public $php_version = '7.2'; |
| 86 | |
| 87 | /** |
| 88 | * Minimum WordPress version. |
| 89 | * |
| 90 | * @since 2.0.0 |
| 91 | * @var float |
| 92 | */ |
| 93 | public $wp_version = '5.2'; |
| 94 | |
| 95 | /** |
| 96 | * Initialize functionality of the plugin. |
| 97 | * |
| 98 | * This is where we kick-start the plugin by defining |
| 99 | * everything required and register all hooks. |
| 100 | * |
| 101 | * @since 2.0.0 |
| 102 | * @access protected |
| 103 | * @return void |
| 104 | */ |
| 105 | protected function __construct() { |
| 106 | if ( ! $this->can_boot() ) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | $this->init(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Main condition that checks if plugin parts should conitnue loading. |
| 115 | * |
| 116 | * @return bool |
| 117 | */ |
| 118 | private function can_boot() { |
| 119 | /** |
| 120 | * Checks |
| 121 | * - PHP version |
| 122 | * - WP Version |
| 123 | * If not then return. |
| 124 | */ |
| 125 | global $wp_version; |
| 126 | |
| 127 | return ( |
| 128 | version_compare( PHP_VERSION, $this->php_version, '>' ) && |
| 129 | version_compare( $wp_version, $this->wp_version, '>' ) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Register all the actions and filters. |
| 135 | * |
| 136 | * @since 2.0.0 |
| 137 | * @access private |
| 138 | * @return void |
| 139 | */ |
| 140 | private function init() { |
| 141 | // Initialize the core files and the app files. |
| 142 | // Core files are the base files that the app classes can rely on. |
| 143 | // Not all core files need to be initiated. |
| 144 | |
| 145 | /** |
| 146 | * Static var to check if app is loaded. |
| 147 | */ |
| 148 | static $loaded = false; |
| 149 | |
| 150 | if ( $loaded ) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | $loaded = true; |
| 155 | |
| 156 | /** |
| 157 | * Start app functions. |
| 158 | */ |
| 159 | add_action( 'init', array( $this, 'init_app' ), 9 ); |
| 160 | |
| 161 | /* |
| 162 | * Setup plugin scripts |
| 163 | */ |
| 164 | add_action( 'wp_enqueue_scripts', array( $this, 'handle_front_scripts' ) ); |
| 165 | add_action( 'admin_enqueue_scripts', array( $this, 'handle_admin_scripts' ) ); |
| 166 | |
| 167 | /** |
| 168 | * Action hook to trigger after initializing all core actions. |
| 169 | * |
| 170 | * @since 2.0.0 |
| 171 | */ |
| 172 | do_action( 'wpmudev_blc_after_core_init' ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Load all App modules. |
| 177 | * |
| 178 | * @since 2.0.0 |
| 179 | * @return void |
| 180 | */ |
| 181 | public function init_app() { |
| 182 | /** |
| 183 | * Start Hub Connector |
| 184 | */ |
| 185 | $this->register_hub_connector(); |
| 186 | |
| 187 | // Load text domain. |
| 188 | load_plugin_textdomain( |
| 189 | 'broken-link-checker', |
| 190 | false, |
| 191 | dirname( WPMUDEV_BLC_BASENAME ) . '/languages' |
| 192 | ); |
| 193 | |
| 194 | /* |
| 195 | * Load plugin components. (Admin pages, Shortcodes, Rest Endpoints etc). |
| 196 | * Structures that build the plugins features and ui. |
| 197 | */ |
| 198 | $this->load_components( |
| 199 | apply_filters( |
| 200 | 'wpmudev_blc_load_components', |
| 201 | array( |
| 202 | 'Action_Links', |
| 203 | 'Admin_Pages', |
| 204 | 'Admin_Notices', |
| 205 | 'Admin_Modals', |
| 206 | 'Rest_Endpoints', |
| 207 | 'Emails', |
| 208 | 'Webhooks', |
| 209 | 'Virtual_Posts', |
| 210 | 'Scheduled_Events', |
| 211 | 'Hub_Endpoints', |
| 212 | 'Options', |
| 213 | 'Submodules', |
| 214 | ) |
| 215 | ) |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Register Hub connector submodule. |
| 221 | * |
| 222 | * @return void |
| 223 | */ |
| 224 | public function register_hub_connector() { |
| 225 | if ( ! file_exists( WPMUDEV_BLC_DIR . 'core/external/hub-connector/connector.php' ) ) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | require_once WPMUDEV_BLC_DIR . 'core/external/hub-connector/connector.php'; |
| 230 | |
| 231 | $options = array( |
| 232 | 'screens' => array( 'toplevel_page_blc_dash' ), |
| 233 | 'extra_args' => array( |
| 234 | 'register' => array( |
| 235 | 'connect_ref' => 'blc', |
| 236 | 'utm_source' => 'blc', |
| 237 | 'utm_medium' => 'plugin', |
| 238 | 'utm_campaign' => 'blc_connector_main', |
| 239 | ), |
| 240 | ), |
| 241 | ); |
| 242 | |
| 243 | \WPMUDEV\Hub\Connector::get()->set_options( 'blc', $options ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Loads components. |
| 248 | * |
| 249 | * @since 2.0.0 |
| 250 | * |
| 251 | * @param array $components An array of components (root folder names). |
| 252 | */ |
| 253 | private function load_components( $components = array() ) { |
| 254 | if ( ! empty( $components ) ) { |
| 255 | array_map( |
| 256 | array( $this, 'load_component' ), |
| 257 | $components |
| 258 | ); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Registers and enqueues plugin scripts and styles for backend |
| 264 | * |
| 265 | * @since 2.0.0 |
| 266 | */ |
| 267 | public function handle_admin_scripts() { |
| 268 | if ( ! empty( self::$admin_scripts ) ) { |
| 269 | $this->handle_scripts( self::$admin_scripts ); |
| 270 | } |
| 271 | |
| 272 | if ( ! empty( self::$admin_styles ) ) { |
| 273 | $this->handle_styles( self::$admin_styles ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Registers and enqueues plugin styles for frontend |
| 279 | * |
| 280 | * @since 2.0.0 |
| 281 | * |
| 282 | * @param array $scripts An array with all scripts to be enqueued. |
| 283 | */ |
| 284 | public function handle_scripts( $scripts = array() ) { |
| 285 | if ( ! empty( $scripts ) ) { |
| 286 | foreach ( $scripts as $handle => $script ) { |
| 287 | $src = $script['src'] ?? ''; |
| 288 | $deps = $script['deps'] ?? array(); |
| 289 | $ver = $script['ver'] ?? WPMUDEV_BLC_SCIPTS_VERSION; |
| 290 | $in_footer = $script['in_footer'] ?? false; |
| 291 | $has_translation = $script['translate'] ?? false; |
| 292 | |
| 293 | wp_register_script( $handle, $src, $deps, $ver, $in_footer ); |
| 294 | |
| 295 | if ( isset( $script['localize'] ) ) { |
| 296 | foreach ( $script['localize'] as $object_name => $localize_array ) { |
| 297 | wp_localize_script( $handle, $object_name, $localize_array ); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | wp_enqueue_script( $handle ); |
| 302 | |
| 303 | if ( $has_translation ) { |
| 304 | wp_set_script_translations( |
| 305 | $handle, |
| 306 | 'broken-link-checker' |
| 307 | ); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Registers and enqueues plugin css files. |
| 315 | * |
| 316 | * @since 2.0.0 |
| 317 | * |
| 318 | * @param array $styles An array with all styles to be enqueued. |
| 319 | */ |
| 320 | public function handle_styles( $styles = array() ) { |
| 321 | if ( ! empty( $styles ) ) { |
| 322 | foreach ( $styles as $handle => $style ) { |
| 323 | $src = $style['src'] ?? ''; |
| 324 | $deps = $style['deps'] ?? array(); |
| 325 | $ver = $style['ver'] ?? WPMUDEV_BLC_SCIPTS_VERSION; |
| 326 | $media = $style['media'] ?? 'all'; |
| 327 | |
| 328 | wp_register_style( $handle, $src, $deps, $ver, $media ); |
| 329 | wp_enqueue_style( $handle ); |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Registers and enqueues plugin scripts and styles for backend |
| 336 | * |
| 337 | * @since 2.0.0 |
| 338 | */ |
| 339 | public function handle_front_scripts() { |
| 340 | if ( ! empty( self::$scripts ) ) { |
| 341 | $this->handle_scripts( self::$scripts ); |
| 342 | } |
| 343 | |
| 344 | if ( ! empty( self::$styles ) ) { |
| 345 | $this->handle_styles( self::$styles ); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Loads component's controller. |
| 351 | * |
| 352 | * @since 2.0.0 |
| 353 | * |
| 354 | * @param string $component The component name which is the folder name that contains the component files (mvc etc). |
| 355 | * @param string $namespace_prefix The namespace where the component belongs to. Default is App which derives from the `plugin_path/app` main folder. |
| 356 | * @throws \Exception If class controller file not found. |
| 357 | */ |
| 358 | private function load_component( $component = null, $namespace_prefix = 'App' ) { |
| 359 | if ( ! is_null( $component ) ) { |
| 360 | $component_path_part = str_replace( '_', '-', $component ); |
| 361 | $component_path = trailingslashit( WPMUDEV_BLC_DIR ) . strtolower( trailingslashit( $namespace_prefix ) . trailingslashit( $component_path_part ) ); |
| 362 | |
| 363 | if ( is_dir( $component_path ) ) { |
| 364 | $component_dir = new DirectoryIterator( $component_path ); |
| 365 | |
| 366 | foreach ( $component_dir as $fileinfo ) { |
| 367 | if ( $fileinfo->isDir() && ! $fileinfo->isDot() ) { |
| 368 | $component_item_dir = $fileinfo->getFilename(); |
| 369 | $component_item = str_replace( '-', '_', $component_item_dir ); |
| 370 | |
| 371 | if ( file_exists( trailingslashit( $component_path ) . trailingslashit( $component_item_dir ) . 'class-controller.php' ) ) { |
| 372 | $component_item = "WPMUDEV_BLC\\{$namespace_prefix}\\{$component}\\{$component_item}\\Controller"; |
| 373 | |
| 374 | try { |
| 375 | if ( method_exists( $component_item::instance(), 'init' ) ) { |
| 376 | $component_item::instance()->init(); |
| 377 | } else { |
| 378 | throw new \Exception( 'Method init() is missing from class ' . get_class( $component_item::instance() ) ); |
| 379 | } |
| 380 | } catch ( \Exception $e ) { |
| 381 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 382 | error_log( $e->getMessage() ); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 |