繁体   English   中英

如何将XML-RPC API的更多选项列入白名单?

[英]How do I whitelist more options for the XML-RPC API?

在将REST API端点添加到WordPress核心之前,我需要一种方法以编程方式更新XML-RPC API默认不包含的选项。 看起来这些选项是通过以下方法添加到文件“ wp-includes / class-wp-xmlrpc-server.php”中的:

public function initialise_blog_option_info() {
    global $wp_version;

    $this->blog_options = array(
        // Read only options
        'software_name'     => array(
            'desc'          => __( 'Software Name' ),
            'readonly'      => true,
            'value'         => 'WordPress'
        ),
        'software_version'  => array(
            'desc'          => __( 'Software Version' ),
            'readonly'      => true,
            'value'         => $wp_version
        ),
        'blog_url'          => array(
            'desc'          => __( 'WordPress Address (URL)' ),
            'readonly'      => true,
            'option'        => 'siteurl'
        ),
        'home_url'          => array(
            'desc'          => __( 'Site Address (URL)' ),
            'readonly'      => true,
            'option'        => 'home'
        ),
        'login_url'          => array(
            'desc'          => __( 'Login Address (URL)' ),
            'readonly'      => true,
            'value'         => wp_login_url( )
        ),
        'admin_url'          => array(
            'desc'          => __( 'The URL to the admin area' ),
            'readonly'      => true,
            'value'         => get_admin_url( )
        ),
        'image_default_link_type' => array(
            'desc'          => __( 'Image default link type' ),
            'readonly'      => true,
            'option'        => 'image_default_link_type'
        ),
        'image_default_size' => array(
            'desc'          => __( 'Image default size' ),
            'readonly'      => true,
            'option'        => 'image_default_size'
        ),
        'image_default_align' => array(
            'desc'          => __( 'Image default align' ),
            'readonly'      => true,
            'option'        => 'image_default_align'
        ),
        'template'          => array(
            'desc'          => __( 'Template' ),
            'readonly'      => true,
            'option'        => 'template'
        ),
        'stylesheet'        => array(
            'desc'          => __( 'Stylesheet' ),
            'readonly'      => true,
            'option'        => 'stylesheet'
        ),
        'post_thumbnail'    => array(
            'desc'          => __('Post Thumbnail'),
            'readonly'      => true,
            'value'         => current_theme_supports( 'post-thumbnails' )
        ),

        // Updatable options
        'time_zone'         => array(
            'desc'          => __( 'Time Zone' ),
            'readonly'      => false,
            'option'        => 'gmt_offset'
        ),
        'blog_title'        => array(
            'desc'          => __( 'Site Title' ),
            'readonly'      => false,
            'option'        => 'blogname'
        ),
        'blog_tagline'      => array(
            'desc'          => __( 'Site Tagline' ),
            'readonly'      => false,
            'option'        => 'blogdescription'
        ),
        'date_format'       => array(
            'desc'          => __( 'Date Format' ),
            'readonly'      => false,
            'option'        => 'date_format'
        ),
        'time_format'       => array(
            'desc'          => __( 'Time Format' ),
            'readonly'      => false,
            'option'        => 'time_format'
        ),
        'users_can_register' => array(
            'desc'          => __( 'Allow new users to sign up' ),
            'readonly'      => false,
            'option'        => 'users_can_register'
        ),
        'thumbnail_size_w'  => array(
            'desc'          => __( 'Thumbnail Width' ),
            'readonly'      => false,
            'option'        => 'thumbnail_size_w'
        ),
        'thumbnail_size_h'  => array(
            'desc'          => __( 'Thumbnail Height' ),
            'readonly'      => false,
            'option'        => 'thumbnail_size_h'
        ),
        'thumbnail_crop'    => array(
            'desc'          => __( 'Crop thumbnail to exact dimensions' ),
            'readonly'      => false,
            'option'        => 'thumbnail_crop'
        ),
        'medium_size_w'     => array(
            'desc'          => __( 'Medium size image width' ),
            'readonly'      => false,
            'option'        => 'medium_size_w'
        ),
        'medium_size_h'     => array(
            'desc'          => __( 'Medium size image height' ),
            'readonly'      => false,
            'option'        => 'medium_size_h'
        ),
        'medium_large_size_w'   => array(
            'desc'          => __( 'Medium-Large size image width' ),
            'readonly'      => false,
            'option'        => 'medium_large_size_w'
        ),
        'medium_large_size_h'   => array(
            'desc'          => __( 'Medium-Large size image height' ),
            'readonly'      => false,
            'option'        => 'medium_large_size_h'
        ),
        'large_size_w'      => array(
            'desc'          => __( 'Large size image width' ),
            'readonly'      => false,
            'option'        => 'large_size_w'
        ),
        'large_size_h'      => array(
            'desc'          => __( 'Large size image height' ),
            'readonly'      => false,
            'option'        => 'large_size_h'
        ),
        'default_comment_status' => array(
            'desc'          => __( 'Allow people to post comments on new articles' ),
            'readonly'      => false,
            'option'        => 'default_comment_status'
        ),
        'default_ping_status' => array(
            'desc'          => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new articles' ),
            'readonly'      => false,
            'option'        => 'default_ping_status'
        )
    );

    /**
     * Filter the XML-RPC blog options property.
     *
     * @since 2.6.0
     *
     * @param array $blog_options An array of XML-RPC blog options.
     */
    $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
}

这是我尝试编辑选项以允许更新我的functions.php文件中的blog_url,home_url和WPCF7选项的尝试失败(原始函数中的site_url和home_url为只读):

public function blog_option_info() {
    global $wp_version;

    $this->blog_options = array(
        // Updatable options
        'blog_url'          => array(
            'desc'          => __( 'WordPress Address (URL)' ),
            'readonly'      => false,
            'option'        => 'siteurl'
        ),
        'home_url'          => array(
            'desc'          => __( 'Site Address (URL)' ),
            'readonly'      => false,
            'option'        => 'home'
        ),
        'wpcf7'             => array(
            'desc'          => __( 'Contact Form 7 Options' ),
            'readonly'      => false,
            'option'        => 'wpcf7'
        ),
    );
    $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );  
}

将更多选项列入白名单的正确方法是什么,以便我可以通过API获取和更新它们?

如果要通过名称为NAME_OF_SOME_OPTION的选项通过XML-RPC添加对读/写选项的支持,请将以下代码添加到functions.php中:

    add_filter('xmlrpc_blog_options', function($blog_options)
    {
        $blog_options['NAME_OF_SOME_OPTION'] = array(
            'desc'     => 'DESCRITPTION OF THE OPTION',
            'readonly' => false,
            'option'   => 'NAME_OF_SOME_OPTION'
        );
        return $blog_options;
    });

如果您希望能够通过XML-RPC更新选项blog_url,请编写以下内容:

    add_filter('xmlrpc_blog_options', function($blog_options)
    {
        $blog_options['blog_url']['readonly'] = false;
        return $blog_options;
    });

我本人只是碰到过这个问题,而且绝对没有提到如何在线进行。 您的帖子指出,在$ this-> blog_options中定义了列入白名单的选项确实有所帮助,但是非常感谢! 这是代码(在自定义插件中):

<?php
include_once(ABSPATH . WPINC . '/class-IXR.php');
include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php');

class LSEOP_XmlRpc extends wp_xmlrpc_server {

    private $version = 1.0;

    public function __construct() {
        parent::__construct();

        $this->blog_options['permalink_structure'] = [
            'desc'          => __( 'Permalink Structure' ),
            'readonly'      => false,
            'option'        => 'permalink_structure'
        ];
    }

    public static function lseop_getName() {
        return __CLASS__;
    }

}

add_filter('wp_xmlrpc_server_class', array('LSEOP_XmlRpc', 'lseop_getName'));
?>

插件中还有很多其他代码,因为我还添加了XMLRPC方法,但这应该是您所需要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM