Jake Vanderwerf
2026-01-25 b38f03c0e7218762d90fa5092696b127f24f36db
inc/rest/routes/FavouritesRoutes.php
@@ -2,8 +2,8 @@
namespace JVBase\rest\routes;
use JVBase\JVB;
use JVBase\managers\Cache;
use JVBase\rest\RestRouteManager;
use JVBase\managers\CacheManager;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
@@ -16,16 +16,21 @@
class FavouritesRoutes extends RestRouteManager
{
    protected array $valid_types;
    protected int $user_id;
   protected Cache $listsCache;
   protected Cache $sharedListsCache;
   protected Cache $favouritesCache;
    public function __construct()
    {
        $this->cache_name = 'favourites';
        parent::__construct();
      $this->cache->connect('post')->connect('user')->connect('taxonomy');
      $this->listsCache = Cache::for('lists')->connect('favourites', true);
      $this->sharedListsCache = Cache::for('sharedLists')->connect('favourites', true);
      $this->favouritesCache = Cache::for('allFavourites')->connect('favourites', true);
        $this->valid_types = array_keys(array_merge(JVB_CONTENT, JVB_TAXONOMY));
        $this->user_id = get_current_user_id();
        $this->action = 'favourites-';
@@ -118,13 +123,14 @@
    {
      $args = $this->buildParams($request);
      if (!$args['user'] || $args['user'] === ''){
         $result = [
         return $this->addCacheHeaders(new WP_REST_Response([
            'success'   => false,
            'message'   => 'No user set'
         ];
         ]));
      }
      $key = $this->cache->generateKey($args);
      // Check HTTP cache headers for user-specific data
      $cache_check = $this->checkUserHeaders($request, $args['user'], 'favourites');
      $cache_check = $this->checkHeaders($request, $key);
      if ($cache_check) {
         return $cache_check;
      }
@@ -133,10 +139,9 @@
            $result = $this->getAllFavourites($args['user']);
      } else {
         $result = $this->cache->remember(
            $args,
            $this->cache->generateKey($args),
            function() use ($args) {
               $response = new WP_REST_Response($this->getFilteredFavourites($args));
               return $this->addCacheHeaders($response);
               return $this->getFilteredFavourites($args);
            }
         );
      }
@@ -237,7 +242,7 @@
        }
      $result = $this->cache->remember(
         'user_'.$user_id.'_all_favourites',
         $user_id,
         function() use ($user_id) {
            return $this->fetchAllFavourites($user_id);
         }
@@ -398,8 +403,15 @@
         ]);
      }
      $params = [
         'user'   => $user_id,
      ];
      if ($request->get_param('id')) {
         $params['list'] = sanitize_text_field($request->get_param('id'));
      }
      $key = $this->listsCache->generateKey($params);
      // Check HTTP cache headers
      $cache_check = $this->checkUserHeaders($request, $user_id, 'favourites_lists');
      $cache_check = $this->checkHeaders($request, $key);
      if ($cache_check) {
         return $cache_check;
      }
@@ -427,18 +439,11 @@
        if (!$this->checkUser($user_id)) {
            return [];
        }
        $key = sprintf(
            'user_%d_lists',
            $user_id
        );
        if ($include_shared) {
            $key = $key.'_shared';
        }
        $cache = $this->cache->get($key, 'favourites_lists');
        if ($cache) {
            return $cache;
        }
      $cache = ($include_shared) ? $this->sharedListsCache : $this->listsCache;
      return $cache->remember(
         $user_id,
         function() use ($user_id, $include_shared) {
        global $wpdb;
        error_log('Attempting to get available lists..');
        $lists_table = $wpdb->prefix . BASE . 'favourites_lists';
@@ -489,10 +494,6 @@
                    'shared'=> $shared_lists,
                ];
            }
            // Cache result
            $this->cache->set($key, ['success' => true, 'lists'=>$lists], 'favourites_lists');
            error_log('Lists: '.print_r($lists, true));
            return [
                'success' => true,
@@ -509,6 +510,8 @@
            return [];
        }
    }
      );
    }
    /**
     * Get detailed information for a single list
@@ -524,7 +527,7 @@
            $user_id,
            $list_id
        );
        $cache = $this->cache->get($key, 'favourites_lists');
        $cache = $this->listsCache->get($key);
        if ($cache) {
            return new WP_REST_Response($cache);
        }
@@ -649,7 +652,7 @@
            ]
        ];
        $this->cache->set($key, $response_data, 'favourites_lists');
        $this->listsCache->set($key, $response_data);
        return new WP_REST_Response($response_data);
    }
@@ -830,11 +833,6 @@
         ]);
      }
      // Check HTTP cache headers
      $cache_check = $this->checkUserHeaders($request, $user_id, 'favourites_shares');
      if ($cache_check) {
         return $cache_check;
      }
      $list_id = $request->get_param('list_id');
        if (!$list_id) {
@@ -845,12 +843,20 @@
            );
        }
        $key = sprintf(
            'user_%d_shares_for_list_%d',
            $user_id,
            $list_id
        );
        $cache = $this->cache->get($key, 'favourites_list_shares');
      $args = [
         'user'   => $user_id,
         'list'   => sanitize_text_field($list_id),
      ];
      $key = $this->sharedListsCache->generateKey($args);
      // Check HTTP cache headers
      $cache_check = $this->checkHeaders($request, $key);
      if ($cache_check) {
         return $cache_check;
      }
        $cache = $this->sharedListsCache->get($key);
        if ($cache) {
            return new WP_REST_Response($cache);
        }
@@ -925,7 +931,7 @@
            ];
            // Cache the results
            $this->cache->set($key, $response_data, 'favourites_list_shares');
            $this->sharedListsCache->set($key, $response_data);
         $response = new WP_REST_Response($response_data);
         return $this->addCacheHeaders($response);
@@ -1265,7 +1271,7 @@
                'target_id' => $term_id,
                'date_added' => $item->date_added ?? current_time('mysql'),
                'notes' => $item->notes ?? '',
                'title' => $term->name,
                'title' => html_entity_decode($term->name),
                'url' => get_term_link($term)
            ];
@@ -1442,8 +1448,8 @@
            }
            error_log('Results: '.print_r($results, true));
            $this->cache->invalidate('favourite_counts_by_type_' . $user_id.'_all');
            $this->cache->invalidate('favourite_counts_by_type_' . $user_id.'_not_all');
            $this->cache->forget('favourite_counts_by_type_' . $user_id.'_all');
            $this->cache->forget('favourite_counts_by_type_' . $user_id.'_not_all');
            return [
                'success' => true,
                'result' => $results
@@ -1637,8 +1643,10 @@
            $this->removeRelatedNotifications($user_id, $type, $target_id);
            // Invalidate cache
            CacheManager::invalidateGroup($this->cache_name);
            CacheManager::invalidateGroup('favourites_lists');
         $this->cache->flush();
         $this->listsCache->flush();
         $this->sharedListsCache->flush();
         $this->favouritesCache->flush();
            return [
                'success' => true,
@@ -1722,9 +1730,9 @@
                }
                // Invalidate notification cache for this user
                if (method_exists(JVB()->notification(), 'clearNotificationCache')) {
                    JVB()->notification()->clearNotificationCache($owner_id);
                }
//                if (method_exists(JVB()->notification(), 'clearNotificationCache')) {
//                    JVB()->notification()->clearNotificationCache($owner_id);
//                }
            }
        } catch (Exception $e) {
            // Log but continue
@@ -2011,7 +2019,7 @@
            $wpdb->query('COMMIT');
            // Invalidate relevant caches
            CacheManager::invalidateGroup('favourites_lists');
         $this->listsCache->flush();
            return [
                'success' => true,
@@ -3302,47 +3310,47 @@
            switch ($operation->type) {
                case 'favourites_batch':
                    $response = $this->processBatches($user_id, $data);
                    CacheManager::invalidateGroup($this->cache_name);
               $this->cache->flush();
                    return $response;
                case 'favourite_notes':
                    $response =  $this->processNote($user_id, $data);
                    CacheManager::invalidateGroup($this->cache_name);
               $this->cache->flush();
                    return $response;
                case 'favourite_list_create':
                    $response = $this->processListCreate($user_id, $data);
                    CacheManager::invalidateGroup('favourites_lists');
               $this->listsCache->flush();
                    return $response;
                case 'favourite_list_update':
                    $response = $this->processUpdateList($user_id, $data);
                    CacheManager::invalidateGroup('favourites_lists');
               $this->listsCache->flush();
                    return $response;
                case 'favourite_list_delete':
                    $response = $this->processListDeletion($user_id, $data);
                    CacheManager::invalidateGroup('favourites_lists');
                    $this->listsCache->flush();
                    return $response;
                case 'favourite_list_add':
                    $response = $this->processAddToList($user_id, $data);
                    CacheManager::invalidateGroup('favourites_lists');
                    $this->listsCache->flush();
                    return $response;
                case 'favourite_list_remove':
                    $response = $this->removeFromList($user_id, $data);
                    CacheManager::invalidateGroup('favourites_lists');
                    $this->listsCache->flush();
                    return $response;
                case 'favourite_list_share':
                    $response = $this->shareList($user_id, $data);
                    CacheManager::invalidateGroup('favourites_lists_shares');
               $this->sharedListsCache->flush();
                    return $response;
                case 'favourite_list_unshare':
                    $response = $this->unshareList($user_id, $data);
                    CacheManager::invalidateGroup('favourites_lists_shares');
               $this->sharedListsCache->flush();
                    return $response;
                default: