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