| | |
| | | { |
| | | $this->cache_name = 'terms'; |
| | | parent::__construct(); |
| | | // $this->cache->invalidateGroup('terms'); |
| | | if (JVB_TESTING) { |
| | | $this->cache->flush(); |
| | | } |
| | | $this->per_page = 20; |
| | | |
| | | add_action('edited_term', [$this, 'deleteTermPath']); |
| | | add_action('wp_login', [$this, 'clearUserTaxonomyCache'], 10, 2); |
| | | } |
| | |
| | | |
| | | $data = [ |
| | | 'id' => $term->term_id, |
| | | 'name' => $term->name, |
| | | 'name' => html_entity_decode($term->name), |
| | | 'slug' => $term->slug, |
| | | 'taxonomy' => $term->taxonomy, |
| | | ]; |
| | |
| | | $term = get_term($rel->related_term_id, $rel->related_taxonomy); |
| | | return [ |
| | | 'id' => $rel->related_term_id, |
| | | 'name' => $term ? $term->name : 'Unknown', |
| | | 'name' => $term ? html_entity_decode($term->name) : 'Unknown', |
| | | 'count' => $rel->relationship_count |
| | | ]; |
| | | }, $relationships); |
| | |
| | | public function handleTermSelectionRequest(WP_REST_Request $request):WP_REST_Response |
| | | { |
| | | $data = $request->get_params(); |
| | | $taxonomy = jvbCheckBase($data['taxonomy'])??''; |
| | | |
| | | $taxonomy = sanitize_text_field($data['taxonomy'])??''; |
| | | // Check HTTP cache headers |
| | | $cache_check = $this->checkHeaders($request, $taxonomy); |
| | | if ($cache_check) { |
| | |
| | | if (str_contains($taxonomy, ',')) { |
| | | return $this->handleBatchTermRequest($taxonomy, $data, $request); |
| | | } |
| | | $taxonomy = jvbCheckBase($taxonomy); |
| | | |
| | | if (array_key_exists('termIDs', $data)) { |
| | | $args = [ |
| | | 'taxonomy' => $taxonomy, |
| | | 'include' => $data['termIDs'], |
| | | 'hide_empty' => false, |
| | | 'hide_empty' => true, |
| | | ]; |
| | | $key = $this->cache->generateKey($args); |
| | | $cached = $this->cache->get($key); |
| | |
| | | 'page' => 1, |
| | | 'per_page' => $per_page, |
| | | 'total_pages' => 0, |
| | | 'total_terms' => 0, |
| | | 'has_more' => false |
| | | ] |
| | | 'total_terms' => 0 |
| | | ], |
| | | 'has_more' => false |
| | | ]); |
| | | } |
| | | |
| | |
| | | // Get terms for current level with child count |
| | | $args = [ |
| | | 'taxonomy' => $taxonomy, |
| | | 'hide_empty' => false, |
| | | 'hide_empty' => true, |
| | | 'parent' => $parent, |
| | | 'number' => $per_page, |
| | | 'orderby'=> 'name', |
| | |
| | | 'per_page' => $per_page, |
| | | 'total_pages' => 0, |
| | | 'total_terms' => 0, |
| | | 'has_more' => false |
| | | ] |
| | | ], |
| | | 'has_more' => false |
| | | ]); |
| | | return $this->addCacheHeaders($response); |
| | | } |
| | |
| | | 'page' => 1, |
| | | 'per_page' => $per_page, |
| | | 'total_pages' => 0, |
| | | 'total_terms' => 0, |
| | | 'has_more' => false |
| | | ] |
| | | 'total_terms' => 0 |
| | | ], |
| | | 'has_more' => false |
| | | ]); |
| | | return $this->addCacheHeaders($response); |
| | | } |
| | |
| | | 'page' => 1, |
| | | 'per_page' => $per_page, |
| | | 'total_pages' => 0, |
| | | 'total_terms' => 0, |
| | | 'has_more' => false |
| | | ] |
| | | 'total_terms' => 0 |
| | | ], |
| | | 'has_more' => false |
| | | ]); |
| | | |
| | | return $this->addCacheHeaders($response); |
| | |
| | | 'page' => $page, |
| | | 'per_page' => $per_page, |
| | | 'total_pages' => $total_pages, |
| | | 'total_terms' => (int)$total_terms, |
| | | 'has_more' => $has_more |
| | | ] |
| | | 'total_terms' => (int)$total_terms |
| | | ], |
| | | 'has_more' => $has_more |
| | | ]; |
| | | |
| | | $this->cache->set($key, $response); |
| | |
| | | 'items' => $all_terms, |
| | | 'pagination'=> [ |
| | | 'page' => $page, |
| | | 'per_page'=> $per_page, |
| | | 'has_more' => true, |
| | | ] |
| | | 'per_page'=> $per_page |
| | | ], |
| | | 'has_more' => true, |
| | | ]; |
| | | |
| | | $response = new WP_REST_Response($response); |
| | |
| | | return $this->cache->remember($cache_key, function() use ($term, $taxonomy) { |
| | | $data = [ |
| | | 'id' => $term->term_id, |
| | | 'name' => $term->name, |
| | | 'name' => html_entity_decode($term->name), |
| | | 'slug' => $term->slug, |
| | | 'parent' => $term->parent, |
| | | 'path' => $this->getTermPath($term->term_id, $term->name, $taxonomy), |
| | |
| | | // When searching, we want to search across all terms regardless of hierarchy |
| | | $args = [ |
| | | 'taxonomy' => $taxonomy, |
| | | 'hide_empty' => false, |
| | | 'hide_empty' => true, |
| | | 'search' => $search, |
| | | 'search_columns' => ['name', 'slug'], |
| | | 'fields' => 'all', |
| | |
| | | 'page' => 0, |
| | | 'per_page' => 20, |
| | | 'total_pages' => 0, |
| | | 'total_terms' => 0, |
| | | 'has_more' => false |
| | | ] |
| | | 'total_terms' => 0 |
| | | ], |
| | | 'has_more' => false |
| | | ]); |
| | | } |
| | | |
| | |
| | | 'page' => (int)$page, |
| | | 'per_page' => (int)$per_page, |
| | | 'total_pages' => $total_pages, |
| | | 'total_terms' => (int)$total_terms, |
| | | 'has_more' => $has_more |
| | | ] |
| | | 'total_terms' => (int)$total_terms |
| | | ], |
| | | 'has_more' => $has_more |
| | | ]; |
| | | |
| | | $this->cache->set($key, $response); |
| | |
| | | 'page' => (int)$page, |
| | | 'per_page' => (int)$per_page, |
| | | 'total_terms'=> $total, |
| | | 'total_pages'=> $total_pages, |
| | | 'has_more' => $page < $total_pages |
| | | ] |
| | | 'total_pages'=> $total_pages |
| | | ], |
| | | 'has_more' => $page < $total_pages |
| | | ]; |
| | | |
| | | // Cache results |
| | |
| | | // Build query args |
| | | $args = [ |
| | | 'taxonomy' => $taxonomy, |
| | | 'hide_empty' => false, |
| | | 'hide_empty' => true, |
| | | 'orderby' => $search ? 'name' : 'count', |
| | | 'order' => $search ? 'ASC' : 'DESC', |
| | | 'number' => $per_page, |
| | |
| | | 'page' => 0, |
| | | 'per_page' => 20, |
| | | 'total_pages' => 0, |
| | | 'total_terms' => 0, |
| | | 'has_more' => 0 |
| | | ] |
| | | 'total_terms' => 0 |
| | | ], |
| | | 'has_more' => 0 |
| | | ]); |
| | | } |
| | | |
| | |
| | | 'page' => (int)$page, |
| | | 'per_page' => (int)$per_page, |
| | | 'total_pages' => $total_pages, |
| | | 'total_terms' => (int)$total, |
| | | 'has_more' => $page < $total_pages |
| | | ] |
| | | 'total_terms' => (int)$total |
| | | ], |
| | | 'has_more' => $page < $total_pages |
| | | ]; |
| | | |
| | | // Cache results |
| | |
| | | 'message' => 'Term already exists', |
| | | 'term' => [ |
| | | 'id' => $term->term_id, |
| | | 'name' => $term->name, |
| | | 'name' => html_entity_decode($term->name), |
| | | 'path' => $this->getTermPath($term->term_id, $term->name, $taxonomy) |
| | | ] |
| | | ]); |