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