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. * Create REST routes.
*/ */
public static function create_routes() { public static function create_routes() {
if ( ! class_exists( 'WP_REST_Posts_Controller' ) ) return;
if ( ! class_exists( 'SP_REST_Posts_Controller' ) ) { if ( ! class_exists( 'SP_REST_Posts_Controller' ) ) {
require_once dirname( __FILE__ ) . '/class-sp-rest-posts-controller.php'; require_once dirname( __FILE__ ) . '/class-sp-rest-posts-controller.php';
} }
$controller = new SP_REST_Posts_Controller( 'sp_event' ); if ( ! class_exists( 'SP_REST_Terms_Controller' ) ) {
$controller->register_routes(); require_once dirname( __FILE__ ) . '/class-sp-rest-terms-controller.php';
}
$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();
do_action( 'sportspress_create_rest_routes' ); do_action( 'sportspress_create_rest_routes' );
} }

View File

@@ -1,64 +1,8 @@
<?php <?php
class SP_REST_Posts_Controller extends WP_REST_Posts_Controller { class SP_REST_Posts_Controller extends WP_REST_Posts_Controller {
protected $post_type;
public function __construct( $post_type ) { public function __construct( $post_type ) {
$this->post_type = $post_type; parent::__construct( $post_type );
$this->namespace = 'sportspress/v2'; $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';
}
}

View File

@@ -60,7 +60,7 @@ class SP_Post_types {
'hierarchical' => true, 'hierarchical' => true,
'rewrite' => array( 'slug' => get_option( 'sportspress_league_slug', 'league' ) ), 'rewrite' => array( 'slug' => get_option( 'sportspress_league_slug', 'league' ) ),
'show_in_rest' => true, 'show_in_rest' => true,
'rest_controller_class' => 'SP_REST_Posts_Controller', 'rest_controller_class' => 'SP_REST_Terms_Controller',
'rest_base' => 'leagues', 'rest_base' => 'leagues',
) ); ) );
$object_types = apply_filters( 'sportspress_league_object_types', array( 'sp_event', 'sp_calendar', 'sp_team', 'sp_table', 'sp_player', 'sp_list', 'sp_staff' ) ); $object_types = apply_filters( 'sportspress_league_object_types', array( 'sp_event', 'sp_calendar', 'sp_team', 'sp_table', 'sp_player', 'sp_list', 'sp_staff' ) );
@@ -96,7 +96,7 @@ class SP_Post_types {
'hierarchical' => true, 'hierarchical' => true,
'rewrite' => array( 'slug' => get_option( 'sportspress_season_slug', 'season' ) ), 'rewrite' => array( 'slug' => get_option( 'sportspress_season_slug', 'season' ) ),
'show_in_rest' => true, 'show_in_rest' => true,
'rest_controller_class' => 'SP_REST_Posts_Controller', 'rest_controller_class' => 'SP_REST_Terms_Controller',
'rest_base' => 'seasons', 'rest_base' => 'seasons',
) ); ) );
$object_types = apply_filters( 'sportspress_season_object_types', array( 'sp_event', 'sp_calendar', 'sp_team', 'sp_table', 'sp_player', 'sp_list', 'sp_staff' ) ); $object_types = apply_filters( 'sportspress_season_object_types', array( 'sp_event', 'sp_calendar', 'sp_team', 'sp_table', 'sp_player', 'sp_list', 'sp_staff' ) );
@@ -132,7 +132,7 @@ class SP_Post_types {
'hierarchical' => true, 'hierarchical' => true,
'rewrite' => array( 'slug' => get_option( 'sportspress_venue_slug', 'venue' ) ), 'rewrite' => array( 'slug' => get_option( 'sportspress_venue_slug', 'venue' ) ),
'show_in_rest' => true, 'show_in_rest' => true,
'rest_controller_class' => 'SP_REST_Posts_Controller', 'rest_controller_class' => 'SP_REST_Terms_Controller',
'rest_base' => 'venues', 'rest_base' => 'venues',
) ); ) );
$object_types = apply_filters( 'sportspress_event_object_types', array( 'sp_event', 'sp_calendar', 'sp_team' ) ); $object_types = apply_filters( 'sportspress_event_object_types', array( 'sp_event', 'sp_calendar', 'sp_team' ) );
@@ -168,7 +168,7 @@ class SP_Post_types {
'hierarchical' => true, 'hierarchical' => true,
'rewrite' => array( 'slug' => get_option( 'sportspress_position_slug', 'position' ) ), 'rewrite' => array( 'slug' => get_option( 'sportspress_position_slug', 'position' ) ),
'show_in_rest' => true, 'show_in_rest' => true,
'rest_controller_class' => 'SP_REST_Posts_Controller', 'rest_controller_class' => 'SP_REST_Terms_Controller',
'rest_base' => 'positions', 'rest_base' => 'positions',
) ); ) );
$object_types = apply_filters( 'sportspress_position_object_types', array( 'sp_player', 'sp_list' ) ); $object_types = apply_filters( 'sportspress_position_object_types', array( 'sp_player', 'sp_list' ) );
@@ -204,7 +204,7 @@ class SP_Post_types {
'hierarchical' => true, 'hierarchical' => true,
'rewrite' => array( 'slug' => get_option( 'sportspress_role_slug', 'role' ) ), 'rewrite' => array( 'slug' => get_option( 'sportspress_role_slug', 'role' ) ),
'show_in_rest' => true, 'show_in_rest' => true,
'rest_controller_class' => 'SP_REST_Posts_Controller', 'rest_controller_class' => 'SP_REST_Terms_Controller',
'rest_base' => 'roles', 'rest_base' => 'roles',
) ); ) );
$object_types = apply_filters( 'sportspress_role_object_types', array( 'sp_staff' ) ); $object_types = apply_filters( 'sportspress_role_object_types', array( 'sp_staff' ) );