ucfirst( $id ), 'description' => '', 'loader' => null, 'register_ui' => null, 'setting' => null, ] ); } /** * Get alle registered modules. * * @return array */ public static function get_registered(): array { return self::$modules; } /** * @param string $module * @param string $setting * * @return bool */ public static function is_active( string $module = '', string $setting = '' ): bool { $prefix = '_' . $module; if ( $setting === '' ) { $registered = self::$modules[ $module ] ?? []; $setting = $registered['setting'] ?? 'adpresso_settings_' . $module; } // We need to use the option directly. Can't use settings class because the settings aren't registered yet. // This is intentional because we want to check the module status before the settings are loaded. $module_option = get_option( $setting, [] ); $is_active = (bool) ( $module_option[ 'enable_module' . $prefix ] ?? false ); return apply_filters( 'adpresso_module_is_active', $is_active, $module, $setting ); } /** * This method is called when the ModuleManager instance is created via hook(). * It now hooks the loaders for *active* modules to a later WordPress action. */ public function hooks(): void { $one_active = false; foreach ( self::$modules as $id => $module ) { if ( self::is_active( $id, $module['setting'] ?? '' ) ) { $one_active = true; if ( is_callable( $module['loader'] ) ) { call_user_func( $module['loader'] ); self::notify_module_active( $id, $module ); } } } if ( $one_active ) { self::notify_modules_root(); } do_action( 'adpresso_load_active_modules' ); } /** * Admin-bar notice that a given module loaded successfully. * * @param string $id * @param array $module * * @return void */ private static function notify_module_active( string $id, array $module ): void { NotificationManager::get()->register( 'adpresso-module-' . $id, sprintf( '%s is enabled', $module['label'] ), [ 'location' => 'admin_bar', 'context' => 'adpresso-modules-root', ] ); } /** * Admin-bar parent notice, shown once when at least one module is active. * * @return void */ private static function notify_modules_root(): void { NotificationManager::get()->register( 'adpresso-modules-root', __( 'Modules', 'adpresso' ), [ 'location' => 'admin_bar', 'context' => 'null', ] ); } /** * Registers the UI for all active modules by calling their `register_ui` method. * @return void */ public static function register_all_ui(): void { foreach ( self::$modules as $id => $module ) { if ( is_callable( $module['register_ui'] ) ) { call_user_func( $module['register_ui'], self::is_active( $id, $module['setting'] ?? '' ) ); } } } }