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