'activitysparks',
'description' => 'A sparkline chart for posts/comments');
$this->WP_Widget('activitysparks', 'Activity Sparks', $widget_ops);
}
function widget($args, $instance) {
extract($args);
$category_id=0;
if(is_category() and $instance['chart_category']){
$category_ids = get_all_category_ids();
foreach($category_ids as $cat_id) {
if($category_id==0)
if(is_category($cat_id))
$category_id = $cat_id;
}
}
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
$cachetime = intval($instance['cachetime'][$category_id]);
if($cachetime){
$url = $instance['url'][$category_id];
if($url=='' OR (time()-intval($instance['url_time'][$category_id]))>$cachetime) {
$settings = get_option($this->option_name);
$url = $settings[$this->number]['url'][$category_id] = $this->build_url($instance);
$settings[$this->number]['url_time'][$category_id]=time();
update_option( $this->option_name, $settings );
}
} else {
// no caching - just build it
$url = $this->build_url($instance,$category_id);
}
// output
echo $before_widget;
if($title)
echo $before_title.$title.$after_title;
echo '';
echo $after_widget;
}
function build_url($instance,$category_id=0) {
$dataset = empty($instance['dataset']) ? 'posts' : $instance['dataset'];
$width_px = empty($instance['width_px']) ? 250 : $instance['width_px'];
$height_px = empty($instance['height_px']) ? 30 : $instance['height_px'];
$period = empty($instance['period']) ? 30 : $instance['period'];
$ticks = empty($instance['ticks']) ? 100 : $instance['ticks'];
$chma = empty($instance['chma']) ? 0 : $instance['chma'];
$bkgrnd = empty($instance['bkgrnd']) ? 'FFFFFF' : $instance['bkgrnd'];
$posts_color = empty($instance['posts_color']) ? '4D89F9' : $instance['posts_color'];
$comments_color = empty($instance['comments_color']) ? 'FF9900' : $instance['comments_color'];
// load the data
if($dataset != 'comments')
$posts_data = $this->get_datapoints('posts',$period,$ticks,$category_id);
if($dataset != 'posts')
$comments_data = $this->get_datapoints('comments',$period,$ticks,$category_id);
// build the URL for Google Chart API
$url = 'http://chart.apis.google.com/chart?chs='.$width_px.'x'.$height_px.'&cht=ls';
$chd = '';
if($dataset != 'comments') {
$chd = $posts_data;
$chco = $posts_color;
if($dataset != 'posts'){
$chd .= '|';
$chco .= ',';
}
}
if($dataset != 'posts') {
$chd .= $comments_data;
$chco .= $comments_color;
}
$url .= '&chd=t:'.$chd;
// line color(s)
$url .= '&chco='.$chco;
// display legend?
if($dataset == 'legend')
$url .= '&chdl=Posts|Comments';
//background color ?
if($bkgrnd=='NONE')
$bkgrnd = 'FFFFFF00'; // transparent background
if($bkgrnd!='FFFFFF')
$url .= '&chf=bg,s,'.$bkgrnd;
// margin padding ?
if($chma) {
$url .= "&chma=$chma,$chma,$chma,$chma";
if($dataset=='legend')
$url .= '|90,20';
}
return $url;
}
function get_datapoints($type='posts', $period=30, $ticks=100,$category_id=0) {
global $wpdb;
$wpdb->show_errors();
$now_tick = $wpdb->get_row("SELECT ROUND((TO_DAYS(now()))/$period) tick")->tick;
if($category_id) {
$category_posts_join = " INNER JOIN {$wpdb->prefix}term_relationships ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id ";
$category_comments_join = " INNER JOIN {$wpdb->prefix}term_relationships ON {$wpdb->prefix}comments.comment_post_ID = {$wpdb->prefix}term_relationships.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id ";
$category_where = " AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category' AND {$wpdb->prefix}term_taxonomy.term_id = $category_id ";
}
if($type=='posts') {
$sql = "SELECT ROUND((TO_DAYS(post_date))/$period) ticker, count(*) value
FROM {$wpdb->prefix}posts $category_posts_join
WHERE post_status='publish' $category_where
AND post_date > date_sub(now(), interval ($ticks*$period) day)
GROUP BY ticker
ORDER BY ticker";
}
if($type=='comments') {
$sql = "SELECT ROUND((TO_DAYS(comment_date))/$period) ticker, count(*) value
FROM {$wpdb->prefix}comments $category_comments_join
WHERE comment_approved='1' $category_where
AND comment_date > date_sub(now(), interval ($ticks*$period) day)
GROUP BY ticker
ORDER BY ticker";
}
$rows = $wpdb->get_results($sql);
$data = array();
$maxval=0;
foreach($rows as $row){
$end_ticker=$row->ticker;
$data[$row->ticker] = $row->value;
if($maxval< $row->value){
$maxval=$row->value;
}
}
// "normalize" data and build CSV
if($maxval<4) $maxval=4;
for($i=$now_tick-$ticks;$i<=$now_tick;$i++){
$value = $maxval ? intval(($data[$i]/$maxval)*100) : 0;
$data_points .= (isset($data_points)) ? ','.$value : $value;
}
return $data_points;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags(stripslashes($new_instance['title']));
$instance['dataset'] = $new_instance['dataset'];
$instance['width_px'] = intval($new_instance['width_px']);
$instance['height_px'] = intval($new_instance['height_px']);
$instance['period'] = intval($new_instance['period']);
$instance['ticks'] = intval($new_instance['ticks']);
$instance['chma'] = intval($new_instance['chma']);
$instance['bkgrnd'] = strtoupper($new_instance['bkgrnd']);
$instance['posts_color'] = strtoupper($new_instance['posts_color']);
$instance['comments_color'] = strtoupper($new_instance['comments_color']);
$instance['cachetime'] = intval($new_instance['cachetime']);
$instance['url'] = ''; // flush the cache
$instance['chart_category'] = intval($new_instance['chart_category']);
return $instance;
}
function form($instance) {
$instance = wp_parse_args((array)$instance, array(
'title' => 'Recent Activity',
'width_px' => 250,
'height_px' => 50,
'period' => 7,
'ticks' => 90,
'chma' => 5,
'bkgrnd' => 'FFFFFF',
'posts_color' => '4D89F9',
'comments_color' => 'FF9900'
));
$title = htmlspecialchars($instance['title']);
$dataset = $instance['dataset'];
$width_px = intval($instance['width_px']);
$height_px = intval($instance['height_px']);
$period = intval($instance['period']);
$ticks = intval($instance['ticks']);
$chma = intval($instance['chma']); // Graph Margin
$bkgrnd = htmlspecialchars($instance['bkgrnd']);
$posts_color = htmlspecialchars($instance['posts_color']);
$comments_color = htmlspecialchars($instance['comments_color']);
$cachetime = intval($instance['cachetime']);
$chart_category = intval($instance['chart_category']);
${'dataset_'.$dataset} = 'SELECTED';
${'cachetime_'.$cachetime} = 'SELECTED';
${'period_'.$period} = 'SELECTED';
$chart_category_checked = $chart_category ? 'checked': '';
echo '
|
|
|
|
Change the Period to suit the frequency of your posts, and Ticks to limit how many periods to graph. | |
|
|
|
|
|
|
e.g. FFFFAA or NONE
'; } } function activitysparks_init() { register_widget('activitysparks'); } function activitysparks($settings = array()) { $activitysparks = new activitysparks; $url = $activitysparks->build_url($settings); echo '