PluginProbe
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.7.3
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.7.3
4.7.3 4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 All 93 releases
cookiebot / src / lib / Consent_API_Helper.php

Consent_API_Helper.php in Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode 4.7.3, at src/lib/Consent_API_Helper.php

195 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace cybot\cookiebot\lib;
4
5 use InvalidArgumentException;
6
7 class Consent_API_Helper {
8
9 public function register_hooks() {
10 // Include integration to WP Consent Level API if available
11 if ( $this->is_wp_consent_api_active() ) {
12 add_action( 'wp_enqueue_scripts', array( $this, 'cookiebot_enqueue_consent_api_scripts' ) );
13 add_filter( 'wp_get_consent_type', array( $this, 'wp_consent_api_get_consent_type' ) );
14 }
15 }
16
17 public function wp_consent_api_get_consent_type() {
18 $region = get_option( 'cookiebot-primary-domain-region' );
19 return ! empty( $region ) ? self::get_consent_type( $region ) : 'optin';
20 }
21
22 public static function get_consent_type( $region ) {
23 $consent_type = 'optin';
24
25 if ( in_array( $region, Supported_Regions::OPTOUT_REGIONS, true ) ) {
26 $consent_type = 'optout';
27 }
28
29 return $consent_type;
30 }
31
32 /**
33 * Cookiebot_WP Check if WP Cookie Consent API is active
34 *
35 * @version 3.5.0
36 * @since 3.5.0
37 */
38 public function is_wp_consent_api_active() {
39 return is_plugin_active( 'wp-consent-api/wp-consent-api.php' );
40 }
41
42 /**
43 * Cookiebot_WP Enqueue JS for integration with WP Consent Level API
44 *
45 * @throws InvalidArgumentException
46 * @since 3.5.0
47 * @version 3.5.0
48 */
49 public function cookiebot_enqueue_consent_api_scripts() {
50 $is_cb_frame = Cookiebot_Frame::is_cb_frame_type() !== 'empty' && Cookiebot_Frame::is_cb_frame_type() === true;
51 wp_register_script(
52 'cookiebot-wp-consent-level-api-integration',
53 $is_cb_frame ? asset_url( self::CB_FRAME_SCRIPT_PATH ) : asset_url( self::UC_FRAME_SCRIPT_PATH ),
54 array( 'wp-consent-api' ),
55 Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION,
56 false
57 );
58 wp_enqueue_script( 'cookiebot-wp-consent-level-api-integration' );
59 wp_localize_script(
60 'cookiebot-wp-consent-level-api-integration',
61 'cookiebot_category_mapping',
62 $this->get_wp_consent_api_mapping()
63 );
64 if ( $is_cb_frame ) {
65 wp_localize_script(
66 'cookiebot-wp-consent-level-api-integration',
67 'cookiebot_consent_type',
68 array(
69 'type' => $this->wp_consent_api_get_consent_type(),
70 )
71 );
72 }
73 }
74
75 /**
76 * Cookiebot_WP Get the mapping between Consent Level API and Cookiebot
77 * Returns array where key is the consent level api category and value
78 * is the mapped Cookiebot category.
79 *
80 * @version 3.5.0
81 * @since 3.5.0
82 */
83 public function get_wp_consent_api_mapping() {
84 $default_wp_consent_api_mapping = $this->get_default_wp_consent_api_mapping();
85
86 if ( Cookiebot_Frame::is_cb_frame_type() === false ) {
87 $mapping = get_option( 'cookiebot-uc-consent-mapping', $default_wp_consent_api_mapping );
88 } else {
89 $mapping = $this->get_cb_mapping( $default_wp_consent_api_mapping );
90 }
91
92 return $mapping;
93 }
94
95 /**
96 * Return CB Frame WP Consent API mapping
97 */
98 private function get_cb_mapping( $default_wp_consent_api_mapping ) {
99 $mapping = get_option( 'cookiebot-consent-mapping', $default_wp_consent_api_mapping );
100
101 $mapping = ( '' === $mapping ) ? $default_wp_consent_api_mapping : $mapping;
102
103 foreach ( $default_wp_consent_api_mapping as $k => $v ) {
104 if ( ! isset( $mapping[ $k ] ) ) {
105 $mapping[ $k ] = $v;
106 } else {
107 foreach ( $v as $vck => $vcv ) {
108 if ( ! isset( $mapping[ $k ][ $vck ] ) ) {
109 $mapping[ $k ][ $vck ] = $vcv;
110 }
111 }
112 }
113 }
114
115 return $mapping;
116 }
117
118 /**
119 * Cookiebot_WP Default consent level mappings
120 *
121 * @version 3.5.0
122 * @since 3.5.0
123 */
124 public function get_default_wp_consent_api_mapping() {
125 if ( Cookiebot_Frame::is_cb_frame_type() === false ) {
126 return array(
127 'functional' => 'preferences',
128 'marketing' => 'marketing',
129 );
130 } else {
131 return array(
132 'n=1;p=1;s=1;m=1' =>
133 array(
134 'preferences' => 1,
135 'statistics' => 1,
136 'statistics-anonymous' => 0,
137 'marketing' => 1,
138 ),
139 'n=1;p=1;s=1;m=0' =>
140 array(
141 'preferences' => 1,
142 'statistics' => 1,
143 'statistics-anonymous' => 1,
144 'marketing' => 0,
145 ),
146 'n=1;p=1;s=0;m=1' =>
147 array(
148 'preferences' => 1,
149 'statistics' => 0,
150 'statistics-anonymous' => 0,
151 'marketing' => 1,
152 ),
153 'n=1;p=1;s=0;m=0' =>
154 array(
155 'preferences' => 1,
156 'statistics' => 0,
157 'statistics-anonymous' => 0,
158 'marketing' => 0,
159 ),
160 'n=1;p=0;s=1;m=1' =>
161 array(
162 'preferences' => 0,
163 'statistics' => 1,
164 'statistics-anonymous' => 0,
165 'marketing' => 1,
166 ),
167 'n=1;p=0;s=1;m=0' =>
168 array(
169 'preferences' => 0,
170 'statistics' => 1,
171 'statistics-anonymous' => 0,
172 'marketing' => 0,
173 ),
174 'n=1;p=0;s=0;m=1' =>
175 array(
176 'preferences' => 0,
177 'statistics' => 0,
178 'statistics-anonymous' => 0,
179 'marketing' => 1,
180 ),
181 'n=1;p=0;s=0;m=0' =>
182 array(
183 'preferences' => 0,
184 'statistics' => 0,
185 'statistics-anonymous' => 0,
186 'marketing' => 0,
187 ),
188 );
189 }
190 }
191
192 const CB_FRAME_SCRIPT_PATH = 'js/frontend/cb_frame/cookiebot-wp-consent-level-api-integration.js';
193 const UC_FRAME_SCRIPT_PATH = 'js/frontend/uc_frame/uc-wp-consent-level-api-integration.js';
194 }
195