2013-04-30 66 views

回答

0

我用我的一個插件以下。查看評論以獲取詳細信息。

/** 
* Function used to print the data 
*/ 
function b5f_print_repository_info($echo = true) 
{ 
    // Grab data and do nothing if fail 
    $plugin_data = b5f_get_repository_info(); 
    if(false === $plugin_data) 
     return; 

    // Custom function used to format the rating 
    $total_downloads = number_format_i18n($plugin_data['total_downloads']); 
    $rating   = b5f_format_rating($plugin_data['rating']/20); 
    $updated   = date_i18n(get_option('date_format'), strtotime($plugin_data['updated'])); 
    $num_rating  = number_format_i18n($plugin_data['num_ratings']); 
    $version   = $plugin_data['version']; 

    if($echo) 
     echo 'Your stuff using the variables above.'; 
    else 
     return 'Your stuff using the variables above.'; 
} 

/** 
* Call WP API and return the data 
*/ 
function b5f_get_repository_info() 
{ 
    $plugin_url = 'http://wpapi.org/api/plugin/advanced-custom-fields.json'; 

    // Cache 
    $cache = get_transient('my_plugin_transient'); 
    if(false !== $cache) 
     return $cache; 

    // Fetch the data 
    if($response = wp_remote_retrieve_body(wp_remote_get($plugin_url))) 
    { 
     // Decode the json response 
     if($response = json_decode($response, true)) 
     { 
      // Double check we have all our data 
      if(!empty($response['added'])) 
      { 
       set_transient('my_plugin_transient', $response, 720); 
       return $response; 
      } 
     } 
    } 
    return false; 
} 

/** 
* Auxiliary function to format the Rating 
*/ 
function b5f_format_rating($number, $cents = 1) 
{ 
    // Check if value can be dealt with 
    if(!is_numeric($number)) 
     return $number; 

    if(!$number) { 
     $rating = ($cents == 2) ? '0.00' : '0'; 
    } 
    else { 
     if(floor($number) == $number) { 
      $rating = number_format($number, ($cents == 2 ? 2 : 0)); 
     } 
     else { 
      $rating = number_format(round($number, 2), ($cents == 0 ? 0 : 2)); 
     } 
    } 
    return $rating; 
} 

而下面響應的縮短版,descriptionstats領域是非常大的。

Array 
(
    [added] => 2011-03-25 
    [author] => Array 
     (
      [name] => Elliot Condon 
      [url] => http://www.elliotcondon.com/ 
      [profile] => http://profiles.wordpress.org/elliotcondon 
     ) 

    [average_downloads] => 1415.61 
    [contributors] => Array 
     (
      [contributor-Elliot Condon] => 
     ) 

    [download_link] => http://downloads.wordpress.org/plugin/advanced-custom-fields.zip 
    [hits] => 0 
    [homepage] => http://www.advancedcustomfields.com/ 
    [last_update_details] => 2013-04-30 17:36:06 
    [last_update_stats] => 2013-04-30 17:36:05 
    [name] => Advanced Custom Fields 
    [num_ratings] => 905 
    [rating] => 98 
    [requires] => 3.0.0 
    [sections] => Array 
     (
      [description] => <p>Advanced Custom Fields is 
     ) 

    [slug] => advanced-custom-fields 
    [stats] => Array 
     (
      [2011-11-09] => 683 
     ) 

    [tags] => Array 
     (
      [tag-admin] => admin 
      [tag-advanced] => advanced 
      [tag-custom] => custom 
      [tag-custom-field] => custom field 
      [tag-edit] => edit 
      [tag-field] => field 
      [tag-file] => file 
      [tag-image] => image 
      [tag-magic-fields] => magic fields 
      [tag-matrix] => matrix 
      [tag-more-fields] => more fields 
      [tag-post] => Post 
      [tag-repeater] => repeater 
      [tag-simple-fields] => simple fields 
      [tag-text] => text 
      [tag-textarea] => textarea 
      [tag-type] => type 
     ) 

    [tested] => 3.5.1 
    [total_days] => 539 
    [total_downloads] => 763012 
    [type] => plugin 
    [updated] => 2013-04-30 
    [version] => 4.1.0 
)