inital of rest api

This commit is contained in:
Patrick 2026-03-25 21:45:22 +01:00
parent b3b7ec0293
commit 410ad6c7b9
3 changed files with 77 additions and 0 deletions

15
src/backend/backend.php Normal file
View File

@ -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();
});

View File

@ -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;
}
}

View File

@ -34,3 +34,5 @@ add_action( 'init', 'TheaterGF\Gallery\block_init' );
if ( is_admin() ) { if ( is_admin() ) {
require_once __DIR__ . '/src/admin/admin.php'; require_once __DIR__ . '/src/admin/admin.php';
} }
require_once __DIR__ . '/src/backend/backend.php';