Date post: | 09-Jan-2017 |
Category: |
Internet |
Upload: | josh-pollock |
View: | 3,083 times |
Download: | 0 times |
Hi, I'm Josh
Slides: JoshPress.net/extending-rest-api-talk
What We Are Covering
Extending The Default Responses
v2.wp-api.org/extending/modifyi
ng/
Creating Your Own Endpoints
v2.wp-api.org/extending/adding/
We're Skipping To The Back of The Book:)
Download: wpeng.in/wp-api-ebook/
Why Extend The Defaults?
Are endpoints of a default endpoint close, but missing a field?Combine POST requests.
add_filter( 'the_content', function( $content ) { …
add_filter( 'template_includes', function( $template ) { ...
Why Make Your Own?
● Unique data sets -- custom queries, custom tables, wrapping existing classes.
● Just the data you need.● Replace admin-ajax$query = new WP_Query( $args );
$collection = new my_class( $params );
global $wpdb;
add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
Customizing Default Routes
● Add fields to response○ Custom field or any other data
● Applies to all read and/or write endpoints of route.
Meet register_api_field()
http://v2.wp-api.org/extending/modifying/
● Adds fields to one or more response● Arguments:
○ Object (post type name or "terms", "meta", "user" or "comments" )
○ Name of field○ Args
■ Read Callback■ Write Callback■ Schema Callback
Example Handlersfunction slug_get_meta_field( $object, $field_name, $request ) {
return get_post_meta( $object[ 'id' ], $field_name );
}
public function slug_update_meta( $value, $object, $field_name ) { if ( ! $value || ! is_string( $value ) ) { return; }
return update_post_meta( $object->ID, $field_name, strip_tags( $value ) );
}
Example Registration
add_action( 'rest_api_init', 'slug_register_spaceship' );function slug_register_spaceship() { register_api_field( 'post', 'starship', array( 'get_callback' => slug_get_meta_field, 'update_callback' => slug_update_meta_field, 'schema' => null, ) );}
Meet register_rest_route()
http://v2.wp-api.org/extending/adding/
Adds a collection of routes.add_action( 'rest_api_init', function () { register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array( 'methods' => 'GET', 'callback' => 'my_awesome_func', ) );} );
register_rest_route() Defines
● Endpoints For Route● Permissions for endpoints● Transport methods for endpoints● Fields for endpoints● Callback for endpoints● Schema
Endpoint URL
public function the_route() { register_rest_route( 'swp_api', '/search',
array( ) );
}Namespace
Route
Transport Methodpublic function the_route() { register_rest_route( 'swp_api', '/search',
array( 'methods' => \WP_REST_Server::READABLE, )
);}
GET
Permissionspublic function the_route() { register_rest_route( 'swp_api', '/search',
array( 'methods' => \WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'permissions_check' ) ));
}
Permissionspublic function permissions_check( $request ) {
$allowed = apply_filters( 'cwp_swp_api_allow_query', true, $request ); return (bool) $allowed;
}public function permissions_check( $request ) {
return current_user_can( 'something' );}
Standard WordPress Permissions/ Authentication!!!!!!!!!
Fieldspublic function the_route() { register_rest_route( 'swp_api', '/search', array(
'methods' => \WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'permissions_check' ), 'args' => $this->the_args(),
) );}
Fields $args = array(
's' => array(
'default' => '', 'sanitize_callback' => 'sanitize_text_field',
),
'engine' => array(
'default' => 'default', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => array( $this, 'validate_engine' ),
),
'page' => array(
'default' => 1,
'sanitize_callback' => 'absint', ),
);
Callbackpublic function the_route() { register_rest_route( 'swp_api', '/search', array( 'methods' => \WP_REST_Server::READABLE, 'permission_callback' => array( $this, 'permissions_check' ), 'args' => $this->the_args(), 'callback' => array( $this, 'callback' ), ) );}
Callbackpublic function callback( $request ) {
$params = $request->get_params();$cb_class = new class_that_does_something( $params );$results = $cb_class->get_results();if( is_array( $results ) {
return rest_ensure_response( $results, 200 );}elseif( is_wp_error( $results ) ) {
return rest_ensure_response( $results, 500 );}else{
return rest_ensure_response( __( 'FAIL!', 'text-domain' ), 500 );}
}
Response
Should be either:● An instance of WP_REST_Response● An instance of WP_Error
rest_ensure_response( $data, $code, $headers );
You're Ready To Learn MoreJoshPress.net/extending-rest-api-talkwpeng.in/wp-api-ebook/
That's About It