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
367 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_delete_auth_token', array( $this, 'ajax_delete_auth_token' ) ); |
| 51 | add_action( 'wp_ajax_cookiebot_store_onboarding_status', array( $this, 'ajax_store_onboarding_status' ) ); |
| 52 | add_action( 'wp_ajax_cookiebot_update_scan_id', array( $this, 'ajax_update_scan_id' ) ); |
| 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_update_scan_id() { |
| 125 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 126 | wp_send_json_error( 'Unauthorized', 401 ); |
| 127 | } |
| 128 | |
| 129 | $scan_id = isset( $_POST['scan_id'] ) ? sanitize_text_field( $_POST['scan_id'] ) : ''; |
| 130 | |
| 131 | if ( empty( $scan_id ) ) { |
| 132 | wp_send_json_error( 'Scan ID is required', 400 ); |
| 133 | } |
| 134 | |
| 135 | update_option( 'cookiebot-scan-id', $scan_id ); |
| 136 | wp_send_json_success(); |
| 137 | } |
| 138 | |
| 139 | public function ajax_post_user_data() { |
| 140 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 141 | wp_send_json_error( 'Unauthorized', 401 ); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | $raw_data = isset( $_POST['data'] ) ? wp_unslash( $_POST['data'] ) : ''; |
| 146 | |
| 147 | if ( empty( $raw_data ) ) { |
| 148 | wp_send_json_error( 'No data provided', 400 ); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | $data = json_decode( $raw_data, true ); |
| 153 | update_option( 'cookiebot-user-data', $data ); |
| 154 | |
| 155 | wp_send_json_success( array( 'message' => 'User data stored successfully' ) ); |
| 156 | } |
| 157 | |
| 158 | public function ajax_get_user_data() { |
| 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 | $user_data = get_option( 'cookiebot-user-data' ); |
| 165 | wp_send_json_success( $user_data ); |
| 166 | } |
| 167 | |
| 168 | public function ajax_store_scan_details() { |
| 169 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 170 | wp_send_json_error( 'Unauthorized', 401 ); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | $scan_id = isset( $_POST['scan_id'] ) ? sanitize_text_field( $_POST['scan_id'] ) : ''; |
| 175 | $scan_status = isset( $_POST['scan_status'] ) ? wp_unslash( $_POST['scan_status'] ) : ''; |
| 176 | |
| 177 | update_option( 'cookiebot-scan-id', $scan_id ); |
| 178 | update_option( 'cookiebot-scan-status', $scan_status ); |
| 179 | |
| 180 | wp_send_json_success( array( 'message' => 'Scan details stored successfully' ) ); |
| 181 | } |
| 182 | |
| 183 | public function ajax_set_banner_enabled() { |
| 184 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 185 | wp_send_json_error( 'Unauthorized', 401 ); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : ''; |
| 190 | |
| 191 | // Save option value |
| 192 | update_option( 'cookiebot-banner-enabled', $value ); |
| 193 | wp_send_json_success(); |
| 194 | } |
| 195 | |
| 196 | public function ajax_set_auto_blocking_mode() { |
| 197 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 198 | wp_send_json_error( 'Unauthorized', 401 ); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : ''; |
| 203 | |
| 204 | // Save option value |
| 205 | update_option( 'cookiebot-uc-auto-blocking-mode', $value ); |
| 206 | wp_send_json_success(); |
| 207 | } |
| 208 | |
| 209 | public function ajax_delete_auth_token() { |
| 210 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 211 | wp_send_json_error( 'Unauthorized', 401 ); |
| 212 | return; |
| 213 | } |
| 214 | delete_option( 'cookiebot-auth-token' ); |
| 215 | wp_send_json_success(); |
| 216 | } |
| 217 | |
| 218 | public function ajax_set_gcm_enabled() { |
| 219 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 220 | wp_send_json_error( 'Unauthorized', 401 ); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : ''; |
| 225 | |
| 226 | // Save option value |
| 227 | update_option( 'cookiebot-gcm', $value ); |
| 228 | wp_send_json_success(); |
| 229 | } |
| 230 | |
| 231 | public function ajax_get_cbid() { |
| 232 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 233 | wp_send_json_error( 'Unauthorized', 401 ); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | $cbid_req = get_option( 'cookiebot-cbid' ); |
| 238 | if ( $cbid_req ) { |
| 239 | wp_send_json_success( $cbid_req ); |
| 240 | } else { |
| 241 | wp_send_json_error( 'No CBID found', 404 ); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | public function ajax_get_auth_token() { |
| 246 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 247 | wp_send_json_error( 'Unauthorized', 401 ); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | $auth_token = get_option( 'cookiebot-auth-token' ); |
| 252 | wp_send_json_success( $auth_token ); |
| 253 | } |
| 254 | |
| 255 | public function ajax_process_auth_code() { |
| 256 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 257 | wp_send_json_error( 'Unauthorized', 401 ); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | $code = isset( $_POST['code'] ) ? sanitize_text_field( $_POST['code'] ) : ''; |
| 262 | |
| 263 | if ( empty( $code ) ) { |
| 264 | wp_send_json_error( 'No code provided', 400 ); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | // Use POST request with code as query parameter |
| 269 | // phpcs:ignore |
| 270 | $api_url = 'https://api.ea.prod.usercentrics.cloud/v1/auth/auth0/exchange?code=' . urlencode( $code ); |
| 271 | |
| 272 | $response = wp_remote_post( |
| 273 | $api_url, |
| 274 | array( |
| 275 | 'timeout' => 45, |
| 276 | 'headers' => array( |
| 277 | 'Content-Type' => 'application/json', |
| 278 | 'Accept' => 'application/json', |
| 279 | ), |
| 280 | 'body' => '', |
| 281 | ) |
| 282 | ); |
| 283 | |
| 284 | if ( is_wp_error( $response ) ) { |
| 285 | $error_message = $response->get_error_message(); |
| 286 | wp_send_json_error( 'Error: ' . $error_message, 500 ); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | $status = wp_remote_retrieve_response_code( $response ); |
| 291 | $body = wp_remote_retrieve_body( $response ); |
| 292 | |
| 293 | $data = json_decode( $body, true ); |
| 294 | $token = $data['token']; |
| 295 | |
| 296 | update_option( 'cookiebot-auth-token', $token ); |
| 297 | } |
| 298 | |
| 299 | public function ajax_dismiss_banner() { |
| 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 | // Store the dismissed state as a site option |
| 306 | update_option( 'cookiebot_banner_live_dismissed', true ); |
| 307 | |
| 308 | wp_send_json_success( array( 'message' => 'Banner dismissed successfully' ) ); |
| 309 | } |
| 310 | |
| 311 | public function ajax_get_scan_details() { |
| 312 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 313 | wp_send_json_error( 'Unauthorized', 401 ); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | $scan_id = get_option( 'cookiebot-scan-id' ); |
| 318 | $scan_status = get_option( 'cookiebot-scan-status', '' ); |
| 319 | |
| 320 | if ( $scan_id ) { |
| 321 | wp_send_json_success( |
| 322 | array( |
| 323 | 'scan_id' => $scan_id, |
| 324 | 'scan_status' => $scan_status, |
| 325 | ) |
| 326 | ); |
| 327 | } else { |
| 328 | wp_send_json_error( 'No scan details found', 404 ); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | public function ajax_store_configuration() { |
| 333 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 334 | wp_send_json_error( 'Unauthorized', 401 ); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | $configuration = isset( $_POST['configuration'] ) ? wp_unslash( $_POST['configuration'] ) : ''; |
| 339 | |
| 340 | if ( empty( $configuration ) ) { |
| 341 | wp_send_json_error( 'Configuration data is required', 400 ); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | $data = json_decode( $configuration, true ); |
| 346 | if ( json_last_error() !== JSON_ERROR_NONE ) { |
| 347 | wp_send_json_error( 'Invalid configuration data format', 400 ); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | update_option( 'cookiebot-configuration', $data ); |
| 352 | wp_send_json_success( array( 'message' => 'Configuration stored successfully' ) ); |
| 353 | } |
| 354 | |
| 355 | public function ajax_store_onboarding_status() { |
| 356 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 357 | wp_send_json_error( 'Unauthorized', 401 ); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | $onboarded = isset( $_POST['onboarded'] ) ? (bool) $_POST['onboarded'] : false; |
| 362 | update_option( 'cookiebot-uc-onboarded-via-signup', $onboarded ); |
| 363 | |
| 364 | wp_send_json_success( array( 'message' => 'Onboarding status stored successfully' ) ); |
| 365 | } |
| 366 | } |
| 367 |