Advertisement
brasofilo

Plugin Data WP API

Apr 17th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. /*
  2.  * Get Plugin Data from WordPress.org
  3.  *
  4.  * Uses WP API : http://wpapi.org/
  5.  * Function extracted from Viper007Bond's plugin: http://wordpress.org/extend/plugins/wordpress-download-counter/
  6.  * Usage: change $plugin_url value and adapt the result to your code
  7.  *
  8. */
  9.  
  10. function get_data() {
  11.     // CHANGE THIS TO YOUR OWN PLUGIN URL, this is mine :)
  12.     $plugin_url = 'http://wpapi.org/api/plugin/many-tips-together.json';
  13.  
  14.     // Check for a cached copy (we don't want to do an HTTP request too often)
  15.     $cachetime = 15; // Seconds between AJAX powered refreshes
  16.     $cache = get_transient('wpdlcounter');
  17.     if ( false !== $cache )
  18.         return $cache;
  19.  
  20.     $data = array();
  21.  
  22.     // Fetch the data
  23.     if ( $response = wp_remote_retrieve_body( wp_remote_get( $plugin_url ) ) ) {
  24.         // Decode the json response
  25.         if ( $response = json_decode( $response, true ) ) {
  26.             // Double check we have all our data
  27.             if ( !empty($response['added']) ) {
  28.                 $data = $response;
  29.             }
  30.         }
  31.     }
  32.     // On a failed scrape, cache that fail for a full minute
  33.     else {
  34.         set_transient( 'wpdlcounter', $data, 60 );
  35.     }
  36.  
  37.     // Cache the data for future usage
  38.     if ( $cachetime < 2 )
  39.         $cachetime = 2;
  40.     set_transient( 'wpdlcounter', $data, $cachetime - 1 );
  41.  
  42.     return $data;
  43. }
  44.  
  45.  
  46. $my_plugin = get_data();
  47. echo 'Total Downloads: ' . number_format_i18n($my_plugin['total_downloads']);
  48. echo 'Rating: ' . $my_plugin['rating'];
  49. echo 'Total ratings: ' . number_format_i18n($my_plugin['num_ratings']);
  50. echo 'Updated: ' . date_i18n(get_option('date_format'), strtotime($my_plugin['updated']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement