From 661968162602c572345491faf7724f3180045241 Mon Sep 17 00:00:00 2001 From: Dave Johnson Date: Mon, 3 Feb 2020 17:22:09 -0500 Subject: [PATCH 1/2] Added REST API Added folder/file and hooks to faciliate implementation of custom REST endpoints. --- CHANGELOG.md | 1 + README.md | 3 +- plugin-name/includes/class-plugin-name.php | 21 ++++ plugin-name/rest/class-plugin-name-rest.php | 132 ++++++++++++++++++++ 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 plugin-name/rest/class-plugin-name-rest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 224e3f66..1e6d22c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 71517375..d696c735 100644 --- a/README.md +++ b/README.md @@ -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/resr` 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. diff --git a/plugin-name/includes/class-plugin-name.php b/plugin-name/includes/class-plugin-name.php index 9906f330..cb186137 100644 --- a/plugin-name/includes/class-plugin-name.php +++ b/plugin-name/includes/class-plugin-name.php @@ -78,6 +78,7 @@ public function __construct() { $this->set_locale(); $this->define_admin_hooks(); $this->define_public_hooks(); + $this->define_rest_hooks(); } @@ -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(); } @@ -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. * diff --git a/plugin-name/rest/class-plugin-name-rest.php b/plugin-name/rest/class-plugin-name-rest.php new file mode 100644 index 00000000..d1eaf6a4 --- /dev/null +++ b/plugin-name/rest/class-plugin-name-rest.php @@ -0,0 +1,132 @@ + + */ +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. + */ + } +} + + + + From f175add8dbb5ad1fff49152a2e3c0166ab461c22 Mon Sep 17 00:00:00 2001 From: Dave Johnson Date: Mon, 3 Feb 2020 17:32:45 -0500 Subject: [PATCH 2/2] corrected doc spelling corrected spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d696c735..0c4ad2ef 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Note that if you include your own classes, or third-party libraries, there are f * `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/resr` is for all rest api 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.