* @copyright Copyright 2008 Edward Dale * @license http://www.gnu.org/licenses/gpl.txt GPL 2.0 * @version $Id: immerstat.php 45292 2008-05-11 19:20:29Z scompt $ * @link http://www.scompt.com/projects/plugin-stats * @since 1.0 */ class PluginStats { var $snoop; // For grabbing the xml from WordPress.org /** * Adds hooks for the shortcode, template function, and initialization */ function PluginStats() { add_action('admin_init', array(&$this, 'init')); add_shortcode('plugin-stats', array(&$this, 'shortcode')); add_action('plugin-stats', array(&$this, 'template_function')); } /** * Adds dashboard filters and loads translations. */ function init() { add_action('wp_dashboard_setup', array(&$this, 'wp_dashboard_setup')); add_filter( 'wp_dashboard_widgets', array(&$this, 'add_widget') ); add_filter('dashwidman_safewidgets', array(&$this, 'add_safe_widget')); load_plugin_textdomain('plugin-stats', str_replace(ABSPATH, '', dirname(__FILE__))); } /** * Action hook for the plugin-stats template function. */ function template_function($slugname='default', $size='360x100', $addlink='0') { echo $this->shortcode(array('slugname'=>$slugname, 'size'=>$size, 'addlink'=>$addlink)); } /** * Handler for the plugin-stats shortcode. */ function shortcode($atts) { extract(shortcode_atts(array( 'slugname' => 'default', 'size' => '360x100', 'addlink' => '0', ), $atts)); $slugname = preg_replace('/[^a-zA-Z0-9_-]/', '', $slugname); if( $addlink ) add_filter('plugin-stats_img-link', array(&$this, 'add_link'), 10, 2); $imgLink = $this->getImageLink($slugname, $size); remove_filter('plugin-stats_img-link', array(&$this, 'add_link'), 10, 2); return $imgLink; } ///////////////////////// // Dashboard functions // ///////////////////////// /** * Register the widget and control function. */ function wp_dashboard_setup() { wp_register_sidebar_widget( 'plugin-stats', __( 'Plugin Stats', 'plugin-stats'), 'wp_dashboard_empty', array(), array(&$this, 'widget_output') ); wp_register_widget_control( 'plugin-stats', __( 'Plugin Stats', 'plugin-stats' ), array(&$this, 'widget_control'), array(), array( 'widget_id' => 'plugin-stats' ) ); } /** * Handles the 'edit' side of the widget. */ function widget_control() { global $user_ID; if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['plugin-stats_slugs']) ) { // sanitize the plugin names and save them $slugs = $_POST['plugin-stats_slugs']; $slugs = preg_replace('/[^a-zA-Z0-9_,-]/', '', $slugs); update_usermeta($user_ID, 'plugin-stats_slugs', $slugs); } else { // display the plugin names $slugs = get_usermeta($user_ID, 'plugin-stats_slugs'); ?>
Plugin Stats plugin! Add some plugins to start tracking your downloads in your dashboard.', 'plugin-stats'); return; } $slugs = explode(',', $slugs); // We want the dashboard output linked add_filter('plugin-stats_img-link', array(&$this, 'add_link'), 10, 2); foreach( $slugs as $slug ) { echo $this->getImageLink($slug, '360x100'); } } /** * Add widget to the current widgets. */ function add_widget( $widgets ) { global $wp_registered_widgets; array_splice( $widgets, 0, 0, 'plugin-stats' ); return $widgets; } /** * Mark the plugin as safe for http://wordpress.org/extend/plugins/dashboard-widget-manager/ */ function add_safe_widget($safe) { $safe[]='plugin-stats'; return $safe; } /** * Makes a link to the stats page on WP.org out of the provided image link. */ function add_link($imgLink, $slugname) { return "$imgLink"; } /** * Builds an image link out of the stats that have already been retrieved */ function getImageLink($slugname, $imgSize) { global $wp_locale; $new_stats = apply_filters('plugin-stats_build-link', $this->fetchStats($slugname)); // Put out the processed data $all_stats = get_option('plugin-stats'); $plugin = $all_stats[$slugname]; $processed = $plugin['processed']; if( !$new_stats && !empty($plugin["link_$imgSize"])) { $chart = $plugin["link_$imgSize"]; } else { // Trim down to what we need $size = min(count($processed['dates']), apply_filters('plugin-stats_num-days', 180)); $dates = array_slice($processed['dates'], -$size); $downloads = array_slice($processed['downloads'], -$size); // Figure out where the month labels should go based on the first day of the month $monthBegins = array(); $labels = array(); for ($i=0; $i < $size; $i++) { $day = $dates[$i]; if( preg_match('/\d{4}-\d{2}-01/', $day)) { $monthBegins []= (float)$i*100/$size; $pieces = explode('-', $day); $labels []= $wp_locale->get_month($pieces[1]); } } // If no months have begun in the interval, stick the current month name in the middle if( empty($labels) ) { $labels[]= $wp_locale->get_month(date('m')); $monthBegins[]= 50; } // Build all of the parameters for the Google Charts API $data = $this->simpleEncode($downloads, $processed['max'], $processed['min']); $chartArgs = array( 'chxr'=>"1,{$processed['min']},{$processed['max']}", 'chxp'=>'0,'.implode(',',$monthBegins), 'chxt'=>'x,y', 'chxl'=>'0:|'.implode('|', $labels), 'chs'=>$imgSize, 'cht'=>'lc', 'chd'=>$data, 'chtt'=>$slugname ); if( $processed['max']!=$processed['min'] ) { $current_ratio = (float)((end($downloads)-$processed['min'])/($processed['max']-$processed['min'])); $chartArgs['chm'] = "h,FF0000,0,$current_ratio,0.5"; $avgDev = $this->averageDeviation($downloads); $begin = max(0,($avgDev[0]-$avgDev[1]-$processed['min'])/($processed['max']-$processed['min'])); $end = min(1,($avgDev[0]+$avgDev[1]-$processed['min'])/($processed['max']-$processed['min'])); $chartArgs['chm'] .= "|r,99FF99,0,$begin,$end"; } // Build the image link, pass it through some filters, and return it $chartArgs = apply_filters('plugin-stats_img-link-args', $chartArgs, $slugname); $chart = add_query_arg($chartArgs, 'http://chart.apis.google.com/chart?'); $plugin["link_$imgSize"] = $chart; $all_stats[$slugname] = $plugin; update_option('plugin-stats', $all_stats); } return apply_filters('plugin-stats_img-link', "