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/rest/Rest.php | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 203 insertions(+), 23 deletions(-)
diff --git a/inc/rest/Rest.php b/inc/rest/Rest.php
index 6435ec8..557929d 100644
--- a/inc/rest/Rest.php
+++ b/inc/rest/Rest.php
@@ -2,7 +2,9 @@
namespace JVBase\rest;
use JVBase\managers\Cache;
-use JVBase\utility\Features;
+use JVBase\meta\Meta;
+use JVBase\registrar\Registrar;
+use JVBase\base\Site;
use WP_REST_Request;
use WP_REST_Response;
use Exception;
@@ -88,6 +90,21 @@
// CACHE MANAGEMENT
// =========================================================================
+ protected function checkCache(string $key, $request):WP_REST_Response|false
+ {
+ // Check HTTP cache headers with the specific content type
+ $cache_check = $this->checkHeaders($request, $key);
+ if ($cache_check) {
+ return $cache_check;
+ }
+
+ $cache = $this->cache->get($key);
+ if ($cache) {
+ $response = Response::success($cache);
+ return $this->addCacheHeaders($response);
+ }
+ return false;
+ }
/**
* Check request headers for conditional caching (ETag, If-Modified-Since)
*/
@@ -207,7 +224,7 @@
// Keep existing author filtering logic
$authorQuery = [];
- foreach (jvbAuthorUsers() as $type) {
+ foreach (Registrar::withFeature('can_create', 'user') as $type) {
if (array_key_exists($type, $data)) {
$artist_ids = array_map(
'absint',
@@ -240,7 +257,7 @@
//Handle random
if (array_key_exists('orderby', $data) && $data['orderby'] === 'random') {
- $current_seed = jvbGetRandomSeed();
+ $current_seed = floor(time() / 1800);
$args['orderby'] = 'RAND(' . $current_seed . ')';
unset($args['order']);
return $args;
@@ -305,19 +322,19 @@
$content = jvbNoBase($post_type);
// Get config for this content type
- $config = Features::getConfig($content);
- if (!$config) {
+ $registrar = Registrar::getInstance($content);
+ if (!$registrar) {
return null;
}
// Check if this orderby is a custom order
- $customOrders = $config['custom_order'] ?? [];
+ $customOrders = $registrar->custom_order??[];
if (empty($customOrders) || !isset($customOrders[$orderby])) {
return null;
}
// Get field definition
- $fields = $config['fields'] ?? [];
+ $fields = $registrar->getFields() ?? [];
if (!isset($fields[$orderby])) {
return null;
}
@@ -456,15 +473,6 @@
}
/**
- * Check if content type exists
- */
- protected function checkContent(string $content, bool $returnBool = false): string|bool
- {
- $result = JVB_CONTENT[$content] ?? JVB_TAXONOMY[$content] ?? JVB_USER[$content] ?? '';
- return $returnBool ? ($result !== '') : $result;
- }
-
- /**
* Check if user exists (cached)
*/
protected function checkUser(int $userId): bool
@@ -525,13 +533,12 @@
***************************************************************************/
protected function isTimeline($args, $data):bool
{
- $post_types = is_array($args['post_type']) ? $args['post_type'] : [$args['post_type']];
- foreach ($post_types as $type) {
- if (Features::forContent($type)->has('is_timeline')) {
- return true;
- }
+ if (!array_key_exists('post_type', $args)) {
+ return false;
}
- return false;
+ $post_types = is_array($args['post_type']) ? $args['post_type'] : [$args['post_type']];
+ $hasTimeline = array_map(function($item) { return jvbCheckBase($item); },Registrar::withFeature('is_timeline', 'post'));
+ return !empty(array_intersect($post_types, $hasTimeline));
}
// =========================================================================
// SECURITY
@@ -542,7 +549,7 @@
*/
protected function verifyTurnstile(string $token): bool
{
- if (!Features::hasIntegration('cloudflare') || !JVB()->connect('cloudflare')->isSetUp()) {
+ if (!Site::hasIntegration('cloudflare') || !JVB()->connect('cloudflare')->isSetUp()) {
return true;
}
@@ -724,4 +731,177 @@
delete_user_meta($user_id, BASE . 'session_fingerprint');
delete_user_meta($user_id, BASE . 'session_timestamp');
}
+
+ /*******************************
+ * META HELPERS
+ *******************************/
+ public function getFieldsOfType(array $fields, string|array $type, Meta $meta, array $subType = []):array
+ {
+ $gotFields = [];
+ if (is_string($type)) {
+ $type = [$type];
+ }
+ foreach ($fields as $field => $value) {
+ //Skip empty values
+ if (empty($value)) {
+ continue;
+ }
+ $config = $meta->config($field);
+ if (in_array($config['type'], ['group', 'repeater', 'tagList'])) {
+ foreach ($config['fields'] as $subfield => $subConfig) {
+ if (is_numeric($subfield) && array_key_exists('name', $subConfig)) {
+ $subfield = $subConfig['name'];
+ }
+ if (is_numeric($subfield)) continue;
+ if (array_key_exists('type', $subConfig) && in_array($subConfig['type'], $type)) {
+ $gotFields[] = $field.':'.$subfield;
+ }
+ }
+ } elseif (in_array($config['type'], $type)) {
+ $gotFields[] = $field;
+ } else if ((!empty($subType) && in_array($config['type'], array_keys($subType)) && in_array($config['subtype'], array_values($subType)))) {
+ $gotFields[] = $field;
+ }
+ }
+ return $gotFields;
+ }
+
+
+ protected function extractImages(array $fields, Meta $meta):array
+ {
+ $images = [];
+ $get = $this->getFieldsOfType($fields, ['upload', 'gallery','image'], $meta);
+ if (!empty($get)) {
+ $baseFields = array_map(function($fieldName) {
+ return (str_contains($fieldName, ':')) ? strtok($fieldName, ':') : $fieldName;
+ }, $get);
+
+ $temp = array_map(
+ function($item) {
+ return explode(':', $item);
+ },
+ array_filter($get, function($fieldName) {
+ return str_contains($fieldName, ':');
+ })
+ );
+ $complex = [];
+ foreach ($temp as $tmp) {
+ $complex[$tmp[0]] = $tmp[1];
+ }
+
+ $fields = array_filter($fields, function ($field) use ($baseFields) {
+ return in_array($field, $baseFields);
+ }, ARRAY_FILTER_USE_KEY);
+
+ foreach ($fields as $fieldName => $value) {
+ //Check if it's a complex field
+ if (array_key_exists($fieldName, $complex)) {
+ $check = $complex[$fieldName];
+ foreach ($value as $row) {
+ foreach ($row as $fName => $fValue) {
+ if ($fName === $check && !empty($fValue)) {
+ $images = $this->addImages($fValue, $images);
+ }
+ }
+ }
+ } else {
+ $images = $this->addImages($value, $images);
+ }
+ }
+ }
+ return $images;
+ }
+ public function addImages(string $imgs, array $images):array
+ {
+ $temp = explode(',', $imgs);
+ foreach ($temp as $img) {
+ if (is_numeric($img) && !array_key_exists($img, $images) && $img > 0) {
+ $images[$img] = jvbImageData((int)$img);
+ }
+ }
+ return $images;
+ }
+
+ protected function extractTerms(array $fields, Meta $meta):array
+ {
+ $terms = [];
+ $get = $this->getFieldsOfType($fields, ['taxonomy'], $meta, ['selector' => 'taxonomy']);
+ if (!empty($get)) {
+ $baseFields = array_map(function($fieldName) {
+ return (str_contains($fieldName, ':')) ? strtok($fieldName, ':') : $fieldName;
+ }, $get);
+
+ $complex = array_map(
+ function($item) {
+ return explode(':', $item);
+ },
+ array_filter($get, function($fieldName) {
+ return str_contains($fieldName, ':');
+ })
+ );
+
+ $fields = array_filter($fields, function ($field) use ($baseFields) {
+ return in_array($field, $baseFields);
+ }, ARRAY_FILTER_USE_KEY);
+
+ foreach ($fields as $fieldName => $value) {
+ $config = $meta->config($fieldName);
+ //Check if it's a complex field
+ if (array_key_exists($fieldName, $complex)) {
+ foreach ($value as $row) {
+ foreach ($row as $fName => $fValue) {
+ if (in_array($fName, $complex[$fieldName])) {
+ $terms = $this->addTerms($fValue, $terms, $config);
+ }
+ }
+ }
+ } else {
+
+ $terms = $this->addTerms($value, $terms, $config);
+ }
+ }
+ }
+ return $terms;
+
+ }
+
+ protected function addTerms(string $value, array $terms, array $config):array
+ {
+ $taxonomy = jvbNoBase($config['taxonomy']);
+ if (empty($value)) {
+ return $terms;
+ }
+ $ids = array_map('absint', explode(',',$value));
+ $cache = Cache::for('term_data')->connect('taxonomy');
+ $cache->flush();
+ if (!array_key_exists($taxonomy, $terms)) {
+ $terms[$taxonomy] = [];
+ $registrar = Registrar::getInstance($taxonomy);
+ $terms[$taxonomy]['icon'] = $registrar ? $registrar->getIcon() : jvbDefaultIcon();;
+ }
+ foreach ($ids as $id) {
+ $data = $cache->remember(
+ $id,
+ function () use ($id, $taxonomy) {
+ $term = get_term($id, $taxonomy);
+ if ($term && !is_wp_error($term)) {
+ return [
+ 'id' => $term->term_id,
+ 'name' => $term->name,
+ 'slug' => $term->slug,
+ 'parent' => $term->parent,
+ 'path' => JVB()->routes('term')->getTermPath($term->term_id, $term->name, $taxonomy),
+ 'taxonomy' => jvbNoBase($term->taxonomy),
+ 'count' => $term->count,
+ ];
+ }
+ return [];
+ }
+ );
+ if (!empty($data)) {
+ $terms[$taxonomy][$id] = $data;
+ }
+ }
+ return $terms;
+ }
}
--
Gitblit v1.10.0