PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.4.2
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.4.2
4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.5 3.6.6 3.7.0 3.7.1 3.8.0 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.2.0 4.2.1 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.3.10 4.3.11 4.3.12 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.7.1 4.3.8 4.3.9 4.3.9.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.7.0
cookiebot / src / addons / Cookiebot_Addons.php
cookiebot / src / addons Last commit date
config 1 year ago controller 1 year ago Cookiebot_Addons.php 1 year ago addons.php 2 years ago
Cookiebot_Addons.php
218 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 /**
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 try {
54 self::$instance = new self();
55 } catch ( Exception $e ) {
56 echo 'Could not initialize Cookiebot addons: ' . esc_html( $e->getMessage() );
57 }
58 }
59
60 return self::$instance;
61 }
62
63 /**
64 * Cookiebot_Addons constructor.
65 *
66 * @throws Exception
67 *
68 * @since 1.3.0
69 */
70 public function __construct() {
71 $this->load_init_files();
72 $this->load_addons();
73 $this->build_container();
74 $this->assign_addons_to_container();
75 $this->assign_ignore_scripts_from_settings();
76
77 /**
78 * Load plugin controller to check if addons are active
79 * If active then load the plugin addon configuration class
80 * Else skip it
81 *
82 * @since 1.1.0
83 */
84 add_action(
85 'after_setup_theme',
86 array(
87 new Plugin_Controller( $this->container->get( 'Settings_Service_Interface' ) ),
88 'load_active_addons',
89 )
90 );
91 /**
92 * Load settings config
93 *
94 * @since 1.1.0
95 */
96 $settings = new Settings_Config( $this->container->get( 'Settings_Service_Interface' ) );
97 $settings->load();
98 }
99
100 /**
101 * Load init files to use 'validate_plugin' and 'is_plugin_active'
102 *
103 * @since 1.3.0
104 */
105 private function load_init_files() {
106 if ( ! function_exists( 'is_plugin_active' ) ) {
107 require_once ABSPATH . '/wp-admin/includes/plugin.php';
108 require_once ABSPATH . '/wp-admin/includes/translation-install.php';
109 require_once ABSPATH . '/wp-includes/l10n.php';
110 }
111 }
112
113 /**
114 * if the cookiebot is activated
115 * run this script to start up
116 *
117 * @throws Exception
118 * @since 2.2.0
119 */
120 public function cookiebot_activated() {
121 /** @var Settings_Service_Interface $settings_service */
122 $settings_service = $this->container->get( 'Settings_Service_Interface' );
123 $settings_service->cookiebot_activated();
124 }
125
126 /**
127 * if the cookiebot is deactivated
128 * run this script to clean up addons.
129 *
130 * @throws Exception
131 * @since 2.2.0
132 */
133 public function cookiebot_deactivated() {
134 /** @var Settings_Service_Interface $settings_service */
135 $settings_service = $this->container->get( 'Settings_Service_Interface' );
136 $settings_service->cookiebot_deactivated();
137 }
138
139 protected function load_addons() {
140 require_once __DIR__ . '/addons.php';
141 $this->addons_list = apply_filters(
142 'cybot_cookiebot_addons_list',
143 array_merge( PLUGIN_ADDONS, THEME_ADDONS, OTHER_ADDONS )
144 );
145 }
146
147 /**
148 * @throws Exception
149 */
150 protected function build_container() {
151 $dependencies = array(
152 'Script_Loader_Tag_Interface' => new Script_Loader_Tag(),
153 'Cookie_Consent_Interface' => new Cookie_Consent(),
154 'Buffer_Output_Interface' => new Buffer_Output(),
155 'addons_list' => $this->addons_list,
156 );
157
158 $this->container = new Dependency_Container( $dependencies );
159
160 $this->container->set(
161 'Settings_Service_Interface',
162 new Settings_Service( $this->container )
163 );
164 }
165
166 /**
167 * @throws Exception
168 */
169 protected function assign_addons_to_container() {
170 /**
171 * Check plugins one by one and load addon configuration
172 */
173 foreach ( $this->addons_list as $addon_class ) {
174 /**
175 * Load addon class to the container
176 */
177 if ( class_exists( $addon_class ) ) {
178 $addon = call_user_func(
179 array( $addon_class, 'get_instance' ),
180 $this->container->get( 'Settings_Service_Interface' ),
181 $this->container->get( 'Script_Loader_Tag_Interface' ),
182 $this->container->get( 'Cookie_Consent_Interface' ),
183 $this->container->get( 'Buffer_Output_Interface' )
184 );
185 if ( ! is_a( $addon, Base_Cookiebot_Addon::class ) ) {
186 throw new InvalidAddonClassException(
187 sprintf( 'Class %s could not be instantiated', $addon_class )
188 );
189 }
190 $this->container->set( $addon_class, $addon );
191 } else {
192 throw new InvalidAddonClassException(
193 sprintf( 'Class %s not found', $addon_class )
194 );
195 }
196 }
197 }
198
199 protected function assign_ignore_scripts_from_settings() {
200 $ignore_scripts = get_option( 'cookiebot-ignore-scripts' );
201
202 if ( empty( $ignore_scripts ) ) {
203 return;
204 }
205
206 $ignore_scripts = explode( PHP_EOL, $ignore_scripts );
207
208 /**
209 * @var Script_Loader_Tag_Interface
210 */
211 $script_loader_tag = $this->container->get( 'Script_Loader_Tag_Interface' );
212
213 foreach ( $ignore_scripts as $ignore_script ) {
214 $script_loader_tag->ignore_script( trim( $ignore_script ) );
215 }
216 }
217 }
218