Skip to content
Permalink
Browse files

Setting up the REST endpoint

  • Loading branch information...
ionofzion committed Nov 2, 2018
1 parent 1f6613b commit e88c075f9d8a6c5bdcd223ff971d709bcf60e7e4
Showing with 33 additions and 20 deletions.
  1. +32 −19 includes/Endpoint/{Example.php → Admin.php}
  2. +1 −1 wpr-contact-form.php
@@ -16,7 +16,7 @@
/**
* @subpackage REST_Controller
*/
class Example {
class Admin {
/**
* Instance of this class.
*
@@ -70,40 +70,53 @@ public static function get_instance() {
public function register_routes() {
$version = '1';
$namespace = $this->plugin_slug . '/v' . $version;
$endpoint = '/example/';
$endpoint = '/admin/';
register_rest_route( $namespace, $endpoint, array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_example' ),
'permission_callback' => array( $this, 'example_permissions_check' ),
'args' => array(),
'callback' => array( $this, 'get_contact_email' ),
'permission_callback' => array( $this, 'admin_permissions_check' ),
),
) );
register_rest_route( $namespace, $endpoint, array(
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'update_example' ),
'permission_callback' => array( $this, 'example_permissions_check' ),
'args' => array(),
'callback' => array( $this, 'update_contact_email' ),
'permission_callback' => array( $this, 'admin_permissions_check' ),
'args' => array(
'email' => array(
'required' => true,
'type' => 'string',
'description' => 'The user\'s email address',
'format' => 'email'
),
),
),
) );
register_rest_route( $namespace, $endpoint, array(
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_example' ),
'permission_callback' => array( $this, 'example_permissions_check' ),
'args' => array(),
'callback' => array( $this, 'update_contact_email' ),
'permission_callback' => array( $this, 'admin_permissions_check' ),
'args' => array(
'email' => array(
'required' => true,
'type' => 'string',
'description' => 'The user\'s email address',
'format' => 'email'
),
),
),
) );
register_rest_route( $namespace, $endpoint, array(
array(
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_example' ),
'permission_callback' => array( $this, 'example_permissions_check' ),
'callback' => array( $this, 'delete_contact_email' ),
'permission_callback' => array( $this, 'admin_permissions_check' ),
'args' => array(),
),
) );
@@ -116,7 +129,7 @@ public function register_routes() {
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Request
*/
public function get_example( $request ) {
public function get_contact_email( $request ) {
$example_option = get_option( 'wpr_contact_email' );
// Don't return false if there is no option
@@ -139,12 +152,12 @@ public function get_example( $request ) {
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Request
*/
public function update_example( $request ) {
$updated = update_option( 'wpr_contact_email', $request->get_param( 'exampleSetting' ) );
public function update_contact_email( $request ) {
$updated = update_option( 'wpr_contact_email', $request->get_param( 'email' ) );
return new \WP_REST_Response( array(
'success' => $updated,
'value' => $request->get_param( 'exampleSetting' )
'value' => $request->get_param( 'email' )
), 200 );
}
@@ -154,7 +167,7 @@ public function update_example( $request ) {
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|WP_REST_Request
*/
public function delete_example( $request ) {
public function delete_contact_email( $request ) {
$deleted = delete_option( 'wpr_contact_email' );
return new \WP_REST_Response( array(
@@ -169,7 +182,7 @@ public function delete_example( $request ) {
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|bool
*/
public function example_permissions_check( $request ) {
public function admin_permissions_check( $request ) {
return current_user_can( 'manage_options' );
}
}
@@ -79,7 +79,7 @@ function init() {
$wpr = Plugin::get_instance();
$wpr_shortcode = Shortcode::get_instance();
$wpr_admin = Admin::get_instance();
$wpr_rest = Endpoint\Example::get_instance();
$wpr_rest_admin = Endpoint\Admin::get_instance();
}
add_action( 'plugins_loaded', 'Pangolin\\WPR\\init' );

0 comments on commit e88c075

Please sign in to comment.
You can’t perform that action at this time.