/** * Registers the controllers routes * * @return null */ public function register_routes() { // lists all widgets register_rest_route(Sidebars::ENDPOINT_NAMESPACE, '/widgets', [['methods' => WP_REST_Server::READABLE, 'callback' => [$this, 'get_items']]]); // lists a single widget based on the base id register_rest_route(Sidebars::ENDPOINT_NAMESPACE, '/widgets/(?P<id_base>[\\w-]+)', [['methods' => WP_REST_Server::READABLE, 'callback' => [$this, 'get_item'], 'args' => ['id_base' => ['description' => 'The base id of a registered widget', 'type' => 'string', 'validate_callback' => function ($id_base) { return !is_null(self::get_widget($id_base)); }]]], ['methods' => WP_REST_Server::CREATABLE, 'callback' => [$this, 'create_item'], 'args' => ['sidebar' => ['description' => 'The id of the sidebar to add the widget to', 'type' => 'string', 'validate_callback' => function ($sidebar_id) { return !is_null(Sidebars_Controller::get_sidebar($sidebar_id)); }]]]]); register_rest_route(Sidebars::ENDPOINT_NAMESPACE, '/widgets/(?P<id_base>[\\w-]+)/(?P<instance_id>[\\w-]+)', [['methods' => WP_REST_Server::READABLE, 'callback' => [$this, 'get_item_instance'], 'args' => ['id_base' => ['description' => 'The base id of a registered widget', 'type' => 'string', 'validate_callback' => function ($id_base) { return !is_null(self::get_widget($id_base)); }], 'instance_id' => ['description' => 'The instance id of a widget', 'type' => 'string', 'validate_callback' => function ($instance_id) { return !is_null(self::get_widget_instance($instance_id)); }]]]]); }
/** * Runs at the "plugins_loaded" hook * * @return null */ public function load() { add_action('rest_api_init', function () { // instantiate controllers $sidebars_controller = new Controllers\Sidebars_Controller(); $widgets_controller = new Controllers\Widgets_Controller(); // register controller routes $sidebars_controller->register_routes(); $widgets_controller->register_routes(); }); }