| 1 |
<?php |
| 2 |
|
| 3 |
namespace CookieAdmin\Admin; |
| 4 |
|
| 5 |
if(!defined('COOKIEADMIN_VERSION') || !defined('ABSPATH')){ |
| 6 |
die('Hacking Attempt'); |
| 7 |
} |
| 8 |
|
| 9 |
class Dashboard{ |
| 10 |
|
| 11 |
static function dashboard(){ |
| 12 |
|
| 13 |
global $cookieadmin_lang, $cookieadmin_error, $cookieadmin_msg, $cookieadmin_settings; |
| 14 |
|
| 15 |
\CookieAdmin\Admin::header_theme(__('Dashboard', 'cookieadmin')); |
| 16 |
|
| 17 |
$view = get_option('cookieadmin_law', 'cookieadmin_gdpr'); |
| 18 |
|
| 19 |
$stats = array(); |
| 20 |
|
| 21 |
$stats['banner'] = array( |
| 22 |
'icon' => 'yes-alt', |
| 23 |
'icon_color' => 'green', |
| 24 |
'label' => __('Consent Banner', 'cookieadmin'), |
| 25 |
'value' => esc_html__('Enabled', 'cookieadmin'), |
| 26 |
'value_class' => 'cookieadmin-green', |
| 27 |
); |
| 28 |
|
| 29 |
$stats['consent_type'] = array( |
| 30 |
'icon' => 'admin-site', |
| 31 |
'icon_color' => 'blue', |
| 32 |
'label' => __('Consent Type', 'cookieadmin'), |
| 33 |
'value' => ($view == 'cookieadmin_us' ? esc_html__('US State Laws', 'cookieadmin') : esc_html__('GDPR', 'cookieadmin')), |
| 34 |
'value_class' => 'cookieadmin-uppercase', |
| 35 |
'edit_url' => admin_url('admin.php?page=cookieadmin-consent'), |
| 36 |
); |
| 37 |
|
| 38 |
$stats['gcm'] = array( |
| 39 |
'icon' => 'google', |
| 40 |
'icon_color' => 'navy', |
| 41 |
'label' => __('Google Consent Mode v2', 'cookieadmin'), |
| 42 |
'value' => (!empty($cookieadmin_settings['google_consent_mode_v2']) ? '<span class="cookieadmin-green">'.esc_html__('Enabled', 'cookieadmin').'</span>' : esc_html__('Disabled', 'cookieadmin')), |
| 43 |
'edit_url' => admin_url('admin.php?page=cookieadmin-settings'), |
| 44 |
); |
| 45 |
|
| 46 |
$stats['auto_scan'] = array( |
| 47 |
'icon' => 'update', |
| 48 |
'icon_color' => 'amber', |
| 49 |
'label' => __('Auto Scan', 'cookieadmin'), |
| 50 |
'value' => (!empty($cookieadmin_settings['cookieadmin_auto_scan']) ? '<span class="cookieadmin-green">'.esc_html__('Enabled', 'cookieadmin').'</span>' : esc_html__('Disabled', 'cookieadmin')), |
| 51 |
'edit_url' => admin_url('admin.php?page=cookieadmin-settings'), |
| 52 |
); |
| 53 |
|
| 54 |
$stats = apply_filters('cookieadmin_dashboard_stats', $stats); |
| 55 |
|
| 56 |
// Stat cards |
| 57 |
echo '<div class="cookieadmin-stats-grid">'; |
| 58 |
foreach($stats as $stat){ |
| 59 |
echo '<div class="cookieadmin-stat-card"> |
| 60 |
<div class="cookieadmin-stat-icon cookieadmin-stat-icon--'.esc_attr($stat['icon_color']).'"><span class="dashicons dashicons-'.esc_attr($stat['icon']).'"></span></div> |
| 61 |
<div class="cookieadmin-stat-label">'.esc_html($stat['label']); |
| 62 |
if(!empty($stat['edit_url'])){ |
| 63 |
echo ' <a class="cookieadmin-stat-edit" href="'.esc_url($stat['edit_url']).'">'.esc_html__('Edit', 'cookieadmin').'</a>'; |
| 64 |
} |
| 65 |
echo '</div> |
| 66 |
<div class="cookieadmin-stat-value '.(!empty($stat['value_class']) ? esc_attr($stat['value_class']) : '').'">'.wp_kses_post($stat['value']).'</div> |
| 67 |
</div>'; |
| 68 |
} |
| 69 |
echo '</div>'; |
| 70 |
|
| 71 |
// ====== DASHBOARD LAYOUT: MAIN 70% + SIDEBAR 30% ====== |
| 72 |
$checklist = self::get_setup_checklist(); |
| 73 |
$inventory = self::get_cookie_inventory(); |
| 74 |
|
| 75 |
echo '<div class="cookieadmin-dashboard-grid">'; |
| 76 |
|
| 77 |
// MAIN AREA (~70%) |
| 78 |
echo '<div class="cookieadmin-dashboard-main">'; |
| 79 |
|
| 80 |
// Setup Checklist |
| 81 |
echo '<div class="cookieadmin-card cookieadmin-mb-16"> |
| 82 |
<div class="cookieadmin-card-header"> |
| 83 |
<span class="cookieadmin-card-title"><span class="dashicons dashicons-performance"></span>'.esc_html__('Setup Checklist', 'cookieadmin').'</span> |
| 84 |
</div> |
| 85 |
<div class="cookieadmin-card-body"> |
| 86 |
<ol class="cookieadmin-checklist">'; |
| 87 |
foreach($checklist as $item){ |
| 88 |
// We should not show the checklist of Pro features if Pro is disabled. |
| 89 |
if(isset($item['pro']) && !defined('COOKIEADMIN_PRO_VERSION')){ |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
$done_class = $item['done'] ? 'cookieadmin-checklist-item--done' : 'cookieadmin-checklist-item--pending'; |
| 94 |
$icon = $item['done'] ? 'yes' : 'marker'; |
| 95 |
echo '<li><a href="'.esc_url($item['url']).'" class="'.esc_attr($done_class).'"><span class="dashicons dashicons-'.esc_attr($icon).'"></span> '.esc_html($item['label']).'</a></li>'; |
| 96 |
} |
| 97 |
echo '</ol> |
| 98 |
</div> |
| 99 |
</div>'; |
| 100 |
|
| 101 |
// Cookie Inventory |
| 102 |
echo '<div class="cookieadmin-card cookieadmin-mb-16"> |
| 103 |
<div class="cookieadmin-card-header"> |
| 104 |
<span class="cookieadmin-card-title"><span class="dashicons dashicons-chart-pie"></span>'.esc_html__('Cookie Inventory', 'cookieadmin').'</span> |
| 105 |
<a href="'.esc_url(admin_url('admin.php?page=cookieadmin-scan-cookies')).'" class="cookieadmin-btn cookieadmin-btn-secondary cookieadmin-btn-sm">'.esc_html__('View All Cookies', 'cookieadmin').'</a> |
| 106 |
</div> |
| 107 |
<div class="cookieadmin-card-body">'; |
| 108 |
if(!empty($inventory['total'])){ |
| 109 |
echo '<div class="cookieadmin-inventory-total"><span class="cookieadmin-inventory-number">'.esc_html($inventory['total']).'</span> <span class="cookieadmin-inventory-label">'.esc_html__('Cookies discovered', 'cookieadmin').'</span></div>'; |
| 110 |
|
| 111 |
$seg_colors = array('necessary' => 'var(--cookieadmin-success)', 'functional' => 'var(--cookieadmin-info)', 'analytics' => 'var(--cookieadmin-warning)', 'marketing' => 'var(--cookieadmin-danger)', 'unknown' => 'var(--cookieadmin-text-soft)'); |
| 112 |
|
| 113 |
echo '<div class="cookieadmin-segment-bar">'; |
| 114 |
foreach($inventory['categories'] as $cat => $count){ |
| 115 |
if($count > 0 && !empty($inventory['total'])){ |
| 116 |
$pct = round(($count / $inventory['total']) * 100, 1); |
| 117 |
echo '<div class="cookieadmin-segment" style="width:'.esc_attr($pct).'%;background:'.esc_attr($seg_colors[$cat]).';" title="'.esc_attr(ucfirst($cat)).': '.esc_attr($count).'"></div>'; |
| 118 |
} |
| 119 |
} |
| 120 |
echo '</div>'; |
| 121 |
|
| 122 |
echo '<div class="cookieadmin-segment-legend">'; |
| 123 |
foreach($inventory['categories'] as $cat => $count){ |
| 124 |
echo '<span class="cookieadmin-legend-item"><span class="cookieadmin-legend-dot" style="background:'.esc_attr($seg_colors[$cat]).';"></span> '.esc_html(ucfirst($cat)).': <strong>'.esc_html($count).'</strong></span>'; |
| 125 |
} |
| 126 |
echo '</div>'; |
| 127 |
}else{ |
| 128 |
echo '<p class="cookieadmin-text-muted">'.esc_html__('No cookies discovered yet. Run a scan to get started.', 'cookieadmin').'</p>'; |
| 129 |
} |
| 130 |
if(!empty($inventory['unknown'])){ |
| 131 |
echo '<div class="cookieadmin-inventory-warning"><span class="dashicons dashicons-warning"></span> '.esc_html__('Some cookies are uncategorized. Run a scan for better compliance.', 'cookieadmin').'</div>'; |
| 132 |
} |
| 133 |
echo '</div> |
| 134 |
</div>'; |
| 135 |
|
| 136 |
// Pro main-area widgets (appears inside main, below checklist + inventory) |
| 137 |
do_action('cookieadmin_dashboard_main_widgets'); |
| 138 |
|
| 139 |
echo '</div>'; // end main |
| 140 |
|
| 141 |
// SIDEBAR (~30%) |
| 142 |
echo '<div class="cookieadmin-dashboard-sidebar">'; |
| 143 |
|
| 144 |
// Recommended Plugins (free) |
| 145 |
self::render_recommended_plugins(); |
| 146 |
|
| 147 |
// Pro sidebar widgets (if any) |
| 148 |
do_action('cookieadmin_dashboard_sidebar_widgets'); |
| 149 |
|
| 150 |
echo '</div>'; // end sidebar |
| 151 |
|
| 152 |
echo '</div>'; // end grid |
| 153 |
|
| 154 |
\CookieAdmin\Admin::footer_theme(); |
| 155 |
} |
| 156 |
|
| 157 |
static function get_setup_checklist(){ |
| 158 |
global $cookieadmin_settings; |
| 159 |
|
| 160 |
$policy = cookieadmin_load_policy(); |
| 161 |
$law = get_option('cookieadmin_law', ''); |
| 162 |
$scan = get_option('cookieadmin_scan', array()); |
| 163 |
$cur = !empty($policy[$law]) ? $policy[$law] : array(); |
| 164 |
|
| 165 |
$checklist = array(); |
| 166 |
|
| 167 |
$checklist[] = array( |
| 168 |
'label' => __('Run Cookie Scan', 'cookieadmin'), |
| 169 |
'done' => (!empty($scan['success']) && $scan['success'] === true), |
| 170 |
'url' => admin_url('admin.php?page=cookieadmin-scan-cookies'), |
| 171 |
); |
| 172 |
|
| 173 |
$checklist[] = array( |
| 174 |
'label' => __('Configure Consent Banner', 'cookieadmin'), |
| 175 |
'done' => !empty($cur), |
| 176 |
'url' => admin_url('admin.php?page=cookieadmin-consent'), |
| 177 |
); |
| 178 |
|
| 179 |
$checklist[] = array( |
| 180 |
'label' => __('Set Consent Type', 'cookieadmin'), |
| 181 |
'done' => (!empty($law) && in_array($law, array('cookieadmin_gdpr', 'cookieadmin_us'), true)), |
| 182 |
'url' => admin_url('admin.php?page=cookieadmin-consent'), |
| 183 |
); |
| 184 |
|
| 185 |
$checklist[] = array( |
| 186 |
'label' => __('Enable Google Consent Mode v2', 'cookieadmin'), |
| 187 |
'done' => !empty($cookieadmin_settings['google_consent_mode_v2']), |
| 188 |
'url' => admin_url('admin.php?page=cookieadmin-settings'), |
| 189 |
'pro' => true, |
| 190 |
); |
| 191 |
|
| 192 |
$has_links = false; |
| 193 |
if(!empty($cur['cookieadmin_privacy_policy']) || !empty($cur['cookieadmin_cookie_policy'])){ |
| 194 |
$has_links = true; |
| 195 |
} |
| 196 |
$checklist[] = array( |
| 197 |
'label' => __('Set Privacy / Cookie Policy Links', 'cookieadmin'), |
| 198 |
'done' => $has_links, |
| 199 |
'url' => admin_url('admin.php?page=cookieadmin-consent'), |
| 200 |
'pro' => true, |
| 201 |
); |
| 202 |
|
| 203 |
$checklist[] = array( |
| 204 |
'label' => __('Configure Auto Scan', 'cookieadmin'), |
| 205 |
'done' => !empty($cookieadmin_settings['cookieadmin_auto_scan']), |
| 206 |
'url' => admin_url('admin.php?page=cookieadmin-settings'), |
| 207 |
'pro' => true, |
| 208 |
); |
| 209 |
|
| 210 |
return $checklist; |
| 211 |
} |
| 212 |
|
| 213 |
static function get_cookie_inventory(){ |
| 214 |
global $wpdb; |
| 215 |
|
| 216 |
$table_name = esc_sql($wpdb->prefix . 'cookieadmin_cookies'); |
| 217 |
|
| 218 |
if(!\CookieAdmin\Admin::cookieadmin_table_exists($table_name)){ |
| 219 |
return array('total' => 0, 'categories' => array('necessary' => 0, 'functional' => 0, 'analytics' => 0, 'marketing' => 0, 'unknown' => 0), 'unknown' => 0); |
| 220 |
} |
| 221 |
|
| 222 |
$total = (int) $wpdb->get_var("SELECT COUNT(*) FROM $table_name"); |
| 223 |
|
| 224 |
$categories = array('necessary' => 0, 'functional' => 0, 'analytics' => 0, 'marketing' => 0, 'unknown' => 0); |
| 225 |
|
| 226 |
$rows = $wpdb->get_results("SELECT category, COUNT(*) AS cnt FROM $table_name GROUP BY category"); |
| 227 |
|
| 228 |
if(!empty($rows)){ |
| 229 |
foreach($rows as $row){ |
| 230 |
$cat = strtolower($row->category); |
| 231 |
if(array_key_exists($cat, $categories)){ |
| 232 |
$categories[$cat] = (int) $row->cnt; |
| 233 |
}else{ |
| 234 |
$categories['unknown'] += (int) $row->cnt; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return array( |
| 240 |
'total' => $total, |
| 241 |
'categories' => $categories, |
| 242 |
'unknown' => $categories['unknown'], |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
static function render_recommended_plugins(){ |
| 247 |
$plugins = array( |
| 248 |
'loginizer' => array( |
| 249 |
'name' => 'Loginizer', |
| 250 |
'desc' => __('Brute Force Protection & Login Security', 'cookieadmin'), |
| 251 |
'icon' => 'dashicons-shield', |
| 252 |
'wporg_url' => 'https://wordpress.org/plugins/loginizer/', |
| 253 |
), |
| 254 |
'gosmtp' => array( |
| 255 |
'name' => 'GoSMTP', |
| 256 |
'desc' => __('SMTP Mailer for WordPress', 'cookieadmin'), |
| 257 |
'icon' => 'dashicons-email-alt', |
| 258 |
'wporg_url' => 'https://wordpress.org/plugins/gosmtp/', |
| 259 |
), |
| 260 |
'backuply' => array( |
| 261 |
'name' => 'Backuply', |
| 262 |
'desc' => __('Backup & Restore made easy', 'cookieadmin'), |
| 263 |
'icon' => 'dashicons-backup', |
| 264 |
'wporg_url' => 'https://wordpress.org/plugins/backuply/', |
| 265 |
), |
| 266 |
'siteseo' => array( |
| 267 |
'name' => 'SiteSEO', |
| 268 |
'desc' => __('SEO Optimization for WordPress', 'cookieadmin'), |
| 269 |
'icon' => 'dashicons-search', |
| 270 |
'wporg_url' => 'https://wordpress.org/plugins/siteseo/', |
| 271 |
), |
| 272 |
); |
| 273 |
|
| 274 |
echo '<div class="cookieadmin-card cookieadmin-recommended-plugins"> |
| 275 |
<div class="cookieadmin-card-header"> |
| 276 |
<span class="cookieadmin-card-title"><span class="dashicons dashicons-admin-plugins"></span>'.esc_html__('Recommended Plugins', 'cookieadmin').'</span> |
| 277 |
</div> |
| 278 |
<div class="cookieadmin-card-body">'; |
| 279 |
|
| 280 |
foreach($plugins as $slug => $plugin){ |
| 281 |
$status = self::get_plugin_status($slug); |
| 282 |
|
| 283 |
echo '<div class="cookieadmin-recommended-plugin" data-slug="'.esc_attr($slug).'"> |
| 284 |
<div class="cookieadmin-recommended-plugin-info"> |
| 285 |
<div class="cookieadmin-recommended-plugin-icon"><span class="dashicons '.esc_attr($plugin['icon']).'"></span></div> |
| 286 |
<div class="cookieadmin-recommended-plugin-details"> |
| 287 |
<div class="cookieadmin-recommended-plugin-name">'.esc_html($plugin['name']).'</div> |
| 288 |
<div class="cookieadmin-recommended-plugin-desc">'.esc_html($plugin['desc']).'</div> |
| 289 |
</div> |
| 290 |
</div> |
| 291 |
<div class="cookieadmin-recommended-plugin-action"> |
| 292 |
<a href="'.esc_url($plugin['wporg_url']).'" target="_blank" class="cookieadmin-recommended-plugin-link">'.esc_html__('Learn more', 'cookieadmin').' </a> |
| 293 |
'; |
| 294 |
|
| 295 |
if($status === 'active'){ |
| 296 |
echo '<span class="cookieadmin-badge cookieadmin-success">'.esc_html__('Active', 'cookieadmin').'</span>'; |
| 297 |
}elseif($status === 'installed'){ |
| 298 |
echo '<button type="button" class="cookieadmin-btn cookieadmin-btn-secondary cookieadmin-btn-sm cookieadmin-plugin-activate-btn" data-slug="'.esc_attr($slug).'">'.esc_html__('Activate', 'cookieadmin').'</button>'; |
| 299 |
}else{ |
| 300 |
echo '<button type="button" class="cookieadmin-btn cookieadmin-btn-primary cookieadmin-btn-sm cookieadmin-plugin-install-btn" data-slug="'.esc_attr($slug).'">'.esc_html__('Install', 'cookieadmin').'</button>'; |
| 301 |
} |
| 302 |
|
| 303 |
echo '</div> |
| 304 |
</div>'; |
| 305 |
} |
| 306 |
|
| 307 |
echo '</div> |
| 308 |
</div>'; |
| 309 |
} |
| 310 |
|
| 311 |
static function get_plugin_status($slug){ |
| 312 |
if(!function_exists('get_plugins')){ |
| 313 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 314 |
} |
| 315 |
|
| 316 |
$all_plugins = get_plugins(); |
| 317 |
|
| 318 |
foreach($all_plugins as $plugin_path => $plugin_data){ |
| 319 |
if(strpos($plugin_path, $slug . '/') === 0){ |
| 320 |
if(is_plugin_active($plugin_path)){ |
| 321 |
return 'active'; |
| 322 |
} |
| 323 |
return 'installed'; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return 'not_installed'; |
| 328 |
} |
| 329 |
|
| 330 |
static function install_recommended_plugin(){ |
| 331 |
check_ajax_referer('cookieadmin_admin_js_nonce', 'cookieadmin_security'); |
| 332 |
|
| 333 |
if(!current_user_can('install_plugins')){ |
| 334 |
wp_send_json_error(array('message' => __('You do not have permission to install plugins.', 'cookieadmin'))); |
| 335 |
} |
| 336 |
|
| 337 |
$slug = !empty($_REQUEST['plugin']) ? sanitize_text_field(wp_unslash($_REQUEST['plugin'])) : ''; |
| 338 |
if(empty($slug)){ |
| 339 |
wp_send_json_error(array('message' => __('Plugin slug is required.', 'cookieadmin'))); |
| 340 |
} |
| 341 |
|
| 342 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 343 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 344 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 345 |
|
| 346 |
$api = plugins_api('plugin_information', array('slug' => $slug, 'fields' => array('sections' => false))); |
| 347 |
if(is_wp_error($api)){ |
| 348 |
wp_send_json_error(array('message' => $api->get_error_message())); |
| 349 |
} |
| 350 |
|
| 351 |
$skin_class = class_exists('\Automatic_Upgrader_Skin') ? '\Automatic_Upgrader_Skin' : '\WP_Upgrader_Skin'; |
| 352 |
$skin = new $skin_class(); |
| 353 |
$upgrader = new \Plugin_Upgrader($skin); |
| 354 |
$result = $upgrader->install($api->download_link); |
| 355 |
|
| 356 |
if(is_wp_error($result)){ |
| 357 |
wp_send_json_error(array('message' => $result->get_error_message())); |
| 358 |
} |
| 359 |
if(!$result){ |
| 360 |
wp_send_json_error(array('message' => __('Plugin installation failed.', 'cookieadmin'))); |
| 361 |
} |
| 362 |
|
| 363 |
$all_plugins = get_plugins(); |
| 364 |
$installed_plugin = ''; |
| 365 |
foreach($all_plugins as $path => $data){ |
| 366 |
if(strpos($path, $slug . '/') === 0){ |
| 367 |
$installed_plugin = $path; |
| 368 |
break; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if(empty($installed_plugin)){ |
| 373 |
wp_send_json_error(array('message' => __('Plugin installed but could not be located for activation.', 'cookieadmin'))); |
| 374 |
} |
| 375 |
|
| 376 |
$result = activate_plugin($installed_plugin); |
| 377 |
if(is_wp_error($result)){ |
| 378 |
wp_send_json_error(array('message' => $result->get_error_message())); |
| 379 |
} |
| 380 |
|
| 381 |
wp_send_json_success(array('message' => __('Plugin installed and activated successfully.', 'cookieadmin'))); |
| 382 |
} |
| 383 |
|
| 384 |
static function activate_recommended_plugin(){ |
| 385 |
check_ajax_referer('cookieadmin_admin_js_nonce', 'cookieadmin_security'); |
| 386 |
|
| 387 |
if(!current_user_can('activate_plugins')){ |
| 388 |
wp_send_json_error(array('message' => __('You do not have permission to activate plugins.', 'cookieadmin'))); |
| 389 |
} |
| 390 |
|
| 391 |
$slug = !empty($_REQUEST['plugin']) ? sanitize_text_field(wp_unslash($_REQUEST['plugin'])) : ''; |
| 392 |
if(empty($slug)){ |
| 393 |
wp_send_json_error(array('message' => __('Plugin slug is required.', 'cookieadmin'))); |
| 394 |
} |
| 395 |
|
| 396 |
if(!function_exists('get_plugins')){ |
| 397 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 398 |
} |
| 399 |
|
| 400 |
$all_plugins = get_plugins(); |
| 401 |
$plugin_path = ''; |
| 402 |
foreach($all_plugins as $path => $data){ |
| 403 |
if(strpos($path, $slug . '/') === 0){ |
| 404 |
$plugin_path = $path; |
| 405 |
break; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
if(empty($plugin_path)){ |
| 410 |
wp_send_json_error(array('message' => __('Plugin not found.', 'cookieadmin'))); |
| 411 |
} |
| 412 |
|
| 413 |
$result = activate_plugin($plugin_path); |
| 414 |
if(is_wp_error($result)){ |
| 415 |
wp_send_json_error(array('message' => $result->get_error_message())); |
| 416 |
} |
| 417 |
|
| 418 |
wp_send_json_success(array('message' => __('Plugin activated successfully.', 'cookieadmin'))); |
| 419 |
} |
| 420 |
} |
| 421 |
|