From 9697f67314848923591a805b7bb24837a87697b4 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Mon, 11 Apr 2016 01:51:02 +1000 Subject: [PATCH] add rest routes for calendars --- modules/sportspress-calendars.php | 53 +++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/modules/sportspress-calendars.php b/modules/sportspress-calendars.php index 9eafe38f..7ff1be96 100644 --- a/modules/sportspress-calendars.php +++ b/modules/sportspress-calendars.php @@ -33,6 +33,8 @@ class SportsPress_Calendars { add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ), 10 ); add_action( 'sportspress_include_post_type_handlers', array( $this, 'include_post_type_handler' ) ); add_action( 'sportspress_widgets', array( $this, 'include_widgets' ) ); + add_action( 'sportspress_create_rest_routes', array( $this, 'create_rest_routes' ) ); + add_action( 'sportspress_register_rest_fields', array( $this, 'register_rest_fields' ) ); // Filters add_filter( 'sportspress_meta_boxes', array( $this, 'add_meta_boxes' ) ); @@ -85,8 +87,11 @@ class SportsPress_Calendars { 'supports' => array( 'title', 'editor', 'author', 'thumbnail' ), 'has_archive' => false, 'show_in_nav_menus' => true, - 'show_in_menu' => 'edit.php?post_type=sp_event', + 'show_in_menu' => 'edit.php?post_type=sp_event', 'show_in_admin_bar' => true, + 'show_in_rest' => true, + 'rest_controller_class' => 'SP_REST_Posts_Controller', + 'rest_base' => 'calendars', ) ) ); @@ -110,8 +115,6 @@ class SportsPress_Calendars { /** * Add widgets. - * - * @return array */ public function include_widgets() { include_once( SP()->plugin_path() . '/includes/widgets/class-sp-widget-event-calendar.php' ); @@ -119,6 +122,50 @@ class SportsPress_Calendars { include_once( SP()->plugin_path() . '/includes/widgets/class-sp-widget-event-blocks.php' ); } + /** + * Create REST API routes. + */ + public function create_rest_routes() { + $controller = new SP_REST_Posts_Controller( 'sp_calendar' ); + $controller->register_routes(); + } + + /** + * Register REST API fields. + */ + public function register_rest_fields() { + register_rest_field( 'sp_calendar', + 'format', + array( + 'get_callback' => 'SP_REST_API::get_post_meta', + 'update_callback' => 'SP_REST_API::update_post_meta', + 'schema' => array( + 'description' => __( 'Layout', 'sportspress' ), + 'type' => 'array', + 'context' => array( 'view', 'edit', 'embed' ), + 'arg_options' => array( + 'sanitize_callback' => 'rest_sanitize_request_arg', + ), + ), + ) + ); + + register_rest_field( 'sp_calendar', + 'data', + array( + 'get_callback' => 'SP_REST_API::get_post_data', + 'schema' => array( + 'description' => __( 'Events', 'sportspress' ), + 'type' => 'array', + 'context' => array( 'view', 'embed' ), + 'arg_options' => array( + 'sanitize_callback' => 'rest_sanitize_request_arg', + ), + ), + ) + ); + } + /** * Add meta boxes to calendars. *