diff --git a/modules/sportspress-player-lists.php b/modules/sportspress-player-lists.php index 4012587f..e2750bec 100644 --- a/modules/sportspress-player-lists.php +++ b/modules/sportspress-player-lists.php @@ -33,6 +33,8 @@ class SportsPress_Player_Lists { 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' ) ); @@ -86,6 +88,9 @@ class SportsPress_Player_Lists { 'show_in_nav_menus' => true, 'show_in_menu' => 'edit.php?post_type=sp_player', 'show_in_admin_bar' => true, + 'show_in_rest' => true, + 'rest_controller_class' => 'SP_REST_Posts_Controller', + 'rest_base' => 'lists', ) ) ); @@ -117,6 +122,51 @@ class SportsPress_Player_Lists { include_once( SP()->plugin_path() . '/includes/widgets/class-sp-widget-player-gallery.php' ); } + /** + * Create REST API routes. + */ + public function create_rest_routes() { + $controller = new SP_REST_Posts_Controller( 'sp_list' ); + $controller->register_routes(); + } + + /** + * Register REST API fields. + */ + public function register_rest_fields() { + register_rest_field( 'sp_list', + '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_list', + 'data', + array( + 'get_callback' => 'SP_REST_API::get_post_data', + 'update_callback' => 'SP_REST_API::update_post_meta', + 'schema' => array( + 'description' => __( 'Player List', 'sportspress' ), + 'type' => 'array', + 'context' => array( 'view', 'edit', 'embed' ), + 'arg_options' => array( + 'sanitize_callback' => 'rest_sanitize_request_arg', + ), + ), + ) + ); + } + /** * Add meta boxes. * @@ -266,4 +316,10 @@ endif; if ( get_option( 'sportspress_load_player_lists_module', 'yes' ) == 'yes' ) { new SportsPress_Player_Lists(); + + /** + * Create alias of SP_Player_List class for REST API. + * Note: class_alias is not supported in PHP < 5.3 so extend the original class instead. + */ + class SP_List extends SP_Player_List {} }