$preferred_chart, 'nonce' => wp_create_nonce('actlogpro_dashboard_widget_nonce'), 'i18n' => array( 'events' => __('Events', 'activity-log-pro') ) )); } /** * Add the dashboard widget */ public static function add_dashboard_widget() { // Only show to users who can view activity logs if (!current_user_can('manage_options')) { return; } wp_add_dashboard_widget( 'actlogpro_activity_stats_widget', __('Activity Log Pro - Statistics', 'activity-log-pro'), array(__CLASS__, 'render_widget'), array(__CLASS__, 'widget_config') ); } /** * Render the dashboard widget content */ public static function render_widget() { // Get user's preferred chart type $user_id = get_current_user_id(); $preferred_chart = get_user_meta($user_id, 'actlogpro_dashboard_widget_chart', true); if (empty($preferred_chart)) { $preferred_chart = 'daily_activity'; } ?>

$insights ))); } /** * Get daily activity data (reusing existing logic) */ private static function get_daily_activity_data() { // Reuse the existing database method from stats page $activity_data = ACTLOGPRO_Database::get_activity_data_by_days(7); return array( 'labels' => $activity_data['labels'], 'counts' => $activity_data['counts'], 'users' => $activity_data['users'] ); } /** * Get event types data (reusing existing logic) */ private static function get_event_types_data() { $event_types = ACTLOGPRO_Database::get_event_types_with_counts(6); $labels = array(); $counts = array(); $colors = array('#1e88e5', '#26a69a', '#7cb342', '#ffa000', '#ef6c00', '#d81b60'); foreach ($event_types as $index => $event) { $labels[] = ucfirst($event['event_type']); $counts[] = $event['count']; } return array( 'labels' => $labels, 'counts' => $counts, 'colors' => array_slice($colors, 0, count($labels)) ); } /** * Get action types data (reusing existing logic) */ private static function get_action_types_data() { $action_types = ACTLOGPRO_Database::get_action_types_with_counts(6); $labels = array(); $counts = array(); $colors = array('#42a5f5', '#66bb6a', '#ffca28', '#ef5350', '#ab47bc', '#ec407a'); foreach ($action_types as $index => $action) { $labels[] = ucfirst($action['action']); $counts[] = $action['count']; } return array( 'labels' => $labels, 'counts' => $counts, 'colors' => array_slice($colors, 0, count($labels)) ); } /** * Get peak times data (reusing existing logic) */ private static function get_peak_times_data() { $days = array( __('Sun', 'activity-log-pro'), __('Mon', 'activity-log-pro'), __('Tue', 'activity-log-pro'), __('Wed', 'activity-log-pro'), __('Thu', 'activity-log-pro'), __('Fri', 'activity-log-pro'), __('Sat', 'activity-log-pro') ); // Initialize activity data array $activity_by_time = array(); foreach ($days as $day_index => $day) { $activity_by_time[$day_index] = array_fill(0, 24, 0); } // Get activity data from database $time_results = ACTLOGPRO_Database::get_activity_by_day_and_hour(); // Fill the activity data array $max_activity = 0; foreach ($time_results as $result) { $day_index = $result['day_of_week'] - 1; $hour = $result['hour_of_day']; $count = (int)$result['count']; // Ensure day_index is within valid bounds (0-6 for Sunday-Saturday) if ($day_index >= 0 && $day_index < 7 && isset($activity_by_time[$day_index])) { $activity_by_time[$day_index][$hour] = $count; $max_activity = max($max_activity, $count); } } return array( 'days' => $days, 'activity' => $activity_by_time, 'max_activity' => $max_activity ); } /** * Generate insights for daily activity */ private static function get_daily_activity_insights($data) { $insights = array(); if (!empty($data['counts'])) { $total_events = array_sum($data['counts']); $avg_daily = round($total_events / count($data['counts'])); $max_day = max($data['counts']); $max_day_index = array_search($max_day, $data['counts']); // translators: %d is the number of events $insights[] = sprintf(__('Total events in last 7 days: %d', 'activity-log-pro'), $total_events ); // translators: %d is the average number of events per day $insights[] = sprintf(__('Average daily activity: %d events', 'activity-log-pro'), $avg_daily ); if (isset($data['labels'][$max_day_index])) { // translators: %1$s is the day name, %2$d is the number of events $insights[] = sprintf(__('Busiest day: %1$s with %2$d events', 'activity-log-pro'), $data['labels'][$max_day_index], $max_day ); } } return $insights; } /** * Generate insights for event types */ private static function get_event_types_insights($data) { $insights = array(); if (!empty($data['counts']) && !empty($data['labels'])) { $total = array_sum($data['counts']); $most_common = $data['labels'][0]; $most_common_count = $data['counts'][0]; $percentage = round(($most_common_count / $total) * 100); // translators: %1$s is the event type name, %2$d is the percentage $insights[] = sprintf(__('%1$s is the most common event type (%2$d%% of all events)', 'activity-log-pro'), $most_common, $percentage ); // translators: %d is the number of different event types $insights[] = sprintf(__('Total of %d different event types recorded', 'activity-log-pro'), count($data['labels']) ); } return $insights; } /** * Generate insights for action types */ private static function get_action_types_insights($data) { $insights = array(); if (!empty($data['counts']) && !empty($data['labels'])) { $total = array_sum($data['counts']); $most_common = $data['labels'][0]; $most_common_count = $data['counts'][0]; $percentage = round(($most_common_count / $total) * 100); // translators: %1$s is the action type, %2$d is the percentage $insights[] = sprintf(__('%1$s actions account for %2$d%% of all activity', 'activity-log-pro'), $most_common, $percentage ); if (count($data['labels']) > 1) { $second_common = $data['labels'][1]; $second_percentage = round(($data['counts'][1] / $total) * 100); // translators: %1$s is first action type, %2$d is first percentage, %3$s is second action type, %4$d is second percentage $insights[] = sprintf(__('Top 2 actions: %1$s (%2$d%%) and %3$s (%4$d%%)', 'activity-log-pro'), $most_common, $percentage, $second_common, $second_percentage ); } } return $insights; } /** * Generate insights for peak times */ private static function get_peak_times_insights($data) { $insights = array(); if (!empty($data['activity'])) { // Find busiest day and time $busiest_count = 0; $busiest_day = ''; $busiest_hour = 0; $busiest_day_total = 0; $busiest_day_name = ''; foreach ($data['activity'] as $day_index => $hours) { $day_total = array_sum($hours); if ($day_total > $busiest_day_total) { $busiest_day_total = $day_total; // Ensure day_index exists in the days array if (isset($data['days'][$day_index])) { $busiest_day_name = $data['days'][$day_index]; } } foreach ($hours as $hour => $count) { if ($count > $busiest_count) { $busiest_count = $count; // Ensure day_index exists in the days array if (isset($data['days'][$day_index])) { $busiest_day = $data['days'][$day_index]; } $busiest_hour = $hour; } } } if ($busiest_count > 0) { $formatted_hour = sprintf('%02d:00', $busiest_hour); // translators: %1$s is the day name, %2$s is the time, %3$d is the number of events $insights[] = sprintf(__('Peak activity: %1$s at %2$s (%3$d events)', 'activity-log-pro'), $busiest_day, $formatted_hour, $busiest_count ); } if ($busiest_day_total > 0) { // translators: %1$s is the day name, %2$d is the total number of events $insights[] = sprintf(__('Busiest day of week: %1$s (%2$d total events)', 'activity-log-pro'), $busiest_day_name, $busiest_day_total ); } } return $insights; } } // AJAX handler for saving user preference add_action('wp_ajax_actlogpro_save_dashboard_widget_preference', function() { if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'actlogpro_dashboard_widget_nonce')) { wp_die('Security check failed'); } if (!current_user_can('manage_options')) { wp_die('Insufficient permissions'); } $chart_type = isset($_POST['chart_type']) ? sanitize_text_field(wp_unslash($_POST['chart_type'])) : 'daily_activity'; $user_id = get_current_user_id(); update_user_meta($user_id, 'actlogpro_dashboard_widget_chart', $chart_type); wp_send_json_success(); });