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