diff --git a/includes/api/class-sp-rest-api.php b/includes/api/class-sp-rest-api.php index f184f5e1..24afe273 100644 --- a/includes/api/class-sp-rest-api.php +++ b/includes/api/class-sp-rest-api.php @@ -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' ); } diff --git a/includes/api/class-sp-rest-posts-controller.php b/includes/api/class-sp-rest-posts-controller.php index 20985eb1..8a6bb60a 100644 --- a/includes/api/class-sp-rest-posts-controller.php +++ b/includes/api/class-sp-rest-posts-controller.php @@ -1,64 +1,8 @@ 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[\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' ), - ) ); - } - } diff --git a/includes/api/class-sp-rest-terms-controller.php b/includes/api/class-sp-rest-terms-controller.php new file mode 100644 index 00000000..c9f15861 --- /dev/null +++ b/includes/api/class-sp-rest-terms-controller.php @@ -0,0 +1,8 @@ +namespace = 'sportspress/v2'; + } +} diff --git a/includes/class-sp-post-types.php b/includes/class-sp-post-types.php index 18c68cff..0f98e7e1 100644 --- a/includes/class-sp-post-types.php +++ b/includes/class-sp-post-types.php @@ -60,7 +60,7 @@ class SP_Post_types { 'hierarchical' => true, 'rewrite' => array( 'slug' => get_option( 'sportspress_league_slug', 'league' ) ), 'show_in_rest' => true, - 'rest_controller_class' => 'SP_REST_Posts_Controller', + 'rest_controller_class' => 'SP_REST_Terms_Controller', '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' ) ); @@ -96,7 +96,7 @@ class SP_Post_types { 'hierarchical' => true, 'rewrite' => array( 'slug' => get_option( 'sportspress_season_slug', 'season' ) ), 'show_in_rest' => true, - 'rest_controller_class' => 'SP_REST_Posts_Controller', + 'rest_controller_class' => 'SP_REST_Terms_Controller', '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' ) ); @@ -132,7 +132,7 @@ class SP_Post_types { 'hierarchical' => true, 'rewrite' => array( 'slug' => get_option( 'sportspress_venue_slug', 'venue' ) ), 'show_in_rest' => true, - 'rest_controller_class' => 'SP_REST_Posts_Controller', + 'rest_controller_class' => 'SP_REST_Terms_Controller', 'rest_base' => 'venues', ) ); $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, 'rewrite' => array( 'slug' => get_option( 'sportspress_position_slug', 'position' ) ), 'show_in_rest' => true, - 'rest_controller_class' => 'SP_REST_Posts_Controller', + 'rest_controller_class' => 'SP_REST_Terms_Controller', 'rest_base' => 'positions', ) ); $object_types = apply_filters( 'sportspress_position_object_types', array( 'sp_player', 'sp_list' ) ); @@ -204,7 +204,7 @@ class SP_Post_types { 'hierarchical' => true, 'rewrite' => array( 'slug' => get_option( 'sportspress_role_slug', 'role' ) ), 'show_in_rest' => true, - 'rest_controller_class' => 'SP_REST_Posts_Controller', + 'rest_controller_class' => 'SP_REST_Terms_Controller', 'rest_base' => 'roles', ) ); $object_types = apply_filters( 'sportspress_role_object_types', array( 'sp_staff' ) );