source: blogtimes/trunk/blogtimes.php @ 457

Revision 457, 4.8 KB checked in by matt, 7 years ago (diff)

Moving into trunk

Line 
1<?php
2
3/*
4Plugin Name: Blogtimes
5Plugin URI: http://dev.wp-plugins.org/wiki/BlogTimes
6Description: This plugin generates a bar graph image showing when posts are made during a period of time. For this to work <code>wp-images/blogtimes.png</code> must be writable by the web server. Original code by Sanjay Sheth of sastools.com.
7Author: Matt Mullenweg
8Author URI: http://photomatt.net/
9Version: 0.2
10*/
11
12
13// Change the defaults to modify anything
14function updateBlogTimePNG($dummy = 'placeholder', $saveFile = '', $last_x_days = 30,
15        $width = 480, $height = 65, $horzpadding = 5, $vertpadding = 5,
16        $show_ticks = 1, $title = "Blog Post Times") {
17
18        if (!$saveFile) $saveFile = ABSPATH . 'wp-images/blogtimes.png';
19    // constants defining image
20    $fontheight = ImageFontHeight(2);
21    $fontwidth  = ImageFontWidth(2);
22    $monthtext = "Last $last_x_days days";
23    $unitname = "hour of day";
24    $show_units = 1;
25
26    // create the basic image
27    $im = @ImageCreate ($width, $height)
28       or die ('Cannot create a new GD image.');
29
30    // generate some colors, format: RED, GREEN, BLUE
31    $white      = ImageColorAllocate ($im, 255,255,255);
32    $black      = ImageColorAllocate ($im, 0,0,0);
33    $beige      = ImageColorAllocate ($im, 238,238,238);
34    $blue       = ImageColorAllocate ($im, 102,102,102);
35    $silver     = ImageColorAllocate ($im, 0xE0,0xE0,0xE0);
36
37    // define what color to use where
38    $back_color = $white;    # this is background of entire image (text & all)
39    $box_color  = $beige;    # this is background of just the posts box
40    $text_color = $black;
41    $line_color = $blue;     # this is color of lines for each post
42    $border_color = $black;
43
44    # query the db and build the list
45    $posttimes = getPostTimes($last_x_days);
46
47    # calculate how many intervals to show
48    $intervals = floor( ($width / 40) );
49    if ($intervals >= 24) $i_mod = 1;
50    else if ($intervals >= 12) $i_mod = 2;
51    else if ($intervals >= 8) $i_mod = 3;
52    else if ($intervals >= 6) $i_mod = 4;
53    else if ($intervals >= 4) $i_mod = 6;
54    else if ($intervals >= 3) $i_mod = 8;
55    else if ($intervals >= 2) $i_mod = 16;
56    else $i_mod = 24;
57
58    # fill the image with the background color
59    ImageFill($im, 0, 0, $back_color);
60
61    # create a filled  rectangle with a solid border
62    $left = $horzpadding; $right = $width - $horzpadding;
63    $top = $fontheight + $vertpadding;
64    $bottom = $height - $vertpadding - $fontheight;
65
66    if ($show_units)
67        $bottom -= $fontheight;
68
69    ImageFilledRectangle($im, $left,$top,$right,$bottom, $box_color);
70    ImageRectangle($im, $left,$top,$right,$bottom, $border_color);
71
72    # write title and monthtext
73    ImageString($im, 2, $left, 0, $title,$text_color);
74    $txtwidth = strlen($monthtext) * $fontwidth;
75    ImageString($im, 2, $right - $txtwidth, 0,$monthtext,$text_color);
76
77    # add the legend on the bottom
78    for ($i = 0; $i <= 23; $i=$i+1)
79    {
80        if ($i % $i_mod == 0) {
81            $curX = $left + ($right - $left)/24 * $i;
82
83            if ($i > 9) {$strX = $curX - 5;}
84            else        {$strX = $curX - 2;}
85
86            ImageString($im, 2, $strX , $bottom, $i, $text_color);
87            if ($show_ticks)
88                ImageLine($im, $curX, $bottom, $curX, $bottom - 5, $tick_color);
89        }
90    }
91    ImageString($im, 2, $right - 5, $bottom,  0, $text_color);
92    if ($show_units) {
93        $curX = ($right + $left) / 2 - ($fontwidth * strlen($unitname)/2);
94        $curY = $bottom + $fontheight + 2;
95        ImageString($im, 2, $curX, $curY, $unitname, $text_color);
96    }
97
98    # now we draw the lines for each post
99    # the post times should be in terms of # of minutes since midnight
100    $arrcount = count($posttimes);
101    for ($i = 0; $i < $arrcount; $i++)
102    {
103        # make sure postTime is between 0 and 1439
104        $curPostTime = abs($posttimes[$i]) % 1440;
105       
106        # calculate the horz pos inside box             
107        $curX = $left + ($right - $left)/1440 * $curPostTime;    # 1440 minutes per day
108
109        # draw the post line
110        ImageLine($im, $curX, $bottom, $curX, $top, $line_color);
111    }
112
113    # save the file to disk in PNG format
114    ImagePNG ($im,$saveFile);
115}
116
117# This function will query the db for all the posts in last x days
118# and build an array of # of minutes since midnight for each post
119function getPostTimes( $last_x_days = 30 ) {
120        global $wpdb, $tableposts;
121
122    $result = $wpdb->get_results("
123                SELECT HOUR(post_date)*60+MINUTE(post_date) AS totmins
124                FROM $tableposts
125                WHERE (TO_DAYS(CURRENT_DATE) - TO_DAYS(post_date)) <= $last_x_days
126                AND post_status = 'publish'
127                ORDER BY totmins ASC
128                ");
129
130    foreach ($result as $row) {
131      $postTimes[] = $row->totmins;
132    }
133   
134    return $postTimes;
135}
136
137
138add_action('publish_post', 'updateBlogTimePNG');
139?>
Note: See TracBrowser for help on using the repository browser.