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", self::get_recent_posts_limit() ) ); } /** * Generate activity data for posts */ private static function generate_activity_data( $recent_posts ) { global $wpdb; $post_views_by_day = array(); $date_labels = array(); $activity_days = self::get_activity_days(); // Generate date labels for the past N days for ( $i = $activity_days - 1; $i >= 0; $i-- ) { $date_labels[] = gmdate( 'Y-m-d', strtotime( "-{$i} days" ) ); } // For each recent post, get actual view data from traffic table foreach ( $recent_posts as $post ) { $post_id = (int) $post->ID; $post_views_by_day[ $post_id ] = array(); // Get actual view data from aistma_traffic_info table for ( $i = 0; $i < count( $date_labels ); $i++ ) { $date = $date_labels[ $i ]; $next_date = gmdate( 'Y-m-d', strtotime( $date . ' +1 day' ) ); // Count views for this post on this specific date // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Traffic analytics from custom table for heatmap $view_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}aistma_traffic_info WHERE post_id = %d AND DATE(viewed_at) = %s", $post_id, $date ) ); $post_views_by_day[ $post_id ][ $i ] = (int) $view_count; } } // Filter empty columns if requested $hide_empty_columns = self::get_hide_empty_columns(); 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_posts as $post ) { $post_id = (int) $post->ID; if ( isset( $post_views_by_day[ $post_id ][ $i ] ) && $post_views_by_day[ $post_id ][ $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_posts as $post ) { $post_id = (int) $post->ID; if ( ! isset( $filtered_post_views_by_day[ $post_id ] ) ) { $filtered_post_views_by_day[ $post_id ] = array(); } $filtered_post_views_by_day[ $post_id ][ $new_index ] = isset( $post_views_by_day[ $post_id ][ $i ] ) ? $post_views_by_day[ $post_id ][ $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; } } return array( 'activity_data' => $post_views_by_day, 'date_labels' => $date_labels ); } /** * Render the dashboard widget * * @param array $args { * @type bool $show_footer Whether to show the View Analytics footer link. Default true. * } */ public static function render_widget( $args = array() ) { $args = wp_parse_args( $args, array( 'show_footer' => true, ) ); $recent_posts = self::get_recent_posts(); if ( empty( $recent_posts ) ) { echo '
' . esc_html__( 'No recent posts found.', 'ai-story-maker' ) . '
'; return; } $activity_data = self::generate_activity_data( $recent_posts ); $post_views_by_day = $activity_data['activity_data']; $date_labels = $activity_data['date_labels']; ?> 90 ) { wp_send_json_error( 'Invalid activity days range' ); } if ( $recent_posts_limit < 1 || $recent_posts_limit > 20 ) { wp_send_json_error( 'Invalid recent posts limit range' ); } // Save options update_option( 'aistma_widget_activity_days', $activity_days ); update_option( 'aistma_widget_recent_posts_limit', $recent_posts_limit ); update_option( 'aistma_widget_hide_empty_columns', $hide_empty_columns ); wp_send_json_success( 'Settings saved successfully' ); } /** * Get widget-specific CSS styles */ public static function get_widget_styles() { return ' .aistma-posts-activity-widget { font-size: 12px; } .aistma-posts-activity-widget .aistma-widget-summary { margin-bottom: 15px; padding: 10px; background: #f8f9fa; border-radius: 4px; text-align: center; } .aistma-posts-activity-widget .aistma-activity-heatmap { max-height: 200px; overflow-y: auto; } .aistma-posts-activity-widget .aistma-heatmap-container-horizontal { display: flex; flex-direction: column; gap: 2px; } .aistma-posts-activity-widget .aistma-heatmap-dates, .aistma-posts-activity-widget .aistma-heatmap-row { display: grid; align-items: center; gap: 2px; } .aistma-posts-activity-widget .aistma-heatmap-dates .date-vertical { width: 14px; font-size: 9px; color: #666; writing-mode: vertical-rl; transform: rotate(180deg); text-align: left; height: 60px; display: flex; align-items: center; justify-content: center; } .aistma-posts-activity-widget .aistma-heatmap-row .aistma-heatmap-day { width: 12px; height: 12px; border-radius: 2px; cursor: pointer; } .aistma-posts-activity-widget .aistma-heatmap-row .post-label { width: 180px; text-align: left; font-size: 11px; color: #333; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 1.2; } .aistma-posts-activity-widget .aistma-heatmap-row .post-label a { text-decoration: none; color: #0073aa; } .aistma-posts-activity-widget .aistma-heatmap-row .post-label a:hover { text-decoration: underline; } .aistma-posts-activity-widget .aistma-heatmap-dates .post-label { width: 180px; font-weight: bold; color: #666; } .aistma-posts-activity-widget .intensity-0 { background-color: #ebedf0; } .aistma-posts-activity-widget .intensity-1 { background-color: #c6e48b; } .aistma-posts-activity-widget .intensity-2 { background-color: #7bc96f; } .aistma-posts-activity-widget .intensity-3 { background-color: #239a3b; } .aistma-posts-activity-widget .intensity-4 { background-color: #196127; } .aistma-posts-activity-widget .aistma-widget-footer { margin-top: 15px; text-align: center; border-top: 1px solid #eee; padding-top: 10px; } .aistma-posts-activity-widget .aistma-widget-footer .button { margin: 0 2px; } '; } } // Initialize the widget AISTMA_Posts_Activity_Widget::init();