使用 XML-RPC 阅读 WordPress 博客

发布于 2024-12-03 20:25:14 字数 509 浏览 0 评论 0原文

无论我在哪里寻找,我都找不到将我的应用程序完全连接到 Wordpress 的方法。我想做的是:在您未登录时显示最近的文章(当然是已发布的)和较早的文章,并方便为登录的用户撰写评论。

看起来我必须从 RSS 中提取博客文章(但是“阅读更多...”下面的文本怎么样?),然后使用 XML-RPC* 对文章发表评论。是这样吗,或者谁有更好的解决方案? (*metaWeblog.getRecentPosts 在 WP 上并不总是可用)

对于相关博客的实现我没有太多要说的,尽管我可以要求所有者安装一些插件。例如WP-RESTful

请为我指明正确的方向,因为我花了几天时间寻找一个应该很简单的解决方案,但似乎并非如此。谢谢!

No matter where I look, I just can't find a way to fully connect my app to Wordpress. What I'd like to do is: show recent articles (published, of course) and older ones when you're not logged in and facilitate writing comments for users that are.

It just looks like I have to pull the blog articles from RSS (but what about the text below 'Read More...'?), and then use XML-RPC* to comment on the articles. Is that the way, or does anyone have a better solution? (*metaWeblog.getRecentPosts isn't always available on WP)

I don't have a lot to say about the implementation of the blog in question, though I could ask the owner to install some plugins. Such as WP-RESTful.

Please point me in the right direction to look, because I've spent days searching for a solution that should be simple but just doesn't seem to be that way. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2024-12-10 20:25:14

WordPress 移动团队有一个用于他们使用的 XML-RPC 解析器的存储库。它的实施和使用相当简单。它由 XMLRPCDecodeXMLRPCEncoder 组成。

https://github.com/wordpress-mobile/wpxmlrpc

编辑

WordPress 中的 XML-RPC 旨在用于发布。获取帖子的功能非常有限,因为 WordPress 实现在 wp.getPosts 方法中使用了 wp_get_recent_posts()。我通过采用 class-wp-xmlrpc-server.php 中的 wp_getPosts 方法并将 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');

The WordPress mobile team has a repository for the XML-RPC parser they use. It's fairly simple to implement and use. It consists of a XMLRPCDecode and XMLRPCEncoder.

https://github.com/wordpress-mobile/wpxmlrpc

EDIT

XML-RPC in WordPress is intended for posting. The functionalities for getting posts are quite limited because the WordPress implementation uses wp_get_recent_posts() in the wp.getPosts method. I made the function better by adopting the wp_getPosts method in class-wp-xmlrpc-server.php and replacing wp_get_recent_posts by get_posts. So I can use all parameters like in Wp_Query class.

The xmlrpc_method filter is then used to override the built in methods. I could post the code, if you want.

EDIT II

Nevermind, I'll post the code... :)

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');
中性美 2024-12-10 20:25:14

我认为 xmlrpc 是可行的方法,例如 WP 自己的应用程序就是这样连接的。为什么不使用它来获取帖子,为什么使用 RSS 呢?

I think xmlrpc is the way to go, that's the way WP's own app connects for instance. Why don't you use it to get the posts as well, why use RSS for that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文