abstracts
2 years ago
admin
1 year ago
database
2 years ago
groups
2 years ago
importers
1 year ago
installation
1 year ago
interfaces
1 year ago
traits
2 years ago
utilities
2 years ago
array_ad_conditions.php
3 years ago
cap_map.php
3 years ago
class-assets-registry.php
1 year ago
class-autoloader.php
1 year ago
class-constants.php
1 year ago
class-entities.php
2 years ago
class-plugin.php
1 year ago
functions.php
3 years ago
index.php
2 years ago
load_modules.php
2 years ago
load_modules.php
77 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
| 2 | use AdvancedAds\Autoloader; |
| 3 | |
| 4 | /** |
| 5 | * Class Advanced_Ads_ModuleLoader |
| 6 | * |
| 7 | * phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase |
| 8 | * phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
| 9 | */ |
| 10 | final class Advanced_Ads_ModuleLoader { |
| 11 | |
| 12 | /** |
| 13 | * Hold modules instances. |
| 14 | * |
| 15 | * @var array |
| 16 | */ |
| 17 | protected static $modules = []; |
| 18 | |
| 19 | /** |
| 20 | * Get the Composer autoloader. |
| 21 | * |
| 22 | * @deprecated 1.46.1 |
| 23 | * |
| 24 | * @return mixed |
| 25 | */ |
| 26 | public static function getLoader() { |
| 27 | _deprecated_function( __METHOD__, '1.46.1', '\AdvancedAds\Autoloader::get()->get_autoloader()' ); |
| 28 | return Autoloader::get()->get_autoloader(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Module loader options: |
| 33 | * - array 'disabled': Pretty name by (module) dirname |
| 34 | * |
| 35 | * @param string $path Path to modules. |
| 36 | * @param array $options Optional. Module loader options. |
| 37 | */ |
| 38 | public static function loadModules( $path, $options = [] ) { |
| 39 | $loader = Autoloader::get()->get_autoloader(); |
| 40 | |
| 41 | $isAdmin = is_admin(); |
| 42 | $disabledModules = (array) ( $options['disabled'] ?? [] ); |
| 43 | |
| 44 | // Iterate modules. |
| 45 | foreach ( glob( $path . '*/main.php' ) as $module ) { |
| 46 | $modulePath = dirname( $module ); |
| 47 | $moduleName = basename( $modulePath ); |
| 48 | |
| 49 | // Configuration is enabled by default (localisation, autoloading and other undemanding stuff). |
| 50 | if ( file_exists( $modulePath . '/config.php' ) ) { |
| 51 | $config = require $modulePath . '/config.php'; |
| 52 | // Append autoload classmap. |
| 53 | if ( isset( $config['classmap'] ) && is_array( $config['classmap'] ) ) { |
| 54 | $loader->addClassmap( $config['classmap'] ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Admin is enabled by default. |
| 59 | if ( $isAdmin && is_readable( $modulePath . '/admin.php' ) ) { |
| 60 | include $modulePath . '/admin.php'; // Do not care if this fails. |
| 61 | } |
| 62 | |
| 63 | // Skip if disabled. |
| 64 | if ( isset( $disabledModules[ $moduleName ] ) ) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | self::$modules[ $moduleName ] = $modulePath; |
| 69 | } |
| 70 | |
| 71 | // Load modules. |
| 72 | foreach ( self::$modules as $name => $path ) { |
| 73 | require_once $path . '/main.php'; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 |