Created
September 22, 2017 15:01
-
-
Save BE-Webdesign/42df5e6117659b7bb8b5066ab4f1c90a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Doc block comment. | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit( 'Silence is golden.' ); | |
} | |
/** | |
* REST API controller. | |
*/ | |
class My_Controller extends WP_REST_Controller { | |
// Here initialize our namespace and resource name. | |
public function __construct() { | |
$this->namespace = '/my-namespace/v1'; | |
$this->resource_name = 'my-resource'; | |
} | |
// Register our routes. | |
public function register_routes() { | |
register_rest_route( $this->namespace, '/' . $this->resource_name, array( | |
// Here we register the readable endpoint for collections. | |
array( | |
'methods' => 'GET', | |
'callback' => array( $this, 'get_items' ), | |
'permission_callback' => array( $this, 'get_items_permissions_check' ), | |
), | |
// Register our schema callback. | |
'schema' => array( $this, 'get_item_schema' ), | |
) ); | |
register_rest_route( $this->namespace, '/' . $this->resource_name . '/(?P<id>[\d]+)', array( | |
// Notice how we are registering multiple endpoints the 'schema' equates to an OPTIONS request. | |
array( | |
'methods' => 'GET', | |
'callback' => array( $this, 'get_item' ), | |
'permission_callback' => array( $this, 'get_item_permissions_check' ), | |
), | |
// Register our schema callback. | |
'schema' => array( $this, 'get_item_schema' ), | |
) ); | |
} | |
/** | |
* Check permissions for the items. | |
* | |
* @param WP_REST_Request $request Current request. | |
*/ | |
public function get_items_permissions_check( $request ) { | |
if ( ! current_user_can( 'read' ) ) { | |
return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the item resource.' ), array( 'status' => $this->authorization_status_code() ) ); | |
} | |
return true; | |
} | |
/** | |
* Grabs the five most recent items and outputs them as a rest response. | |
* | |
* @param WP_REST_Request $request Current request. | |
*/ | |
public function get_items( $request ) { | |
$args = array( | |
'item_per_page' => 5, | |
); | |
$items = get_items( $args ); | |
$data = array(); | |
if ( empty( $items ) ) { | |
return rest_ensure_response( $data ); | |
} | |
foreach ( $items as $item ) { | |
$response = $this->prepare_item_for_response( $item, $request ); | |
$data[] = $this->prepare_response_for_collection( $response ); | |
} | |
// Return all of our comment response data. | |
return rest_ensure_response( $data ); | |
} | |
/** | |
* Check permissions for the items. | |
* | |
* @param WP_REST_Request $request Current request. | |
*/ | |
public function get_item_permissions_check( $request ) { | |
if ( ! current_user_can( 'read' ) ) { | |
return new WP_Error( 'rest_forbidden', esc_html__( 'You cannot view the item resource.' ), array( 'status' => $this->authorization_status_code() ) ); | |
} | |
return true; | |
} | |
/** | |
* Grabs the five most recent items and outputs them as a rest response. | |
* | |
* @param WP_REST_Request $request Current request. | |
*/ | |
public function get_item( $request ) { | |
$id = (int) $request['id']; | |
$item = get_item( $id ); | |
if ( empty( $item ) ) { | |
return rest_ensure_response( array() ); | |
} | |
$response = prepare_item_for_response( $item ); | |
// Return all of our item response data. | |
return $response; | |
} | |
/** | |
* Matches the item data to the schema we want. | |
* | |
* @param WP_Post $item The comment object whose response is being prepared. | |
*/ | |
public function prepare_item_for_response( $item, $request ) { | |
$item_data = array(); | |
$schema = $this->get_item_schema( $request ); | |
// We are also renaming the fields to more understandable names. | |
if ( isset( $schema['properties']['id'] ) ) { | |
$item_data['id'] = (int) $item->ID; | |
} | |
if ( isset( $schema['properties']['content'] ) ) { | |
$item_data['content'] = apply_filters( 'the_content', $item->item_content, $item ); | |
} | |
return rest_ensure_response( $item_data ); | |
} | |
/** | |
* Prepare a response for inserting into a collection of responses. | |
* | |
* This is copied from WP_REST_Controller class in the WP REST API v2 plugin. | |
* | |
* @param WP_REST_Response $response Response object. | |
* @return array Response data, ready for insertion into collection data. | |
*/ | |
public function prepare_response_for_collection( $response ) { | |
if ( ! ( $response instanceof WP_REST_Response ) ) { | |
return $response; | |
} | |
$data = (array) $response->get_data(); | |
$server = rest_get_server(); | |
if ( method_exists( $server, 'get_compact_response_links' ) ) { | |
$links = call_user_func( array( $server, 'get_compact_response_links' ), $response ); | |
} else { | |
$links = call_user_func( array( $server, 'get_response_links' ), $response ); | |
} | |
if ( ! empty( $links ) ) { | |
$data['_links'] = $links; | |
} | |
return $data; | |
} | |
/** | |
* Get our sample schema for an item. | |
* | |
* @param WP_REST_Request $request Current request. | |
*/ | |
public function get_item_schema( $request ) { | |
$schema = array( | |
// This tells the spec of JSON Schema we are using which is draft 4. | |
'$schema' => 'http://json-schema.org/draft-04/schema#', | |
// The title property marks the identity of the resource. | |
'title' => 'item', | |
'type' => 'object', | |
// In JSON Schema you can specify object properties in the properties attribute. | |
'properties' => array( | |
'id' => array( | |
'description' => esc_html__( 'Unique identifier for the object.', 'my-textdomain' ), | |
'type' => 'integer', | |
'context' => array( 'view', 'edit', 'embed' ), | |
'readonly' => true, | |
), | |
'content' => array( | |
'description' => esc_html__( 'The content for the object.', 'my-textdomain' ), | |
'type' => 'string', | |
), | |
), | |
); | |
return $schema; | |
} | |
// Sets up the proper HTTP status code for authorization. | |
public function authorization_status_code() { | |
$status = 401; | |
if ( is_user_logged_in() ) { | |
$status = 403; | |
} | |
return $status; | |
} | |
} | |
// Function to register our new routes from the controller. | |
function prefix_register_my_rest_routes() { | |
$controller = new My_Controller(); | |
$controller->register_routes(); | |
} | |
add_action( 'rest_api_init', 'prefix_register_my_rest_routes' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment