| 1 |
<?php |
| 2 |
|
| 3 |
namespace CookieAdmin\Admin; |
| 4 |
|
| 5 |
if(!defined('COOKIEADMIN_VERSION') || !defined('ABSPATH')){ |
| 6 |
die('Hacking Attempt'); |
| 7 |
} |
| 8 |
|
| 9 |
class Scan{ |
| 10 |
|
| 11 |
static function show_cookies(){ |
| 12 |
global $cookieadmin_lang, $cookieadmin_error, $cookieadmin_msg, $wpdb; |
| 13 |
|
| 14 |
\CookieAdmin\Admin::header_theme(__('Manage Cookies', 'cookieadmin')); |
| 15 |
|
| 16 |
$cookieadmin_requires_pro = \CookieAdmin\Admin::is_feature_available(1); |
| 17 |
|
| 18 |
$table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); |
| 19 |
|
| 20 |
$categorized = []; |
| 21 |
$categorized_cookies = []; |
| 22 |
|
| 23 |
|
| 24 |
$scanned_cookies = $wpdb->get_results("SELECT * FROM {$table_name}"); |
| 25 |
|
| 26 |
foreach($scanned_cookies as $row => $data){ |
| 27 |
|
| 28 |
$expires = 0; |
| 29 |
|
| 30 |
if(!empty($data->expires) && !empty($data->scan_timestamp)){ |
| 31 |
$expires = strtotime($data->expires); |
| 32 |
$timestamp = $data->scan_timestamp; |
| 33 |
|
| 34 |
if(!empty($expires) && ($expires > 0) && !empty($timestamp)){ |
| 35 |
$expires = round(($expires - $timestamp) / 86400); |
| 36 |
}else{ |
| 37 |
$expires = 0; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
if($expires < 1){ |
| 42 |
$exp = __('Session', 'cookieadmin'); |
| 43 |
}else{ |
| 44 |
$exp = $expires . ' '.__('Day(s)', 'cookieadmin'); |
| 45 |
} |
| 46 |
|
| 47 |
if(empty($data->category)){ |
| 48 |
$data->category = 'Unknown'; |
| 49 |
} |
| 50 |
|
| 51 |
if(!isset($categorized[$data->category])){ |
| 52 |
$categorized[$data->category] = ''; |
| 53 |
} |
| 54 |
|
| 55 |
if(empty($data->description)){ |
| 56 |
$data->description = 'Not Available'; |
| 57 |
} |
| 58 |
|
| 59 |
$categorized[$data->category] .= '<tr><td>'.esc_html($data->cookie_name).'</td><td>'.esc_html($data->description).'</td><td>'.esc_html($exp).'</td><td> <span class="dashicons dashicons-edit cookieadmin_edit_icon" id="edit_'.esc_attr($data->id).'"></span> <span class="dashicons dashicons-trash cookieadmin_delete_icon" id="delete_'.esc_attr($data->id).'"></span> </td></tr>'; |
| 60 |
|
| 61 |
$categorized_cookies[$data->id]['id'] = $data->id; |
| 62 |
$categorized_cookies[$data->id]['cookie_name'] = $data->cookie_name; |
| 63 |
$categorized_cookies[$data->id]['description'] = $data->description; |
| 64 |
$categorized_cookies[$data->id]['category'] = $data->category; |
| 65 |
$categorized_cookies[$data->id]['expires'] = $expires; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
wp_register_script('cookieadmin_categorized_cookies', '', array('jquery'), COOKIEADMIN_VERSION, true); |
| 70 |
wp_enqueue_script('cookieadmin_categorized_cookies'); |
| 71 |
wp_localize_script('cookieadmin_categorized_cookies', 'categorized_cookies', $categorized_cookies); |
| 72 |
|
| 73 |
$no_cookies = '<tr class="cookieadmin-empty-row"><td colspan=4>'.esc_html__('No Cookies Found!', 'cookieadmin').'</td></tr>'; |
| 74 |
$no_cookies_hidden = '<tr class="cookieadmin-empty-row" hidden><td colspan=4>'.esc_html__('No Cookies Found!', 'cookieadmin').'</td></tr>'; |
| 75 |
|
| 76 |
echo ' |
| 77 |
<div class="cookieadmin_consent-wrap"> |
| 78 |
<form action="" method="post"> |
| 79 |
<div class="cookieadmin_consent-contents"> |
| 80 |
<div class="cookieadmin_consent_settings"> |
| 81 |
<div class="cookieadmin-setting cookieadmin-manager-cookie-scan"> |
| 82 |
<div class="cookieadmin-scan-cookie-info"> |
| 83 |
<label class="cookieadmin-title">'.esc_html__('Scanned Cookies', 'cookieadmin').'</label> |
| 84 |
<p class="cookieadmin-desc">'.esc_html__('Scanned cookies will be automatically categorised and displayed here. You can add, edit and delete cookies as per your needs.', 'cookieadmin').'</p> |
| 85 |
</div> |
| 86 |
<div class="cookieadmin-setting-contents cookieadmin-buttons-div"> |
| 87 |
<div class="cookieadmin-setting-contents cookieadmin-add-cookie-div"> |
| 88 |
<input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-add-cookie" value="'.esc_html__('Add Cookie', 'cookieadmin').'" cookieadmin-pro-only="1"></input> |
| 89 |
'.wp_kses_post($cookieadmin_requires_pro).' |
| 90 |
</div> |
| 91 |
<div class="cookieadmin-setting-contents cookieadmin-cookie-scan'.( !empty($cookieadmin_requires_pro) ? ' cookieadmin-tooltip-box' : '').'" data-tip="'.esc_html__('Basic scan might miss some cookies.', 'cookieadmin').'">'. |
| 92 |
( !empty($cookieadmin_requires_pro) ? ' |
| 93 |
<input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-scan" value="'.esc_html__('Scan', 'cookieadmin').'"></input> ' : '').' |
| 94 |
<input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-scan" value="'.esc_html__('Full Scan', 'cookieadmin').'" cookieadmin-pro-only="1"> |
| 95 |
'.wp_kses_post($cookieadmin_requires_pro).' |
| 96 |
</div> |
| 97 |
</div> |
| 98 |
<div class="cookieadmin-manager-result"> |
| 99 |
<table class="cookieadmin-table cookieadmin-cookie-categorized"> |
| 100 |
<thead> |
| 101 |
<tr> |
| 102 |
<th width="30%">'.esc_html__('Name', 'cookieadmin').'</th> |
| 103 |
<th width="50%">'.esc_html__('Description', 'cookieadmin').'</th> |
| 104 |
<th width="10%">'.esc_html__('Expiry', 'cookieadmin').'</th> |
| 105 |
<th width="10%">'.esc_html__('Action', 'cookieadmin').'</th> |
| 106 |
</tr> |
| 107 |
</thead> |
| 108 |
<tbody id="necessary_tbody"> |
| 109 |
<tr><td colspan=4>'.esc_html__('Necessary Cookies', 'cookieadmin').'</td></tr> |
| 110 |
'.( !empty($categorized['Necessary']) ? $no_cookies_hidden . wp_kses_post($categorized['Necessary']) : $no_cookies ).' |
| 111 |
</tbody> |
| 112 |
<tbody id="functional_tbody"> |
| 113 |
<tr><td colspan=4>'.esc_html__('Functional Cookies', 'cookieadmin').'</td></tr> |
| 114 |
'.( !empty($categorized['Functional']) ? $no_cookies_hidden . wp_kses_post($categorized['Functional']) : $no_cookies ).' |
| 115 |
</tbody> |
| 116 |
<tbody id="analytics_tbody"> |
| 117 |
<tr><td colspan=4>'.esc_html__('Analytical Cookies', 'cookieadmin').'</td></tr> |
| 118 |
'.( !empty($categorized['Analytics']) ? $no_cookies_hidden . wp_kses_post($categorized['Analytics']) :$no_cookies ).' |
| 119 |
</tbody> |
| 120 |
<tbody id="marketing_tbody"> |
| 121 |
<tr><td colspan=4>'.esc_html__('Marketing Cookies', 'cookieadmin').'</td></tr> |
| 122 |
'.( !empty($categorized['Marketing']) ? $no_cookies_hidden . wp_kses_post($categorized['Marketing']) : $no_cookies ).' |
| 123 |
</tbody> |
| 124 |
<tbody id="unknown_tbody"> |
| 125 |
<tr><td colspan=4>'.esc_html__('Unknown Cookies', 'cookieadmin').'</td></tr> |
| 126 |
'.( !empty($categorized['Unknown']) ? $no_cookies_hidden . wp_kses_post($categorized['Unknown']) : $no_cookies ).' |
| 127 |
</tbody> |
| 128 |
</table> |
| 129 |
</div> |
| 130 |
</div> |
| 131 |
</div> |
| 132 |
</div>'; |
| 133 |
|
| 134 |
wp_nonce_field('cookieadmin_admin_nonce', 'cookieadmin_security'); |
| 135 |
|
| 136 |
echo ' |
| 137 |
</div> |
| 138 |
</form> |
| 139 |
<br/>'; |
| 140 |
|
| 141 |
\CookieAdmin\Admin::footer_theme(); |
| 142 |
|
| 143 |
echo ' |
| 144 |
<!-- Modal Overlay --> |
| 145 |
<div class="cookieadmin_modal-overlay" id="edit-cookie-modal" hidden> |
| 146 |
<div class="cookieadmin_modal-container"> |
| 147 |
<div class="cookieadmin_modal-header"> |
| 148 |
<h2>'.esc_html__('Edit Cookie', 'cookieadmin').'</h2> |
| 149 |
<button class="cookieadmin_dialog_modal_close_btn">×</button> |
| 150 |
</div> |
| 151 |
|
| 152 |
<div class="cookieadmin_modal-body"> |
| 153 |
<div class="cookieadmin_form-group"> |
| 154 |
<label for="cookieadmin-dialog-cookie-category">'.esc_html__('Category', 'cookieadmin').'</label> |
| 155 |
<select id="cookieadmin-dialog-cookie-category"> |
| 156 |
<option value="" selected>'.esc_html__('Select a category', 'cookieadmin').'</option> |
| 157 |
<option value="Necessary">'.esc_html__('Necessary', 'cookieadmin').'</option> |
| 158 |
<option value="Functional">'.esc_html__('Functional', 'cookieadmin').'</option> |
| 159 |
<option value="Analytics">'.esc_html__('Analytical', 'cookieadmin').'</option> |
| 160 |
<option value="Marketing">'.esc_html__('Marketing', 'cookieadmin').'</option> |
| 161 |
<option value="Unknown">'.esc_html__('Unknown', 'cookieadmin').'</option> |
| 162 |
</select> |
| 163 |
</div> |
| 164 |
|
| 165 |
<div class="cookieadmin_form-group"> |
| 166 |
<label for="cookie_id">'.esc_html__('Cookie Name/ID', 'cookieadmin').'</label> |
| 167 |
<input type="text" id="cookieadmin-dialog-cookie-name" Placeholder="'.esc_html__('Enter Cookie Name or id', 'cookieadmin').'"> |
| 168 |
</div> |
| 169 |
|
| 170 |
<div class="cookieadmin_form-group"> |
| 171 |
<label for="description">'.esc_html__('Description', 'cookieadmin').'</label> |
| 172 |
<textarea id="cookieadmin-dialog-cookie-desc" Placeholder="'.esc_html__('Enter Cookie description here', 'cookieadmin').'"></textarea> |
| 173 |
</div> |
| 174 |
|
| 175 |
<div class="cookieadmin_form-group"> |
| 176 |
<label for="duration">'.esc_html__('Duration', 'cookieadmin').'</label> |
| 177 |
<input type="number" min=0 id="cookieadmin-dialog-cookie-duration" Placeholder="'.esc_html__('Set 0 for Session or expiry in days', 'cookieadmin').'"> |
| 178 |
</div> |
| 179 |
</div> |
| 180 |
<div class="cookieadmin_modal-footer"> |
| 181 |
<span id="cookieadmin-message"></span> |
| 182 |
<button class="cookieadmin-btn cookieadmin-btn-primary" id="cookieadmin_dialog_save_btn" form="edit-cookie-form">'.esc_html__('Save', 'cookieadmin').'</button> |
| 183 |
</div> |
| 184 |
</div> |
| 185 |
</div>'; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
static function scan_cookies_ajax(){ |
| 191 |
global $cookieadmin_error; |
| 192 |
|
| 193 |
$urls = []; |
| 194 |
if(!empty($_REQUEST['urls'])){ |
| 195 |
$urls = map_deep(wp_unslash($_REQUEST['urls']), 'sanitize_url'); |
| 196 |
} |
| 197 |
|
| 198 |
if(cookieadmin_is_pro()){ |
| 199 |
$scanner_info = get_option('cookieadmin_pro_scanner', []); |
| 200 |
|
| 201 |
if(!empty($scanner_info['last_scan']) && (time() > $scanner_info['last_scan'] + 3600)){ |
| 202 |
wp_send_json([ |
| 203 |
'success' => false, |
| 204 |
'message' => __('Cookie Scan can be triggered once an hour', 'cookieadmin') |
| 205 |
]); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
self::scan_cookies($urls); |
| 210 |
|
| 211 |
if(!empty($cookieadmin_error)){ |
| 212 |
wp_send_json([ |
| 213 |
'success' => false, |
| 214 |
'message' => $cookieadmin_error] |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
wp_send_json(['success' => true, 'data' => null]); |
| 219 |
} |
| 220 |
|
| 221 |
// Orchestrator function for scanning cookies |
| 222 |
static function scan_cookies($urls = []){ |
| 223 |
global $cookieadmin_error; |
| 224 |
|
| 225 |
if(cookieadmin_is_pro()){ |
| 226 |
|
| 227 |
if(!method_exists('\CookieAdminPro\Admin', 'cookieadmin_get_site_urls')){ |
| 228 |
$urls = [home_url()]; |
| 229 |
} else { |
| 230 |
$urls = \CookieAdminPro\Admin::cookieadmin_get_site_urls($urls, 2); |
| 231 |
} |
| 232 |
|
| 233 |
$cookieData = apply_filters('cookieadmin_pro_scan_cookies', $urls); |
| 234 |
|
| 235 |
//Server side scann - skipped for now - need to discuss. |
| 236 |
// $cookieData2 = \CookieAdmin\Scanner::start_scan($urls); |
| 237 |
// $cookieData = array_replace_recursive($cookieData2, $cookieData1); |
| 238 |
|
| 239 |
if(!empty($cookieadmin_error)){ |
| 240 |
update_option('cookieadmin_scan', [ |
| 241 |
'status' => 3, |
| 242 |
'success' => false, |
| 243 |
'message' => $cookieadmin_error, |
| 244 |
'update' => time() |
| 245 |
]); |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
if(!empty($cookieData)){ |
| 250 |
|
| 251 |
if(function_exists('cookieadmin_pro_get_remaining_urls')){ |
| 252 |
// Check Remaining urls |
| 253 |
$remaining_urls = cookieadmin_pro_get_remaining_urls($urls); |
| 254 |
|
| 255 |
if(!empty($remaining_urls)){ |
| 256 |
//send next batch for scan |
| 257 |
wp_schedule_single_event(time() + 5, 'cookieadmin_run_auto_scan_batch', [$remaining_urls]); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
$res = self::save_raw_scan_results($cookieData); |
| 262 |
if(function_exists('cookieadmin_pro_update_scan_count')){ |
| 263 |
cookieadmin_pro_update_scan_count($res); |
| 264 |
} |
| 265 |
|
| 266 |
self::cookieadmin_auto_configure_cookies(); |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
}else{ |
| 271 |
$cookieData = \CookieAdmin\Scanner::start_scan(); |
| 272 |
if(!empty($cookieData)){ |
| 273 |
self::save_raw_scan_results($cookieData); |
| 274 |
return self::cookieadmin_auto_configure_cookies(); |
| 275 |
} |
| 276 |
} |
| 277 |
// cookieadmin_r_print($cookieData); |
| 278 |
|
| 279 |
if(defined('DOING_CRON') && get_transient('cookieadmin_auto_scan_in_progress')){ |
| 280 |
delete_transient('cookieadmin_auto_scan_in_progress'); |
| 281 |
} |
| 282 |
|
| 283 |
update_option('cookieadmin_scan', [ |
| 284 |
'status' => 3, |
| 285 |
'success' => false, |
| 286 |
'message' => __('No Cookies Found', 'cookieadmin'), |
| 287 |
'update' => time() |
| 288 |
]); |
| 289 |
|
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
static function save_raw_scan_results(array $found_cookies){ |
| 294 |
|
| 295 |
global $wpdb; |
| 296 |
|
| 297 |
$table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); |
| 298 |
|
| 299 |
if (empty($found_cookies)) { |
| 300 |
return ['inserted' => 0, 'updated' => 0]; |
| 301 |
} |
| 302 |
|
| 303 |
// Step 1: Fetch all existing cookie names from our database in one efficient query. |
| 304 |
$existing_cookies_in_db = $wpdb->get_col("SELECT cookie_name FROM {$table_name}"); |
| 305 |
// Use array_flip for very fast 'isset' lookups instead of slow 'in_array' in a loop. |
| 306 |
$existing_cookies_lookup = !empty($existing_cookies_in_db) ? array_flip($existing_cookies_in_db) : []; |
| 307 |
|
| 308 |
$results = ['inserted' => 0, 'updated' => 0]; |
| 309 |
|
| 310 |
// Step 2: Loop through each cookie found by the scanner. |
| 311 |
foreach ($found_cookies as $cookie_name => $cookie_data) { |
| 312 |
|
| 313 |
// Step 3: Check if the cookie exists in our DB. |
| 314 |
if (isset($existing_cookies_lookup[$cookie_name])) { |
| 315 |
|
| 316 |
$wpdb->update( |
| 317 |
$table_name, |
| 318 |
[ |
| 319 |
'domain' => sanitize_text_field($cookie_data['domain']), |
| 320 |
'path' => sanitize_text_field($cookie_data['path']), |
| 321 |
'expires' => !empty($cookie_data['session']) ? 0 : (!empty($cookie_data['expires']) ? $cookie_data['expires'] : null), |
| 322 |
'samesite' => !empty($cookie_data['samesite']) ? sanitize_text_field($cookie_data['samesite']) : null, |
| 323 |
'secure' => (int)($cookie_data['secure'] ?? 0), |
| 324 |
'httponly' => (int)($cookie_data['httponly'] ?? 0), |
| 325 |
'raw_name' => sanitize_text_field($cookie_name), |
| 326 |
'scan_timestamp' => time(), |
| 327 |
], // Data to update |
| 328 |
[ 'cookie_name' => $cookie_name ], // WHERE clause |
| 329 |
['%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d'], // Format for the data |
| 330 |
[ '%s' ] // Format for the WHERE clause |
| 331 |
); |
| 332 |
$results['updated']++; |
| 333 |
|
| 334 |
} else { |
| 335 |
|
| 336 |
// ------ INSERT a NEW cookie ------ |
| 337 |
$data = [ |
| 338 |
'cookie_name' => sanitize_text_field($cookie_name), |
| 339 |
'domain' => sanitize_text_field($cookie_data['domain']), |
| 340 |
'path' => sanitize_text_field($cookie_data['path']), |
| 341 |
'expires' => !empty($cookie_data['session']) ? 0 : (!empty($cookie_data['expires']) ? $cookie_data['expires'] : null), |
| 342 |
'samesite' => !empty($cookie_data['samesite']) ? sanitize_text_field($cookie_data['samesite']) : null, |
| 343 |
'secure' => (int)($cookie_data['secure'] ?? 0), |
| 344 |
'httponly' => (int)($cookie_data['httponly'] ?? 0), |
| 345 |
'raw_name' => sanitize_text_field($cookie_name), |
| 346 |
'scan_timestamp' => time(), |
| 347 |
]; |
| 348 |
|
| 349 |
$formats = ['%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d']; |
| 350 |
|
| 351 |
if ($wpdb->insert($table_name, $data, $formats)) { |
| 352 |
$results['inserted']++; |
| 353 |
} else { |
| 354 |
//error_log("CookieAdmin: Error inserting cookie data: " . $wpdb->last_error); |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
return $results; |
| 359 |
} |
| 360 |
|
| 361 |
static function cookieadmin_auto_configure_cookies(){ |
| 362 |
global $wpdb, $cookieadmin_error; |
| 363 |
|
| 364 |
$table_name = $wpdb->prefix . 'cookieadmin_cookies'; |
| 365 |
$categorized_cookies = []; |
| 366 |
$uncategorized_cookies = []; |
| 367 |
|
| 368 |
$all_cookies = $wpdb->get_results("SELECT id, cookie_name, category FROM {$table_name}"); |
| 369 |
|
| 370 |
foreach($all_cookies as $cookie){ |
| 371 |
|
| 372 |
if(!empty($cookie->category)){ |
| 373 |
$categorized_cookies[$cookie->id] = $cookie->cookie_name; |
| 374 |
}else{ |
| 375 |
$uncategorized_cookies[] = $cookie->cookie_name; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
if(!empty($uncategorized_cookies)){ |
| 380 |
|
| 381 |
$uncategorized_cookies = array_flip($uncategorized_cookies); |
| 382 |
$categorized_cookies = array_flip($categorized_cookies); |
| 383 |
|
| 384 |
|
| 385 |
$categorizd_cookies = \CookieAdmin\CookieCategorizer::categorize_cookies($uncategorized_cookies, $categorized_cookies); |
| 386 |
|
| 387 |
$remove_cookies = !empty($categorizd_cookies['remove_cookies']) ? $categorizd_cookies['remove_cookies'] : []; |
| 388 |
|
| 389 |
unset($categorizd_cookies['remove_cookies']); |
| 390 |
if(!empty($remove_cookies)){ |
| 391 |
$placeholders = implode(',', array_fill(0, count($remove_cookies), '%s')); |
| 392 |
$sql = $wpdb->prepare("DELETE FROM {$table_name} WHERE id IN ({$placeholders})", ...$remove_cookies); |
| 393 |
$wpdb->query($sql); |
| 394 |
} |
| 395 |
|
| 396 |
foreach($categorizd_cookies as $cookie_data){ |
| 397 |
|
| 398 |
$count = $wpdb->update( |
| 399 |
$table_name, |
| 400 |
[ 'cookie_name' => $cookie_data['cookie_name'], 'category' => $cookie_data['category'], 'description' => $cookie_data['description'], 'edited' => 1, 'patterns' => $cookie_data['patterns'] ], // Data to update |
| 401 |
[ 'raw_name' => $cookie_data['raw_name'] ], // WHERE |
| 402 |
[ '%s', '%s', '%s', '%d', '%s' ], // Format for the data |
| 403 |
[ '%s' ] // Format for the WHERE clause |
| 404 |
); |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
update_option('cookieadmin_scan', [ |
| 409 |
'status' => 3, |
| 410 |
'update' => time(), |
| 411 |
'success' => true, |
| 412 |
'count' => $count |
| 413 |
]); |
| 414 |
|
| 415 |
$categorized_cookies = $wpdb->get_results("SELECT id, cookie_name, category, expires, scan_timestamp, description FROM {$table_name}"); |
| 416 |
|
| 417 |
delete_option('cookieadmin_first_scan'); |
| 418 |
|
| 419 |
return $categorized_cookies; |
| 420 |
} |
| 421 |
|
| 422 |
$cookieadmin_error = $cookieadmin_error . ' ' . __('No new cookies Found!', 'cookieadmin'); |
| 423 |
|
| 424 |
return false; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
static function edit_cookies(){ |
| 429 |
global $wpdb; |
| 430 |
|
| 431 |
$table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); |
| 432 |
$data = null; |
| 433 |
|
| 434 |
if(empty($_REQUEST['cookie_info'])){ |
| 435 |
wp_send_json(['success' => false, |
| 436 |
'data' => null, |
| 437 |
'message' => __('Error : Cookie details missing', 'cookieadmin')] |
| 438 |
); |
| 439 |
} |
| 440 |
|
| 441 |
$cookie_info = map_deep(wp_unslash($_REQUEST['cookie_info']), 'sanitize_text_field'); |
| 442 |
|
| 443 |
$scan_timestamp = $wpdb->get_col($wpdb->prepare("SELECT scan_timestamp FROM {$table_name} WHERE id = %d", $cookie_info['id'])); |
| 444 |
|
| 445 |
if(empty($scan_timestamp)){ |
| 446 |
wp_send_json(['success' => false, |
| 447 |
'data' => null, |
| 448 |
'message' => __('Error : Invalid cookie record', 'cookieadmin')] |
| 449 |
); |
| 450 |
} |
| 451 |
|
| 452 |
$calculated_expiry_seconds = ($cookie_info['duration'] * 86400) + $scan_timestamp[0]; |
| 453 |
$calculated_expiry = date('Y-m-d H:i:s', $calculated_expiry_seconds); |
| 454 |
|
| 455 |
$resp = $wpdb->update( |
| 456 |
$table_name, |
| 457 |
[ 'cookie_name' => $cookie_info['name'], 'description' => $cookie_info['description'], 'expires' => $calculated_expiry, 'category' => $cookie_info['type'], 'edited' => 1], // Data to update |
| 458 |
[ 'id' => $cookie_info['id'] ], // WHERE |
| 459 |
[ '%s', '%s', '%s', '%s', '%d' ], // Format for the data |
| 460 |
[ '%d' ] // Format for the WHERE clause |
| 461 |
); |
| 462 |
|
| 463 |
if ($wpdb->last_error || $resp === false) { |
| 464 |
//error_log('DB Error: ' . $wpdb->last_error); // Log it |
| 465 |
wp_send_json(['success' => false, |
| 466 |
'data' => null, |
| 467 |
'message' => __('Cookie updation Failed, Error: ', 'cookieadmin') . esc_html($wpdb->last_error)]); |
| 468 |
} |
| 469 |
|
| 470 |
wp_send_json(['success' => true, |
| 471 |
'data' => $data, |
| 472 |
'message' => __('Cookie updation successful', 'cookieadmin')]); |
| 473 |
|
| 474 |
} |
| 475 |
|
| 476 |
static function delete_cookies(){ |
| 477 |
global $wpdb; |
| 478 |
|
| 479 |
$table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); |
| 480 |
|
| 481 |
if(empty($_REQUEST['cookie_raw_id'])){ |
| 482 |
wp_send_json(['success' => false, |
| 483 |
'data' => null, |
| 484 |
'message' => __('Error : Cookie Id missing', 'cookieadmin')]); |
| 485 |
} |
| 486 |
|
| 487 |
$cookie_id = (int) sanitize_text_field(wp_unslash($_REQUEST['cookie_raw_id'])); |
| 488 |
|
| 489 |
$resp = $wpdb->delete( $table_name, ['id' => $cookie_id], [ '%s' ] ); |
| 490 |
|
| 491 |
if ($wpdb->last_error || $resp === false) { |
| 492 |
//error_log('DB Error: ' . $wpdb->last_error); //Log it |
| 493 |
wp_send_json(['success' => false, |
| 494 |
'data' => null, |
| 495 |
'message' => __('Cookie deletion Failed, Error: ', 'cookieadmin') . esc_html($wpdb->last_error)]); |
| 496 |
} |
| 497 |
|
| 498 |
wp_send_json(['success' => true, |
| 499 |
'message' => __('Cookie deletion successful', 'cookieadmin')]); |
| 500 |
} |
| 501 |
|
| 502 |
} |