Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
WordPress Plugin Download Stats for Panic Status Board Original blog post: http://simple-fields.com/2013/status-board-panel-to-show-wordpress-plugin-downloads/
<?php
// Read the original blog post here to understand what this is:
// http://simple-fields.com/2013/status-board-panel-to-show-wordpress-plugin-downloads/
define('WP_USE_THEMES', false);
define("WP_CACHE", false); // i get 404 if enabled
require('../wordpress/wp-blog-header.php');
class panic_dashboard_wp_plugin_stats {
var $url_template = 'http://wpapi.org/api/plugin/%1$s.json',
$plugin_slugs = "simple-fields,simple-history,cms-tree-page-view",
$cache_group = "panic_dashboard_wp_plugin_stats",
$cache_expire = 15, // in minutes
$days_to_show = 30;
function __construct() {
if ( isset( $_GET["slugs"] ) && ! empty( $_GET["slugs"] ) ) $this->plugin_slugs = $_GET["slugs"];
if ( isset( $_GET["days"] ) && ! empty( $_GET["days"] ) ) $this->days_to_show = $_GET["days"];
$cache_key = md5( serialize($this->plugin_slugs) );
$arr_plugins_data = wp_cache_get( $cache_key, $this->cache_group );
// $force_reload = true;
if ( true === $force_reload || false === $arr_plugins_data ) {
// echo "<br>load from server";
$arr_plugins_data = array();
$arr_slugs = explode(",", $this->plugin_slugs);
foreach ( $arr_slugs as $one_slug ) {
$plugin_stats_url = sprintf($this->url_template, $one_slug);
$remote_data = wp_remote_get( $plugin_stats_url );
$arr_plugins_data[] = json_decode( $remote_data["body"] );
}
wp_cache_set( $cache_key, $arr_plugins_data, $this->cache_group, MINUTE_IN_SECONDS * $this->cache_expire );
} else {
// echo "<br>got from cache";
}
// some useful info in the plugins data array:
// name, num_ratings, rating, stats, total_downloads, updated, version
$arr_datasequences = array();
foreach ( $arr_plugins_data as $key => $one_plugin_data ) {
$arr_plugins_data[$key]->dashboard_stats = array();
foreach ($one_plugin_data->stats as $stat_key => $stat_val) {
$arr_plugins_data[$key]->dashboard_stats[] = array("title" => $stat_key, "value" => $stat_val);
}
$arr_plugins_data[$key]->dashboard_stats = array_slice( $arr_plugins_data[$key]->dashboard_stats, sizeof( $arr_plugins_data[$key]->dashboard_stats ) - $this->days_to_show );
$arr_datasequences[] = array(
"title" => $one_plugin_data->name,
"datapoints" => $one_plugin_data->dashboard_stats
);
}
$arr_json = array(
"graph" => array(
"title" => "Plugin stats for last $this->days_to_show days",
"total" => true,
"datasequences" => $arr_datasequences
)
);
header("HTTP/1.1 200 OK");
header('Content-Type: application/json');
echo json_encode( $arr_json );
}
}
new panic_dashboard_wp_plugin_stats();
/*
echo "<br>" . $wpdb->num_queries; ?> <?php _e('queries'); ?>. <?php timer_stop(1); ?> <?php _e('seconds');
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment