| 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 |
/** |
| 26 |
* @var Dependency_Container |
| 27 |
*/ |
| 28 |
public $container; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
private $addons_list = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Cookiebot_Addons |
| 37 |
*/ |
| 38 |
private static $instance; |
| 39 |
|
| 40 |
/** |
| 41 |
* Main Cookiebot_WP Instance |
| 42 |
* |
| 43 |
* Ensures only one instance of Cookiebot_Addons is loaded or can be loaded. |
| 44 |
* |
| 45 |
* @return Cookiebot_Addons |
| 46 |
* @since 2.2.0 |
| 47 |
* @static |
| 48 |
* |
| 49 |
* @version 2.2.0 |
| 50 |
*/ |
| 51 |
public static function instance() { |
| 52 |
if ( ! is_a( self::$instance, self::class ) ) { |
| 53 |
self::$instance = new self(); |
| 54 |
} |
| 55 |
|
| 56 |
return self::$instance; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Cookiebot_Addons constructor. |
| 61 |
* |
| 62 |
* @throws Exception |
| 63 |
* |
| 64 |
* @since 1.3.0 |
| 65 |
*/ |
| 66 |
public function __construct() { |
| 67 |
$this->load_init_files(); |
| 68 |
$this->load_addons(); |
| 69 |
$this->build_container(); |
| 70 |
$this->assign_addons_to_container(); |
| 71 |
$this->assign_ignore_scripts_from_settings(); |
| 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 __DIR__ . '/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 InvalidAddonClassException( |
| 183 |
sprintf( 'Class %s could not be instantiated', esc_html( $addon_class ) ) |
| 184 |
); |
| 185 |
} |
| 186 |
$this->container->set( $addon_class, $addon ); |
| 187 |
} else { |
| 188 |
throw new InvalidAddonClassException( |
| 189 |
sprintf( 'Class %s not found', esc_html( $addon_class ) ) |
| 190 |
); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
protected function assign_ignore_scripts_from_settings() { |
| 196 |
$ignore_scripts = get_option( 'cookiebot-ignore-scripts' ); |
| 197 |
|
| 198 |
if ( empty( $ignore_scripts ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
$ignore_scripts = explode( PHP_EOL, $ignore_scripts ); |
| 203 |
|
| 204 |
/** |
| 205 |
* @var Script_Loader_Tag_Interface |
| 206 |
*/ |
| 207 |
$script_loader_tag = $this->container->get( 'Script_Loader_Tag_Interface' ); |
| 208 |
|
| 209 |
foreach ( $ignore_scripts as $ignore_script ) { |
| 210 |
$script_loader_tag->ignore_script( trim( $ignore_script ) ); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|