From 235ce5716edc2f7cbe80fdccf26eac7269587839 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Mon, 08 Jun 2026 04:38:18 +0000
Subject: [PATCH] =FavouritesManager.php and FavouritesRoutes.php fixes. Moving all logic to FavouritesManager.php. Still some left to do
---
inc/managers/CustomTable.php | 62 ++++++++++++++++++++++--------
1 files changed, 45 insertions(+), 17 deletions(-)
diff --git a/inc/managers/CustomTable.php b/inc/managers/CustomTable.php
index cb458ce..1a6719c 100644
--- a/inc/managers/CustomTable.php
+++ b/inc/managers/CustomTable.php
@@ -49,15 +49,16 @@
*
* @example CustomTable::for('favourites')->insert($data);
*/
- public static function for(string $tableName): self
+ public static function for(string $tableName, bool $user = false, bool $auth = false, bool $useTransactions = false): self
{
if (!isset(self::$instances[$tableName])) {
- self::$instances[$tableName] = new self($tableName);
+ self::$instances[$tableName] = new self($tableName, $user, $auth, $useTransactions);
}
return self::$instances[$tableName];
}
+
public static function destroyInstance(string $tableName):void
{
if (isset(self::$instances[$tableName])) {
@@ -260,7 +261,7 @@
* @param string $tableName Table name without prefix/BASE (e.g., 'favourites', 'notifications')
* @param bool $useTransactions Whether to auto-wrap operations in transactions
*/
- public function __construct(string $tableName, bool $useTransactions = false)
+ public function __construct(string $tableName, bool $user = false, bool $auth = false, bool $useTransactions = false)
{
global $wpdb;
$this->wpdb = $wpdb;
@@ -269,6 +270,12 @@
$this->useTransactions = $useTransactions;
$this->cache = Cache::for($tableName);
+ if ($user) {
+ $this->cache->user();
+ }
+ if ($auth) {
+ $this->cache->auth();
+ }
$usersStatus = $this->wpdb->get_row("SHOW TABLE STATUS LIKE '{$this->wpdb->users}'");
$parentCollation = $usersStatus->Collation ?? 'utf8mb4_general_ci';
@@ -312,16 +319,16 @@
}
/**
- * Set LIMIT
+ * Set pagination
*
- * @param int $limit Number of records
- * @param int $offset Optional offset
+ * @param int $perPage Number of records per page
+ * @param int $page Page number (1-based)
* @return self
*/
- public function limit(int $limit, int $offset = 0): self
+ public function limit(int $perPage, int $page = 1): self
{
- $this->builder['limit'] = $limit;
- $this->builder['offset'] = $offset;
+ $this->builder['per_page'] = $perPage;
+ $this->builder['page'] = $page;
return $this;
}
@@ -594,12 +601,12 @@
* 'where' => ['user_id' => 1],
* 'orderby' => 'date_added',
* 'order' => 'DESC',
- * 'limit' => 20
+ * 'per_page' => 20
* ]);
*/
- public function getMany(array $args = [], string $output = OBJECT): array
+ public function getMany(array $args = [], bool $itemsOnly = true, string $output = OBJECT): array
{
- return $this->cache->remember(
+ $items = $this->cache->remember(
$this->cache->generateKey(array_merge($args, ['output' => $output])),
function () use ($args, $output) {
$query = "SELECT * FROM {$this->fullTableName}";
@@ -621,10 +628,19 @@
}
// LIMIT
- if (!empty($args['limit'])) {
- $limit = absint($args['limit']);
- $offset = !empty($args['offset']) ? absint($args['offset']) : 0;
- $query .= " LIMIT {$offset}, {$limit}";
+ if (array_key_exists('limit', $args)) {
+ error_log('[CustomTable]::getMany deprecated key \'limit\' - use \'per_page\' instead. '.print_r($args, true));
+ $args['per_page'] = $args['limit'];
+ }
+ if (array_key_exists('offset', $args)) {
+ error_log('[CustomTable]::getMany deprecated key \'offset\' - use \'page\' instead. '.print_r($args, true));
+ $args['page'] = $args['offset'] / $args['limit'];
+ }
+ if (!empty($args['per_page'])) {
+ $perPage = absint($args['per_page']);
+ $page = !empty($args['page']) ? absint($args['page']) : 1;
+ $offset = ($page - 1) * $perPage;
+ $query .= " LIMIT {$offset}, {$perPage}";
}
if (empty($values)) {
@@ -635,8 +651,20 @@
}
);
+ if ($itemsOnly) {
+ return $items;
+ }
+ $page = max(1, $args['page'] ?? 1);
+ $perPage = $args['per_page']??false;
+ $total = $this->count($args['where']);
+ return [
+ 'items' => $items,
+ 'total' => $total,
+ 'has_more' => $perPage && ($page * $perPage) < $total,
+ ];
}
+
/**
* Get a specific column value from all matches
* @param string $column
@@ -662,7 +690,7 @@
$args['order'] = $order;
}
if ($limit) {
- $args['limit'] = $limit;
+ $args['per_page'] = $limit;
}
return array_column($this->getMany($args), $column);
--
Gitblit v1.10.0