問題描述
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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
