/
minimus
/
simple-view
Обзор
Документация
Войти
/
minimus
/
simple-view
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
plugin/sv-rest-api.php
191 строка
7 KB
minimus
feat: frontend galleries DataGrid
03 авг 2026, 18:54
03 авг 2026, 18:54
633f599
Код
Авторство
О чём код?
<?php if (!defined('ABSPATH')) { exit; } const svRestBase = 'sv/v2'; include_once 'sv-tools.php'; include_once 'sv-db-tools.php'; if (!class_exists("SvRestApi")) { class SvRestApi { public object $currentUser; public object $svTools; public object $svDbTools; public function __construct($currentUser) { $this->currentUser = $currentUser; $this->svTools = new SvTools(); $this->svDbTools = new SvDbTools(); } public function adminAccess($request): bool { return current_user_can('manage_options'); } public function getSettings(): array { $options = $this->svTools->getSettings(); $settings = $this->svTools->convertOptions($options); return [ 'success' => true, 'data' => $settings ]; } public function setSettings(object $request): array { $settings = $request->get_json_params(); if (!$settings) { return [ 'success' => false, 'data' => null ]; } else { $options = $this->svTools->convertToOptions($settings); $this->svTools->setSettings($options); return [ 'success' => true, 'data' => $settings ]; } } public function getLocaleStrings(): array { return [ 'success' => true, 'data' => [ 'footer' => [ 'rights' => __('All rights reserved', 'simple-view'), ], 'header' => [ 'version' => __('Version', 'simple-view'), 'dbVersion' => __('DB Version', 'simple-view'), ], 'menu' => [ 'galleries' => __('Image Galleries', 'simple-view'), 'newGallery' => __('New Image Gallery', 'simple-view'), 'settings' => __('Settings', 'simple-view'), ], 'appHeader' => [ 'galleries' => __('Image Galleries', 'simple-view'), 'editor' => __('Editor', 'simple-view'), 'items' => __('Images', 'simple-view'), 'settings' => __('Settings', 'simple-view'), ], ], ]; } public function getSysInfo(): array { global $wpdb; $row = $wpdb->get_row('SELECT VERSION() AS ver', ARRAY_A); $sqlVersion = $row['ver']; $mem = ini_get('memory_limit'); return [ 'data' => [ 'version' => SV_VERSION, 'dbVersion' => SV_DB_VERSION, 'phpVersion' => PHP_VERSION, 'sqlVersion' => $sqlVersion, 'memoryLimit' => $mem, 'wpMemoryLimit' => WP_MEMORY_LIMIT, 'wpMemoryLimitMax' => WP_MAX_MEMORY_LIMIT, 'locale' => get_locale(), ], 'success' => true, ]; } public function getGalleries(WP_REST_Request $request): WP_REST_Response { $limit = (int)$request['per_page']; $offset = ((int)$request['page'] ?? 0) * $limit; $sortBy = $request['sort_by'] ?? ''; $sortDir = $request['sort_dir'] ?? ''; $filter = $request['filter']; $data = $this->svDbTools->getGalleries($limit, $offset, $sortBy, $sortDir, $filter); $responseData = [ 'data' => $data, 'success' => true, ]; return new WP_REST_Response($responseData, 200); } public function setAdminRoutes(): void { // ** SETTINGS ROUTE START ** register_rest_route(svRestBase, '/admin/settings', [ [ 'methods' => WP_REST_Server::READABLE, 'callback' => [$this, 'getSettings'], 'permission_callback' => [$this, 'adminAccess'], ], [ 'methods' => WP_REST_Server::EDITABLE, 'callback' => [$this, 'setSettings'], 'permission_callback' => [$this, 'adminAccess'], ] ]); // ** SETTINGS ROUTE END ** // ** LOCALIZATION STRINGS START register_rest_route(svRestBase, '/admin/locales', [ 'methods' => WP_REST_Server::READABLE, 'callback' => [$this, 'getLocaleStrings'], 'permission_callback' => [$this, 'adminAccess'], ]); // ** LOCALIZATION STRINGS END // ** SYSINFO ROUTE START ** register_rest_route(svRestBase, '/admin/sysinfo', [ 'methods' => WP_REST_Server::READABLE, 'callback' => [$this, 'getSysInfo'], 'permission_callback' => [$this, 'adminAccess'], ]); // ** SYSINFO ROUTE END ** // ** GALLERIES ROUTE START ** register_rest_route(svRestBase, '/admin/galleries', [ 'methods' => WP_REST_Server::READABLE, 'callback' => [$this, 'getGalleries'], 'permission_callback' => [$this, 'adminAccess'], 'args' => [ 'filter' => [ 'type' => 'string', 'default' => 'ALL', 'enum' => ['ALL', 'ACTIVE', 'TRASH'], ], 'page' => [ 'description' => 'Current page of the collection.', 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ], 'per_page' => [ 'description' => 'Maximum number of items to be returned in result set.', 'type' => 'integer', 'default' => 10, 'sanitize_callback' => 'absint', ], 'sort_dir' => [ 'type' => 'string', 'enum' => ['ASC', 'DESC'], ], 'sort_by' => [ 'type' => 'string', 'enum' => ['id', 'name', 'trash',], ], ], ]); // ** GALLERIES ROUTE END ** } } }