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