/
posting-streak.php
85 lines (69 loc) · 2.5 KB
/
posting-streak.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function posting_streak( $blog_id, $after = '' ) {
global $wpdb;
switch_to_blog( $blog_id );
$longest_streak = $longest_slump = array(
'days' => 0,
);
$after_sql = '';
if ( $after ) {
$after_sql = $wpdb->prepare( 'AND post_date_gmt > %s', $after );
}
$posts = $wpdb->get_col(
"SELECT post_date
FROM $wpdb->posts
WHERE post_type = 'post'
AND post_status = 'publish'
$after_sql"
);
if ( ! count( $posts ) ) {
echo "\n\nNo posts found. Looks like your life is a slump!\n\n";
return;
}
// First post always starts a streak
$first_day = $streak_start_day = $last_post_day = date( 'Y-m-d', strtotime( $posts[0] ) );
$day_seconds = 24 * 60 * 60;
foreach ( $posts as $date ) {
// Strip times
$day = date( 'Y-m-d', strtotime( $date ) );
// Multiple posts in one day
if ( $day == $last_post_day ) {
continue;
}
$previous_day_ts = strtotime( $day . ' -1 day' );
if ( $last_post_day != date( 'Y-m-d', $previous_day_ts ) ) {
$slump_days = ( $previous_day_ts - strtotime( $last_post_day ) ) / $day_seconds + 1;
if ( $slump_days > $longest_slump['days'] ) {
$longest_slump['start'] = date( 'Y-m-d', strtotime( $last_post_day . ' +1 day' ) );
$longest_slump['end'] = date( 'Y-m-d', $previous_day_ts );
$longest_slump['days'] = $slump_days;
}
$streak_days = ( strtotime( $last_post_day ) - strtotime( $streak_start_day ) ) / $day_seconds + 1;
if ( $streak_days > $longest_streak['days'] ) {
$longest_streak['start'] = $streak_start_day;
$longest_streak['end'] = $last_post_day;
$longest_streak['days'] = $streak_days;
}
$streak_start_day = $day;
}
$last_post_day = $day;
}
echo "\nPosts published: " . number_format( count( $posts ) ) . " since $first_day";
echo "\n\nLongest streak: " . number_format( $longest_streak['days'] ) . " ({$longest_streak['start']} - {$longest_streak['end']})";
echo "\nLongest slump: " . number_format( $longest_slump['days'] ) . " ({$longest_slump['start']} - {$longest_slump['end']})";
echo "\n\nCurrently on a ";
$yesterday = date( 'Y-m-d', strtotime( 'yesterday' ) );
if ( $last_post_day >= $yesterday ) {
$streak_days = ( strtotime( $last_post_day ) - strtotime( $streak_start_day ) ) / $day_seconds + 1;
echo "$streak_days day streak, since $streak_start_day. Post ";
if ( $last_post_day == $yesterday ) {
echo 'today';
} else {
echo 'tomorrow';
}
echo ' to keep it going!';
} else {
echo "$slump_days day slump. Last post was on $last_post_day.";
}
echo "\n\n";
restore_current_blog();
}