PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.5.3
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.5.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 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
369 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_store_scan_details', array( $this, 'ajax_store_scan_details' ) );
47 add_action( 'wp_ajax_cookiebot_get_scan_details', array( $this, 'ajax_get_scan_details' ) );
48 add_action( 'wp_ajax_cookiebot_store_configuration', array( $this, 'ajax_store_configuration' ) );
49 add_action( 'wp_ajax_cookiebot_clear_config_data', array( $this, 'ajax_clear_config_data' ) );
50 add_action( 'wp_ajax_cookiebot_clear_config_data_keep_cbid', array( $this, 'ajax_clear_config_data_keep_cbid' ) );
51 add_action( 'wp_ajax_cookiebot_delete_auth_token', array( $this, 'ajax_delete_auth_token' ) );
52 add_action( 'wp_ajax_cookiebot_store_onboarding_status', array( $this, 'ajax_store_onboarding_status' ) );
53 }
54
55 public function cookiebot_admin_script( $hook ) {
56 if ( 'toplevel_page_' . Dashboard_Page::ADMIN_SLUG !== $hook ) {
57 return;
58 }
59
60 $is_authenticated = ! empty( Cookiebot_WP::get_auth_token() );
61 $cbid = Cookiebot_WP::get_cbid();
62 $user_data = Cookiebot_WP::get_user_data();
63 $was_onboarded = Cookiebot_WP::was_onboarded_via_signup();
64
65 if ( ! $is_authenticated && ! empty( $cbid ) && ! empty( $user_data ) && ! empty( $was_onboarded ) ) {
66 wp_enqueue_script(
67 'cookiebot-account-static-js',
68 CYBOT_COOKIEBOT_PLUGIN_URL . 'assets/js/backend/account-static.js',
69 array( 'jquery' ),
70 Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION,
71 true
72 );
73
74 wp_localize_script(
75 'cookiebot-account-static-js',
76 'cookiebot_account',
77 array(
78 'ajax_url' => admin_url( 'admin-ajax.php' ),
79 'nonce' => wp_create_nonce( 'cookiebot-account' ),
80 'has_user_data' => ! empty( $user_data ),
81 'has_cbid' => ! empty( $cbid ),
82 'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
83 'auth_expired_flow' => true,
84 )
85 );
86 } else {
87 wp_enqueue_script(
88 'cookiebot-account-js',
89 CYBOT_COOKIEBOT_PLUGIN_URL . 'assets/js/backend/account.js',
90 array(),
91 Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION,
92 true
93 );
94
95 wp_localize_script(
96 'cookiebot-account-js',
97 'cookiebot_account',
98 array(
99 'ajax_url' => admin_url( 'admin-ajax.php' ),
100 'nonce' => wp_create_nonce( 'cookiebot-account' ),
101 'debug' => true,
102 )
103 );
104 }
105 }
106
107 public function ajax_store_cbid() {
108 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
109 wp_send_json_error( 'Unauthorized', 401 );
110 }
111
112 $cbid = isset( $_POST['cbid'] ) ? sanitize_text_field( $_POST['cbid'] ) : '';
113
114 if ( empty( $cbid ) ) {
115 wp_send_json_error( 'CBID is required', 400 );
116 }
117
118 // Store with consistent name
119 update_option( 'cookiebot-cbid', $cbid );
120
121 wp_send_json_success();
122 }
123
124 public function ajax_post_user_data() {
125 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
126 wp_send_json_error( 'Unauthorized', 401 );
127 return;
128 }
129
130 $raw_data = isset( $_POST['data'] ) ? wp_unslash( $_POST['data'] ) : '';
131
132 if ( empty( $raw_data ) ) {
133 wp_send_json_error( 'No data provided', 400 );
134 return;
135 }
136
137 $data = json_decode( $raw_data, true );
138 update_option( 'cookiebot-user-data', $data );
139
140 wp_send_json_success( array( 'message' => 'User data stored successfully' ) );
141 }
142
143 public function ajax_store_scan_details() {
144 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
145 wp_send_json_error( 'Unauthorized', 401 );
146 return;
147 }
148
149 $scan_id = isset( $_POST['scan_id'] ) ? sanitize_text_field( $_POST['scan_id'] ) : '';
150 $scan_status = isset( $_POST['scan_status'] ) ? wp_unslash( $_POST['scan_status'] ) : '';
151
152 update_option( 'cookiebot-scan-id', $scan_id );
153 update_option( 'cookiebot-scan-status', $scan_status );
154
155 wp_send_json_success( array( 'message' => 'Scan details stored successfully' ) );
156 }
157
158 public function ajax_set_banner_enabled() {
159 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
160 wp_send_json_error( 'Unauthorized', 401 );
161 return;
162 }
163
164 $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : '';
165
166 // Save option value
167 update_option( 'cookiebot-banner-enabled', $value );
168 wp_send_json_success();
169 }
170
171 public function ajax_set_auto_blocking_mode() {
172 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
173 wp_send_json_error( 'Unauthorized', 401 );
174 return;
175 }
176
177 $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : '';
178
179 // Save option value
180 update_option( 'cookiebot-uc-auto-blocking-mode', $value );
181 wp_send_json_success();
182 }
183
184 public function ajax_delete_auth_token() {
185 delete_option( 'cookiebot-auth-token' );
186 wp_send_json_success();
187 }
188
189 public function ajax_set_gcm_enabled() {
190 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
191 wp_send_json_error( 'Unauthorized', 401 );
192 return;
193 }
194
195 $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : '';
196
197 // Save option value
198 update_option( 'cookiebot-gcm', $value );
199 wp_send_json_success();
200 }
201
202 public function ajax_get_cbid() {
203 $cbid_req = get_option( 'cookiebot-cbid' );
204 if ( $cbid_req ) {
205 wp_send_json_success( $cbid_req );
206 } else {
207 wp_send_json_error( 'No CBID found', 404 );
208 }
209 }
210
211 public function ajax_get_auth_token() {
212 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
213 wp_send_json_error( 'Unauthorized', 401 );
214 return;
215 }
216
217 $auth_token = get_option( 'cookiebot-auth-token' );
218 wp_send_json_success( $auth_token );
219 }
220
221 public function ajax_process_auth_code() {
222 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
223 wp_send_json_error( 'Unauthorized', 401 );
224 return;
225 }
226
227 $code = isset( $_POST['code'] ) ? sanitize_text_field( $_POST['code'] ) : '';
228
229 if ( empty( $code ) ) {
230 wp_send_json_error( 'No code provided', 400 );
231 return;
232 }
233
234 // Use POST request with code as query parameter
235 // phpcs:ignore
236 $api_url = 'https://api.ea.prod.usercentrics.cloud/v1/auth/auth0/exchange?code=' . urlencode( $code );
237
238 $response = wp_remote_post(
239 $api_url,
240 array(
241 'timeout' => 45,
242 'headers' => array(
243 'Content-Type' => 'application/json',
244 'Accept' => 'application/json',
245 ),
246 'body' => '',
247 )
248 );
249
250 if ( is_wp_error( $response ) ) {
251 $error_message = $response->get_error_message();
252 wp_send_json_error( 'Error: ' . $error_message, 500 );
253 return;
254 }
255
256 $status = wp_remote_retrieve_response_code( $response );
257 $body = wp_remote_retrieve_body( $response );
258
259 $data = json_decode( $body, true );
260 $token = $data['token'];
261
262 update_option( 'cookiebot-auth-token', $token );
263 }
264
265 public function ajax_dismiss_banner() {
266 // Check if user has permission
267 if ( ! current_user_can( 'manage_options' ) ) {
268 wp_send_json_error( 'Unauthorized', 401 );
269 return;
270 }
271
272 // Store the dismissed state as a site option
273 update_option( 'cookiebot_banner_live_dismissed', true );
274
275 wp_send_json_success( array( 'message' => 'Banner dismissed successfully' ) );
276 }
277
278 public function ajax_get_scan_details() {
279 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
280 wp_send_json_error( 'Unauthorized', 401 );
281 return;
282 }
283
284 $scan_id = get_option( 'cookiebot-scan-id' );
285 $scan_status = get_option( 'cookiebot-scan-status', '' );
286
287 if ( $scan_id ) {
288 wp_send_json_success(
289 array(
290 'scan_id' => $scan_id,
291 'scan_status' => $scan_status,
292 )
293 );
294 } else {
295 wp_send_json_error( 'No scan details found', 404 );
296 }
297 }
298
299 public function ajax_store_configuration() {
300 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
301 wp_send_json_error( 'Unauthorized', 401 );
302 return;
303 }
304
305 $configuration = isset( $_POST['configuration'] ) ? wp_unslash( $_POST['configuration'] ) : '';
306
307 if ( empty( $configuration ) ) {
308 wp_send_json_error( 'Configuration data is required', 400 );
309 return;
310 }
311
312 $data = json_decode( $configuration, true );
313 if ( json_last_error() !== JSON_ERROR_NONE ) {
314 wp_send_json_error( 'Invalid configuration data format', 400 );
315 return;
316 }
317
318 update_option( 'cookiebot-configuration', $data );
319 wp_send_json_success( array( 'message' => 'Configuration stored successfully' ) );
320 }
321
322 public function ajax_clear_config_data() {
323 if ( ! current_user_can( 'manage_options' ) ) {
324 wp_send_json_error( 'Unauthorized', 401 );
325 return;
326 }
327
328 // Save option value
329 delete_option( 'cookiebot-cbid' );
330 delete_option( 'cookiebot-auth-token' );
331 delete_option( 'cookiebot-user-data' );
332 delete_option( 'cookiebot-configuration' );
333 delete_option( 'cookiebot-scan-id' );
334 delete_option( 'cookiebot-scan-status' );
335 delete_option( 'cookiebot-banner-enabled' );
336 delete_option( 'cookiebot_banner_live_dismissed' );
337 wp_send_json_success();
338 }
339
340 public function ajax_clear_config_data_keep_cbid() {
341 if ( ! current_user_can( 'manage_options' ) ) {
342 wp_send_json_error( 'Unauthorized', 401 );
343 return;
344 }
345
346 // Save option value
347 delete_option( 'cookiebot-auth-token' );
348 delete_option( 'cookiebot-user-data' );
349 delete_option( 'cookiebot-configuration' );
350 delete_option( 'cookiebot-scan-id' );
351 delete_option( 'cookiebot-scan-status' );
352 delete_option( 'cookiebot-banner-enabled' );
353 delete_option( 'cookiebot_banner_live_dismissed' );
354 wp_send_json_success();
355 }
356
357 public function ajax_store_onboarding_status() {
358 if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) {
359 wp_send_json_error( 'Unauthorized', 401 );
360 return;
361 }
362
363 $onboarded = isset( $_POST['onboarded'] ) ? (bool) $_POST['onboarded'] : false;
364 update_option( 'cookiebot-uc-onboarded-via-signup', $onboarded );
365
366 wp_send_json_success( array( 'message' => 'Onboarding status stored successfully' ) );
367 }
368 }
369