Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/WpCore/Blueprints/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Ayctor\Api;

use WpCore\Controllers\ApiController;

class Blueprint extends ApiController
{
public function register_routes()
{
//
}
}
21 changes: 21 additions & 0 deletions src/WpCore/Commands/BlueprintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ class BlueprintCommand extends \WP_CLI_Command
'label' => 'Page'
];

/**
* Setup to create API controller
* @var array
*/
protected $api = [
'blueprint' => __DIR__ . '/../Blueprints/Api.php',
'directory' => TEMPLATEPATH . '/app/Api/',
'label' => 'API Controller'
];

/**
* Command to generate Model
* @param array $args Command arguments
Expand Down Expand Up @@ -208,6 +218,17 @@ public function page($args)
$this->createFile($this->page, $name);
}

/**
* Command to generate API Controller
* @param array $args Command arguments
*/
public function api($args)
{
list($name) = $args;

$this->createFile($this->api, $name);
}

/**
* Helper to create files
* @param array $type Setup to create the file
Expand Down
121 changes: 121 additions & 0 deletions src/WpCore/Controllers/ApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
namespace WpCore\Controllers;
/**
* Class ApiController, api controller
*/
class ApiController extends \WP_REST_Controller
{
/**
* Default namespace for the WP REST API.
* Can be overridden in child class to change the namespace.
* @var string
*/
protected $namespace = 'api/v1';


/**
* ApiController constructor.
* Once the controller has been instantiated, we hook an action to register the routes upon full initialisation of WP API.
*/
public function __construct()
{
add_action('rest_api_init', [$this, 'register_routes']);
}

/**
* Registers a route for GET transport method.
* @param $route
* @param $callback
* @param array $args
*/
public function register_readeable($route, $callback, $args = [])
{
register_rest_route($this->namespace, $route, [
'methods' => \WP_REST_Server::READABLE,
'callback' => $callback,
'args' => $args,
'permission_callback' => [$this, 'get_items_permissions_check']
]);
}

/**
* Registers a route for POST transport method.
* @param $route
* @param $callback
* @param array $args
*/
public function register_creatable($route, $callback, $args = [])
{
register_rest_route($this->namespace, $route, [
'methods' => \WP_REST_Server::CREATABLE,
'callback' => $callback,
'args' => $args,
'permission_callback' => [$this, 'create_item_permissions_check']
]);
}

/**
* Registers a route for POST, PUT, PATCH transport methods together.
* @param $route
* @param $callback
* @param array $args
*/
public function register_editable($route, $callback, $args = [])
{
register_rest_route($this->namespace, $route, [
'methods' => \WP_REST_Server::EDITABLE,
'callback' => $callback,
'args' => $args,
'permission_callback' => [$this, 'update_item_permissions_check']
]);
}

/**
* Registers a route for DELETE transport method.
* @param $route
* @param $callback
* @param array $args
*/
public function register_deletable($route, $callback, $args = [])
{
register_rest_route($this->namespace, $route, [
'methods' => \WP_REST_Server::DELETABLE,
'callback' => $callback,
'args' => $args,
'permission_callback' => [$this, 'delete_item_permissions_check']
]);
}

/**
* Registers routes for a collection of items.
* One to return ALL the items and another one to return ONE specific item.
* @param $route
* @param string $pluralCallback
* @param string $singularCallback
*/
public function register_collection($route, $pluralCallback = 'get_items', $singularCallback = 'get_item')
{
register_rest_route($this->namespace, $route, [
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, $pluralCallback],
'permission_callback' => [$this, 'get_items_permissions_check']
]);

register_rest_route($this->namespace, $route . '(?P<id>[\d]+)', [
'methods' => \WP_REST_Server::READABLE,
'callback' => [$this, $singularCallback],
'permission_callback' => [$this, 'get_items_permissions_check']
]);
}

/**
* Override methods to grant permissions or not for each of the above routes.
* These methods MUST be overridden in child class if you want to check authentication cases or else it'll continuously authorize access to REST API routes.
* @param $request
* @return bool
*/
public function get_items_permissions_check($request)
{
return true;
}
}