How to add day number and initial to my post graph?

I have been building this recent-post-count dashboard widget graph – or, more accurately, modifying and refining a comment-specific version, for posts.

I’m really pleased. Some of it may be inefficient, I’m learning as I go along, but I got it working…

enter image description here

You see, for each date, starting at the specific date, it increments through each date up to the current one, generating a WP_Query to get the post count for that date, adding each to an array, which powers the chart.

<?php
/**
 * Plugin Name: Dashboard Widget for Comments
 * Description: Displays a comment-count graph in a dashboard widget. Code inspired by WPMUDev article https://premium.wpmudev.org/blog/adding-custom-widgets-to-the-wordpress-admin-dashboard/
 * Version: 1.0.0
 * Author: Robert Andrews
 */





 function dashboard_widget_display_enqueues( $hook ) {
    if( 'index.php' != $hook ) {
        return;
    }

    wp_enqueue_style( 'dashboard-widget-styles', plugins_url( '', __FILE__ ) . '/widgets.css' );
 }

 add_action( 'admin_enqueue_scripts', 'dashboard_widget_display_enqueues' );





 function register_comment_stats_dashboard_widget() {
    wp_add_dashboard_widget(
        'comment_stats_widget',
        'Post Stats',
        'comment_stats_dashboard_widget_display'
    );

 }
 add_action( 'wp_dashboard_setup', 'register_comment_stats_dashboard_widget' );












 function comment_stats_dashboard_widget_display() {

   // Initialise array to hold our daily post counts
   $comment_counts = array();

   // Number of days to chart
   $num_days = 14;
   // Start at this time ago
    $date = new DateTime('-'.$num_days.' days');

   // For each day of the period
    for (
     $x = 1; // Initial value
     $x <= $num_days; // Iterate this many times
     $x++ // Increment each time
   ) {
     // Go to next day
     $date->modify('+1 day');
     // echo 'Date: '.$date->format('Y-m-d') . '<br />';
     $day_before = new DateTime($date->format('Y-m-d').' -1 day');
     // echo 'day_before: '.$day_before->format('Y-m-d') . '<br />';
     $day_after = new DateTime($date->format('Y-m-d').' +1 day');
     // echo 'day_after: '.$day_after->format('Y-m-d') .'<br />';

     // WordPress query for posts in time period
     $date_query = array(
                         array(
                             'after'=>($day_before->format('Y-m-d')),
                                                        'before'=>($day_after->format('Y-m-d')),
                             'inclusive' => false,
                             )
                         );
     $args = array(
                     'post_type' => 'quote',
                     'post_status'=>'publish',
                     // 'category__in' => (array)$ids,
                     'date_query' => $date_query,
                     'no_found_rows' => true,
                     'suppress_filters' => true,
                     'fields'=>'ids',
                     'posts_per_page'=>-1,
                     'orderby' => 'ID'
                 );
     $query = new WP_Query( $args );

     // echo $query->post_count;

     // Add daily post count to the array
     $comment_counts[] = $query->post_count;


    }









    // eg. $comment_counts = array( 20, 29, 39, 33, 17, 12, 2, 20, 29, 39, 33, 17, 12, 2 );
    $highest_value = max( $comment_counts );
    $data_points = count( $comment_counts );

    $bar_width = 100 / $data_points - 2;

    $total_height = 120;

    ?>

    <div class="comment-stat-bars" style="height:<?php echo $total_height ?>px;">
        <?php
            foreach( $comment_counts as $count ) :
                $count_percentage = $count/$highest_value;
                $bar_height = $total_height * $count_percentage;
                $border_width = $total_height - $bar_height;
        ?>
        <div class="comment-stat-bar" style="height:<?php echo $total_height ?>px; border-top-width:<?php echo $border_width ?>px; width: <?php echo $bar_width ?>%;"></div>
        <?php endforeach ?>
    </div>

    <div class="comment-stat-labels">
        <?php foreach( $comment_counts as $count ) : ?>
        <div class="comment-stat-label" style="width: <?php echo $bar_width ?>%;"><?php echo $count ?></div>
    <?php endforeach ?>
    </div>

    <div class="comment-stat-caption">Posts added in the last <?php echo $num_days; ?> days</div>


    <?php
 }


?>

Now I’d like to add two things, below the post count on the chart…

  1. the day number (eg. “10”) and
  2. the initial of the day (eg. “S” for “Saturday”)

Here is what I am guessing…

A.

I guess this may mean populating the $comment_count array with not just values but keys and values, wherein the key is the related date, $date->format('Y-m-d'). I don’t know anything about this type of array. Is this an “associative” array? How would I modify…

 // Add daily post count to the array
 $comment_counts[] = $query->post_count;

… to do that?

B.

Then maybe, in the foreach( $comment_counts as $count ) that steps through the $comment_count array, I would need to turn the stored key date in to both a “10” and an “S”? How would I do that?

Am I on the right track?

Edit:

Here is the finished product…

enter image description here

1 Answer
1

I would go like this:

A. Use the Unix timestamp as array key.

$comment_counts[$date->format('U')] = $query->post_count;

B. Loop with key included.

foreach( $comment_counts as $count_key => $count ) :

Then you obtain the day number and the initial of the day from the key:

// "d" means with leading zero, use "j" in place of "d" for no leading zero 
$day_number = date("d", $count_key);
$day_initial = substr(date("D" , $count_key), 0, 1);

Leave a Comment