This repository was archived by the owner on Sep 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 651
This repository was archived by the owner on Sep 24, 2018. It is now read-only.
Registering endpoint code API #551
Copy link
Copy link
Closed
Milestone
Description
During the wordPress core workshops we discussed the need for a more standard way to register endpoints. After a lot of discussion I put together an initial idea of how that code may look that settles in the middle of a lot of our ideas and disagreements.
This is by no means final, just a place to put it for discussion, so any opinion welcome :)
<?php
/**
* Register an API route for /comic but re-use the inbuilt
* callback for getting posts ( WP_JSON_API_Posts::get )
*/
register_api_route( 'jetpack', 1.0, '/comic', array(
'callback' => array( 'WP_JSON_API_Posts', 'get' ),
'methods' => array( 'GET' ),
'args' => array(
'page' => array(
'required' => false
),
'per_page' => array(
'required' => false
)
),
) );
/**
* Register an API route for /comic and use a closure as the callback
* Simple.
*/
register_api_route( 'jetpack', 1.0, '/comic', array(
'callback' => function( $args ) {
return array( array( 'id' => 1, 'name' => 'Hello' ) );
},
'methods' => array( 'GET' ),
'args' => array(
'page' => array(
'required' => false
),
'per_page' => array(
'required' => false
)
)
) );
/**
* Register an API route for a deleteing a single comic,
* but re-use the method from core.
*/
register_api_route( 'jetpack', 1.0, '/comic/(?P<post_id>)', array(
'callback' => array( 'WP_JSON_API_Posts', 'delete' ),
'methods' => array( 'DELETE' )
) );
/**
* Register an API route for updating a single comic,
* do some stuff and internally re-use the API's post update method.
*
* Also, perhaps some crazy way to re-use arguments from an existing route.
*/
register_api_route( 'jetpack', 1.0, '/comic/(?P<post_id>)', array(
'callback' => function( $args ) {
if ( 1 == 2 ) {
return new WP_Error( 403 );
}
WP_JSON_API_Posts::update( $args );
}
'methods' => array( 'POST' ),
'args' => WP_JSON_API::route( 'wp.post-update' )->args
) );