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