PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.3.0
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.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 / src / lib / Settings_Service.php
cookiebot / src / lib Last commit date
buffer 3 years ago script_loader_tag 2 years ago traits 3 years ago Consent_API_Helper.php 3 years ago Cookie_Consent.php 3 years ago Cookie_Consent_Interface.php 4 years ago Cookiebot_Activated.php 3 years ago Cookiebot_Automatic_Updates.php 3 years ago Cookiebot_Deactivated.php 4 years ago Cookiebot_Javascript_Helper.php 2 years ago Cookiebot_Review.php 2 years ago Cookiebot_WP.php 2 years ago Dependency_Container.php 3 years ago Settings_Page_Tab.php 3 years ago Settings_Service.php 3 years ago Settings_Service_Interface.php 3 years ago Supported_Languages.php 4 years ago Supported_Regions.php 3 years ago WP_Rocket_Helper.php 3 years ago Widgets.php 3 years ago global-deprecations.php 3 years ago helper.php 2 years ago
Settings_Service.php
462 lines
1 <?php
2
3 namespace cybot\cookiebot\lib;
4
5 use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Addon;
6 use Exception;
7 use Generator;
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 bool
35 *
36 * @since 1.3.0
37 */
38 public function is_addon_enabled( $addon ) {
39 $option = get_option( static::OPTION_NAME );
40
41 return isset( $option[ $addon ]['enabled'] );
42 }
43
44 /**
45 * Returns all cookie type for given addon
46 *
47 * @param $addon string option name
48 * @param $default array default cookie types
49 *
50 * @return array
51 *
52 * @since 1.3.0
53 */
54 public function get_cookie_types( $addon, $default = array() ) {
55 $option = get_option( static::OPTION_NAME );
56
57 if ( isset( $option[ $addon ]['cookie_type'] ) && is_array( $option[ $addon ]['cookie_type'] ) ) {
58 return $option[ $addon ]['cookie_type'];
59 }
60
61 return $default;
62 }
63
64 /**
65 * Returns regex for given addon
66 *
67 * @param $addon string option name
68 * @param $default string default regex
69 *
70 * @return string
71 *
72 * @since 2.4.5
73 */
74 public function get_addon_regex( $addon, $default = '' ) {
75 $option = get_option( static::OPTION_NAME );
76
77 if ( isset( $option[ $addon ]['regex'] ) ) {
78 return $option[ $addon ]['regex'];
79 }
80
81 return $default;
82 }
83
84 /**
85 * @return Generator
86 * @throws Exception
87 */
88 public function get_addons() {
89 foreach ( $this->container->get( 'addons_list' ) as $addon ) {
90 yield $this->container->get( $addon );
91 }
92 }
93
94 /**
95 * Returns active addons
96 *
97 * @return array
98 * @throws Exception
99 *
100 * @since 1.3.0
101 */
102 public function get_active_addons() {
103 $active_addons = array();
104
105 foreach ( $this->get_addons() as $addon ) {
106 /**
107 * @var $addon Base_Cookiebot_Addon
108 * Load addon code if the plugin is active and addon is activated
109 */
110 if ( $addon->is_addon_enabled() && $addon->is_addon_installed() && $addon->is_addon_activated() ) {
111 $active_addons[] = $addon;
112 }
113 }
114
115 return $active_addons;
116 }
117
118 /**
119 * Returns widget cookie types
120 *
121 * @param $option_key
122 * @param $widget
123 * @param array $default
124 *
125 * @return array
126 *
127 * @since 1.3.0
128 */
129 public function get_widget_cookie_types( $option_key, $widget, $default = array() ) {
130 $option = get_option( $option_key );
131
132 if ( isset( $option[ $widget ]['cookie_type'] ) && is_array( $option[ $widget ]['cookie_type'] ) ) {
133 return $option[ $widget ]['cookie_type'];
134 }
135
136 return $default;
137 }
138
139 /**
140 * Is widget enabled
141 *
142 * @param $option_key
143 * @param $widget
144 *
145 * @return bool
146 */
147 public function is_widget_enabled( $option_key, $widget ) {
148 $option = get_option( $option_key );
149
150 if ( isset( $option[ $widget ] ) && ! isset( $option[ $widget ]['enabled'] ) ) {
151 return false;
152 }
153
154 return true;
155 }
156
157 /**
158 * Is placeholder enabled for a widget
159 *
160 * @param $option_key
161 * @param $widget
162 *
163 * @return bool
164 */
165 public function is_widget_placeholder_enabled( $option_key, $widget ) {
166 $option = get_option( $option_key );
167
168 if ( isset( $option[ $widget ] ) && ! isset( $option[ $widget ]['placeholder']['enabled'] ) ) {
169 return false;
170 }
171
172 return true;
173 }
174
175 /**
176 * Checks if addon has placeholders
177 *
178 * @param $option_key
179 * @param $widget_key
180 *
181 * @return bool
182 *
183 * @since 1.8.0
184 */
185 public function widget_has_placeholder( $option_key, $widget_key ) {
186 $option = get_option( $option_key );
187
188 if ( isset( $option[ $widget_key ]['placeholder']['languages'] ) ) {
189 return true;
190 }
191
192 return false;
193 }
194
195 /**
196 * @param $option_key
197 * @param $widget_key
198 *
199 * @return array
200 */
201 public function get_widget_placeholders( $option_key, $widget_key ) {
202 $option = get_option( $option_key );
203
204 if ( isset( $option[ $widget_key ]['placeholder']['languages'] ) &&
205 is_array( $option[ $widget_key ]['placeholder']['languages'] ) ) {
206 return (array) $option[ $widget_key ]['placeholder']['languages'];
207 }
208
209 return array();
210 }
211
212 /**
213 * @param $option_key
214 *
215 * @return array
216 */
217 public function get_placeholders( $option_key ) {
218 $option = get_option( static::OPTION_NAME );
219
220 if ( isset( $option[ $option_key ]['placeholder']['languages'] ) &&
221 is_array( $option[ $option_key ]['placeholder']['languages'] ) ) {
222 return (array) $option[ $option_key ]['placeholder']['languages'];
223 }
224
225 return array();
226 }
227
228 /**
229 * Checks if addon has placeholders
230 *
231 * @param $option_key
232 *
233 * @return bool
234 *
235 * @since 1.8.0
236 */
237 public function has_placeholder( $option_key ) {
238 $option = get_option( static::OPTION_NAME );
239
240 if ( isset( $option[ $option_key ]['placeholder']['languages'] ) ) {
241 return true;
242 }
243
244 return false;
245 }
246
247 /**
248 * returns true if the addon placeholder is enabled
249 *
250 * @param $option_key
251 *
252 * @return bool
253 *
254 * @since 1.8.0
255 */
256 public function is_placeholder_enabled( $option_key ) {
257 $option = get_option( static::OPTION_NAME );
258
259 if ( isset( $option[ $option_key ]['placeholder']['enabled'] ) ) {
260 return true;
261 }
262
263 return false;
264 }
265
266 /**
267 * returns the placeholder if it does exist
268 *
269 * @param $option_key
270 * @param $default_placeholder
271 * @param $cookies
272 * @param string $src
273 *
274 * @return bool|mixed
275 *
276 * @since 1.8.0
277 */
278 public function get_placeholder( $option_key, $default_placeholder, $cookies, $src = '' ) {
279 $option = get_option( static::OPTION_NAME );
280
281 if ( isset( $option[ $option_key ]['placeholder']['enabled'] ) ) {
282 return $this->get_translated_placeholder( $option, $option_key, $default_placeholder, $cookies, $src );
283 }
284
285 return false;
286 }
287
288 /**
289 * returns the placeholder if it does exist
290 *
291 * @param $option_key
292 * @param $widget_key
293 * @param $default_placeholder
294 * @param string $cookies
295 *
296 * @return bool|mixed
297 *
298 * @since 1.8.0
299 */
300 public function get_widget_placeholder( $option_key, $widget_key, $default_placeholder, $cookies = '' ) {
301 $option = get_option( $option_key );
302
303 if ( isset( $option[ $widget_key ]['placeholder']['enabled'] ) ) {
304 return $this->get_translated_placeholder( $option, $widget_key, $default_placeholder, $cookies );
305 }
306
307 return false;
308 }
309
310 /**
311 * Translates the placeholder text in the current page language
312 *
313 * @param $option
314 * @param $option_key
315 * @param $default_placeholder
316 * @param $cookies
317 * @param string $src
318 *
319 * @return mixed
320 *
321 * @since 1.9.0
322 */
323 private function get_translated_placeholder( $option, $option_key, $default_placeholder, $cookies, $src = '' ) {
324 $current_lang = cookiebot_get_current_site_language();
325
326 if ( $current_lang === false || $current_lang === '' ) {
327 $current_lang = 'site-default';
328 }
329
330 /**
331 * Loop every language and match current language
332 */
333 if ( isset( $option[ $option_key ]['placeholder']['languages'] ) &&
334 is_array( $option[ $option_key ]['placeholder']['languages'] ) ) {
335 foreach ( $option[ $option_key ]['placeholder']['languages'] as $key => $value ) {
336
337 /**
338 * if current lang match with the prefix language in the database then get the text
339 */
340 if ( $key === $current_lang ) {
341 $cookies_array = explode( ', ', $cookies );
342 $translated_cookie_names = cookiebot_translate_cookie_names( $cookies_array );
343 $translated_cookie_names = implode( ', ', $translated_cookie_names );
344 $placeholder = cookiebot_translate_placeholder(
345 $option[ $option_key ]['placeholder']['languages'][ $key ]
346 );
347 return $this->placeholder_merge_tag(
348 $placeholder,
349 $translated_cookie_names,
350 $src
351 );
352 }
353 }
354 }
355
356 /**
357 * Returns site-default text if no match found.
358 */
359 if ( isset( $option[ $option_key ]['placeholder']['languages']['site-default'] ) ) {
360 $placeholder = cookiebot_translate_placeholder( $option[ $option_key ]['placeholder']['languages']['site-default'] );
361 return $this->placeholder_merge_tag(
362 $placeholder,
363 $cookies,
364 $src
365 );
366 }
367
368 /**
369 * Returns addon default placeholder (code)
370 */
371 return $this->placeholder_merge_tag( $default_placeholder, $cookies, $src );
372 }
373
374 /**
375 * Merges placeholder tags with values
376 *
377 * @param $placeholder
378 * @param $cookies
379 * @param $src
380 * @return mixed
381 *
382 * @since 1.8.0
383 */
384 private function placeholder_merge_tag( $placeholder, $cookies, $src ) {
385 if ( strpos( $placeholder, '%cookie_types' ) !== false ) {
386 $placeholder = str_replace( '%cookie_types', $cookies, $placeholder );
387 }
388
389 if ( strpos( $placeholder, '%src' ) !== false ) {
390 $placeholder = str_replace( '%src', $src, $placeholder );
391 }
392
393 if ( strpos( $placeholder, '[renew_consent]' ) !== false ) {
394 $placeholder = str_replace( '[renew_consent]', '<a href="javascript:Cookiebot.renew()">', $placeholder );
395 }
396
397 if ( strpos( $placeholder, '[/renew_consent]' ) !== false ) {
398 $placeholder = str_replace( '[/renew_consent]', '</a>', $placeholder );
399 }
400
401 return $placeholder;
402 }
403
404 /**
405 * Check if the addon option name matchs with the parameter
406 * then run the post_hook_after_enabling function in the addon class.
407 *
408 * @param $addon_option_name string Addon option name
409 *
410 * @throws Exception
411 *
412 * @since 2.2.0
413 */
414 public function post_hook_after_enabling_addon_on_settings_page( $addon_option_name ) {
415 $addons = $this->get_addons();
416
417 /** @var Base_Cookiebot_Addon $addon */
418 foreach ( $addons as $addon ) {
419 if ( $addon::OPTION_NAME === $addon_option_name ) {
420 $addon->post_hook_after_enabling();
421 }
422 }
423 }
424
425 /**
426 * The cookiebot plugin is deactivated
427 * so run this function to cleanup the addons.
428 *
429 * @throws Exception
430 * @since 2.2.0
431 */
432 public function cookiebot_deactivated() {
433 /** @var Base_Cookiebot_Addon $addon */
434 foreach ( $this->get_active_addons() as $addon ) {
435 $addon->plugin_deactivated();
436 }
437 }
438
439 /**
440 * The cookiebot plugin is activated and the addon settings is activated
441 *
442 * @throws Exception
443 * @since 3.6.3
444 */
445 public function cookiebot_activated() {
446 $option = get_option( static::OPTION_NAME );
447
448 if ( $option === false ) {
449 $option = array();
450
451 /** @var Base_Cookiebot_Addon $addon */
452 foreach ( $this->get_addons() as $addon ) {
453 if ( $addon::ENABLE_ADDON_BY_DEFAULT ) {
454 $option[ $addon::OPTION_NAME ] = $addon->get_default_enable_setting();
455 }
456 }
457
458 update_option( static::OPTION_NAME, $option );
459 }
460 }
461 }
462