Congratulations! You have completed our initial preliminary questionnaire. Now that we know a little bit about you, we want to a deeper dive into your WordPress programming skills. Please complete and submit your answer.

Please note that what we would like to see are:

  1. Adhere to the WordPress-Core coding standard (feel free to run it through wpcs codesniffer)
  2. Please make sure to add comprehensive comments

Hope you have fun while you complete this. Good luck!

Task

Requirements:

  1. REST API Endpoint:
    • Create a custom REST API endpoint at /wp-json/techtest/v1/books.
    • The endpoint should support GET and POST methods.
  2. GET Method:
    • The GET method should retrieve a list of “Books” posts, including custom fields:title, author, and isbn.
  3. POST Method:
    • The POST method should allow adding a new “Book” with custom fields: title, author, and isbn.

Templates

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...
]

Submission

Create a gist on your github gist page (https://gist.github.com/) and submit the link address