Skip to content

Configuring Sample REST API

Chris Chasm edited this page Feb 3, 2021 · 9 revisions

Home

Disciple.Tools REST API Documentation

The Disciple.Tools REST API is documented extensively in the developer docs for Disciple.Tools.

Go to REST API documentation

Customize Sample REST API

This is an overview for adding your own custom endpoint with this plugin. Notes are included in the code with @todo. Here is a link to the full class to review. link

 /**
     * @todo Set the permissions your endpoint needs
     * @link https://github.com/DiscipleTools/Documentation/blob/master/Theme-Core/capabilities.md
     * @var string[]
     */
    public $permissions = [ 'access_contacts', 'dt_all_access_contacts', 'view_project_metrics' ];


    /**
     * @todo define the name of the $namespace
     * @todo define the name of the rest rout
     * @todo defne method (CREATABLE, READABLE)
     * @todo apply permission strategy. '__return_true' essentially skips the permission check.
     */
    //See https://github.com/DiscipleTools/disciple-tools-theme/wiki/Site-to-Site-Link for outside of wordpress authentication
    public function add_api_routes() {
        $namespace = 'dt_plugin_starter/v1';

        register_rest_route(
            $namespace, '/endpoint', [
                'methods'  => WP_REST_Server::CREATABLE,
                'callback' => [ $this, 'private_endpoint' ],
                'permission_callback' => function( WP_REST_Request $request ) {
                    return $this->has_permission();
                },
            ]
        );
        register_rest_route(
            $namespace, '/public_endpoint', [
                'methods'  => WP_REST_Server::CREATABLE,
                'callback' => [ $this, 'public_endpoint' ],
                'permission_callback' => '__return_true',
            ]
        );
    }


    public function private_endpoint( WP_REST_Request $request ) {

        // @todo run your function here

        return true;
    }

    public function public_endpoint( WP_REST_Request $request ) {

        // @todo run your function here

        return true;
    }