| 1 |
<?php |
| 2 |
|
| 3 |
namespace CookieAdmin; |
| 4 |
|
| 5 |
if(!defined('COOKIEADMIN_VERSION') || !defined('ABSPATH')){ |
| 6 |
die('Hacking Attempt'); |
| 7 |
} |
| 8 |
|
| 9 |
class Enduser{ |
| 10 |
|
| 11 |
static $http_cookies = array(); |
| 12 |
static $categorized_cookies = array(); |
| 13 |
|
| 14 |
static function enqueue_scripts(){ |
| 15 |
global $wpdb; |
| 16 |
|
| 17 |
$view = get_option('cookieadmin_law', 'cookieadmin_gdpr'); |
| 18 |
$policy = cookieadmin_load_policy(); |
| 19 |
$table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); |
| 20 |
//cookieadmin_r_print($view); |
| 21 |
//cookieadmin_r_print($policy); |
| 22 |
|
| 23 |
if(!empty($policy) && !empty($view) && !cookieadmin_is_editor_mode()){ |
| 24 |
|
| 25 |
wp_enqueue_style('cookieadmin-style', COOKIEADMIN_PLUGIN_URL . 'assets/css/consent.css', [], COOKIEADMIN_VERSION); |
| 26 |
|
| 27 |
wp_enqueue_script('cookieadmin_js', COOKIEADMIN_PLUGIN_URL . 'assets/js/consent.js', [], COOKIEADMIN_VERSION); |
| 28 |
|
| 29 |
$policy[$view]['ajax_url'] = admin_url('admin-ajax.php'); |
| 30 |
$policy[$view]['nonce'] = wp_create_nonce('cookieadmin_js_nonce'); |
| 31 |
$policy[$view]['http_cookies'] = self::$http_cookies; |
| 32 |
$policy[$view]['home_url'] = home_url(); |
| 33 |
$policy[$view]['plugin_url'] = COOKIEADMIN_URL; |
| 34 |
$policy[$view]['is_pro'] = (defined('COOKIEADMIN_PREMIUM') ? COOKIEADMIN_PREMIUM : 0); |
| 35 |
$policy[$view]['ssl'] = is_ssl(); |
| 36 |
|
| 37 |
$base_path = parse_url(home_url(), PHP_URL_PATH) ?: '/'; |
| 38 |
$base_path = ($base_path !== '/') ? rtrim($base_path, '/') . '/' : '/'; |
| 39 |
|
| 40 |
// Used for setting cookie |
| 41 |
$policy[$view]['base_path'] = $base_path; |
| 42 |
|
| 43 |
$policy[$view]['lang']['show_less'] = __('Show less', 'cookieadmin'); |
| 44 |
$policy[$view]['lang']['duration'] = __('Duration', 'cookieadmin'); |
| 45 |
$policy[$view]['lang']['session'] = __('Session', 'cookieadmin'); |
| 46 |
$policy[$view]['lang']['days'] = __('Days', 'cookieadmin'); |
| 47 |
|
| 48 |
// cookieadmin_r_print($policy);die(); |
| 49 |
|
| 50 |
$rows = $wpdb->get_results("SELECT cookie_name, category, expires, description, patterns FROM {$table_name}"); |
| 51 |
$cookie_data = array(); |
| 52 |
|
| 53 |
foreach ($rows as $row) { |
| 54 |
$cookie_data[$row->cookie_name] = $row; |
| 55 |
} |
| 56 |
|
| 57 |
$policy[$view]['categorized_cookies'] = self::$categorized_cookies = $cookie_data; |
| 58 |
|
| 59 |
wp_localize_script('cookieadmin_js', 'cookieadmin_policy', $policy[$view]); |
| 60 |
|
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/* static function cookieadmin_block_cookie_init_php(){ |
| 65 |
|
| 66 |
//New - To catch, remove and send cookies in WP enqueue |
| 67 |
$http_cookies = array(); |
| 68 |
$headers = headers_list(); |
| 69 |
|
| 70 |
foreach($headers as $header) { |
| 71 |
|
| 72 |
if (stripos(trim($header), 'Set-Cookie:') === 0) { |
| 73 |
$header = trim(substr($header, strlen('Set-Cookie:'))); |
| 74 |
$name = trim(explode('=', $header)[0]); |
| 75 |
$http_cookies[$name]['string'] = trim($header); |
| 76 |
setcookie($name, '', time() - 999999, '/'); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$http_cookies['cookieadmin_consent'] = ["string" => "cookieadmin_consent=CookieAdmin Cookie Initialization"]; |
| 81 |
|
| 82 |
self::$http_cookies = $http_cookies; |
| 83 |
} */ |
| 84 |
|
| 85 |
static function check_if_cookies_allowed($tag, $handle, $src){ |
| 86 |
|
| 87 |
$cookieadmin_consent = isset($_COOKIE['cookieadmin_consent']) |
| 88 |
? json_decode(wp_unslash($_COOKIE['cookieadmin_consent']), true) |
| 89 |
: []; |
| 90 |
|
| 91 |
array_walk( $cookieadmin_consent, function( $value, $key ) use ( &$cookieadmin_consent ) { |
| 92 |
$sanitized_key = sanitize_key( $key ); |
| 93 |
$cookieadmin_consent[ $sanitized_key ] = sanitize_text_field($value); |
| 94 |
} ); |
| 95 |
|
| 96 |
foreach (self::$categorized_cookies as $item) { |
| 97 |
$category = !empty($item->category) ? strtolower($item->category) : ''; |
| 98 |
$patterns = !empty($item->patterns) ? json_decode($item->patterns, true) : ''; |
| 99 |
|
| 100 |
if (!empty($patterns) && !empty($category)) { |
| 101 |
foreach ($patterns as $pattern) { |
| 102 |
if (strpos($src, $pattern) !== false) { |
| 103 |
|
| 104 |
if ( $category !== 'necessary' && |
| 105 |
(empty($cookieadmin_consent) || |
| 106 |
(!empty($cookieadmin_consent[$category]) && $cookieadmin_consent[$category] == 'false') || |
| 107 |
(!empty($cookieadmin_consent['reject']) && $cookieadmin_consent['reject'] == 'true') |
| 108 |
) |
| 109 |
) { |
| 110 |
|
| 111 |
// User has NOT consented -> block the script |
| 112 |
|
| 113 |
// Option 1 - completely remove script: |
| 114 |
// return ''; |
| 115 |
|
| 116 |
// Option 2 - transform to type="text/plain" |
| 117 |
$tag = str_replace( |
| 118 |
'<script ', |
| 119 |
'<script type="text/plain" data-cookieadmin-category="' . esc_attr($category) . '" ', |
| 120 |
$tag |
| 121 |
); |
| 122 |
|
| 123 |
return $tag; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return $tag; |
| 131 |
} |
| 132 |
|
| 133 |
static function cookieadmin_show_banner(){ |
| 134 |
|
| 135 |
$view = get_option('cookieadmin_law', 'cookieadmin_gdpr'); |
| 136 |
$policy = cookieadmin_load_policy(); |
| 137 |
|
| 138 |
$templates = implode("", cookieadmin_load_consent_template($policy[$view], $view)); |
| 139 |
|
| 140 |
$allowed_tags = cookieadmin_kses_allowed_html(); |
| 141 |
|
| 142 |
// var_dump($policy[$view]); |
| 143 |
echo wp_kses($templates, $allowed_tags); |
| 144 |
} |
| 145 |
|
| 146 |
static function cookieadmin_table_exists($table_name) { |
| 147 |
global $wpdb; |
| 148 |
|
| 149 |
$query = $wpdb->prepare("SHOW TABLES LIKE %s", $table_name); |
| 150 |
|
| 151 |
return $wpdb->get_var($query) === $table_name; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|