| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
function 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 |
|
|---|
| 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 |
|
|---|
| 27 |
$im = @ImageCreate ($width, $height) |
|---|
| 28 |
or die ('Cannot create a new GD image.'); |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 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 |
|
|---|
| 38 |
$back_color = $white; |
|---|
| 39 |
$box_color = $beige; |
|---|
| 40 |
$text_color = $black; |
|---|
| 41 |
$line_color = $blue; |
|---|
| 42 |
$border_color = $black; |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
$posttimes = getPostTimes($last_x_days); |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 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 |
|
|---|
| 59 |
ImageFill($im, 0, 0, $back_color); |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 104 |
$curPostTime = abs($posttimes[$i]) % 1440; |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
$curX = $left + ($right - $left)/1440 * $curPostTime; |
|---|
| 108 |
|
|---|
| 109 |
# draw the post line |
|---|
| 110 |
ImageLine($im, $curX, $bottom, $curX, $top, $line_color); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
ImagePNG ($im,$saveFile); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
function 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 |
|
|---|
| 138 |
add_action('publish_post', 'updateBlogTimePNG'); |
|---|
| 139 |
?> |
|---|