ucfirst( $id ), 'description' => '', 'loader' => null, 'register_ui' => 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 === '' ) { $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'] ); Notification_Center::get()->register( [ 'slug' => 'adpresso-module-' . $id, 'message' => $module['label'] . ' is enabled', 'location' => 'admin_bar', 'context' => 'adpresso-modules-root', ] ); } } } if ( $one_active ) { Notification_Center::get()->register( [ 'slug' => 'adpresso-modules-root', 'message' => 'Modules', 'location' => 'admin_bar', 'context' => null, ] ); } do_action( 'adpresso_load_active_modules' ); } /** * 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'] ?? '' ) ); } } } }