| 1 |
<?php |
| 2 |
|
| 3 |
namespace cookiebot_addons\lib; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Cookie_Consent |
| 7 |
* @package cookiebot_addons\lib |
| 8 |
*/ |
| 9 |
class Cookie_Consent implements Cookie_Consent_Interface { |
| 10 |
|
| 11 |
/** |
| 12 |
* Array of cookiebot consent states |
| 13 |
* |
| 14 |
* It can have 4 items: |
| 15 |
* - necessary |
| 16 |
* - preferences |
| 17 |
* - statistics |
| 18 |
* - marketing |
| 19 |
* |
| 20 |
* @var array consent state |
| 21 |
* |
| 22 |
* @since 1.2.0 |
| 23 |
*/ |
| 24 |
private $states = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* @var array|null |
| 28 |
*/ |
| 29 |
private $cookie; |
| 30 |
|
| 31 |
/** |
| 32 |
* Scan cookiebot cookie |
| 33 |
* |
| 34 |
* @param $default_cookie |
| 35 |
* |
| 36 |
* @since 1.2.0 |
| 37 |
* @version 2.4.1 |
| 38 |
*/ |
| 39 |
public function __construct( $default_cookie = null ) { |
| 40 |
$this->cookie = ( isset( $_COOKIE['CookieConsent'] ) ) ? $_COOKIE['CookieConsent'] : $default_cookie; |
| 41 |
|
| 42 |
$this->scan_cookie(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Scans cookiebot consent cookie and fills in $states with accepted consents. |
| 47 |
* |
| 48 |
* @since 1.2.0 |
| 49 |
* @version 2.4.1 |
| 50 |
*/ |
| 51 |
public function scan_cookie() { |
| 52 |
//default - set strictly necessary cookies |
| 53 |
$this->add_state( 'necessary' ); |
| 54 |
|
| 55 |
if ( ! empty( $this->cookie ) ) { |
| 56 |
switch ( $this->cookie ) { |
| 57 |
case '0': |
| 58 |
//The user has not accepted cookies - set strictly necessary cookies only |
| 59 |
break; |
| 60 |
|
| 61 |
case '-1': |
| 62 |
//The user is not within a region that requires consent - all cookies are accepted |
| 63 |
$this->add_state( 'preferences' ); |
| 64 |
$this->add_state( 'statistics' ); |
| 65 |
$this->add_state( 'marketing' ); |
| 66 |
break; |
| 67 |
|
| 68 |
default: |
| 69 |
//Read current user consent in encoded JavaScript format |
| 70 |
$valid_php_json = preg_replace( |
| 71 |
'/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', |
| 72 |
':"$1"$2', |
| 73 |
preg_replace( |
| 74 |
'/([{\[,])\s*([a-zA-Z0-9_]+?):/', |
| 75 |
'$1"$2":', |
| 76 |
str_replace( "'", '"', stripslashes( $this->cookie ) ) |
| 77 |
) |
| 78 |
); |
| 79 |
$cookie_consent = json_decode( $valid_php_json ); |
| 80 |
|
| 81 |
if ( isset( $cookie_consent->preferences ) && filter_var( $cookie_consent->preferences, FILTER_VALIDATE_BOOLEAN ) ) { |
| 82 |
//Current user accepts preference cookies |
| 83 |
$this->add_state( 'preferences' ); |
| 84 |
} else { |
| 85 |
//Current user does NOT accept preference cookies |
| 86 |
} |
| 87 |
|
| 88 |
if ( isset( $cookie_consent->statistics ) && filter_var( $cookie_consent->statistics, FILTER_VALIDATE_BOOLEAN ) ) { |
| 89 |
//Current user accepts statistics cookies |
| 90 |
$this->add_state( 'statistics' ); |
| 91 |
} else { |
| 92 |
//Current user does NOT accept statistics cookies |
| 93 |
} |
| 94 |
|
| 95 |
if ( isset( $cookie_consent->marketing ) && filter_var( $cookie_consent->marketing, FILTER_VALIDATE_BOOLEAN ) ) { |
| 96 |
//Current user accepts marketing cookies |
| 97 |
$this->add_state( 'marketing' ); |
| 98 |
} else { |
| 99 |
//Current user does NOT accept marketing cookies |
| 100 |
} |
| 101 |
} |
| 102 |
} else { |
| 103 |
//The user has not accepted cookies - set strictly necessary cookies only |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Adds state to $states variable |
| 109 |
* |
| 110 |
* @param $state string new state |
| 111 |
* |
| 112 |
* @since 1.2.0 |
| 113 |
*/ |
| 114 |
public function add_state( $state ) { |
| 115 |
if ( ! in_array( $state, $this->states ) ) { |
| 116 |
$this->states[] = $state; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns cookiebot cookie consent state |
| 122 |
* |
| 123 |
* @return array array List of accepted cookiebot consents |
| 124 |
* |
| 125 |
* @since 1.2.0 |
| 126 |
*/ |
| 127 |
public function get_cookie_states() { |
| 128 |
if ( count( $this->states ) == 0 ) { |
| 129 |
$this->scan_cookie(); |
| 130 |
} |
| 131 |
|
| 132 |
return $this->states; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Checks if the cookie states are accepted. |
| 137 |
* |
| 138 |
* @param $states array Cookie states to check if it is accepted. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
* |
| 142 |
* @since 1.3.0 |
| 143 |
*/ |
| 144 |
public function are_cookie_states_accepted( $states ) { |
| 145 |
if ( is_array( $states ) ) { |
| 146 |
foreach ( $states as $state ) { |
| 147 |
if ( ! in_array( $state, $this->states ) ) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Checks if the cookie state is accepted |
| 158 |
* |
| 159 |
* @param $state string Cookie state to check if it is accepted |
| 160 |
* |
| 161 |
* @return bool |
| 162 |
*/ |
| 163 |
public function is_cookie_state_accepted( $state ) { |
| 164 |
return in_array( $state, $this->states ); |
| 165 |
} |
| 166 |
} |
| 167 |
|