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