-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,48 @@ 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 $this->ok( $results ); | ||
} | ||
|
||
/** | ||
* @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, | ||
) ); | ||
|
||
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 +476,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 +525,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 +549,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 strip_tags directly | ||
return strip_tags( $item ); | ||
} | ||
|
||
/** | ||
* @param WP_REST_Request $object | ||
* @param $var | ||
|
@@ -498,6 +573,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( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!