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
312 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_process_auth_code', array( $this, 'ajax_process_auth_code' ) ); |
| 43 | add_action( 'wp_ajax_cookiebot_dismiss_banner', array( $this, 'ajax_dismiss_banner' ) ); |
| 44 | add_action( 'wp_ajax_cookiebot_post_user_data', array( $this, 'ajax_post_user_data' ) ); |
| 45 | add_action( 'wp_ajax_cookiebot_store_scan_details', array( $this, 'ajax_store_scan_details' ) ); |
| 46 | add_action( 'wp_ajax_cookiebot_get_scan_details', array( $this, 'ajax_get_scan_details' ) ); |
| 47 | add_action( 'wp_ajax_cookiebot_store_configuration', array( $this, 'ajax_store_configuration' ) ); |
| 48 | add_action( 'wp_ajax_cookiebot_clear_config_data', array( $this, 'ajax_clear_config_data' ) ); |
| 49 | add_action( 'wp_ajax_cookiebot_clear_config_data_keep_cbid', array( $this, 'ajax_clear_config_data_keep_cbid' ) ); |
| 50 | } |
| 51 | |
| 52 | public function cookiebot_admin_script( $hook ) { |
| 53 | if ( 'toplevel_page_' . Dashboard_Page::ADMIN_SLUG !== $hook ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | wp_enqueue_script( |
| 58 | 'cookiebot-account-js', |
| 59 | CYBOT_COOKIEBOT_PLUGIN_URL . 'assets/js/backend/account.js', |
| 60 | array(), |
| 61 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 62 | true |
| 63 | ); |
| 64 | |
| 65 | wp_localize_script( |
| 66 | 'cookiebot-account-js', |
| 67 | 'cookiebot_account', |
| 68 | array( |
| 69 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 70 | 'nonce' => wp_create_nonce( 'cookiebot-account' ), |
| 71 | 'debug' => true, |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | public function ajax_store_cbid() { |
| 77 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 78 | wp_send_json_error( 'Unauthorized', 401 ); |
| 79 | } |
| 80 | |
| 81 | $cbid = isset( $_POST['cbid'] ) ? sanitize_text_field( $_POST['cbid'] ) : ''; |
| 82 | |
| 83 | if ( empty( $cbid ) ) { |
| 84 | wp_send_json_error( 'CBID is required', 400 ); |
| 85 | } |
| 86 | |
| 87 | // Store with consistent name |
| 88 | update_option( 'cookiebot-cbid', $cbid ); |
| 89 | |
| 90 | wp_send_json_success(); |
| 91 | } |
| 92 | |
| 93 | public function ajax_post_user_data() { |
| 94 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 95 | wp_send_json_error( 'Unauthorized', 401 ); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $raw_data = isset( $_POST['data'] ) ? wp_unslash( $_POST['data'] ) : ''; |
| 100 | |
| 101 | if ( empty( $raw_data ) ) { |
| 102 | wp_send_json_error( 'No data provided', 400 ); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | $data = json_decode( $raw_data, true ); |
| 107 | |
| 108 | // Add onboarding flag |
| 109 | $data['onboarded_via_signup'] = true; |
| 110 | update_option( 'cookiebot-user-data', $data ); |
| 111 | |
| 112 | wp_send_json_success( array( 'message' => 'User data stored successfully' ) ); |
| 113 | } |
| 114 | |
| 115 | public function ajax_store_scan_details() { |
| 116 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 117 | wp_send_json_error( 'Unauthorized', 401 ); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $scan_id = isset( $_POST['scan_id'] ) ? sanitize_text_field( $_POST['scan_id'] ) : ''; |
| 122 | $scan_status = isset( $_POST['scan_status'] ) ? wp_unslash( $_POST['scan_status'] ) : ''; |
| 123 | |
| 124 | update_option( 'cookiebot-scan-id', $scan_id ); |
| 125 | update_option( 'cookiebot-scan-status', $scan_status ); |
| 126 | |
| 127 | wp_send_json_success( array( 'message' => 'Scan details stored successfully' ) ); |
| 128 | } |
| 129 | |
| 130 | public function ajax_set_banner_enabled() { |
| 131 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 132 | wp_send_json_error( 'Unauthorized', 401 ); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : ''; |
| 137 | |
| 138 | // Save option value |
| 139 | update_option( 'cookiebot-banner-enabled', $value ); |
| 140 | wp_send_json_success(); |
| 141 | } |
| 142 | |
| 143 | public function ajax_set_gcm_enabled() { |
| 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 | $value = isset( $_POST['value'] ) ? trim( $_POST['value'] ) : ''; |
| 150 | |
| 151 | // Save option value |
| 152 | update_option( 'cookiebot-gcm', $value ); |
| 153 | wp_send_json_success(); |
| 154 | } |
| 155 | |
| 156 | public function ajax_get_cbid() { |
| 157 | $cbid_req = get_option( 'cookiebot-cbid' ); |
| 158 | if ( $cbid_req ) { |
| 159 | wp_send_json_success( $cbid_req ); |
| 160 | } else { |
| 161 | wp_send_json_error( 'No CBID found', 404 ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | public function ajax_get_auth_token() { |
| 166 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 167 | wp_send_json_error( 'Unauthorized', 401 ); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | $auth_token = get_option( 'cookiebot-auth-token' ); |
| 172 | wp_send_json_success( $auth_token ); |
| 173 | } |
| 174 | |
| 175 | public function ajax_process_auth_code() { |
| 176 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 177 | wp_send_json_error( 'Unauthorized', 401 ); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | $code = isset( $_POST['code'] ) ? sanitize_text_field( $_POST['code'] ) : ''; |
| 182 | |
| 183 | if ( empty( $code ) ) { |
| 184 | wp_send_json_error( 'No code provided', 400 ); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // Use POST request with code as query parameter |
| 189 | // $api_url = 'https://api.ea.dev.usercentrics.cloud/v1/auth/auth0/exchange?code=' . urlencode( $code ); |
| 190 | // phpcs:ignore |
| 191 | $api_url = 'https://api.ea.prod.usercentrics.cloud/v1/auth/auth0/exchange?code=' . urlencode( $code ); |
| 192 | |
| 193 | $response = wp_remote_post( |
| 194 | $api_url, |
| 195 | array( |
| 196 | 'timeout' => 45, |
| 197 | 'headers' => array( |
| 198 | 'Content-Type' => 'application/json', |
| 199 | 'Accept' => 'application/json', |
| 200 | ), |
| 201 | 'body' => '', |
| 202 | ) |
| 203 | ); |
| 204 | |
| 205 | if ( is_wp_error( $response ) ) { |
| 206 | $error_message = $response->get_error_message(); |
| 207 | wp_send_json_error( 'Error: ' . $error_message, 500 ); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | $status = wp_remote_retrieve_response_code( $response ); |
| 212 | $body = wp_remote_retrieve_body( $response ); |
| 213 | |
| 214 | $data = json_decode( $body, true ); |
| 215 | $token = $data['token']; |
| 216 | |
| 217 | update_option( 'cookiebot-auth-token', $token ); |
| 218 | } |
| 219 | |
| 220 | public function ajax_dismiss_banner() { |
| 221 | // Check if user has permission |
| 222 | if ( ! current_user_can( 'manage_options' ) ) { |
| 223 | wp_send_json_error( 'Unauthorized', 401 ); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // Store the dismissed state as a site option |
| 228 | update_option( 'cookiebot_banner_live_dismissed', true ); |
| 229 | |
| 230 | wp_send_json_success( array( 'message' => 'Banner dismissed successfully' ) ); |
| 231 | } |
| 232 | |
| 233 | public function ajax_get_scan_details() { |
| 234 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 235 | wp_send_json_error( 'Unauthorized', 401 ); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | $scan_id = get_option( 'cookiebot-scan-id' ); |
| 240 | $scan_status = get_option( 'cookiebot-scan-status', '' ); |
| 241 | |
| 242 | if ( $scan_id ) { |
| 243 | wp_send_json_success( |
| 244 | array( |
| 245 | 'scan_id' => $scan_id, |
| 246 | 'scan_status' => $scan_status, |
| 247 | ) |
| 248 | ); |
| 249 | } else { |
| 250 | wp_send_json_error( 'No scan details found', 404 ); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | public function ajax_store_configuration() { |
| 255 | if ( ! check_ajax_referer( 'cookiebot-account', 'nonce', false ) || ! current_user_can( 'manage_options' ) ) { |
| 256 | wp_send_json_error( 'Unauthorized', 401 ); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | $configuration = isset( $_POST['configuration'] ) ? wp_unslash( $_POST['configuration'] ) : ''; |
| 261 | |
| 262 | if ( empty( $configuration ) ) { |
| 263 | wp_send_json_error( 'Configuration data is required', 400 ); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | $data = json_decode( $configuration, true ); |
| 268 | if ( json_last_error() !== JSON_ERROR_NONE ) { |
| 269 | wp_send_json_error( 'Invalid configuration data format', 400 ); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | update_option( 'cookiebot-configuration', $data ); |
| 274 | wp_send_json_success( array( 'message' => 'Configuration stored successfully' ) ); |
| 275 | } |
| 276 | |
| 277 | public function ajax_clear_config_data() { |
| 278 | if ( ! current_user_can( 'manage_options' ) ) { |
| 279 | wp_send_json_error( 'Unauthorized', 401 ); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | // Save option value |
| 284 | delete_option( 'cookiebot-cbid' ); |
| 285 | delete_option( 'cookiebot-auth-token' ); |
| 286 | delete_option( 'cookiebot-user-data' ); |
| 287 | delete_option( 'cookiebot-configuration' ); |
| 288 | delete_option( 'cookiebot-scan-id' ); |
| 289 | delete_option( 'cookiebot-scan-status' ); |
| 290 | delete_option( 'cookiebot-banner-enabled' ); |
| 291 | delete_option( 'cookiebot_banner_live_dismissed' ); |
| 292 | wp_send_json_success(); |
| 293 | } |
| 294 | |
| 295 | public function ajax_clear_config_data_keep_cbid() { |
| 296 | if ( ! current_user_can( 'manage_options' ) ) { |
| 297 | wp_send_json_error( 'Unauthorized', 401 ); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | // Save option value |
| 302 | delete_option( 'cookiebot-auth-token' ); |
| 303 | delete_option( 'cookiebot-user-data' ); |
| 304 | delete_option( 'cookiebot-configuration' ); |
| 305 | delete_option( 'cookiebot-scan-id' ); |
| 306 | delete_option( 'cookiebot-scan-status' ); |
| 307 | delete_option( 'cookiebot-banner-enabled' ); |
| 308 | delete_option( 'cookiebot_banner_live_dismissed' ); |
| 309 | wp_send_json_success(); |
| 310 | } |
| 311 | } |
| 312 |