Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added boilerplate for REST interface. #526

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Changelog
* (2 February 2020) Added REST interface folder and links
* (3 July 2015). Flattened the folder structure so there is no .org repo parent folder.
* (4 September 2014). Updating the `README` with Windows symbolic link instructions.
* (3 September 2014). Updating the `README` to describe how to install the Boilerplate.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ For reference, [here's a discussion](http://make.wordpress.org/themes/2013/03/04

### Includes

Note that if you include your own classes, or third-party libraries, there are three locations in which said files may go:
Note that if you include your own classes, or third-party libraries, there are four locations in which said files may go:

* `plugin-name/includes` is where functionality shared between the admin area and the public-facing parts of the site reside
* `plugin-name/admin` is for all admin-specific functionality
* `plugin-name/public` is for all public-facing functionality
* `plugin-name/rest` is for all rest api functionality

Note that previous versions of the Boilerplate did not include `Plugin_Name_Loader` but this class is used to register all filters and actions with WordPress.

Expand Down
21 changes: 21 additions & 0 deletions plugin-name/includes/class-plugin-name.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function __construct() {
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_rest_hooks();

}

Expand Down Expand Up @@ -122,6 +123,12 @@ private function load_dependencies() {
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-plugin-name-public.php';

/**
* The class responsible for defining all actions that occur in the rest api
* of the site.
*/
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'rest/class-plugin-name-rest.php';

$this->loader = new Plugin_Name_Loader();

}
Expand Down Expand Up @@ -175,6 +182,20 @@ private function define_public_hooks() {

}

/**
* Register all of the hooks related to the REST functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_rest_hooks() {

$plugin_rest = new Plugin_Name_Rest( $this->get_plugin_name(), $this->get_version());
$this->loader->add_action( 'rest_api_init', $plugin_rest, 'register_routes');

}

/**
* Run the loader to execute all of the hooks with WordPress.
*
Expand Down
132 changes: 132 additions & 0 deletions plugin-name/rest/class-plugin-name-rest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* The rest functionality of the plugin.
*
* @link http://example.com
* @since 1.0.0
*
* @package Plugin_Name
* @subpackage Plugin_Name/public
*/

/**
* The public-facing functionality of the plugin.
*
* Defines the plugin name, version, and examples to create your REST access
* methods. Don't forget to validate and sanatize incoming data!
*
* @package Plugin_Name
* @subpackage Plugin_Name/public
* @author Your Name <email@example.com>
*/
class Plugin_Name_Rest {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;

/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;

/**
* The text domain of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_text_domain The text domain of this plugin.
*/
private $plugin_text_domain;

/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
* @param string $plugin_text_domain The text domain of this plugin.
*/
public function __construct( $plugin_name, $version) {

$this->plugin_name = $plugin_name;
$this->version = $version;
}

public function register_routes() {

$version = '1';
$namespace = 'pilotdata/v' . $version;
$base = 'route'; // ntfs: /wp-json/plugin_name/v1
register_rest_route( $namespace, '/plugin_name/',
array(
'methods' => \WP_REST_Server::READABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => array( $this, 'plugin_name_get_callback' ),
// Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
'permission_callback' => array($this, 'plugin_name_private_access_check' ),),
array(
'methods' => \WP_REST_Server::CREATABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => array( $this, 'plugin_name_post_callback' ),
// Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
'permission_callback' => array($this, 'plugin_name_private_access_check' ),),
array(
'methods' => \WP_REST_Server::EDITABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => array( $this, 'plugin_name_put_pilotdata' ),
// Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
'permission_callback' => array($this, 'plugin_name_private_access_check' ),),
array (
'methods' => \WP_REST_Server::DELETABLE,
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
'callback' => array( $this, 'plugin_name_delete_callback' ),
// Here we register our permissions callback. The callback is fired before the main callback to check if the current user can access the endpoint.
'permission_callback' => array($this, 'plugin_name_private_access_check' ),
));
}

public function plugin_name_private_access_check(){
// put your access requirements here. You might have different requirements for each
// access method. I'm showing only one here.
if ( ! (current_user_can( 'edit_users' ) || current_user_can('edit_gc_operations') || current_user_can('edit_gc_dues') ||
current_user_can('edit_gc_instruction') || current_user_can('edit_gc_tow') || current_user_can('edit_gc_tow') || current_user_can('read')
)) {
return new \WP_Error( 'rest_forbidden', esc_html__( 'Sorry, you are not authorized for that.', 'my-text-domain' ), array( 'status' => 401 ) );
}
// This is a black-listing approach. You could alternatively do this via white-listing, by returning false here and changing the permissions check.
return true;
}
public function plugin_name_get_callback( \WP_REST_Request $request) {
/*
Process your GET request here.
*/
}
public function plugin_name_post_pilotdata( \WP_REST_Request $request) {
/*
Process your POST request here.
*/
}
public function plugin_name_put_pilotdata( \WP_REST_Request $request) {
/*
Process your PUT request here.
*/
}
public function plugin_name_delete_pilotdata( \WP_REST_Request $request) {
/*
Process your DELETE request here.
*/
}
}