0

有人可以指出我如何通过其网页从 WordPress 插件目录中检索插件的最新版本号吗?

例如,在http://wordpress.org/extend/plugins/advanced-custom-fields/

我要抢4.0.1

4

2 回答 2

1

您可以与 WordPress 存储库 API 交互:

WordPress 存储库 API 是用于获取插件和主题信息的 API。

http://wp.tutsplus.com/tutorials/creative-coding/interacting-with-wordpress-plug-in-theme-api/

于 2013-04-30T19:41:24.353 回答
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;
}

以下是响应的缩短版,description字段stats很大。

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
)
于 2013-05-01T00:32:58.070 回答