无论我在哪里,我都找不到将我的应用程序完全连接到Wordpress的方法。我想做的是:当你没有登录时显示最近的文章(当然是已发布的)和较旧的文章,并为那些用户撰写评论提供便利。
看起来我必须从RSS中提取博客文章(但是下面的文字“阅读更多...”?),然后使用XML-RPC *来评论这些文章。是这样的,还是有人有更好的解决方案? (* metaWeblog.getRecentPosts并不总是在WP上可用)
虽然我可以要求所有者安装一些插件,但我对相关博客的实施没有太多要说的。例如WP-RESTful。
请指出正确的方向,因为我花了几天时间寻找一个简单的解决方案,但似乎并不是那样。谢谢!
答案 0 :(得分:2)
WordPress移动团队拥有他们使用的XML-RPC解析器的存储库。它的实现和使用相当简单。它由XMLRPCDecode
和XMLRPCEncoder
组成。
https://github.com/wordpress-mobile/wpxmlrpc
修改强>
WordPress中的 XML-RPC
用于发布。获取帖子的功能非常有限,因为WordPress实现在wp_get_recent_posts()
方法中使用wp.getPosts
。我在wp_getPosts
中采用class-wp-xmlrpc-server.php
方法并将wp_get_recent_posts
替换为get_posts
,从而使功能更加出色。所以我可以使用Wp_Query
类中的所有参数。
然后使用xmlrpc_method
过滤器覆盖内置方法。如果你愿意,我可以发布代码。
编辑II
没关系,我会发布代码......:)
include_once(ABSPATH . 'wp-admin/includes/admin.php');
include_once(ABSPATH . WPINC . '/post.php');
include_once(ABSPATH . WPINC . '/class-IXR.php');
include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php');
class jw_xmlrpc_server extends wp_xmlrpc_server {
function __construct($wp_xmlrpc_server) {
parent::__construct();
foreach(get_object_vars($wp_xmlrpc_server) as $key => $val) {
$wp_xmlrpc_server->key = $val;
}
}
public function minimum_args($args, $count) {
return parent::minimum_args($args, $count);
}
public function _prepare_post($post, $fields) {
return parent::_prepare_post($post, $fields);
}
}
class WP_Post_Converter {
public function to_array($post) {
$array = array();
foreach(get_object_vars($post) as $key => $val) {
$array[$key] = $val;
}
return $array;
}
}
function wp_getPosts($args) {
global $wp_xmlrpc_server;
$wp_server_save = $wp_xmlrpc_server;
$wp_xmlrpc_server = new jw_xmlrpc_server($wp_xmlrpc_server);
if (!$wp_xmlrpc_server->minimum_args($args, 3)) {
return $wp_xmlrpc_server->error;
}
$wp_xmlrpc_server->escape($args);
$blog_id = (int) $args[0];
$username = $args[1];
$password = $args[2];
$filter = isset( $args[3] ) ? $args[3] : array();
if (isset($args[4])) {
$fields = $args[4];
}
else {
$fields = apply_filters('xmlrpc_default_post_fields', array('post', 'terms', 'custom_fields'), 'wp.getPosts');
}
if (!$user = $wp_xmlrpc_server->login($username, $password)) {
return $wp_xmlrpc_server->error;
}
do_action('xmlrpc_call', 'wp.getPosts');
if (isset($filter['post_type'])) {
$post_type = get_post_type_object($filter['post_type']);
if (!((bool)$post_type)) {
return new IXR_Error(403, __('The post type specified is not valid'));
}
}
else {
$post_type = get_post_type_object('post');
}
if (!current_user_can($post_type->cap->edit_posts)) {
return new IXR_Error(401, __('Sorry, you are not allowed to edit posts in this post type'));
}
$filter['post_type'] = $post_type->name;
// $posts_list = wp_get_recent_posts( $query );
$posts_list = get_posts($filter);
if (!$posts_list) {
return array();
}
// holds all the posts data
$struct = array();
foreach ($posts_list as $post) {
$post = WP_Post_Converter::to_array($post);
$post_type = get_post_type_object( $post['post_type'] );
if (!current_user_can($post_type->cap->edit_post, $post['ID'])) {
continue;
}
$struct[] = $wp_xmlrpc_server->_prepare_post($post, $fields);
}
$wp_xmlrpc_server = $wp_server_save;
return $struct;
}
function wp_get_categories($args) {
global $wp_xmlrpc_server;
$wp_server_save = $wp_xmlrpc_server;
$wp_xmlrpc_server = new jw_xmlrpc_server($wp_xmlrpc_server);
$wp_xmlrpc_server->escape($args);
$blog_ID = (int) $args[0];
$username = $args[1];
$password = $args[2];
if (!$user = $wp_xmlrpc_server->login($username, $password)) {
return $wp_xmlrpc_server->error;
}
if (!current_user_can('edit_posts')) {
return new IXR_Error(401, __('Sorry, you must be able to edit posts on this site in order to view categories.'));
}
do_action('xmlrpc_call', 'metaWeblog.getCategories');
$categories_struct = array();
if ($cats = get_categories(array('get' => 'all'))) {
foreach ($cats as $cat) {
$struct['categoryId'] = $cat->term_id;
$struct['parentId'] = $cat->parent;
$struct['postCount'] = $cat->count;
$struct['description'] = $cat->name;
$struct['categoryDescription'] = $cat->description;
$struct['categoryName'] = $cat->name;
$struct['htmlUrl'] = esc_html(get_category_link($cat->term_id));
$struct['rssUrl'] = esc_html(get_category_feed_link($cat->term_id, 'rss2'));
$categories_struct[] = $struct;
}
}
$wp_xmlrpc_server = $wp_server_save;
return $categories_struct;
}
function wp_method_setter($methods) {
$methods['wp.getPosts'] = 'wp_getPosts';
$methods['wp.getCategories'] = 'wp_get_categories';
return $methods;
}
add_filter('xmlrpc_methods', 'wp_method_setter');
答案 1 :(得分:1)
我认为xmlrpc是可行的方式,这就是WP自己的应用程序连接的方式。为什么不使用它来获取帖子,为什么要使用RSS?