PluginProbe
CookieAdmin – Cookie Consent Banner / 1.2.3
CookieAdmin – Cookie Consent Banner v1.2.3
1.2.3 1.2.2 1.2.1 1.2.0 1.1.9 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
← All changes | includes/admin/scan.php +548 -502 1.1.51.2.3 View file →
@@ -1,502 +1,548 @@
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">&times;</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, 1);
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 - $remainig_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 -}
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 + $patterns = !empty($data->patterns) ? $data->patterns : '';
60 + if(!empty($patterns)){
61 + $patterns = strtr($patterns, ['[' => '', ']' => '', '"' => '']);
62 + }
63 +
64 + $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>';
65 +
66 + $categorized_cookies[$data->id]['id'] = $data->id;
67 + $categorized_cookies[$data->id]['cookie_name'] = $data->cookie_name;
68 + $categorized_cookies[$data->id]['description'] = $data->description;
69 + $categorized_cookies[$data->id]['category'] = $data->category;
70 + $categorized_cookies[$data->id]['expires'] = $expires;
71 + $categorized_cookies[$data->id]['patterns'] = $patterns;
72 +
73 + }
74 +
75 + wp_register_script('cookieadmin_categorized_cookies', '', array('jquery'), COOKIEADMIN_VERSION, true);
76 + wp_enqueue_script('cookieadmin_categorized_cookies');
77 + wp_localize_script('cookieadmin_categorized_cookies', 'categorized_cookies', $categorized_cookies);
78 +
79 + $no_cookies = '<tr class="cookieadmin-empty-row"><td colspan=4>'.esc_html__('No Cookies Found!', 'cookieadmin').'</td></tr>';
80 + $no_cookies_hidden = '<tr class="cookieadmin-empty-row" hidden><td colspan=4>'.esc_html__('No Cookies Found!', 'cookieadmin').'</td></tr>';
81 +
82 + echo '
83 + <div class="cookieadmin_consent-wrap">
84 + <form action="" method="post">';
85 + do_action('cookieadmin_before_scan_results');
86 +
87 + echo '<div class="cookieadmin-card">
88 + <div class="cookieadmin-card-header">
89 + <div>
90 + <span class="cookieadmin-card-title"><span class="dashicons dashicons-list-view"></span> '.esc_html__('Scanned Cookies', 'cookieadmin').'</span>
91 + <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>
92 + </div>
93 + <div class="cookieadmin-toolbar-actions">
94 + <input type="button" class="cookieadmin-btn cookieadmin-btn-secondary cookieadmin-add-cookie" value="'.esc_html__('Add Cookie', 'cookieadmin').'" cookieadmin-pro-only="1"></input>
95 + '.wp_kses_post($cookieadmin_requires_pro).
96 + ( !empty($cookieadmin_requires_pro) ? '
97 + <input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-scan" value="'.esc_html__('Scan', 'cookieadmin').'"></input> ' : '').'
98 + <input type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-scan" value="'.esc_html__('Full Scan', 'cookieadmin').'" cookieadmin-pro-only="1">
99 + '.wp_kses_post($cookieadmin_requires_pro).'
100 + </div>
101 + </div>
102 + <div class="cookieadmin-card-body">
103 + <div class="cookieadmin-manager-result">
104 + <table class="cookieadmin-table cookieadmin-cookie-categorized">
105 + <thead>
106 + <tr>
107 + <th width="30%">'.esc_html__('Name', 'cookieadmin').'</th>
108 + <th width="50%">'.esc_html__('Description', 'cookieadmin').'</th>
109 + <th width="10%">'.esc_html__('Expiry', 'cookieadmin').'</th>
110 + <th width="10%">'.esc_html__('Action', 'cookieadmin').'</th>
111 + </tr>
112 + </thead>
113 + <tbody id="necessary_tbody">
114 + <tr><td colspan=4>'.esc_html__('Necessary Cookies', 'cookieadmin').'</td></tr>
115 + '.( !empty($categorized['Necessary']) ? $no_cookies_hidden . wp_kses_post($categorized['Necessary']) : $no_cookies ).'
116 + </tbody>
117 + <tbody id="functional_tbody">
118 + <tr><td colspan=4>'.esc_html__('Functional Cookies', 'cookieadmin').'</td></tr>
119 + '.( !empty($categorized['Functional']) ? $no_cookies_hidden . wp_kses_post($categorized['Functional']) : $no_cookies ).'
120 + </tbody>
121 + <tbody id="analytics_tbody">
122 + <tr><td colspan=4>'.esc_html__('Analytical Cookies', 'cookieadmin').'</td></tr>
123 + '.( !empty($categorized['Analytics']) ? $no_cookies_hidden . wp_kses_post($categorized['Analytics']) :$no_cookies ).'
124 + </tbody>
125 + <tbody id="marketing_tbody">
126 + <tr><td colspan=4>'.esc_html__('Marketing Cookies', 'cookieadmin').'</td></tr>
127 + '.( !empty($categorized['Marketing']) ? $no_cookies_hidden . wp_kses_post($categorized['Marketing']) : $no_cookies ).'
128 + </tbody>
129 + <tbody id="unknown_tbody">
130 + <tr><td colspan=4>'.esc_html__('Unknown Cookies', 'cookieadmin').'</td></tr>
131 + '.( !empty($categorized['Unknown']) ? $no_cookies_hidden . wp_kses_post($categorized['Unknown']) : $no_cookies ).'
132 + </tbody>
133 + </table>
134 + </div>
135 + </div>
136 + </div>';
137 +
138 + wp_nonce_field('cookieadmin_admin_nonce', 'cookieadmin_security');
139 +
140 + echo '
141 + </div>
142 + </form>
143 + <br/>';
144 +
145 + \CookieAdmin\Admin::footer_theme();
146 +
147 + echo '
148 + <!-- Modal Overlay -->
149 + <div class="cookieadmin_modal-overlay" id="edit-cookie-modal" hidden>
150 + <div class="cookieadmin_modal-container">
151 + <div class="cookieadmin_modal-header">
152 + <h2>'.esc_html__('Edit Cookie', 'cookieadmin').'</h2>
153 + <button class="cookieadmin_dialog_modal_close_btn">&times;</button>
154 + </div>
155 +
156 + <div class="cookieadmin_modal-body">
157 + <div class="cookieadmin_form-group">
158 + <label for="cookieadmin-dialog-cookie-category">'.esc_html__('Category', 'cookieadmin').'</label>
159 + <select id="cookieadmin-dialog-cookie-category">
160 + <option value="" selected>'.esc_html__('Select a category', 'cookieadmin').'</option>
161 + <option value="Necessary">'.esc_html__('Necessary', 'cookieadmin').'</option>
162 + <option value="Functional">'.esc_html__('Functional', 'cookieadmin').'</option>
163 + <option value="Analytics">'.esc_html__('Analytical', 'cookieadmin').'</option>
164 + <option value="Marketing">'.esc_html__('Marketing', 'cookieadmin').'</option>
165 + <option value="Unknown">'.esc_html__('Unknown', 'cookieadmin').'</option>
166 + </select>
167 + </div>
168 +
169 + <div class="cookieadmin_form-group">
170 + <label for="cookie_id">'.esc_html__('Cookie Name/ID', 'cookieadmin').'</label>
171 + <input type="text" id="cookieadmin-dialog-cookie-name" Placeholder="'.esc_html__('Enter Cookie Name or id', 'cookieadmin').'">
172 + </div>
173 +
174 + <div class="cookieadmin_form-group">
175 + <label for="description">'.esc_html__('Description', 'cookieadmin').'</label>
176 + <textarea id="cookieadmin-dialog-cookie-desc" Placeholder="'.esc_html__('Enter Cookie description here', 'cookieadmin').'"></textarea>
177 + </div>
178 +
179 + <div class="cookieadmin_form-group">
180 + <label for="duration">'.esc_html__('Duration', 'cookieadmin').'</label>
181 + <input type="number" min=0 id="cookieadmin-dialog-cookie-duration" Placeholder="'.esc_html__('Set 0 for Session or expiry in days', 'cookieadmin').'">
182 + </div>';
183 +
184 + if(empty($cookieadmin_requires_pro)){
185 + echo '<div class="cookieadmin_form-group">
186 + <label for="patterns">'.esc_html__('Scripts patterns', 'cookieadmin').'</label>
187 + <span>'.__('If scripts has "src" attribute then paste the src or add comma separated patterns to match the scripts. Try choosing log patterns to block only targeted script', 'cookieadmin').'</span>
188 + <br /><span><strong>'.__('Note: ', 'cookieadmin').'</strong>'.__('Wildcards (*, ?, ^, % etc) are not allowed', 'cookieadmin').'</span>
189 + <textarea id="cookieadmin-dialog-cookie-patterns" Placeholder="'.esc_attr__('https://connect.facebook.net/en_US/fbevents.js OR fbq.push, window.facebookSignals', 'cookieadmin').'"></textarea>
190 + </div>';
191 + }
192 + echo '</div>
193 + <div class="cookieadmin_modal-footer">
194 + <span id="cookieadmin-message"></span>
195 + <button class="cookieadmin-btn cookieadmin-btn-primary" id="cookieadmin_dialog_save_btn" form="edit-cookie-form">'.esc_html__('Save', 'cookieadmin').'</button>
196 + </div>
197 + </div>
198 + </div>';
199 + }
200 +
201 +
202 +
203 + static function scan_cookies_ajax(){
204 + global $cookieadmin_error;
205 +
206 + $urls = [];
207 + if(!empty($_REQUEST['urls'])){
208 + $urls = map_deep(wp_unslash($_REQUEST['urls']), 'sanitize_url');
209 + }
210 +
211 + if(cookieadmin_is_pro()){
212 + $scanner_info = get_option('cookieadmin_pro_scanner', []);
213 +
214 + if(!empty($scanner_info['last_scan']) && (time() < $scanner_info['last_scan'] + 3600)){
215 + wp_send_json([
216 + 'success' => false,
217 + 'message' => __('Cookie Scan can be triggered once an hour', 'cookieadmin')
218 + ]);
219 + }
220 + }
221 +
222 + self::scan_cookies($urls);
223 +
224 + if(!empty($cookieadmin_error)){
225 + wp_send_json([
226 + 'success' => false,
227 + 'message' => $cookieadmin_error]
228 + );
229 + }
230 +
231 + wp_send_json(['success' => true, 'data' => null]);
232 + }
233 +
234 + // Orchestrator function for scanning cookies
235 + static function scan_cookies($urls = []){
236 + global $cookieadmin_error;
237 +
238 + if(cookieadmin_is_pro()){
239 +
240 + if(!method_exists('\CookieAdminPro\Admin', 'cookieadmin_get_site_urls')){
241 + $urls = [home_url()];
242 + } else {
243 + $urls = \CookieAdminPro\Admin::cookieadmin_get_site_urls($urls);
244 + }
245 +
246 + $cookieData = apply_filters('cookieadmin_pro_scan_cookies', $urls);
247 +
248 + //Server side scann - skipped for now - need to discuss.
249 + // $cookieData2 = \CookieAdmin\Scanner::start_scan($urls);
250 + // $cookieData = array_replace_recursive($cookieData2, $cookieData1);
251 +
252 + if(!empty($cookieadmin_error)){
253 + update_option('cookieadmin_scan', [
254 + 'status' => 3,
255 + 'success' => false,
256 + 'message' => $cookieadmin_error,
257 + 'update' => time()
258 + ]);
259 + return;
260 + }
261 +
262 + if(!empty($cookieData)){
263 +
264 + if(function_exists('cookieadmin_pro_get_remaining_urls')){
265 + // Check Remaining urls
266 + $remaining_urls = cookieadmin_pro_get_remaining_urls($urls);
267 +
268 + if(!empty($remaining_urls)){
269 + //send next batch for scan
270 + wp_schedule_single_event(time() + 5, 'cookieadmin_run_auto_scan_batch', [$remaining_urls]);
271 + }
272 + }
273 +
274 + $res = self::save_raw_scan_results($cookieData);
275 + if(function_exists('cookieadmin_pro_update_scan_count')){
276 + cookieadmin_pro_update_scan_count($res);
277 + }
278 +
279 + self::cookieadmin_auto_configure_cookies();
280 + return;
281 + }
282 +
283 + }else{
284 + $cookieData = \CookieAdmin\Scanner::start_scan();
285 + if(!empty($cookieData)){
286 + self::save_raw_scan_results($cookieData);
287 + return self::cookieadmin_auto_configure_cookies();
288 + }
289 + }
290 + // cookieadmin_r_print($cookieData);
291 +
292 + if(defined('DOING_CRON') && get_transient('cookieadmin_auto_scan_in_progress')){
293 + delete_transient('cookieadmin_auto_scan_in_progress');
294 + }
295 +
296 + update_option('cookieadmin_scan', [
297 + 'status' => 3,
298 + 'success' => false,
299 + 'message' => __('No Cookies Found', 'cookieadmin'),
300 + 'update' => time()
301 + ]);
302 +
303 + return false;
304 + }
305 +
306 + static function save_raw_scan_results(array $found_cookies){
307 +
308 + global $wpdb;
309 +
310 + $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies');
311 +
312 + if (empty($found_cookies)) {
313 + return ['inserted' => 0, 'updated' => 0];
314 + }
315 +
316 + // Step 1: Fetch all existing cookie names from our database in one efficient query.
317 + $existing_cookies_in_db = $wpdb->get_col("SELECT cookie_name FROM {$table_name}");
318 + // Use array_flip for very fast 'isset' lookups instead of slow 'in_array' in a loop.
319 + $existing_cookies_lookup = !empty($existing_cookies_in_db) ? array_flip($existing_cookies_in_db) : [];
320 +
321 + $results = ['inserted' => 0, 'updated' => 0];
322 +
323 + // Step 2: Loop through each cookie found by the scanner.
324 + foreach ($found_cookies as $cookie_name => $cookie_data) {
325 +
326 + // Step 3: Check if the cookie exists in our DB.
327 + if (isset($existing_cookies_lookup[$cookie_name])) {
328 + $wpdb->update(
329 + $table_name,
330 + [
331 + 'domain' => sanitize_text_field($cookie_data['domain']),
332 + 'path' => sanitize_text_field($cookie_data['path']),
333 + 'expires' => !empty($cookie_data['session']) ? 0 : (!empty($cookie_data['expires']) ? $cookie_data['expires'] : null),
334 + 'samesite' => !empty($cookie_data['samesite']) ? sanitize_text_field($cookie_data['samesite']) : null,
335 + 'secure' => (int)($cookie_data['secure'] ?? 0),
336 + 'httponly' => (int)($cookie_data['httponly'] ?? 0),
337 + 'raw_name' => sanitize_text_field($cookie_name),
338 + 'scan_timestamp' => time(),
339 + ], // Data to update
340 + [ 'cookie_name' => $cookie_name ], // WHERE clause
341 + ['%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d'], // Format for the data
342 + [ '%s' ] // Format for the WHERE clause
343 + );
344 + $results['updated']++;
345 +
346 + } else {
347 +
348 + if($cookie_name == 'cookieadmin_consent' && empty($cookie_data['expires'])){
349 + $view = get_option('cookieadmin_law', 'cookieadmin_gdpr');
350 + $policy = cookieadmin_load_policy();
351 +
352 + if(!empty($policy) && !empty($policy[$view])){
353 + $cookie_exp_days = (int) (!empty($policy[$view]['cookieadmin_days']) ? $policy[$view]['cookieadmin_days'] : 365);
354 + $utc_time = gmdate('Y-m-d H:i:s', strtotime('+'.$cookie_exp_days.' days'));
355 + } else {
356 + $utc_time = gmdate('Y-m-d H:i:s', strtotime('+365 days'));
357 + }
358 +
359 + $cookie_data['expires'] = $utc_time;
360 + }
361 +
362 + // ------ INSERT a NEW cookie ------
363 + $data = [
364 + 'cookie_name' => sanitize_text_field($cookie_name),
365 + 'domain' => sanitize_text_field($cookie_data['domain']),
366 + 'path' => sanitize_text_field($cookie_data['path']),
367 + 'expires' => !empty($cookie_data['session']) ? 0 : (!empty($cookie_data['expires']) ? $cookie_data['expires'] : null),
368 + 'samesite' => !empty($cookie_data['samesite']) ? sanitize_text_field($cookie_data['samesite']) : null,
369 + 'secure' => (int)($cookie_data['secure'] ?? 0),
370 + 'httponly' => (int)($cookie_data['httponly'] ?? 0),
371 + 'raw_name' => sanitize_text_field($cookie_name),
372 + 'scan_timestamp' => time(),
373 + ];
374 +
375 + $formats = ['%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%d'];
376 +
377 + if ($wpdb->insert($table_name, $data, $formats)) {
378 + $results['inserted']++;
379 + } else {
380 + //error_log("CookieAdmin: Error inserting cookie data: " . $wpdb->last_error);
381 + }
382 + }
383 + }
384 + return $results;
385 + }
386 +
387 + static function cookieadmin_auto_configure_cookies(){
388 + global $wpdb, $cookieadmin_error;
389 +
390 + $table_name = $wpdb->prefix . 'cookieadmin_cookies';
391 + $categorized_cookies = [];
392 + $uncategorized_cookies = [];
393 +
394 + $all_cookies = $wpdb->get_results("SELECT id, cookie_name, category FROM {$table_name}");
395 +
396 + foreach($all_cookies as $cookie){
397 +
398 + if(!empty($cookie->category)){
399 + $categorized_cookies[$cookie->id] = $cookie->cookie_name;
400 + }else{
401 + $uncategorized_cookies[] = $cookie->cookie_name;
402 + }
403 + }
404 +
405 + if(!empty($uncategorized_cookies)){
406 +
407 + $uncategorized_cookies = array_flip($uncategorized_cookies);
408 + $categorized_cookies = array_flip($categorized_cookies);
409 +
410 +
411 + $categorizd_cookies = \CookieAdmin\CookieCategorizer::categorize_cookies($uncategorized_cookies, $categorized_cookies);
412 +
413 + $remove_cookies = !empty($categorizd_cookies['remove_cookies']) ? $categorizd_cookies['remove_cookies'] : [];
414 +
415 + unset($categorizd_cookies['remove_cookies']);
416 + if(!empty($remove_cookies)){
417 + $placeholders = implode(',', array_fill(0, count($remove_cookies), '%s'));
418 + $sql = $wpdb->prepare("DELETE FROM {$table_name} WHERE id IN ({$placeholders})", ...$remove_cookies);
419 + $wpdb->query($sql);
420 + }
421 +
422 + foreach($categorizd_cookies as $cookie_data){
423 +
424 + $count = $wpdb->update(
425 + $table_name,
426 + [ 'cookie_name' => $cookie_data['cookie_name'], 'category' => $cookie_data['category'], 'description' => $cookie_data['description'], 'edited' => 1, 'patterns' => $cookie_data['patterns'] ], // Data to update
427 + [ 'raw_name' => $cookie_data['raw_name'] ], // WHERE
428 + [ '%s', '%s', '%s', '%d', '%s' ], // Format for the data
429 + [ '%s' ] // Format for the WHERE clause
430 + );
431 +
432 + }
433 +
434 + update_option('cookieadmin_scan', [
435 + 'status' => 3,
436 + 'update' => time(),
437 + 'success' => true,
438 + 'count' => $count
439 + ]);
440 +
441 + $categorized_cookies = $wpdb->get_results("SELECT id, cookie_name, category, expires, scan_timestamp, description FROM {$table_name}");
442 +
443 + delete_option('cookieadmin_first_scan');
444 +
445 + return $categorized_cookies;
446 + }
447 +
448 + $cookieadmin_error = $cookieadmin_error . ' ' . __('No new cookies Found!', 'cookieadmin');
449 +
450 + return false;
451 + }
452 +
453 +
454 + static function edit_cookies(){
455 + global $wpdb;
456 +
457 + $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies');
458 + $data = null;
459 +
460 + if(empty($_REQUEST['cookie_info'])){
461 + wp_send_json(['success' => false,
462 + 'data' => null,
463 + 'message' => __('Error : Cookie details missing', 'cookieadmin')]
464 + );
465 + }
466 +
467 + $cookie_info = map_deep(wp_unslash($_REQUEST['cookie_info']), 'sanitize_text_field');
468 + // $scan_timestamp = $wpdb->get_col($wpdb->prepare("SELECT scan_timestamp FROM {$table_name} WHERE id = %d", $cookie_info['id']));
469 +
470 + $scanned_cookies = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$table_name} WHERE id = %d", $cookie_info['id']));
471 + $scan_timestamp = !empty($scanned_cookies->scan_timestamp) ? $scanned_cookies->scan_timestamp : 0;
472 +
473 + if(empty($scan_timestamp)){
474 + wp_send_json(['success' => false,
475 + 'data' => null,
476 + 'message' => __('Error : Invalid cookie record', 'cookieadmin')]
477 + );
478 + }
479 +
480 + $calculated_expiry_seconds = ($cookie_info['duration'] * 86400) + $scan_timestamp;
481 + $calculated_expiry = date('Y-m-d H:i:s', $calculated_expiry_seconds);
482 +
483 + // Update the additional patterns into the saved patterns
484 + $patterns = '[]';
485 + if(!empty($cookie_info['patterns'])){
486 + $script_patterns = explode(',', $cookie_info['patterns']);
487 + if(!empty($script_patterns) && is_array($script_patterns)){
488 + foreach($script_patterns as $pat){
489 + if(strlen(str_replace(['*', '?', '^', '%'], '', $pat)) < strlen($pat)){
490 + continue;
491 + }
492 + // do not ad comma if the saved pattern is empty
493 + $comma = ($patterns === '[]') ? '' : ', ';
494 + $patterns = str_replace(']', $comma . '"' .(string) trim($pat).'"]', $patterns);
495 + }
496 + }
497 + }else{
498 + $patterns = !empty($scanned_cookies->patterns) ? $scanned_cookies->patterns : '[]';
499 + }
500 +
501 + $resp = $wpdb->update(
502 + $table_name,
503 + [ 'cookie_name' => $cookie_info['name'], 'description' => $cookie_info['description'], 'expires' => $calculated_expiry, 'category' => $cookie_info['type'], 'patterns' => $patterns, 'edited' => 1], // Data to update
504 + [ 'id' => $cookie_info['id'] ], // WHERE
505 + [ '%s', '%s', '%s', '%s', '%s', '%d' ], // Format for the data
506 + [ '%d' ] // Format for the WHERE clause
507 + );
508 +
509 + if ($wpdb->last_error || $resp === false) {
510 + //error_log('DB Error: ' . $wpdb->last_error); // Log it
511 + wp_send_json(['success' => false,
512 + 'data' => null,
513 + 'message' => __('Cookie updation Failed, Error: ', 'cookieadmin') . esc_html($wpdb->last_error)]);
514 + }
515 +
516 + wp_send_json(['success' => true,
517 + 'data' => $data,
518 + 'message' => __('Cookie updation successful', 'cookieadmin')]);
519 +
520 + }
521 +
522 + static function delete_cookies(){
523 + global $wpdb;
524 +
525 + $table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies');
526 +
527 + if(empty($_REQUEST['cookie_raw_id'])){
528 + wp_send_json(['success' => false,
529 + 'data' => null,
530 + 'message' => __('Error : Cookie Id missing', 'cookieadmin')]);
531 + }
532 +
533 + $cookie_id = (int) sanitize_text_field(wp_unslash($_REQUEST['cookie_raw_id']));
534 +
535 + $resp = $wpdb->delete( $table_name, ['id' => $cookie_id], [ '%s' ] );
536 +
537 + if ($wpdb->last_error || $resp === false) {
538 + //error_log('DB Error: ' . $wpdb->last_error); //Log it
539 + wp_send_json(['success' => false,
540 + 'data' => null,
541 + 'message' => __('Cookie deletion Failed, Error: ', 'cookieadmin') . esc_html($wpdb->last_error)]);
542 + }
543 +
544 + wp_send_json(['success' => true,
545 + 'message' => __('Cookie deletion successful', 'cookieadmin')]);
546 + }
547 +
548 +}