restructured backend
This commit is contained in:
parent
e2ab3ad612
commit
80ca6eb40a
|
|
@ -364,9 +364,7 @@ class Editor {
|
||||||
save() {
|
save() {
|
||||||
const region = this._cutSelector.getMappedPosition();
|
const region = this._cutSelector.getMappedPosition();
|
||||||
|
|
||||||
console.log(region)
|
fetch(wpAPISettings.root + 'theatergf/gallery/v1/images/create', {
|
||||||
|
|
||||||
fetch(wpAPISettings.root + 'theatergf/gallery/v1/images/new', {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'X-WP-Nonce': wpAPISettings.nonce,
|
'X-WP-Nonce': wpAPISettings.nonce,
|
||||||
|
|
@ -383,8 +381,6 @@ class Editor {
|
||||||
|
|
||||||
response.json().then((json) => {
|
response.json().then((json) => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
console.log("response:", json)
|
|
||||||
|
|
||||||
createSuccessNotice("Successfully created");
|
createSuccessNotice("Successfully created");
|
||||||
} else {
|
} else {
|
||||||
createErrorNotice(`Failed to create image: ${json?.error ?? "Unknown Error"}`);
|
createErrorNotice(`Failed to create image: ${json?.error ?? "Unknown Error"}`);
|
||||||
|
|
@ -563,7 +559,7 @@ async function load_gallery() {
|
||||||
const gallery = document.querySelector('.gallery');
|
const gallery = document.querySelector('.gallery');
|
||||||
|
|
||||||
try {
|
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',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'X-WP-Nonce': wpAPISettings.nonce
|
'X-WP-Nonce': wpAPISettings.nonce
|
||||||
|
|
@ -571,7 +567,6 @@ async function load_gallery() {
|
||||||
})
|
})
|
||||||
|
|
||||||
const json = await response.json()
|
const json = await response.json()
|
||||||
console.log(json);
|
|
||||||
|
|
||||||
if (!Array.isArray(json)) {
|
if (!Array.isArray(json)) {
|
||||||
throw TypeError(`Did not receive an array of posts as a result! Got ${typeof 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 items = gallery.querySelectorAll("ttgf-gallery-item");
|
||||||
const payload = Array.from(items, (v) => ({ ID: v.imageId, selected: v.selected }))
|
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',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'X-WP-Nonce': wpAPISettings.nonce,
|
'X-WP-Nonce': wpAPISettings.nonce,
|
||||||
|
|
|
||||||
|
|
@ -2,25 +2,22 @@
|
||||||
|
|
||||||
namespace TheaterGF\Gallery\Backend;
|
namespace TheaterGF\Gallery\Backend;
|
||||||
|
|
||||||
require_once __DIR__ . '/endpoints/crop.php';
|
require_once __DIR__ . '/endpoints/admin.php';
|
||||||
require_once __DIR__ . '/endpoints/gallery.php';
|
require_once __DIR__ . '/endpoints/public.php';
|
||||||
|
|
||||||
add_action( 'rest_api_init', function () {
|
add_action( 'rest_api_init', function () {
|
||||||
$namespace = 'theatergf/gallery/v1';
|
$namespace = 'theatergf/gallery/v1';
|
||||||
|
|
||||||
$crop_controller = new Rest\CROP_Endpoints($namespace, 'images');
|
$admin_controller = new Rest\AdminGalleryEndpoints($namespace, 'admin');
|
||||||
$crop_controller->register_routes();
|
$admin_controller->register_routes();
|
||||||
|
|
||||||
$gallery_controller = new Rest\GALLERY_Endpoints($namespace, 'gallery');
|
|
||||||
$gallery_controller->register_routes();
|
|
||||||
|
|
||||||
|
$public_controller = new Rest\PublicGalleryEndpoints($namespace, 'gallery');
|
||||||
|
$public_controller->register_routes();
|
||||||
});
|
});
|
||||||
|
|
||||||
add_action( 'delete_attachment', function ( $post_id ) {
|
add_action( 'delete_attachment', function ( $post_id ) {
|
||||||
$selected = get_post_meta($post_id, '_ttgf_gallery_selected', true);
|
$selected = get_post_meta($post_id, '_ttgf_gallery_selected', true);
|
||||||
|
|
||||||
error_log("deleting: " . $post_id . ", selected: " . $selected);
|
|
||||||
|
|
||||||
if ( (! $selected) || empty($selected) ) {
|
if ( (! $selected) || empty($selected) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use \WP_REST_Server;
|
||||||
|
|
||||||
use function \TheaterGF\Core\get_media_manager;
|
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 ) {
|
public function __construct( $namespace, $base_path ) {
|
||||||
$this->namespace = $namespace;
|
$this->namespace = $namespace;
|
||||||
|
|
@ -19,14 +19,14 @@ class CROP_Endpoints extends WP_REST_Controller {
|
||||||
|
|
||||||
public function register_routes() {
|
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,
|
'methods' => WP_REST_Server::READABLE,
|
||||||
'callback' => [ $this, 'get_items' ],
|
'callback' => [ $this, 'get_items' ],
|
||||||
'permission_callback' => [ $this, 'get_items_permissions_check'],
|
'permission_callback' => [ $this, 'get_items_permissions_check'],
|
||||||
'args' => []
|
'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,
|
'methods' => WP_REST_Server::CREATABLE,
|
||||||
'callback' => [ $this, 'set_selected' ],
|
'callback' => [ $this, 'set_selected' ],
|
||||||
'permission_callback' => [ $this, 'create_item_permissions_check' ],
|
'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,
|
'methods' => WP_REST_Server::CREATABLE,
|
||||||
'callback' => [ $this, 'create_item' ],
|
'callback' => [ $this, 'create_item' ],
|
||||||
'permission_callback' => [ $this, 'create_item_permissions_check' ],
|
'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 ) {
|
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 ) {
|
public function create_item_permissions_check( $request ) {
|
||||||
return $this->editable_permission_check($request);
|
return $this->admin_permission_check($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function get_items( $request ) {
|
public function get_items( $request ) {
|
||||||
|
|
||||||
$posts = get_posts(array(
|
$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()) {
|
if ( ! is_user_logged_in()) {
|
||||||
return new WP_Error( 'unauthenticated', 'Log in to interact with this endpoint.', array( 'status' => 401 ));
|
return new WP_Error( 'unauthenticated', 'Log in to interact with this endpoint.', array( 'status' => 401 ));
|
||||||
|
|
@ -6,14 +6,13 @@ use \WP_Error;
|
||||||
use \WP_REST_Controller;
|
use \WP_REST_Controller;
|
||||||
use \WP_REST_Server;
|
use \WP_REST_Server;
|
||||||
|
|
||||||
class GALLERY_Endpoints extends WP_REST_Controller {
|
class PublicGalleryEndpoints extends WP_REST_Controller {
|
||||||
|
|
||||||
public function __construct( $namespace, $base_path ) {
|
public function __construct( $namespace, $base_path ) {
|
||||||
$this->namespace = $namespace;
|
$this->namespace = $namespace;
|
||||||
$this->rest_base = $base_path;
|
$this->rest_base = $base_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function register_routes() {
|
public function register_routes() {
|
||||||
register_rest_route($this->namespace, '/' . $this->rest_base, [
|
register_rest_route($this->namespace, '/' . $this->rest_base, [
|
||||||
'methods' => WP_REST_Server::READABLE,
|
'methods' => WP_REST_Server::READABLE,
|
||||||
|
|
@ -28,19 +27,21 @@ class GALLERY_Endpoints extends WP_REST_Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_items( $request ) {
|
public function get_items( $request ) {
|
||||||
$posts = get_posts(array(
|
$posts = get_posts([
|
||||||
'numberposts' => -1,
|
'numberposts' => -1,
|
||||||
'post_type' => "attachment",
|
'post_type' => "attachment",
|
||||||
'meta_key' => "_ttgf_gallery_selected",
|
'meta_key' => "_ttgf_gallery_selected",
|
||||||
));
|
'meta_value' => 0,
|
||||||
|
'meta_compare' => ">"
|
||||||
|
]);
|
||||||
|
|
||||||
$data = [];
|
$data = [];
|
||||||
|
|
||||||
foreach ( $posts as $post ) {
|
foreach ( $posts as $post ) {
|
||||||
$img_data = wp_get_attachment_image_src($post->ID, "full");
|
$img_data = wp_get_attachment_image_src($post->ID, "full");
|
||||||
|
|
||||||
if ($img_data !== false) {
|
if ($img_data !== false) {
|
||||||
$data[] = $img_data[0];
|
$data[] = $img_data[0];
|
||||||
error_log($img_data[0] . " - " . $img_data[1] . "x" . $img_data[2] . " - resized: " . (int)$img_data[3]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue