inital of rest api
This commit is contained in:
parent
b3b7ec0293
commit
410ad6c7b9
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace TheaterGF\Gallery\Backend;
|
||||
|
||||
require_once __DIR__ . '/util.php';
|
||||
|
||||
require_once __DIR__ . '/endpoints/crop.php';
|
||||
|
||||
add_action( 'rest_api_init', function () {
|
||||
$namespace = 'theatergf/gallery/v1';
|
||||
|
||||
$crop_controller = new Rest\CROP_Endpoints($namespace, 'crop');
|
||||
$crop_controller->register_routes();
|
||||
});
|
||||
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace TheaterGF\Gallery\Backend\Rest;
|
||||
|
||||
class CROP_Endpoints extends \WP_REST_Controller {
|
||||
|
||||
public function __construct( $namespace, $base_path ) {
|
||||
$this->namespace = $namespace;
|
||||
$this->rest_base = $base_path;
|
||||
}
|
||||
|
||||
public function register_routes() {
|
||||
|
||||
register_rest_route($this->namespace, '/' . $this->rest_base . '/new', [
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => [ $this, 'create_item' ],
|
||||
'permission_callback' => [ $this, 'create_item_permissions_check' ],
|
||||
'args' => [
|
||||
'img_id' => [
|
||||
'required' => true,
|
||||
'validate_callback' => function ( $param, $request, $key) { return wp_attachment_is_image($param); }
|
||||
],
|
||||
'x' => [
|
||||
'required' => true,
|
||||
'validate_callback' => function ( $param, $request, $key) { return is_numeric($param) && $param > 0; }
|
||||
],
|
||||
'y' => [
|
||||
'required' => true,
|
||||
'validate_callback' => function ( $param, $request, $key) { return is_numeric($param) && $param > 0; }
|
||||
],
|
||||
'width' => [
|
||||
'required' => true,
|
||||
'validate_callback' => function ( $param, $request, $key) { return is_numeric($param) && $param > 0; }
|
||||
],
|
||||
'height' => [
|
||||
'required' => true,
|
||||
'validate_callback' => function ( $param, $request, $key) { return is_numeric($param) && $param > 0; }
|
||||
]
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function create_item_permissions_check( $request ) {
|
||||
|
||||
if ( ! is_user_logged_in()) {
|
||||
return new \WP_Error( 'unauthenticated', 'Log in to interact with this endpoint.', array( 'status' => 401 ));
|
||||
}
|
||||
|
||||
if ( ! (is_user_logged_in() && current_user_can( 'edit_pages' )) ) {
|
||||
return new \WP_Error( 'forbidden', 'No Permission for this endpoint.', array( 'status' => 403 ));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function create_item( $request ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -34,3 +34,5 @@ add_action( 'init', 'TheaterGF\Gallery\block_init' );
|
|||
if ( is_admin() ) {
|
||||
require_once __DIR__ . '/src/admin/admin.php';
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/src/backend/backend.php';
|
||||
|
|
|
|||
Loading…
Reference in New Issue