| | |
| | | |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\Instance; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\managers\queue\executors\ContentExecutor; |
| | | use JVBase\managers\queue\Queue; |
| | | use JVBase\managers\queue\TypeConfig; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\registrar\Registrar; |
| | |
| | | |
| | | class ContentRoutes extends Rest |
| | | { |
| | | use Instance; |
| | | protected array $fields = []; |
| | | protected array $taxonomies = []; |
| | | protected string $post_type = ''; |
| | |
| | | protected array $timelineUniqueFields = []; |
| | | protected static ?string $action = 'dash-'; |
| | | protected Meta $meta; |
| | | protected Cache $termCache; |
| | | |
| | | public function __construct() |
| | | { |
| | | $this->cacheName = 'user_content_' . get_current_user_id(); |
| | | $this->cacheName = 'user_content'; |
| | | parent::__construct(); |
| | | $this->cache = Cache::for($this->cacheName, 'post', DAY_IN_SECONDS); |
| | | $this->cache->user(); |
| | | $this->termCache = Cache::for($this->cacheName.'_terms', 'term', DAY_IN_SECONDS); |
| | | $this->termCache->user(); |
| | | |
| | | |
| | | if (JVB_TESTING) { |
| | | $this->cache->flush(); |
| | | } |
| | | $this->cache->connect('post', true); |
| | | $this->cache->connect('term', true); |
| | | add_action('init', [$this, 'registerContentExecutors'], 5); |
| | | } |
| | | |
| | |
| | | */ |
| | | public function registerContentExecutors(): void |
| | | { |
| | | $registry = JVB()->queue()->registry(); |
| | | $registry = Queue::getInstance()->registry(); |
| | | $executor = new ContentExecutor(); |
| | | |
| | | // Content updates - chunked at 10 posts |
| | |
| | | 'dateFrom' => 'string', |
| | | 'dateTo' => 'string', |
| | | ]) |
| | | ->rateLimit(20) |
| | | ->rateLimit(30) |
| | | ->post([$this, 'postContent']) |
| | | ->auth(PermissionHandler::combine(['user', 'nonce', ['actionNonce'=>'dash-']])) |
| | | ->rateLimit(30) |
| | | ->rateLimit() |
| | | ->args([ |
| | | 'user' => 'int|required', |
| | | 'posts' => 'required', |
| | | 'content' => 'string', |
| | | ]) |
| | |
| | | public function postContent(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $data = $request->get_params(); |
| | | $user_id = $data['user']; |
| | | $user_id = get_current_user_id(); |
| | | |
| | | if (!array_key_exists('posts', $data) || !is_array($data['posts'])) { |
| | | return Response::success(['message'=>'No posts found in request']); |
| | |
| | | |
| | | $count = count($data['posts']); |
| | | $operationId = $data['id']; |
| | | unset($data['user']); |
| | | unset($data['id']); |
| | | |
| | | error_log('[CONTENT]:'.print_r($data, true)); |
| | | $queue = JVB()->queue(); |
| | | $queue = Queue::getInstance(); |
| | | $queue->queueOperation( |
| | | 'content_update', |
| | | $user_id, |
| | |
| | | } |
| | | |
| | | $key = $this->cache->generateKey($args); |
| | | $cached = $this->checkCache($key, $request); |
| | | $cached = $this->cache->get($key); |
| | | if ($cached) { |
| | | return $cached; |
| | | } |
| | |
| | | |
| | | $this->cache->set($key, $data); |
| | | |
| | | $response = Response::success($data); |
| | | return $this->addCacheHeaders($response); |
| | | return Response::success($data); |
| | | } |
| | | public function getTerms(WP_REST_Request $request, array $params, Registrar $registrar):WP_REST_Response |
| | | { |
| | |
| | | |
| | | $key = $this->cache->generateKey($args); |
| | | // Check HTTP cache headers with the specific content type |
| | | $cache_check = $this->checkHeaders($request, $key); |
| | | if ($cache_check) { |
| | | return $cache_check; |
| | | } |
| | | // $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 Response::success($cache); |
| | | } |
| | | $this->post_type = jvbCheckBase($params['content'] ?? $params['type']); |
| | | // Only expand search to taxonomies if we're actually going to query |
| | |
| | | |
| | | $this->cache->set($key, $data); |
| | | |
| | | $response = Response::success($data); |
| | | return $this->addCacheHeaders($response); |
| | | return Response::success($data); |
| | | } |
| | | |
| | | protected function applySearchFilters(array $args, array $params): array |