Add taxonomies to REST API

This commit is contained in:
Brian Miyaji
2016-12-16 15:57:42 +11:00
parent 2aca097f5e
commit 39a3f12ff1
4 changed files with 17 additions and 74 deletions

View File

@@ -41,23 +41,14 @@ class SP_REST_API {
* Create REST routes.
*/
public static function create_routes() {
if ( ! class_exists( 'WP_REST_Posts_Controller' ) ) return;
if ( ! class_exists( 'SP_REST_Posts_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-sp-rest-posts-controller.php';
}
$controller = new SP_REST_Posts_Controller( 'sp_event' );
$controller->register_routes();
$controller = new SP_REST_Posts_Controller( 'sp_team' );
$controller->register_routes();
$controller = new SP_REST_Posts_Controller( 'sp_player' );
$controller->register_routes();
$controller = new SP_REST_Posts_Controller( 'sp_staff' );
$controller->register_routes();
if ( ! class_exists( 'SP_REST_Terms_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-sp-rest-terms-controller.php';
}
do_action( 'sportspress_create_rest_routes' );
}

View File

@@ -1,64 +1,8 @@
<?php
class SP_REST_Posts_Controller extends WP_REST_Posts_Controller {
protected $post_type;
public function __construct( $post_type ) {
$this->post_type = $post_type;
parent::__construct( $post_type );
$this->namespace = 'sportspress/v2';
$obj = get_post_type_object( $post_type );
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
}
/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
register_rest_route( $this->namespace, '/' . $this->rest_base, array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
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( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'force' => array(
'default' => false,
'description' => __( 'Whether to bypass trash and force deletion.' ),
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
}
}

View File

@@ -0,0 +1,8 @@
<?php
class SP_REST_Terms_Controller extends WP_REST_Terms_Controller {
public function __construct( $taxonomy ) {
parent::__construct( $taxonomy );
$this->namespace = 'sportspress/v2';
}
}