| | |
| | | add_action('before_delete_post', [$this, 'cleanupPostFavourites']); |
| | | add_action('delete_term', [$this, 'cleanupTermFavourites'], 10, 3); |
| | | |
| | | add_action('jvbUserRegistered', [$this, 'maybeAcceptListInvite'], 10, 3); |
| | | |
| | | // Register cleanup scheduler |
| | | add_action('jvb_cleanupOrphanedFavourites', [$this, 'cleanupOrphanedFavourites']); |
| | |
| | | 'success' => false, |
| | | 'message' => 'No user set' |
| | | ]; |
| | | }elseif (count($args) === 1 || (array_key_exists('all', $args) && $args['all'] === true)) { |
| | | } |
| | | // Check HTTP cache headers for user-specific data |
| | | $cache_check = $this->checkUserHeaders($request, $args['user'], 'favourites'); |
| | | if ($cache_check) { |
| | | return $cache_check; |
| | | } |
| | | |
| | | if (count($args) === 1 || (array_key_exists('all', $args) && $args['all'] === true)) { |
| | | $result = $this->getAllFavourites($args['user']); |
| | | } else { |
| | | $result = $this->cache->remember( |
| | | $args, |
| | | function() use ($args) { |
| | | return $this->getFilteredFavourites($args); |
| | | $response = new WP_REST_Response($this->getFilteredFavourites($args)); |
| | | return $this->addCacheHeaders($response); |
| | | } |
| | | ); |
| | | } |
| | | return new WP_REST_Response($result); |
| | | $response = new WP_REST_Response($result); |
| | | return $this->addCacheHeaders($response); |
| | | } |
| | | |
| | | protected function getFilteredFavourites(array $args):array |
| | |
| | | public function getLists(WP_REST_Request $request):WP_REST_Response |
| | | { |
| | | $user_id = get_current_user_id(); |
| | | |
| | | if (!$user_id || !$this->userCheck($user_id)) { |
| | | return new WP_REST_Response([ |
| | | 'success' => false, |
| | | 'message' => 'Invalid user' |
| | | ]); |
| | | } |
| | | |
| | | // Check HTTP cache headers |
| | | $cache_check = $this->checkUserHeaders($request, $user_id, 'favourites_lists'); |
| | | if ($cache_check) { |
| | | return $cache_check; |
| | | } |
| | | |
| | | $list_id = $request->get_param('id'); |
| | | |
| | | if ($list_id) { |
| | |
| | | $response = $this->getAvailableLists($user_id); |
| | | } |
| | | |
| | | return new WP_REST_Response($response); |
| | | $response = new WP_REST_Response($response); |
| | | return $this->addCacheHeaders($response); |
| | | } |
| | | /** |
| | | * Get lists available to a user (owned and shared) |
| | |
| | | */ |
| | | public function getShares(WP_REST_Request $request):WP_REST_Response |
| | | { |
| | | $list_id = $request->get_param('list_id'); |
| | | $user_id = get_current_user_id(); |
| | | $user_id = $request->get_param('user'); |
| | | |
| | | if (!$user_id || !$this->userCheck($user_id)) { |
| | | return new WP_REST_Response([ |
| | | 'success' => false, |
| | | 'message' => 'Invalid user' |
| | | ]); |
| | | } |
| | | |
| | | // 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) { |
| | | return $this->createErrorResponse( |
| | |
| | | // Cache the results |
| | | $this->cache->set($key, $response_data, 'favourites_list_shares'); |
| | | |
| | | return new WP_REST_Response($response_data); |
| | | $response = new WP_REST_Response($response_data); |
| | | return $this->addCacheHeaders($response); |
| | | |
| | | } catch (Exception $e) { |
| | | return $this->createErrorResponse( |
| | |
| | | } |
| | | } |
| | | |
| | | public function maybeAcceptListInvite(int $user_id, string $email, array $data):void |
| | | { |
| | | if (array_key_exists('list_token', $data) && !empty($data['list_token'])) { |
| | | $this->acceptListInvitation($data['list_token'], $email); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Get the owner ID for a content item |
| | | * |