| @@ -1,39 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace RadiusTheme\SB\Modules; |
| 4 | - | |
| 5 | 4 | defined( 'ABSPATH' ) || exit(); |
| 6 | 5 | |
| 7 | 6 | use RadiusTheme\SB\Models\ModuleList; |
| 7 | + | |
| 8 | 8 | use RadiusTheme\SB\Traits\SingletonTrait; |
| 9 | 9 | |
| 10 | -/** | |
| 11 | - * Module Manager class. | |
| 12 | - * | |
| 13 | - * Handles loading and instantiation of all plugin modules. | |
| 14 | - */ | |
| 15 | 10 | class ModuleManager { |
| 16 | 11 | |
| 17 | 12 | use SingletonTrait; |
| 18 | 13 | |
| 19 | - /** | |
| 20 | - * Module list. | |
| 21 | - * | |
| 22 | - * @var array|null | |
| 23 | - */ | |
| 24 | 14 | private $module_list; |
| 25 | 15 | |
| 26 | - /** | |
| 27 | - * Module instances. | |
| 28 | - * | |
| 29 | - * @var array | |
| 30 | - */ | |
| 31 | - private $module_instances = []; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * Constructor. | |
| 35 | - */ | |
| 16 | + private $module_instances = array(); | |
| 36 | 17 | private function __construct() { |
| 37 | 18 | add_action( 'init', [ $this, 'load_modules' ], 15 ); |
| 38 | 19 | } |
| 39 | 20 | |
| @@ -62,14 +43,21 @@ | ||
| 62 | 43 | if ( ! class_exists( $module['base_class'] ) ) { |
| 63 | 44 | continue; |
| 64 | 45 | } |
| 65 | 46 | |
| 66 | - try { | |
| 67 | - $module['base_class']::instance(); | |
| 68 | - } catch ( \Throwable $e ) { | |
| 69 | - error_log( 'ShopBuilder module error (' . $module['base_class'] . '): ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log | |
| 70 | - continue; | |
| 47 | + // Check if the instance is already in the cache | |
| 48 | + $cached_instance = wp_cache_get( $module['base_class'], 'module_instances' ); | |
| 49 | + if ( false === $cached_instance ) { | |
| 50 | + // Create an instance and store it in the cache | |
| 51 | + $this->module_instances[ $module['base_class'] ] = $module['base_class']::instance(); | |
| 52 | + wp_cache_set( $module['base_class'], $this->module_instances[ $module['base_class'] ], 'module_instances' ); | |
| 53 | + } else { | |
| 54 | + // Use the cached instance | |
| 55 | + $this->module_instances[ $module['base_class'] ] = $cached_instance; | |
| 71 | 56 | } |
| 72 | 57 | |
| 73 | 58 | } |
| 59 | + | |
| 74 | 60 | } |
| 75 | -} | |
| 61 | + | |
| 62 | + | |
| 63 | +} | |