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

Add get_zones & create_zone REST API endpints #56

Merged
merged 6 commits into from
Aug 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 103 additions & 6 deletions includes/class-zoninator-api-controller.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
/**
* @packae Zoninator/Rest
* @package Zoninator/Rest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this!

*/


/**
* Class Zoninator_Api_Controller
*/
Expand All @@ -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
*
Expand Down Expand Up @@ -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' )
Copy link
Contributor

Choose a reason for hiding this comment

The 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' )
Expand Down Expand Up @@ -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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice created usage! :)

}

/**
* Add a post to zone
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
}

/**
Expand All @@ -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
Expand All @@ -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(
Expand Down
28 changes: 27 additions & 1 deletion tests/unit/class-zoninator-api-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ function login_as_admin() {
return $this->login_as( $this->admin_id );
}


/**
* Login as
*
Expand Down Expand Up @@ -191,6 +190,33 @@ function setUp() {
$this->environment = Zoninator()->rest_api->bootstrap->environment();
}

/**
* T test_create_zone_responds_with_created_when_method_post
*
* @throws Exception E.
*/
function test_create_zone_responds_with_created_when_method_post() {
$this->login_as_admin();
$response = $this->post( '/zoninator/v1/zones', array(
'slug' => 'test-zone',
) );
$this->assertResponseStatus( $response, 201 );
}

/**
* T test_create_zone_fail_if_invalid_data
*
* @throws Exception E.
*/
function test_create_zone_fail_if_invalid_data() {
$this->login_as_admin();
$response = $this->post( '/zoninator/v1/zones', array(
'name' => 'Test zone',
'description' => 'No slug provided.'
) );
$this->assertResponseStatus( $response, 400 );
}

/**
* T test_add_post_to_zone_responds_with_created_when_method_post
*
Expand Down