PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 2.3.0
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v2.3.0
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 / addons / lib / settings-service.php
cookiebot / addons / lib Last commit date
buffer 7 years ago ioc 7 years ago script-loader-tag 7 years ago autoloader.php 7 years ago cookie-consent-interface.php 7 years ago cookie-consent.php 7 years ago helper.php 7 years ago settings-service-interface.php 7 years ago settings-service.php 7 years ago
settings-service.php
542 lines
1 <?php
2
3 namespace cookiebot_addons\lib;
4
5 class Settings_Service implements Settings_Service_Interface {
6
7 /**
8 * @var \DI\Container
9 */
10 public $container;
11
12 CONST OPTION_NAME = 'cookiebot_available_addons';
13
14 /**
15 * Settings_Service constructor.
16 *
17 * @param $container
18 *
19 * @since 1.3.0
20 */
21 public function __construct( $container ) {
22 $this->container = $container;
23 }
24
25 /**
26 * Returns true if the addon is enabled in the backend
27 *
28 * @param $addon
29 *
30 * @return mixed
31 *
32 * @since 1.3.0
33 */
34 public function is_addon_enabled( $addon ) {
35 $option = get_option( static::OPTION_NAME );
36
37 if ( isset( $option[ $addon ]['enabled'] ) ) {
38 return true;
39 }
40
41 return false;
42 }
43
44 /**
45 * Returns true if the addon is installed
46 *
47 * @param $addon
48 *
49 * @return int|\WP_Error
50 *
51 * @since 1.3.0
52 */
53 public function is_addon_installed( $addon ) {
54 return ( $addon !== false && is_wp_error( validate_plugin( $addon ) ) ) ? false : true;
55 }
56
57 /**
58 * Returns the addon version
59 *
60 * @param $addon
61 *
62 * @return bool
63 *
64 * @since 2.2.1
65 */
66 public function get_addon_version( $addon ) {
67 $plugin_data = get_file_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $addon, array( 'Version' => 'version' ), false );
68
69 return ( isset( $plugin_data['Version'] ) ) ? $plugin_data['Version'] : false;
70 }
71
72 /**
73 * Returns true if the addon plugin is activated
74 *
75 * @param $addon
76 *
77 * @return bool
78 *
79 * @since 1.3.0
80 */
81 public function is_addon_activated( $addon ) {
82 return ( $addon === false || is_plugin_active( $addon ) ) ? true : false;
83 }
84
85 /**
86 * Returns all cookie type for given addon
87 *
88 * @param $addon string option name
89 * @param $default array default cookie types
90 *
91 * @return array
92 *
93 * @since 1.3.0
94 */
95 public function get_cookie_types( $addon, $default = array() ) {
96 $option = get_option( static::OPTION_NAME );
97
98 if ( isset( $option[ $addon ]['cookie_type'] ) && is_array( $option[ $addon ]['cookie_type'] ) ) {
99 return $option[ $addon ]['cookie_type'];
100 }
101
102 return $default;
103 }
104
105 /**
106 * Returns addons one by one through a generator
107 *
108 * @return array
109 * @throws \DI\DependencyException
110 * @throws \DI\NotFoundException
111 *
112 * @since 1.3.0
113 */
114 public function get_addons() {
115 $addons = array();
116
117 foreach ( $this->container->get( 'plugins' ) as $addon ) {
118 $addons[] = $this->container->get( $addon->class );
119 }
120
121 return $addons;
122 }
123
124 /**
125 * Returns active addons
126 *
127 * @return array
128 * @throws \DI\DependencyException
129 * @throws \DI\NotFoundException
130 *
131 * @since 1.3.0
132 */
133 public function get_active_addons() {
134 $active_addons = array();
135
136 foreach ( $this->get_addons() as $addon ) {
137 /**
138 * Load addon code if the plugin is active and addon is activated
139 */
140 if ( $addon->is_addon_enabled() && $addon->is_addon_installed() && $addon->is_addon_activated() ) {
141 $active_addons[] = $addon;
142 }
143 }
144
145 return $active_addons;
146 }
147
148 /**
149 * Returns widget cookie types
150 *
151 * @param $option_key
152 * @param $widget
153 * @param array $default
154 *
155 * @return array
156 *
157 * @since 1.3.0
158 */
159 public function get_widget_cookie_types( $option_key, $widget, $default = array() ) {
160 $option = get_option( $option_key );
161
162 if ( isset( $option[ $widget ]['cookie_type'] ) && is_array( $option[ $widget ]['cookie_type'] ) ) {
163 return $option[ $widget ]['cookie_type'];
164 }
165
166 return $default;
167 }
168
169 /**
170 * Is widget enabled
171 *
172 * @param $option_key
173 * @param $widget
174 *
175 * @return bool
176 */
177 public function is_widget_enabled( $option_key, $widget ) {
178 $option = get_option( $option_key );
179
180 if ( isset( $option[ $widget ] ) && ! isset( $option[ $widget ]['enabled'] ) ) {
181 return false;
182 }
183
184 return true;
185 }
186
187 /**
188 * Is placeholder enabled for a widget
189 *
190 * @param $option_key
191 * @param $widget
192 *
193 * @return bool
194 */
195 public function is_widget_placeholder_enabled( $option_key, $widget ) {
196 $option = get_option( $option_key );
197
198 if ( isset( $option[ $widget ] ) && ! isset( $option[ $widget ]['placeholder']['enabled'] ) ) {
199 return false;
200 }
201
202 return true;
203 }
204
205 /**
206 * Checks if addon has placeholders
207 *
208 * @param $option_key
209 *
210 * @return bool
211 *
212 * @since 1.8.0
213 */
214 public function widget_has_placeholder( $option_key, $widget_key ) {
215 $option = get_option( $option_key );
216
217 if ( isset( $option[ $widget_key ]['placeholder']['languages'] ) ) {
218 return true;
219 }
220
221 return false;
222 }
223
224 /**
225 * Returns widget placeholders
226 *
227 * @param $option_key
228 * @param $widget_key
229 *
230 * @return bool
231 *
232 * @since 1.8.0
233 */
234 public function get_widget_placeholders( $option_key, $widget_key ) {
235 $option = get_option( $option_key );
236
237 if ( isset( $option[ $widget_key ]['placeholder']['languages'] ) ) {
238 return $option[ $widget_key ]['placeholder']['languages'];
239 }
240
241 return false;
242 }
243
244 /**
245 * Returns all placeholders
246 *
247 * @param $option_key
248 *
249 * @return bool
250 *
251 * @since 1.8.0
252 */
253 public function get_placeholders( $option_key ) {
254 $option = get_option( static::OPTION_NAME );
255
256 if ( isset( $option[ $option_key ]['placeholder']['languages'] ) ) {
257 return $option[ $option_key ]['placeholder']['languages'];
258 }
259
260 return false;
261 }
262
263 /**
264 * Checks if addon has placeholders
265 *
266 * @param $option_key
267 *
268 * @return bool
269 *
270 * @since 1.8.0
271 */
272 public function has_placeholder( $option_key ) {
273 $option = get_option( static::OPTION_NAME );
274
275 if ( isset( $option[ $option_key ]['placeholder']['languages'] ) ) {
276 return true;
277 }
278
279 return false;
280 }
281
282 /**
283 * returns true if the addon placeholder is enabled
284 *
285 * @param $option_key
286 *
287 * @return bool
288 *
289 * @since 1.8.0
290 */
291 public function is_placeholder_enabled( $option_key ) {
292 $option = get_option( static::OPTION_NAME );
293
294 if ( isset( $option[ $option_key ]['placeholder']['enabled'] ) ) {
295 return true;
296 }
297
298 return false;
299 }
300
301 /**
302 * returns the placeholder if it does exist
303 *
304 * @param $option_key
305 * @param $default_placeholder
306 * @param $cookies
307 *
308 * @return bool|mixed
309 *
310 * @since 1.8.0
311 */
312 public function get_placeholder( $option_key, $default_placeholder, $cookies, $src = '' ) {
313 $option = get_option( static::OPTION_NAME );
314
315 if ( isset( $option[ $option_key ]['placeholder']['enabled'] ) ) {
316 return $this->get_translated_placeholder( $option, $option_key, $default_placeholder, $cookies, $src );
317 }
318
319 return false;
320 }
321
322 /**
323 * returns the placeholder if it does exist
324 *
325 * @param $option_key
326 * @param $default_placeholder
327 * @param $cookies
328 *
329 * @return bool|mixed
330 *
331 * @since 1.8.0
332 */
333 public function get_widget_placeholder( $option_key, $widget_key, $default_placeholder, $cookies = '' ) {
334 $option = get_option( $option_key );
335
336 if ( isset( $option[ $widget_key ]['placeholder']['enabled'] ) ) {
337 return $this->get_translated_placeholder( $option, $widget_key, $default_placeholder, $cookies );
338 }
339
340 return false;
341 }
342
343 /**
344 * Translates the placeholder text in the current page language
345 *
346 * @param $option
347 * @param $option_key
348 * @param $default_placeholder
349 * @param $cookies
350 * @param string $src
351 *
352 * @return mixed
353 *
354 * @since 1.9.0
355 */
356 private function get_translated_placeholder( $option, $option_key, $default_placeholder, $cookies, $src = '' ) {
357 $current_lang = cookiebot_addons_get_language();
358
359 if ( $current_lang == false || $current_lang == '' ) {
360 $current_lang = 'site-default';
361 }
362
363 /**
364 * Loop every language and match current language
365 */
366 if ( isset( $option[ $option_key ]['placeholder']['languages'] ) && is_array( $option[ $option_key ]['placeholder']['languages'] ) ) {
367 foreach ( $option[ $option_key ]['placeholder']['languages'] as $key => $value ) {
368
369 /**
370 * if current lang match with the prefix language in the database then get the text
371 */
372 if ( $key == $current_lang ) {
373 return $this->placeholder_merge_tag( $option[ $option_key ]['placeholder']['languages'][ $key ], $cookies, $src );
374 }
375 }
376 }
377
378 /**
379 * Returns site-default text if no match found.
380 */
381 if ( isset( $option[ $option_key ]['placeholder']['languages']['site-default'] ) ) {
382 return $this->placeholder_merge_tag( $option[ $option_key ]['placeholder']['languages']['site-default'], $cookies, $src );
383 }
384
385 /**
386 * Returns addon default placeholder (code)
387 */
388 return $this->placeholder_merge_tag( $default_placeholder, $cookies, $src );
389 }
390
391 /**
392 * Merges placeholder tags with values
393 *
394 * @param $placeholder
395 * @param $cookies
396 *
397 * @return mixed
398 *
399 * @since 1.8.0
400 */
401 private function placeholder_merge_tag( $placeholder, $cookies, $src ) {
402 if ( strpos( $placeholder, '%cookie_types' ) !== false ) {
403 $placeholder = str_replace( '%cookie_types', $cookies, $placeholder );
404 }
405
406 if ( strpos( $placeholder, '%src' ) !== false ) {
407 $placeholder = str_replace( '%src', $src, $placeholder );
408 }
409
410 if ( strpos( $placeholder, '[renew_consent]' ) !== false ) {
411 $placeholder = str_replace( '[renew_consent]', '<a href="javascript:Cookiebot.renew()">', $placeholder );
412 }
413
414 if ( strpos( $placeholder, '[/renew_consent]' ) !== false ) {
415 $placeholder = str_replace( '[/renew_consent]', '</a>', $placeholder );
416 }
417
418 return $placeholder;
419 }
420
421 /**
422 * returns true if the "remove tag" option is enabled
423 *
424 * @param $option_key
425 *
426 * @return bool
427 *
428 * @since 2.1.0
429 */
430 public function is_remove_tag_enabled( $option_key ) {
431 //Always return false if COOKIEBOT_OPTION_REMOVE_TAG
432 if ( ! defined( 'COOKIEBOT_OPTION_REMOVE_TAG' ) || ! COOKIEBOT_OPTION_REMOVE_TAG ) {
433 return false;
434 }
435
436 $option = get_option( static::OPTION_NAME );
437
438 if ( isset( $option[ $option_key ]['remove_tag'] ) ) {
439 return true;
440 }
441
442 return false;
443 }
444
445 /**
446 * returns true if the "remove tag" option is enabled
447 *
448 * @param $option_key
449 * @param $widget_key
450 *
451 * @return bool
452 *
453 * @since 2.1.0
454 */
455 public function is_widget_remove_tag_enabled( $option_key, $widget_key ) {
456 //Always return false if COOKIEBOT_OPTION_REMOVE_TAG
457 if ( ! defined( 'COOKIEBOT_OPTION_REMOVE_TAG' ) || ! COOKIEBOT_OPTION_REMOVE_TAG ) {
458 return false;
459 }
460
461 $option = get_option( $option_key );
462
463 if ( isset( $option[ $widget_key ]['remove_tag'] ) ) {
464 return true;
465 }
466
467 return false;
468 }
469
470 /**
471 * Check if the previous version is active
472 *
473 * @param $addons array List of addons
474 * @param $addon_class string The name of the class
475 *
476 * @return bool
477 *
478 * @since 2.1.3
479 */
480 public function is_previous_version_active( $addons, $addon_class ) {
481 foreach ( $addons as $addon ) {
482 $parent_class = $addon->get_parent_class();
483
484 if ( $parent_class !== false ) {
485 if ( $parent_class == $addon_class ) {
486 if ( $addon->is_addon_activated() ) {
487 return true;
488 }
489 }
490 }
491 }
492
493 return false;
494 }
495
496 /**
497 * Checks if the addon is the latest plugin version.
498 * Latest plugin version doesn't have extended class.
499 *
500 * @param $addon
501 *
502 * @return bool
503 *
504 * @since 2.1.3
505 */
506 public function is_latest_plugin_version( $addon ) {
507 return ( get_parent_class( $addon ) === false ) ? true : false;
508 }
509
510 /**
511 * Check if the addon option name matchs with the parameter
512 * then run the post_hook_after_enabling function in the addon class.
513 *
514 * @param $addon_option_name string Addon option name
515 *
516 * @throws \DI\DependencyException
517 * @throws \DI\NotFoundException
518 *
519 * @since 2.2.0
520 */
521 public function post_hook_after_enabling_addon_on_settings_page( $addon_option_name ) {
522 $addons = $this->get_addons();
523
524 foreach( $addons as $addon ) {
525 if( $addon->get_option_name() == $addon_option_name ) {
526 $addon->post_hook_after_enabling();
527 }
528 }
529 }
530
531 /**
532 * The cookiebot plugin is deactivated
533 * so run this function to cleanup the addons.
534 *
535 * @since 2.2.0
536 */
537 public function cookiebot_deactivated() {
538 foreach( $this->get_active_addons() as $addon ) {
539 $addon->plugin_deactivated();
540 }
541 }
542 }