-
Notifications
You must be signed in to change notification settings - Fork 50
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
Add get_zones & create_zone REST API endpints #56
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
<?php | ||
/** | ||
* @packae Zoninator/Rest | ||
* @package Zoninator/Rest | ||
*/ | ||
|
||
|
||
/** | ||
* Class Zoninator_Api_Controller | ||
*/ | ||
|
@@ -22,6 +21,7 @@ class Zoninator_Api_Controller extends Zoninator_REST_Controller { | |
const PERMISSION_DENIED = 'permission-denied'; | ||
const ZONE_NOT_FOUND = 'zone-not-found'; | ||
const POST_NOT_FOUND = 'post-not-found'; | ||
const INVALID_ZONE_SETTINGS = 'invalid-zone-settings'; | ||
/** | ||
* Instance | ||
* | ||
|
@@ -50,16 +50,24 @@ function __construct( $instance ) { | |
* Set up this controller | ||
*/ | ||
function setup() { | ||
// $this->add_route( 'zones' ) | ||
// ->add_action( $this->action( 'index', 'get_zones' ) ) | ||
// ->add_action( $this->action( 'create', 'create_zone' ) ); | ||
$this->translations = array( | ||
self::ZONE_NOT_FOUND => __( 'Zone not found', 'zoninator' ), | ||
self::INVALID_POST_ID => __( 'Invalid post id', 'zoninator' ), | ||
self::INVALID_ZONE_ID => __( 'Zone not found', 'zoninator' ), | ||
self::ZONE_ID_POST_ID_REQUIRED => __( 'post id and zone id required', 'zoninator' ), | ||
); | ||
|
||
$this->add_route( 'zones' ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great form here! |
||
->add_action( | ||
$this->action( 'index', 'get_zones' ) | ||
->permissions( 'get_zones_permissions_check' ) | ||
) | ||
->add_action( | ||
$this->action( 'create', 'create_zone' ) | ||
->permissions( 'add_zone_permissions_check' ) | ||
->args( '_params_for_create_zone' ) | ||
); | ||
|
||
$this->add_route( 'zones/(?P<zone_id>[\d]+)' ) | ||
->add_action( $this->action( 'index', 'get_zone_posts' ) | ||
->permissions( 'get_zone_posts_permissions_check' ) | ||
|
@@ -100,6 +108,46 @@ function setup() { | |
->args( '_params_for_get_recent_posts' ) ); | ||
} | ||
|
||
/** | ||
* Get the list of all zones | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|WP_REST_Response | ||
*/ | ||
function get_zones( $request ) { | ||
$results = $this->instance->get_zones(); | ||
|
||
if ( is_wp_error( $results ) ) { | ||
return $this->_bad_request( array( | ||
'message' => $results->get_error_message() | ||
) ); | ||
} | ||
|
||
return new WP_REST_Response( $results, 200 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can also do |
||
} | ||
|
||
/** | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|WP_REST_Response | ||
*/ | ||
function create_zone( $request ) { | ||
$name = $this->_get_param( $request, 'name', '' ); | ||
$slug = $this->_get_param( $request, 'slug', '' ); | ||
$description = $this->_get_param( $request, 'description', '', 'strip_tags' ); | ||
|
||
$result = $this->instance->insert_zone( $slug, $name, array( 'description' => $description ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! Better if we split this into multiple lines (standard php practice on arrays with string keys, leave a comma in the array's end. Like so: $result = $this->instance->insert_zone( $slug, $name, array(
'description' => $description,
) ); |
||
|
||
if ( is_wp_error( $result ) ) { | ||
return $this->bad_request( array( | ||
'message' => $result->get_error_message() | ||
) ); | ||
} | ||
|
||
$zone = $this->instance->get_zone( $result[ 'term_id' ] ); | ||
|
||
return $this->created( $zone ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice created usage! :) |
||
} | ||
|
||
/** | ||
* Add a post to zone | ||
* | ||
|
@@ -426,6 +474,26 @@ public function get_recent_posts( $request ) { | |
return $response; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to the zones index. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|bool | ||
*/ | ||
public function get_zones_permissions_check( $request ) { | ||
return true; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to add new zones. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|bool | ||
*/ | ||
public function add_zone_permissions_check( $request ) { | ||
return $this->_permissions_check( 'insert' ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get zone posts. | ||
* | ||
|
@@ -455,7 +523,7 @@ public function remove_post_from_zone_permissions_check( $request ) { | |
*/ | ||
public function add_post_to_zone_permissions_check( $request ) { | ||
$zone_id = $this->_get_param( $request, 'zone_id', 0, 'absint' ); | ||
return $this->_permissions_check( 'insert', $zone_id ); | ||
return $this->_permissions_check( 'update', $zone_id ); | ||
} | ||
|
||
/** | ||
|
@@ -479,6 +547,11 @@ public function strip_slashes( $item ) { | |
return stripslashes( $item ); | ||
} | ||
|
||
public function strip_tags( $item ) { | ||
// see https://github.com/WP-API/WP-API/issues/1520 on why we do not use stripslashes directly | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update comment to be about strip_tags :) |
||
return strip_tags( $item ); | ||
} | ||
|
||
/** | ||
* @param WP_REST_Request $object | ||
* @param $var | ||
|
@@ -498,6 +571,28 @@ private function _get_param( $object, $var, $default = '', $sanitize_callback = | |
return $value; | ||
} | ||
|
||
public function _params_for_create_zone() { | ||
return array( | ||
'name' => array( | ||
'type' => 'string', | ||
'sanitize_callback' => array( $this, 'strip_slashes' ), | ||
'default' => '', | ||
'required' => false | ||
), | ||
'slug' => array( | ||
'type' => 'string', | ||
'sanitize_callback' => array( $this, 'strip_slashes' ), | ||
'required' => true, | ||
), | ||
'description' => array( | ||
'type' => 'string', | ||
'sanitize_callback' => array( $this, 'strip_tags' ), | ||
'default' => '', | ||
'required' => false, | ||
) | ||
); | ||
} | ||
|
||
public function _get_zone_id_param() { | ||
return array( | ||
'zone_id' => array( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this!