PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.5.5
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.5.5
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 3.6.1 3.6.2 3.6.5 3.6.6 3.7.0 3.7.1 3.8.0 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.2.0 4.2.1 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.3.10 4.3.11 4.3.12 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.7.1 4.3.8 4.3.9 4.3.9.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.7.0
cookiebot / src / lib / Account_Service.php
cookiebot / src / lib Last commit date
buffer 1 year ago script_loader_tag 1 year ago traits 1 year ago Account_Service.php 1 year ago Consent_API_Helper.php 1 year ago Cookie_Consent.php 1 year ago Cookie_Consent_Interface.php 1 year ago Cookiebot_Activated.php 1 year ago Cookiebot_Admin_Links.php 1 year ago Cookiebot_Automatic_Updates.php 1 year ago Cookiebot_Deactivated.php 1 year ago Cookiebot_Frame.php 1 year ago Cookiebot_Javascript_Helper.php 1 year ago Cookiebot_Review.php 1 year ago Cookiebot_WP.php 1 year ago Dependency_Container.php 1 year ago Settings_Page_Tab.php 1 year ago Settings_Service.php 1 year ago Settings_Service_Interface.php 1 year ago Supported_Languages.php 1 year ago Supported_Regions.php 1 year ago WP_Rocket_Helper.php 1 year ago Widgets.php 1 year ago global-deprecations.php 1 year ago helper.php 1 year ago
Account_Service.php
380 lines
1 <?php
2
3 namespace cybot\cookiebot\lib;
4
5 use cybot\cookiebot\settings\pages\Dashboard_Page;
6 use cybot\cookiebot\lib\Cookiebot_WP;
7 use function add_action;
8 use function admin_url;
9 use function check_ajax_referer;
10 use function current_user_can;
11 use function esc_html__;
12 use function esc_url_raw;
13 use function is_email;
14 use function sanitize_email;
15 use function sanitize_text_field;
16 use function update_option;
17 use function wp_enqueue_script;
18 use function wp_json_encode;
19 use function wp_localize_script;
20 use function wp_remote_post;
21 use function wp_remote_retrieve_body;
22 use function wp_remote_retrieve_response_code;
23 use function wp_send_json_error;
24 use function wp_send_json_success;
25 use function wp_script_is;
26 use function wp_remote_get;
27 use function current_time;
28
29 class Account_Service {
30
31 public function __construct() {
32 $this->register_hooks();
33 }
34
35 public function register_hooks() {
36 add_action( 'admin_enqueue_scripts', array( $this, 'cookiebot_admin_script' ), 100 );
37 add_action( 'wp_ajax_cookiebot_store_cbid', array( $this, 'ajax_store_cbid' ) );
38 add_action( 'wp_ajax_cookiebot_get_cbid', array( $this, 'ajax_get_cbid' ) );
39 add_action( 'wp_ajax_cookiebot_get_auth_token', array( $this, 'ajax_get_auth_token' ) );
40 add_action( 'wp_ajax_cookiebot_set_gcm_enabled', array( $this, 'ajax_set_gcm_enabled' ) );
41 add_action( 'wp_ajax_cookiebot_set_banner_enabled', array( $this, 'ajax_set_banner_enabled' ) );
42 add_action( 'wp_ajax_cookiebot_set_auto_blocking_mode', array( $this, 'ajax_set_auto_blocking_mode' ) );
43 add_action( 'wp_ajax_cookiebot_process_auth_code', array( $this, 'ajax_process_auth_code' ) );
44 add_action( 'wp_ajax_cookiebot_dismiss_banner', array( $this, 'ajax_dismiss_banner' ) );
45 add_action( 'wp_ajax_cookiebot_post_user_data', array( $this, 'ajax_post_user_data' ) );
46 add_action( 'wp_ajax_cookiebot_get_user_data', array( $this, 'ajax_get_user_data' ) );
47 add_action( 'wp_ajax_cookiebot_store_scan_details', array( $this, 'ajax_store_scan_details' ) );
48 add_action( 'wp_ajax_cookiebot_get_scan_details', array( $this, 'ajax_get_scan_details' ) );
49 add_action( 'wp_ajax_cookiebot_store_configuration', array( $this, 'ajax_store_configuration' ) );
50 add_action( 'wp_ajax_cookiebot_clear_config_data', array( $this, 'ajax_clear_config_data' ) );
51 add_action( 'wp_ajax_cookiebot_clear_config_data_keep_cbid', array( $this, 'ajax_clear_config_data_keep_cbid' ) );
52 add_action( 'wp_ajax_cookiebot_delete_auth_token', array( $this, 'ajax_delete_auth_token' ) );
53 add_action( 'wp_ajax_cookiebot_store_onboarding_status', array( $this, 'ajax_store_onboarding_status' ) );
54 }
55
56 public function cookiebot_admin_script( $hook ) {
57 if ( 'toplevel_page_' . Dashboard_Page::ADMIN_SLUG !== $hook ) {
58 return;
59 }
60
61 $is_authenticated = ! empty( Cookiebot_WP::get_auth_token() );
62 $cbid = Cookiebot_WP::get_cbid();
63 $user_data = Cookiebot_WP::get_user_data();
64 $was_onboarded = Cookiebot_WP::was_onboarded_via_signup();
65
66 if ( ! $is_authenticated && ! empty( $cbid ) && ! empty( $user_data ) && ! empty( $was_onboarded ) ) {
67 wp_enqueue_script(
68 'cookiebot-account-static-js',
69 CYBOT_COOKIEBOT_PLUGIN_URL . 'assets/js/backend/account-static.js',
70 array( 'jquery' ),
71 Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION,
72 true
73 );
74
75 wp_localize_script(
76 'cookiebot-account-static-js',
77 'cookiebot_account',
78 array(
79 'ajax_url' => admin_url( 'admin-ajax.php' ),
80 'nonce' => wp_create_nonce( 'cookiebot-account' ),
81 'has_user_data' => ! empty( $user_data ),
82 'has_cbid' => ! empty( $cbid ),
83 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
84 'auth_expired_flow' => true,
85 )
86 );
87 } else {
88 wp_enqueue_script(
89 'cookiebot-account-js',
90 CYBOT_COOKIEBOT_PLUGIN_URL . 'assets/js/backend/account.js',
91 array(),
92 Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION,
93 true
94 );
95
96 wp_localize_script(
97 'cookiebot-account-js',
98 'cookiebot_account',
99 array(
100 'ajax_url' => admin_url( 'admin-ajax.php' ),
101 'nonce' => wp_create_nonce( 'cookiebot-account' ),
102 'debug' => true,
103 )
104 );
105 }
106 }
107
108 public function ajax_store_cbid() {
109 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
110 wp_send_json_error( 'Unauthorized', 401 );
111 }
112
113 $cbid = isset( $_POST['cbid'] ) ? sanitize_text_field( $_POST['cbid'] ) : '';
114
115 if ( empty( $cbid ) ) {
116 wp_send_json_error( 'CBID is required', 400 );
117 }
118
119 // Store with consistent name
120 update_option( 'cookiebot-cbid', $cbid );
121
122 wp_send_json_success();
123 }
124
125 public function ajax_post_user_data() {
126 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
127 wp_send_json_error( 'Unauthorized', 401 );
128 return;
129 }
130
131 $raw_data = isset( $_POST['data'] ) ? wp_unslash( $_POST['data'] ) : '';
132
133 if ( empty( $raw_data ) ) {
134 wp_send_json_error( 'No data provided', 400 );
135 return;
136 }
137
138 $data = json_decode( $raw_data, true );
139 update_option( 'cookiebot-user-data', $data );
140
141 wp_send_json_success( array( 'message' => 'User data stored successfully' ) );
142 }
143
144 public function ajax_get_user_data() {
145 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
146 wp_send_json_error( 'Unauthorized', 401 );
147 return;
148 }
149
150 $user_data = get_option( 'cookiebot-user-data' );
151 wp_send_json_success( $user_data );
152 }
153
154 public function ajax_store_scan_details() {
155 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
156 wp_send_json_error( 'Unauthorized', 401 );
157 return;
158 }
159
160 $scan_id = isset( $_POST['scan_id'] ) ? sanitize_text_field( $_POST['scan_id'] ) : '';
161 $scan_status = isset( $_POST['scan_status'] ) ? wp_unslash( $_POST['scan_status'] ) : '';
162
163 update_option( 'cookiebot-scan-id', $scan_id );
164 update_option( 'cookiebot-scan-status', $scan_status );
165
166 wp_send_json_success( array( 'message' => 'Scan details stored successfully' ) );
167 }
168
169 public function ajax_set_banner_enabled() {
170 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
171 wp_send_json_error( 'Unauthorized', 401 );
172 return;
173 }
174
175 $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : '';
176
177 // Save option value
178 update_option( 'cookiebot-banner-enabled', $value );
179 wp_send_json_success();
180 }
181
182 public function ajax_set_auto_blocking_mode() {
183 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
184 wp_send_json_error( 'Unauthorized', 401 );
185 return;
186 }
187
188 $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : '';
189
190 // Save option value
191 update_option( 'cookiebot-uc-auto-blocking-mode', $value );
192 wp_send_json_success();
193 }
194
195 public function ajax_delete_auth_token() {
196 delete_option( 'cookiebot-auth-token' );
197 wp_send_json_success();
198 }
199
200 public function ajax_set_gcm_enabled() {
201 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
202 wp_send_json_error( 'Unauthorized', 401 );
203 return;
204 }
205
206 $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : '';
207
208 // Save option value
209 update_option( 'cookiebot-gcm', $value );
210 wp_send_json_success();
211 }
212
213 public function ajax_get_cbid() {
214 $cbid_req = get_option( 'cookiebot-cbid' );
215 if ( $cbid_req ) {
216 wp_send_json_success( $cbid_req );
217 } else {
218 wp_send_json_error( 'No CBID found', 404 );
219 }
220 }
221
222 public function ajax_get_auth_token() {
223 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
224 wp_send_json_error( 'Unauthorized', 401 );
225 return;
226 }
227
228 $auth_token = get_option( 'cookiebot-auth-token' );
229 wp_send_json_success( $auth_token );
230 }
231
232 public function ajax_process_auth_code() {
233 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
234 wp_send_json_error( 'Unauthorized', 401 );
235 return;
236 }
237
238 $code = isset( $_POST['code'] ) ? sanitize_text_field( $_POST['code'] ) : '';
239
240 if ( empty( $code ) ) {
241 wp_send_json_error( 'No code provided', 400 );
242 return;
243 }
244
245 // Use POST request with code as query parameter
246 // phpcs:ignore
247 $api_url = 'https://api.ea.prod.usercentrics.cloud/v1/auth/auth0/exchange?code=' . urlencode( $code );
248
249 $response = wp_remote_post(
250 $api_url,
251 array(
252 'timeout' => 45,
253 'headers' => array(
254 'Content-Type' => 'application/json',
255 'Accept' => 'application/json',
256 ),
257 'body' => '',
258 )
259 );
260
261 if ( is_wp_error( $response ) ) {
262 $error_message = $response->get_error_message();
263 wp_send_json_error( 'Error: ' . $error_message, 500 );
264 return;
265 }
266
267 $status = wp_remote_retrieve_response_code( $response );
268 $body = wp_remote_retrieve_body( $response );
269
270 $data = json_decode( $body, true );
271 $token = $data['token'];
272
273 update_option( 'cookiebot-auth-token', $token );
274 }
275
276 public function ajax_dismiss_banner() {
277 // Check if user has permission
278 if ( ! current_user_can( 'manage_options' ) ) {
279 wp_send_json_error( 'Unauthorized', 401 );
280 return;
281 }
282
283 // Store the dismissed state as a site option
284 update_option( 'cookiebot_banner_live_dismissed', true );
285
286 wp_send_json_success( array( 'message' => 'Banner dismissed successfully' ) );
287 }
288
289 public function ajax_get_scan_details() {
290 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
291 wp_send_json_error( 'Unauthorized', 401 );
292 return;
293 }
294
295 $scan_id = get_option( 'cookiebot-scan-id' );
296 $scan_status = get_option( 'cookiebot-scan-status', '' );
297
298 if ( $scan_id ) {
299 wp_send_json_success(
300 array(
301 'scan_id' => $scan_id,
302 'scan_status' => $scan_status,
303 )
304 );
305 } else {
306 wp_send_json_error( 'No scan details found', 404 );
307 }
308 }
309
310 public function ajax_store_configuration() {
311 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
312 wp_send_json_error( 'Unauthorized', 401 );
313 return;
314 }
315
316 $configuration = isset( $_POST['configuration'] ) ? wp_unslash( $_POST['configuration'] ) : '';
317
318 if ( empty( $configuration ) ) {
319 wp_send_json_error( 'Configuration data is required', 400 );
320 return;
321 }
322
323 $data = json_decode( $configuration, true );
324 if ( json_last_error() !== JSON_ERROR_NONE ) {
325 wp_send_json_error( 'Invalid configuration data format', 400 );
326 return;
327 }
328
329 update_option( 'cookiebot-configuration', $data );
330 wp_send_json_success( array( 'message' => 'Configuration stored successfully' ) );
331 }
332
333 public function ajax_clear_config_data() {
334 if ( ! current_user_can( 'manage_options' ) ) {
335 wp_send_json_error( 'Unauthorized', 401 );
336 return;
337 }
338
339 // Save option value
340 delete_option( 'cookiebot-cbid' );
341 delete_option( 'cookiebot-auth-token' );
342 delete_option( 'cookiebot-user-data' );
343 delete_option( 'cookiebot-configuration' );
344 delete_option( 'cookiebot-scan-id' );
345 delete_option( 'cookiebot-scan-status' );
346 delete_option( 'cookiebot-banner-enabled' );
347 delete_option( 'cookiebot_banner_live_dismissed' );
348 wp_send_json_success();
349 }
350
351 public function ajax_clear_config_data_keep_cbid() {
352 if ( ! current_user_can( 'manage_options' ) ) {
353 wp_send_json_error( 'Unauthorized', 401 );
354 return;
355 }
356
357 // Save option value
358 delete_option( 'cookiebot-auth-token' );
359 delete_option( 'cookiebot-user-data' );
360 delete_option( 'cookiebot-configuration' );
361 delete_option( 'cookiebot-scan-id' );
362 delete_option( 'cookiebot-scan-status' );
363 delete_option( 'cookiebot-banner-enabled' );
364 delete_option( 'cookiebot_banner_live_dismissed' );
365 wp_send_json_success();
366 }
367
368 public function ajax_store_onboarding_status() {
369 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
370 wp_send_json_error( 'Unauthorized', 401 );
371 return;
372 }
373
374 $onboarded = isset( $_POST['onboarded'] ) ? (bool) $_POST['onboarded'] : false;
375 update_option( 'cookiebot-uc-onboarded-via-signup', $onboarded );
376
377 wp_send_json_success( array( 'message' => 'Onboarding status stored successfully' ) );
378 }
379 }
380