問題描述

WordPress.org 外掛庫最近有一些補充。最顯著的是外掛頁面和作者簡介頁面的更改現在顯示 authors favorite plugins

我想建立一個側邊欄小工具外掛,顯示外掛作者收藏夾。我知道如何使用 API​​來獲取外掛統計資訊,並且還讀取了 DD32’s API Docs,但是我不認為檔案存在於配置檔案上,或者配置檔案 API 是否存在。

我試圖使用 wp_remote_get,我能夠從配置檔案頁面獲取 body html,但是沒有嘗試嘗試解析它,因為它似乎是一種混亂的方法。如果我可以使用 XML 或 json 獲取配置檔案,那將是很棒的。

有沒有任何方法,我缺少或一個配置檔案 API 存在?

編輯:

好的,我有一個 beta 版本的 github 使用 SimpleHTML Dom 解析器。我不認為我可以得到星級評分,但我很高興的結果作為第一次沒有一個 API 。

WordPress.org 不允許刪除內容並禁止您 (透過 @otto) 。所以這是一個沒有去,直到公開的 API 被釋放。

最佳解決方案

受歡迎的外掛已新增到 WordPress.org API 中。 3.5 中有一個新功能,允許您從外掛安裝程式訪問您的收藏夾。

有關如何在核心中使用的資訊,請參閱 http://core.trac.wordpress.org/ticket/22002

API 允許您檢索包含每個外掛的物件

  • 名稱

  • 描述

  • 作者

  • 評分

  • 最後更新日期

  • 更改日誌

  • 穩定版

  • 使用 wp 版本

檢索物件

使用 wp_remote_post 呼叫 http://api.wordpress.org/plugins/info/1.0/,傳遞一系列引數,包括’query_plugins’ 和 wp dot org 用來檢索收藏夾的操作。

$request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'timeout' => 15, 'body' => array('action' => $action, 'request' => serialize($args))) );

在你有一個漂亮的乾淨的物件之前,你需要做一些錯誤處理和其他解析。這是一個示例函式,它將返回一個包含所有外掛細節的漂亮乾淨物件。

function api( $action, $args ) {
        if ( is_array( $args ) )
            $args = (object) $args;

        $request = wp_remote_post('http://api.wordpress.org/plugins/info/1.0/', array( 'timeout' => 15, 'body' => array('action' => $action, 'request' => serialize($args))) );
        if ( is_wp_error($request) ) {
            $res = new WP_Error('plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), $request->get_error_message() );
        } else {
            $res = maybe_unserialize( wp_remote_retrieve_body( $request ) );
            if ( ! is_object( $res ) && ! is_array( $res ) )
                $res = new WP_Error('plugins_api_failed', __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="http://wordpress.org/support/">support forums</a>.' ), wp_remote_retrieve_body( $request ) );
        }

        return apply_filters( 'c3m_favorite_results', $res, $action, $args );
    }

Usage

此示例用法將為您提供一個無序的喜愛的外掛列表,以及點元件上的外掛連結,作者 uri 的連結和星級。

$api_data = api( 'query_plugins', array( 'user' => 'my_dot_org_username' ) );
$api_plugins = $api_data->plugins;

echo '<ul class="c3m-favorites">';
        foreach( $api_plugins as $plugin ) {

            $name = $plugin->name; ?>
            <li><strong><a target="_blank" href="http://wordpress.org/extend/plugins/<?php%20echo%20$plugin->slug%20?>/"><?php echo esc_html( $name ); ?></a></strong><br>

                <div class="star-holder" title="<?php printf( _n( '(based on %s rating)', '(based on %s ratings)', $plugin->num_ratings ), number_format_i18n( $plugin->num_ratings ) ); ?>">
                <div class="star star-rating" style="width: <?php echo esc_attr( str_replace( ',', '.', $plugin->rating ) ); ?>px"></div></div>

                <em><?php _e('By: ') ?></em> <?php echo links_add_target( $plugin->author, '_blank' ). '<br>'; ?>
            </li><?php
        }
        echo '</ul>';

Result

Widget 螢幕截圖從我最喜愛的外掛小工具外掛:http://wordpress.org/extend/plugins/favorite-plugins-widget/

次佳解決方案

還沒。

奧託週三表示,’soon’ 。但是他週末去燒烤,所以’soon’ 可能是’This month.’;)

編輯:

Otto42:@Ipstenu @EricMann 我有程式碼來做,但尚未部署。一些爭論最好的方法。最終會在那裡。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。