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