Cookiebot_Addons.php
191 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\addons; |
| 4 | |
| 5 | use cybot\cookiebot\addons\config\Settings_Config; |
| 6 | use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Addon; |
| 7 | use cybot\cookiebot\addons\controller\Plugin_Controller; |
| 8 | use cybot\cookiebot\lib\buffer\Buffer_Output; |
| 9 | use cybot\cookiebot\lib\Cookie_Consent; |
| 10 | use cybot\cookiebot\lib\Dependency_Container; |
| 11 | use cybot\cookiebot\lib\script_loader_tag\Script_Loader_Tag; |
| 12 | use cybot\cookiebot\lib\Settings_Service; |
| 13 | use cybot\cookiebot\lib\Settings_Service_Interface; |
| 14 | use Exception; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; |
| 18 | } // Exit if accessed directly |
| 19 | |
| 20 | class Cookiebot_Addons { |
| 21 | |
| 22 | /** |
| 23 | * @var Dependency_Container |
| 24 | */ |
| 25 | public $container; |
| 26 | |
| 27 | /** |
| 28 | * @var array |
| 29 | */ |
| 30 | private $addons_list = array(); |
| 31 | |
| 32 | /** |
| 33 | * @var Cookiebot_Addons |
| 34 | */ |
| 35 | private static $instance; |
| 36 | |
| 37 | /** |
| 38 | * Main Cookiebot_WP Instance |
| 39 | * |
| 40 | * Ensures only one instance of Cookiebot_Addons is loaded or can be loaded. |
| 41 | * |
| 42 | * @return Cookiebot_Addons |
| 43 | * @since 2.2.0 |
| 44 | * @static |
| 45 | * |
| 46 | * @version 2.2.0 |
| 47 | */ |
| 48 | public static function instance() { |
| 49 | if ( ! is_a( self::$instance, self::class ) ) { |
| 50 | try { |
| 51 | self::$instance = new self(); |
| 52 | } catch ( Exception $e ) { |
| 53 | echo 'Could not initialize Cookiebot addons: ' . esc_html( $e->getMessage() ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return self::$instance; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Cookiebot_Addons constructor. |
| 62 | * |
| 63 | * @throws Exception |
| 64 | * |
| 65 | * @since 1.3.0 |
| 66 | */ |
| 67 | public function __construct() { |
| 68 | $this->load_init_files(); |
| 69 | $this->load_addons(); |
| 70 | $this->build_container(); |
| 71 | $this->assign_addons_to_container(); |
| 72 | |
| 73 | /** |
| 74 | * Load plugin controller to check if addons are active |
| 75 | * If active then load the plugin addon configuration class |
| 76 | * Else skip it |
| 77 | * |
| 78 | * @since 1.1.0 |
| 79 | */ |
| 80 | add_action( |
| 81 | 'after_setup_theme', |
| 82 | array( |
| 83 | new Plugin_Controller( $this->container->get( 'Settings_Service_Interface' ) ), |
| 84 | 'load_active_addons', |
| 85 | ) |
| 86 | ); |
| 87 | /** |
| 88 | * Load settings config |
| 89 | * |
| 90 | * @since 1.1.0 |
| 91 | */ |
| 92 | $settings = new Settings_Config( $this->container->get( 'Settings_Service_Interface' ) ); |
| 93 | $settings->load(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Load init files to use 'validate_plugin' and 'is_plugin_active' |
| 98 | * |
| 99 | * @since 1.3.0 |
| 100 | */ |
| 101 | private function load_init_files() { |
| 102 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 103 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 104 | require_once ABSPATH . '/wp-admin/includes/translation-install.php'; |
| 105 | require_once ABSPATH . '/wp-includes/l10n.php'; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * if the cookiebot is activated |
| 111 | * run this script to start up |
| 112 | * |
| 113 | * @throws Exception |
| 114 | * @since 2.2.0 |
| 115 | */ |
| 116 | public function cookiebot_activated() { |
| 117 | /** @var Settings_Service_Interface $settings_service */ |
| 118 | $settings_service = $this->container->get( 'Settings_Service_Interface' ); |
| 119 | $settings_service->cookiebot_activated(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * if the cookiebot is deactivated |
| 124 | * run this script to clean up addons. |
| 125 | * |
| 126 | * @throws Exception |
| 127 | * @since 2.2.0 |
| 128 | */ |
| 129 | public function cookiebot_deactivated() { |
| 130 | /** @var Settings_Service_Interface $settings_service */ |
| 131 | $settings_service = $this->container->get( 'Settings_Service_Interface' ); |
| 132 | $settings_service->cookiebot_deactivated(); |
| 133 | } |
| 134 | |
| 135 | protected function load_addons() { |
| 136 | require_once 'addons.php'; |
| 137 | $this->addons_list = apply_filters( |
| 138 | 'cybot_cookiebot_addons_list', |
| 139 | array_merge( PLUGIN_ADDONS, THEME_ADDONS, OTHER_ADDONS ) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @throws Exception |
| 145 | */ |
| 146 | protected function build_container() { |
| 147 | $dependencies = array( |
| 148 | 'Script_Loader_Tag_Interface' => new Script_Loader_Tag(), |
| 149 | 'Cookie_Consent_Interface' => new Cookie_Consent(), |
| 150 | 'Buffer_Output_Interface' => new Buffer_Output(), |
| 151 | 'addons_list' => $this->addons_list, |
| 152 | ); |
| 153 | |
| 154 | $this->container = new Dependency_Container( $dependencies ); |
| 155 | |
| 156 | $this->container->set( |
| 157 | 'Settings_Service_Interface', |
| 158 | new Settings_Service( $this->container ) |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @throws Exception |
| 164 | */ |
| 165 | protected function assign_addons_to_container() { |
| 166 | /** |
| 167 | * Check plugins one by one and load addon configuration |
| 168 | */ |
| 169 | foreach ( $this->addons_list as $addon_class ) { |
| 170 | /** |
| 171 | * Load addon class to the container |
| 172 | */ |
| 173 | if ( class_exists( $addon_class ) ) { |
| 174 | $addon = call_user_func( |
| 175 | array( $addon_class, 'get_instance' ), |
| 176 | $this->container->get( 'Settings_Service_Interface' ), |
| 177 | $this->container->get( 'Script_Loader_Tag_Interface' ), |
| 178 | $this->container->get( 'Cookie_Consent_Interface' ), |
| 179 | $this->container->get( 'Buffer_Output_Interface' ) |
| 180 | ); |
| 181 | if ( ! is_a( $addon, Base_Cookiebot_Addon::class ) ) { |
| 182 | throw new Exception( 'Class ' . $addon_class . ' could not be instantiated' ); |
| 183 | } |
| 184 | $this->container->set( $addon_class, $addon ); |
| 185 | } else { |
| 186 | throw new Exception( 'Class ' . $addon_class . ' not found' ); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 |