restructured backend

This commit is contained in:
Patrick 2026-05-20 14:27:34 +02:00
parent e2ab3ad612
commit 80ca6eb40a
5 changed files with 22 additions and 48 deletions

View File

@ -364,9 +364,7 @@ class Editor {
save() {
const region = this._cutSelector.getMappedPosition();
console.log(region)
fetch(wpAPISettings.root + 'theatergf/gallery/v1/images/new', {
fetch(wpAPISettings.root + 'theatergf/gallery/v1/images/create', {
method: 'POST',
headers: {
'X-WP-Nonce': wpAPISettings.nonce,
@ -383,8 +381,6 @@ class Editor {
response.json().then((json) => {
if (response.ok) {
console.log("response:", json)
createSuccessNotice("Successfully created");
} else {
createErrorNotice(`Failed to create image: ${json?.error ?? "Unknown Error"}`);
@ -563,7 +559,7 @@ async function load_gallery() {
const gallery = document.querySelector('.gallery');
try {
const response = await fetch(wpAPISettings.root + 'theatergf/gallery/v1/images/all', {
const response = await fetch(wpAPISettings.root + 'theatergf/gallery/v1/admin/images', {
method: 'GET',
headers: {
'X-WP-Nonce': wpAPISettings.nonce
@ -571,7 +567,6 @@ async function load_gallery() {
})
const json = await response.json()
console.log(json);
if (!Array.isArray(json)) {
throw TypeError(`Did not receive an array of posts as a result! Got ${typeof json}`)
@ -661,7 +656,7 @@ jQuery(document).ready( ($) => {
const items = gallery.querySelectorAll("ttgf-gallery-item");
const payload = Array.from(items, (v) => ({ ID: v.imageId, selected: v.selected }))
fetch(wpAPISettings.root + 'theatergf/gallery/v1/images/selected/set', {
fetch(wpAPISettings.root + 'theatergf/gallery/v1/admin/images/selected', {
method: 'POST',
headers: {
'X-WP-Nonce': wpAPISettings.nonce,

View File

@ -2,25 +2,22 @@
namespace TheaterGF\Gallery\Backend;
require_once __DIR__ . '/endpoints/crop.php';
require_once __DIR__ . '/endpoints/gallery.php';
require_once __DIR__ . '/endpoints/admin.php';
require_once __DIR__ . '/endpoints/public.php';
add_action( 'rest_api_init', function () {
$namespace = 'theatergf/gallery/v1';
$crop_controller = new Rest\CROP_Endpoints($namespace, 'images');
$crop_controller->register_routes();
$gallery_controller = new Rest\GALLERY_Endpoints($namespace, 'gallery');
$gallery_controller->register_routes();
$admin_controller = new Rest\AdminGalleryEndpoints($namespace, 'admin');
$admin_controller->register_routes();
$public_controller = new Rest\PublicGalleryEndpoints($namespace, 'gallery');
$public_controller->register_routes();
});
add_action( 'delete_attachment', function ( $post_id ) {
$selected = get_post_meta($post_id, '_ttgf_gallery_selected', true);
error_log("deleting: " . $post_id . ", selected: " . $selected);
if ( (! $selected) || empty($selected) ) {
return;
}

View File

@ -10,7 +10,7 @@ use \WP_REST_Server;
use function \TheaterGF\Core\get_media_manager;
class CROP_Endpoints extends WP_REST_Controller {
class AdminGalleryEndpoints extends WP_REST_Controller {
public function __construct( $namespace, $base_path ) {
$this->namespace = $namespace;
@ -19,14 +19,14 @@ class CROP_Endpoints extends WP_REST_Controller {
public function register_routes() {
register_rest_route($this->namespace, '/' . $this->rest_base . '/all', [
register_rest_route($this->namespace, '/' . $this->rest_base . '/images', [
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_items' ],
'permission_callback' => [ $this, 'get_items_permissions_check'],
'args' => []
]);
register_rest_route($this->namespace, '/' . $this->rest_base . '/selected/set', [
register_rest_route($this->namespace, '/' . $this->rest_base . '/images/selected', [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'set_selected' ],
'permission_callback' => [ $this, 'create_item_permissions_check' ],
@ -47,7 +47,7 @@ class CROP_Endpoints extends WP_REST_Controller {
]
]);
register_rest_route($this->namespace, '/' . $this->rest_base . '/new', [
register_rest_route($this->namespace, '/' . $this->rest_base . '/images/create', [
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'create_item' ],
'permission_callback' => [ $this, 'create_item_permissions_check' ],
@ -77,14 +77,13 @@ class CROP_Endpoints extends WP_REST_Controller {
}
public function get_items_permissions_check( $request ) {
return $this->editable_permission_check($request);
return $this->admin_permission_check($request);
}
public function create_item_permissions_check( $request ) {
return $this->editable_permission_check($request);
return $this->admin_permission_check($request);
}
public function get_items( $request ) {
$posts = get_posts(array(
@ -186,7 +185,7 @@ class CROP_Endpoints extends WP_REST_Controller {
}
protected function editable_permission_check( $request ) {
protected function admin_permission_check( $request ) {
if ( ! is_user_logged_in()) {
return new WP_Error( 'unauthenticated', 'Log in to interact with this endpoint.', array( 'status' => 401 ));

View File

@ -6,14 +6,13 @@ use \WP_Error;
use \WP_REST_Controller;
use \WP_REST_Server;
class GALLERY_Endpoints extends WP_REST_Controller {
class PublicGalleryEndpoints 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, [
'methods' => WP_REST_Server::READABLE,
@ -28,19 +27,21 @@ class GALLERY_Endpoints extends WP_REST_Controller {
}
public function get_items( $request ) {
$posts = get_posts(array(
$posts = get_posts([
'numberposts' => -1,
'post_type' => "attachment",
'meta_key' => "_ttgf_gallery_selected",
));
'meta_value' => 0,
'meta_compare' => ">"
]);
$data = [];
foreach ( $posts as $post ) {
$img_data = wp_get_attachment_image_src($post->ID, "full");
if ($img_data !== false) {
$data[] = $img_data[0];
error_log($img_data[0] . " - " . $img_data[1] . "x" . $img_data[2] . " - resized: " . (int)$img_data[3]);
}
}

View File

@ -1,18 +0,0 @@
<?php
namespace TheaterGF\Gallery\Backend;
/**
* Checks whether all required fields are present. Does not support duplicate keys.
*
* @return if a field is missing, its key, otherwise null
*/
function check_required_fields(WP_REST_Request $request, iterable $fields): string|null {
foreach ($fields as $key) {
}
return null;
}