/
minimus
/
simple-view
Обзор
Документация
Войти
/
minimus
/
simple-view
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
plugin/sv-db-tools.php
99 строк
3 KB
minimus
feat: frontend galleries DataGrid
03 авг 2026, 18:54
03 авг 2026, 18:54
633f599
Код
Авторство
О чём код?
<?php if (!defined('ABSPATH')) { exit; } require_once ABSPATH . 'wp-admin/includes/upgrade.php'; if (!class_exists('SvDbTools')) { class SvDbTools { private string $gTable = ''; private string $iTable = ''; public function __construct($gTable = 'sv_galleries', $iTable = 'sv_galleries_items') { global $wpdb; $this->gTable = $wpdb->prefix . $gTable; $this->iTable = $wpdb->prefix . $iTable; } public function tableExists(string $table): bool { global $wpdb; return $wpdb->get_var("SHOW TABLES LIKE '$table'") === $table; } public function createGalleriesTable(bool $force = false): void { if (!self::tableExists($this->gTable) || $force) { $gSql = "CREATE TABLE " . $this->gTable . "( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, code_before VARCHAR(255) DEFAULT NULL, code_after VARCHAR(255) DEFAULT NULL, trash TINYINT(1) DEFAULT 0, PRIMARY KEY (id) )"; dbDelta($gSql); } } public function createItemsTable(bool $force = false): void { if (!self::tableExists($this->iTable) || $force) { $iSql = "CREATE TABLE " . $this->iTable . "( id INT(11) NOT NULL AUTO_INCREMENT, gid INT(11) NOT NULL, caption VARCHAR(255) DEFAULT NULL, caption2 VARCHAR(255) DEFAULT NULL, source_link VARCHAR(255) DEFAULT NULL, source_type VARCHAR(25) DEFAULT NULL, content VARCHAR(255) DEFAULT NULL, content_type VARCHAR(25) DEFAULT NULL, code_before VARCHAR(255) DEFAULT NULL, code_after VARCHAR(255) DEFAULT NULL, thumb_padding INT(11) NOT NULL DEFAULT 0, thumb_margins INT(11) NOT NULL DEFAULT 5, thumb_border_width INT(11) NOT NULL DEFAULT 0, thumb_border_color VARCHAR(255) NOT NULL DEFAULT '000000', trash TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (id, gid) )"; dbDelta($iSql); } } public function upgradeDb(bool $force = false): bool { self::createGalleriesTable($force); self::createItemsTable($force); return self::tableExists($this->gTable) && self::tableExists($this->iTable); } public function deleteDbTables(): void { global $wpdb; $wpdb->query("DROP TABLE IF EXISTS {$this->iTable};"); $wpdb->query("DROP TABLE IF EXISTS {$this->gTable};"); } public function getGalleries(int $limit, int $offset, string $sortBy, string $sortDir, string $filter): array { global $wpdb; $gTable = $this->gTable; $filterValue = $filter === 'ACTIVE' ? 0 : 1; $whereClause = $filter === 'ALL' ? '' : $wpdb->prepare("WHERE trash = %d", $filterValue); $gLimits = "LIMIT $limit OFFSET $offset"; $sort = $sortBy !== '' ? "ORDER BY $sortBy $sortDir" : ''; $sql = "SELECT id, name, code_before AS codeBefore, code_after AS codeAfter, trash FROM $gTable $whereClause $sort $gLimits"; $countSql = "SELECT COUNT(*) FROM $gTable"; return [ 'rows' => $wpdb->get_results($wpdb->prepare($sql), ARRAY_A), 'rowCount' => $wpdb->get_var($countSql) ]; } } }