abstracts
2 months ago
admin
2 months ago
ads
3 months ago
compatibility
3 months ago
crons
3 months ago
frontend
2 months ago
groups
3 months ago
importers
2 months ago
installation
1 year ago
interfaces
4 months ago
license
3 months ago
placements
3 months ago
rest
1 year ago
traits
4 months ago
utilities
3 months ago
array_ad_conditions.php
1 year ago
cap_map.php
3 years ago
class-assets-registry.php
3 months ago
class-autoloader.php
1 year ago
class-constants.php
1 year ago
class-content-injector.php
2 months ago
class-entities.php
3 months ago
class-modal.php
1 year ago
class-modules.php
1 year ago
class-options.php
1 year ago
class-plugin.php
2 months ago
class-post-data.php
10 months ago
class-shortcodes.php
2 months ago
class-upgrades.php
1 year ago
class-widget.php
11 months ago
default-hooks.php
4 months ago
functions-ad.php
1 year ago
functions-components.php
3 months ago
functions-conditional.php
1 year ago
functions-core.php
1 year ago
functions-group.php
1 year ago
functions-placement.php
1 year ago
functions.php
3 months ago
index.php
2 years ago
load_modules.php
2 years ago
class-modules.php
93 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Modules. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds; |
| 11 | |
| 12 | use AdvancedAds\Interfaces\Module_Interface; |
| 13 | use AdvancedAds\Framework\Interfaces\Initializer_Interface; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Modules. |
| 19 | */ |
| 20 | class Modules implements Initializer_Interface { |
| 21 | /** |
| 22 | * Modules. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | private $modules = []; |
| 27 | |
| 28 | /** |
| 29 | * Running modules. |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | private $running = []; |
| 34 | |
| 35 | /** |
| 36 | * Runs this initializer. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function initialize(): void { |
| 41 | add_action( 'init', [ $this, 'load_modules' ], 0 ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Register a module. |
| 46 | * |
| 47 | * @param string $module Module class name. |
| 48 | * |
| 49 | * @return void |
| 50 | */ |
| 51 | public function register_module( string $module ): void { |
| 52 | $module = new $module(); |
| 53 | $name = $module->get_name(); |
| 54 | |
| 55 | $this->modules[ $name ] = $module; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Load modules. |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public function load_modules(): void { |
| 64 | foreach ( $this->modules as $module ) { |
| 65 | if ( $this->can_load( $module ) ) { |
| 66 | $module->load(); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check if a module can be loaded. |
| 73 | * |
| 74 | * @param Module_Interface $module Module object. |
| 75 | * |
| 76 | * @return bool |
| 77 | */ |
| 78 | private function can_load( Module_Interface $module ): bool { |
| 79 | $check = apply_filters( 'advanced-ads-can-load-module', true, $module ); |
| 80 | if ( ! $check ) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if ( in_array( $module->get_name(), $this->running, true ) ) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | $this->running[] = $module->get_name(); |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 |