Skip to content

Instantly share code, notes, and snippets.

@rhyswynne
Created March 23, 2015 18:29
Show Gist options
  • Save rhyswynne/453828b34bb2716132bb to your computer and use it in GitHub Desktop.
Save rhyswynne/453828b34bb2716132bb to your computer and use it in GitHub Desktop.
Getting WordPress Downloads Stats
function peadig_remote_request( $key, array $payload ) {
$body = false;
if ( false === ( $body = get_site_transient( 'peadig_rapi_'.$key ) ) ) {
$body = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => $payload) );
// cache for 3 days
set_site_transient( 'peadig_rapi_'.$key, $value, 60 * 60 * 24 * 3);
}
return $body;
}
function peadig_get_plugin_dl_count($wpurl){
$payload = array(
'action' => 'plugin_information',
'request' => serialize(
(object)array(
'slug' => $wpurl,
'fields' => array('description' => true)
)
)
);
$body = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => $payload) );
//$body = peadig_remote_request( 'count', $payload );
$downloaded = unserialize($body['body'])->downloaded;
$returnstring = $downloaded;
return $returnstring;
}
function peadig_get_plugin_star_count($wpurl){
$payload = array(
'action' => 'plugin_information',
'request' => serialize(
(object)array(
'slug' => $wpurl,
'fields' => array('description' => true)
)
)
);
$body = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => $payload) );
//$body = peadig_remote_request( 'star', $payload );
$rated = round(unserialize($body['body'])->rating / 20);
$numrated = unserialize($body['body'])->num_ratings;
//$returnstring = '<span itemprop="ratingValue">' . $rated . '</span>/5 (<span itemprop="ratingCount">' . $numrated . '</span> votes)';
$stars = '';
if($rated%1 == 0){
for($i=1;$i<=$rated;$i++){
$stars .= '<i class="fa fa-star fa-2x"></i>';
}
} else {
for($i=1;$i<=$rated;$i++){
$stars .= '<i class="fa fa-star fa-2x"></i>';
}
$stars .= '<i class="fa fa-star-half fa-2x"></i>';
}
//<span class="ratings"><i class="fa fa-star fa-2x"></i><i class="fa fa-star fa-2x"></i><i class="fa fa-star fa-2x"></i><i class="fa fa-star fa-2x"></i><i class="fa fa-star fa-2x"></i></span>
$returnstring = '<span content="'.$rated.'" itemprop="ratingValue">'.$stars.'</span><br />(<span content="'.$numrated.'" itemprop="ratingCount">' . $numrated . '</span> votes)';
return $returnstring;
}
function peadig_get_plugin_rating($wpurl){
$payload = array(
'action' => 'plugin_information',
'request' => serialize(
(object)array(
'slug' => $wpurl,
'fields' => array('description' => true)
)
)
);
$body = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', array( 'body' => $payload) );
//$body = peadig_remote_request( 'rating', $payload );
$rated = unserialize($body['body'])->rating / 20;
return $rated;
}
function peadig_get_plugin_slug($wpurl)
{
$wpurl = str_replace("http://wordpress.org/plugins/","",$wpurl);
$wpurl = str_replace("http://www.wordpress.org/extend/plugins/","",$wpurl);
$wpurl = str_replace("www.wordpress.org/extend/plugins/","",$wpurl);
$wpurl = str_replace("wordpress.org/extend/plugins/","",$wpurl);
$wpurl = str_replace("/","",$wpurl);
return $wpurl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment