Please note that what we would like to see are:
WordPress-Core
coding standard (feel free to run it through wpcs
codesniffer)Hope you have fun while you complete this. Good luck!
Requirements:
/wp-json/techtest/v1/books
.GET
and POST
methods.GET
method should retrieve a list of “Books” posts, including custom fields:title
, author
, and isbn
.POST
method should allow adding a new “Book” with custom fields: title
, author
, and isbn
.Below is the skeleton class template for the custom cli. Add your function in this class.
<?php
/**
* REST API Books Endpoint
*
* @package Tech Test
* @since 1.0.0
*/
class Books {
/**
* Books constructor.
*
* Set endpoint actions.
*
*/
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_custom_endpoints' ), 10 );
}
public function register_custom_endpoints() {
register_rest_route( 'techtest/v1', '/books', array(
'methods' => 'GET',
'callback' => array( $this, 'get_books' ),
));
register_rest_route( 'techtest/v1', '/books', array(
'methods' => 'POST',
'callback' => array( $this, 'add_book' ),
));
}
public function get_books( $request ) {
// Your code here.
}
public function add_book( $request ) {
// Your code here.
}
}
The expected JSON output for GET method is
[
{
"id": 1,
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"isbn": "9780743273565"
},
{
"id": 2,
"title": "1984",
"author": "George Orwell",
"isbn": "9780451524935"
},
{
"id": 3,
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"isbn": "9780446310789"
}
// More books...
]
Create a gist on your github gist page (https://gist.github.com/) and submit the link address