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