buffer
4 years ago
script_loader_tag
4 years ago
traits
4 years ago
Consent_API_Helper.php
4 years ago
Cookie_Consent.php
4 years ago
Cookie_Consent_Interface.php
4 years ago
Cookiebot_Activated.php
4 years ago
Cookiebot_Automatic_Updates.php
4 years ago
Cookiebot_Deactivated.php
4 years ago
Cookiebot_Javascript_Helper.php
4 years ago
Cookiebot_WP.php
3 years ago
Dependency_Container.php
4 years ago
Settings_Page_Tab.php
4 years ago
Settings_Service.php
4 years ago
Settings_Service_Interface.php
4 years ago
Supported_Languages.php
4 years ago
Supported_Regions.php
3 years ago
WP_Rocket_Helper.php
4 years ago
Widgets.php
4 years ago
global-deprecations.php
4 years ago
helper.php
4 years ago
Settings_Service.php
455 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'] ) && is_array( $option[ $widget_key ]['placeholder']['languages'] ) ) { |
| 205 | return (array) $option[ $widget_key ]['placeholder']['languages']; |
| 206 | } |
| 207 | |
| 208 | return array(); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @param $option_key |
| 213 | * |
| 214 | * @return array |
| 215 | */ |
| 216 | public function get_placeholders( $option_key ) { |
| 217 | $option = get_option( static::OPTION_NAME ); |
| 218 | |
| 219 | if ( isset( $option[ $option_key ]['placeholder']['languages'] ) && is_array( $option[ $option_key ]['placeholder']['languages'] ) ) { |
| 220 | return (array) $option[ $option_key ]['placeholder']['languages']; |
| 221 | } |
| 222 | |
| 223 | return array(); |
| 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 has_placeholder( $option_key ) { |
| 236 | $option = get_option( static::OPTION_NAME ); |
| 237 | |
| 238 | if ( isset( $option[ $option_key ]['placeholder']['languages'] ) ) { |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * returns true if the addon placeholder is enabled |
| 247 | * |
| 248 | * @param $option_key |
| 249 | * |
| 250 | * @return bool |
| 251 | * |
| 252 | * @since 1.8.0 |
| 253 | */ |
| 254 | public function is_placeholder_enabled( $option_key ) { |
| 255 | $option = get_option( static::OPTION_NAME ); |
| 256 | |
| 257 | if ( isset( $option[ $option_key ]['placeholder']['enabled'] ) ) { |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * returns the placeholder if it does exist |
| 266 | * |
| 267 | * @param $option_key |
| 268 | * @param $default_placeholder |
| 269 | * @param $cookies |
| 270 | * @param string $src |
| 271 | * |
| 272 | * @return bool|mixed |
| 273 | * |
| 274 | * @since 1.8.0 |
| 275 | */ |
| 276 | public function get_placeholder( $option_key, $default_placeholder, $cookies, $src = '' ) { |
| 277 | $option = get_option( static::OPTION_NAME ); |
| 278 | |
| 279 | if ( isset( $option[ $option_key ]['placeholder']['enabled'] ) ) { |
| 280 | return $this->get_translated_placeholder( $option, $option_key, $default_placeholder, $cookies, $src ); |
| 281 | } |
| 282 | |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * returns the placeholder if it does exist |
| 288 | * |
| 289 | * @param $option_key |
| 290 | * @param $widget_key |
| 291 | * @param $default_placeholder |
| 292 | * @param string $cookies |
| 293 | * |
| 294 | * @return bool|mixed |
| 295 | * |
| 296 | * @since 1.8.0 |
| 297 | */ |
| 298 | public function get_widget_placeholder( $option_key, $widget_key, $default_placeholder, $cookies = '' ) { |
| 299 | $option = get_option( $option_key ); |
| 300 | |
| 301 | if ( isset( $option[ $widget_key ]['placeholder']['enabled'] ) ) { |
| 302 | return $this->get_translated_placeholder( $option, $widget_key, $default_placeholder, $cookies ); |
| 303 | } |
| 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Translates the placeholder text in the current page language |
| 310 | * |
| 311 | * @param $option |
| 312 | * @param $option_key |
| 313 | * @param $default_placeholder |
| 314 | * @param $cookies |
| 315 | * @param string $src |
| 316 | * |
| 317 | * @return mixed |
| 318 | * |
| 319 | * @since 1.9.0 |
| 320 | */ |
| 321 | private function get_translated_placeholder( $option, $option_key, $default_placeholder, $cookies, $src = '' ) { |
| 322 | $current_lang = cookiebot_get_current_site_language(); |
| 323 | |
| 324 | if ( $current_lang === false || $current_lang === '' ) { |
| 325 | $current_lang = 'site-default'; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Loop every language and match current language |
| 330 | */ |
| 331 | if ( isset( $option[ $option_key ]['placeholder']['languages'] ) && is_array( $option[ $option_key ]['placeholder']['languages'] ) ) { |
| 332 | foreach ( $option[ $option_key ]['placeholder']['languages'] as $key => $value ) { |
| 333 | |
| 334 | /** |
| 335 | * if current lang match with the prefix language in the database then get the text |
| 336 | */ |
| 337 | if ( $key === $current_lang ) { |
| 338 | $cookies_array = explode( ', ', $cookies ); |
| 339 | $translated_cookie_names = cookiebot_translate_cookie_names( $cookies_array ); |
| 340 | $translated_cookie_names = implode( ', ', $translated_cookie_names ); |
| 341 | return $this->placeholder_merge_tag( |
| 342 | $option[ $option_key ]['placeholder']['languages'][ $key ], |
| 343 | $translated_cookie_names, |
| 344 | $src |
| 345 | ); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Returns site-default text if no match found. |
| 352 | */ |
| 353 | if ( isset( $option[ $option_key ]['placeholder']['languages']['site-default'] ) ) { |
| 354 | return $this->placeholder_merge_tag( |
| 355 | $option[ $option_key ]['placeholder']['languages']['site-default'], |
| 356 | $cookies, |
| 357 | $src |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Returns addon default placeholder (code) |
| 363 | */ |
| 364 | return $this->placeholder_merge_tag( $default_placeholder, $cookies, $src ); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Merges placeholder tags with values |
| 369 | * |
| 370 | * @param $placeholder |
| 371 | * @param $cookies |
| 372 | * @param $src |
| 373 | * @return mixed |
| 374 | * |
| 375 | * @since 1.8.0 |
| 376 | */ |
| 377 | private function placeholder_merge_tag( $placeholder, $cookies, $src ) { |
| 378 | if ( strpos( $placeholder, '%cookie_types' ) !== false ) { |
| 379 | $placeholder = str_replace( '%cookie_types', $cookies, $placeholder ); |
| 380 | } |
| 381 | |
| 382 | if ( strpos( $placeholder, '%src' ) !== false ) { |
| 383 | $placeholder = str_replace( '%src', $src, $placeholder ); |
| 384 | } |
| 385 | |
| 386 | if ( strpos( $placeholder, '[renew_consent]' ) !== false ) { |
| 387 | $placeholder = str_replace( '[renew_consent]', '<a href="javascript:Cookiebot.renew()">', $placeholder ); |
| 388 | } |
| 389 | |
| 390 | if ( strpos( $placeholder, '[/renew_consent]' ) !== false ) { |
| 391 | $placeholder = str_replace( '[/renew_consent]', '</a>', $placeholder ); |
| 392 | } |
| 393 | |
| 394 | return $placeholder; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Check if the addon option name matchs with the parameter |
| 399 | * then run the post_hook_after_enabling function in the addon class. |
| 400 | * |
| 401 | * @param $addon_option_name string Addon option name |
| 402 | * |
| 403 | * @throws Exception |
| 404 | * |
| 405 | * @since 2.2.0 |
| 406 | */ |
| 407 | public function post_hook_after_enabling_addon_on_settings_page( $addon_option_name ) { |
| 408 | $addons = $this->get_addons(); |
| 409 | |
| 410 | /** @var Base_Cookiebot_Addon $addon */ |
| 411 | foreach ( $addons as $addon ) { |
| 412 | if ( $addon::OPTION_NAME === $addon_option_name ) { |
| 413 | $addon->post_hook_after_enabling(); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * The cookiebot plugin is deactivated |
| 420 | * so run this function to cleanup the addons. |
| 421 | * |
| 422 | * @throws Exception |
| 423 | * @since 2.2.0 |
| 424 | */ |
| 425 | public function cookiebot_deactivated() { |
| 426 | /** @var Base_Cookiebot_Addon $addon */ |
| 427 | foreach ( $this->get_active_addons() as $addon ) { |
| 428 | $addon->plugin_deactivated(); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * The cookiebot plugin is activated and the addon settings is activated |
| 434 | * |
| 435 | * @throws Exception |
| 436 | * @since 3.6.3 |
| 437 | */ |
| 438 | public function cookiebot_activated() { |
| 439 | $option = get_option( static::OPTION_NAME ); |
| 440 | |
| 441 | if ( $option === false ) { |
| 442 | $option = array(); |
| 443 | |
| 444 | /** @var Base_Cookiebot_Addon $addon */ |
| 445 | foreach ( $this->get_addons() as $addon ) { |
| 446 | if ( $addon::ENABLE_ADDON_BY_DEFAULT ) { |
| 447 | $option[ $addon::OPTION_NAME ] = $addon->get_default_enable_setting(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | update_option( static::OPTION_NAME, $option ); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 |