get_results($wpdb->prepare( "SELECT DATE(post_date) as date, COUNT(*) as count FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' AND post_date >= %s AND ID IN ( SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'ai_story_maker_generated_via' ) GROUP BY DATE(post_date) ORDER BY date ASC", $six_months_ago )); // If no AI-generated posts found, get all published posts if (empty($ai_results)) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Fallback analytics query $results = $wpdb->get_results($wpdb->prepare( "SELECT DATE(post_date) as date, COUNT(*) as count FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' AND post_date >= %s GROUP BY DATE(post_date) ORDER BY date ASC", $six_months_ago )); } else { $results = $ai_results; } $data = array(); foreach ($results as $row) { $data[$row->date] = (int)$row->count; } return $data; } // Generate calendar data function aistma_generate_calendar_data($story_data, $hide_empty = false) { $calendar = array(); $month_labels = array(); // Store month labels for columns that have first day $start_date = new DateTime('-180 days'); // 6 months ago $end_date = new DateTime('180 days'); for ($date = clone $start_date; $date <= $end_date; $date->add(new DateInterval('P1D'))) { $date_str = $date->format('Y-m-d'); $week = $date->format('W'); $day_of_week = $date->format('N') - 1; // 0-6 (Monday-Sunday) $is_first_day_of_month = $date->format('j') === '1'; if (!isset($calendar[$week])) { $calendar[$week] = array_fill(0, 7, array('count' => 0, 'is_first_day' => false)); } $calendar[$week][$day_of_week] = array( 'count' => isset($story_data[$date_str]) ? $story_data[$date_str] : 0, 'is_first_day' => $is_first_day_of_month ); // Store month label for weeks with first day of month if ($is_first_day_of_month) { $month_labels[$week] = $date->format('M'); // 3-letter month name } } // Filter out empty weeks if requested if ($hide_empty) { $filtered_calendar = array(); $filtered_month_labels = array(); foreach ($calendar as $week_num => $week_data) { $total_count = 0; foreach ($week_data as $day_data) { $total_count += is_array($day_data) ? $day_data['count'] : $day_data; } if ($total_count > 0) { $filtered_calendar[$week_num] = $week_data; if (isset($month_labels[$week_num])) { $filtered_month_labels[$week_num] = $month_labels[$week_num]; } } } $calendar = $filtered_calendar; $month_labels = $filtered_month_labels; } return array('calendar' => $calendar, 'month_labels' => $month_labels); } // Initialize data $story_data = aistma_get_story_generation_data(); $total_stories = array_sum($story_data); $max_stories_per_day = !empty($story_data) ? max($story_data) : 0; $hide_empty_columns = aistma_get_hide_empty_columns(); $calendar_result = aistma_generate_calendar_data($story_data, $hide_empty_columns); $calendar_data = $calendar_result['calendar']; $month_labels = $calendar_result['month_labels']; // Get debug/stats information global $wpdb; $debug_info = array(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Debug statistics for analytics dashboard $debug_info['total_posts'] = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish'"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- AI content statistics $debug_info['ai_generated_posts'] = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} p JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id WHERE p.post_type = 'post' AND p.post_status = 'publish' AND pm.meta_key = 'ai_story_maker_generated_via'"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Recent posts for dashboard display $debug_info['recent_posts'] = $wpdb->get_results("SELECT ID, post_title, post_date FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5"); $debug_info['story_data_count'] = count($story_data); $debug_info['total_stories'] = $total_stories; // Get configurable widget settings function aistma_get_activity_days() { return (int) get_option( 'aistma_widget_activity_days', 14 ); } function aistma_get_recent_posts_limit() { return (int) get_option( 'aistma_widget_recent_posts_limit', 5 ); } function aistma_get_hide_empty_columns() { return (bool) get_option( 'aistma_widget_hide_empty_columns', false ); } // Get total clicks by post tag function aistma_get_tag_clicks_data() { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Complex analytics query for tag click data $results = $wpdb->get_results( "SELECT t.name as tag_name, t.term_id as tag_id, COUNT(ti.id) as total_clicks FROM {$wpdb->terms} t INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id INNER JOIN {$wpdb->term_relationships} tr ON tt.term_taxonomy_id = tr.term_taxonomy_id INNER JOIN {$wpdb->posts} p ON tr.object_id = p.ID INNER JOIN {$wpdb->prefix}aistma_traffic_info ti ON p.ID = ti.post_id WHERE tt.taxonomy = 'post_tag' AND p.post_type = 'post' AND p.post_status = 'publish' GROUP BY t.term_id, t.name ORDER BY total_clicks DESC LIMIT 20" ); return $results; } // Get tag clicks data $tag_clicks_data = aistma_get_tag_clicks_data(); // Build Post Views Heatmap data (recent posts x last N days) $aistma_views_days_window = aistma_get_activity_days(); $recent_posts_limit = aistma_get_recent_posts_limit(); // Get recent posts with configurable limit // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Recent posts query for analytics display $recent_posts_list = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title, post_date FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT %d", $recent_posts_limit ) ); // Ensure we have IDs only for recent posts $recent_post_ids = array(); foreach ( $recent_posts_list as $rp ) { $recent_post_ids[] = (int) $rp->ID; } $post_views_by_day = array(); $date_labels = array(); $max_views_per_cell = 0; if ( ! empty( $recent_post_ids ) ) { // Generate date labels for the past N days for ( $i = $aistma_views_days_window - 1; $i >= 0; $i-- ) { $date_labels[] = gmdate( 'Y-m-d', strtotime( "-{$i} days" ) ); } // For each recent post, build views array per day foreach ( $recent_post_ids as $pid ) { $post_views_by_day[ $pid ] = array(); // Initialize all days to 0 for ( $i = 0; $i < count( $date_labels ); $i++ ) { $post_views_by_day[ $pid ][ $i ] = 0; } // Get actual views data from aistma_traffic_info table for ( $i = 0; $i < count( $date_labels ); $i++ ) { $date = $date_labels[ $i ]; // Count views for this post on this specific date // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Traffic analytics from custom table $view_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aistma_traffic_info WHERE post_id = %d AND DATE(viewed_at) = %s", $pid, $date ) ); $actual_views = (int) $view_count; $post_views_by_day[ $pid ][ $i ] = $actual_views; if ( $actual_views > $max_views_per_cell ) { $max_views_per_cell = $actual_views; } } } } // Filter empty columns for activity heatmap if requested if ( $hide_empty_columns && ! empty( $post_views_by_day ) && ! empty( $date_labels ) ) { $filtered_date_labels = array(); $filtered_post_views_by_day = array(); // Check each day to see if any post has views for ( $i = 0; $i < count( $date_labels ); $i++ ) { $has_activity = false; foreach ( $recent_post_ids as $pid ) { if ( isset( $post_views_by_day[ $pid ][ $i ] ) && $post_views_by_day[ $pid ][ $i ] > 0 ) { $has_activity = true; break; } } // If this day has activity, include it if ( $has_activity ) { $new_index = count( $filtered_date_labels ); $filtered_date_labels[] = $date_labels[ $i ]; // Copy views for this day to the filtered array foreach ( $recent_post_ids as $pid ) { if ( ! isset( $filtered_post_views_by_day[ $pid ] ) ) { $filtered_post_views_by_day[ $pid ] = array(); } $filtered_post_views_by_day[ $pid ][ $new_index ] = isset( $post_views_by_day[ $pid ][ $i ] ) ? $post_views_by_day[ $pid ][ $i ] : 0; } } } // Use filtered data if we have any days with activity if ( ! empty( $filtered_date_labels ) ) { $date_labels = $filtered_date_labels; $post_views_by_day = $filtered_post_views_by_day; } } ?>