WordPress REST API插件开发 - 添加自定义端点

时间:2015-09-22 22:25:50

标签: php rest wordpress

我几乎肯定做了一些愚蠢的事 - 或者说不做我应该做的事情。我正在编写一个插件来通过http POST公开一个函数,所以我可以将JSON发送到我的wordpress应用程序并将其存储在数据库中。我正在使用WP REST API插件并遵循“添加自定义端点”指南http://v2.wp-api.org/extending/adding/

我的插件如下: -

# Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

require_once(ABSPATH . 'wp-content/plugins/rest-api/plugin.php');
require_once(dirname(__FILE__) . '/classes/myPlugin.php');
require_once(dirname(__FILE__) . '/classes/myController.php');

// Action hook to initialize the plugin
add_action('rest_api_init', array('myPlugin_Class', 'init' ));

register_activation_hook(__FILE__, array('myPlugin_Class', 'on_activation'));
register_deactivation_hook(__FILE__, array('myPlugin_Class', 'on_deactivation'));
register_uninstall_hook(__FILE__, array('myPlugin_Class', 'on_uninstall'));

myPlugin_Class只是主要插件位的封装...它包含对静态类myController的引用,它扩展了WP_REST_Controller。插件类摘录及其init方法和构造函数如下: -

class MyPlugin_Class
{
    private static $instance;
    private static $myController = null;

    public static function init()
    {
        if(self::$instance == null) {
           self::$instance = new MyPlugin_Class();
        }
        return self::$instance;
    }

    private function __construct()
    {
        Static::$myController = MyController::init();
        Static::$myController->register_routes();
        flush_rewrite_rules();
    }

    public static function on_activation()
    {
        Static::init();

        if ( ! current_user_can( 'activate_plugins' ) )
            return;
        $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
        check_admin_referer( "activate-plugin_{$plugin}" );

        //Register routes and don't forget to flush
        $this->myController->register_routes();
        flush_rewrite_rules();
    }
}

Controller类摘录如下: -

class MyController extends WP_REST_Controller {

    private static $instance;
    private $namespace = 'api/vendor/v1';
    private $base = 'default';

    private function __construct(){
    }

    public static function init() {
        if(self::$instance == null) {
            self::$instance = new MyController();
        }
        return self::$instance;
    }

    /**
    * Register the routes for the objects of the controller.
    */
    public function register_routes() {
        $base = $this->default-base;
        register_rest_route( $this->namespace, '/' . $base, array(
            array(
                'methods'         => WP_REST_Server::READABLE,
                'callback'        => array( $this, 'get_items' ),
                'permission_callback' => array( $this, 'get_items_permissions_check' ),
                'args'            => array(

                ),
            ),
            array(
                'methods'         => WP_REST_Server::CREATABLE,
                'callback'        => array( $this, 'create_item' ),
                'permission_callback' => array( $this, 'create_item_permissions_check' ),
                'args'            => $this->get_endpoint_args_for_item_schema( true ),
            ),
        ) );
    }
}

我无法获得注册路线。激活后,在对register_rest_route的调用中,我收到错误: -

Fatal error: Call to a member function register_route() on null in .../wp-content/plugins/rest-api/plugin.php on line 92

REST API插件中的函数register_route()

function register_rest_route( $namespace, $route, $args = array(), $override = false ) {

    /** @var WP_REST_Server $wp_rest_server */
    global $wp_rest_server;

    if ( isset( $args['callback'] ) ) {
        // Upgrade a single set to multiple
        $args = array( $args );
    }

    $defaults = array(
        'methods'         => 'GET',
        'callback'        => null,
        'args'            => array(),
    );
    foreach ( $args as $key => &$arg_group ) {
        if ( ! is_numeric( $arg_group ) ) {
            // Route option, skip here
            continue;
        }

        $arg_group = array_merge( $defaults, $arg_group );
    }

    if ( $namespace ) {
        $full_route = '/' . trim( $namespace, '/' ) . '/' . trim( $route, '/' );
    } else {
        // Non-namespaced routes are not allowed, with the exception of the main
        // and namespace indexes. If you really need to register a
        // non-namespaced route, call `WP_REST_Server::register_route` directly.
        _doing_it_wrong( 'register_rest_route', 'Routes must be namespaced with plugin name and version', 'WPAPI-2.0' );

        $full_route = '/' . trim( $route, '/' );
    }

    $wp_rest_server->register_route( $namespace, $full_route, $args, $override );
}

这个全局$ wp_rest_server在哪里创建(或者我的情况下没有创建)?

即。我忘记做了什么?

加分问题:

我应该在rest_api_init挂钩,register_activation_hook或两者中调用flush_rewrite_rules()吗?

1 个答案:

答案 0 :(得分:0)

您对add_action('rest_api_init', array('myPlugin_Class', 'init' ));的来电很晚,而且在插件激活挂钩之前

register_activation_hook(__FILE__, array('myPlugin_Class', 'on_activation'));

正在接受调用。因此无效。