| | |
| | | add_action('post_type_link', [$this, 'rewriteTaxonomySingle'], 15, 2); |
| | | add_filter('post_type_archive_link', [$this, 'rewriteTaxonomyArchive'], 15, 2); |
| | | } |
| | | |
| | | $postType = $this->post_type; |
| | | add_action("save_post_{$this->post_type}", function($post_id, $post, $update) use ($postType) { |
| | | if (jvbNoSaveIt($post_id, $post)) { |
| | | return; |
| | | } |
| | | $this->invalidatePostCache($postType, $post, $update ? 'update' : 'create'); |
| | | }, 10, 3); |
| | | |
| | | add_action("delete_post", function($post_id, $post) use ($postType) { |
| | | $post = get_post($post_id); |
| | | if ($post && $post->post_type === $postType) { |
| | | $this->invalidatePostCache($postType, $post, 'delete'); |
| | | } |
| | | }, 10, 2); |
| | | |
| | | add_action("transition_post_status", function($new_status, $old_status, $post) use ($postType) { |
| | | if ($post->post_type === $postType && $new_status !== $old_status) { |
| | | $this->invalidatePostCache($postType, $post, 'status_change'); |
| | | } |
| | | }, 10, 3); |
| | | } |
| | | |
| | | protected function invalidatePostCache(string $type, $post, string $action) { |
| | | error_log('Clearing Cache for '.print_r($type, true)); |
| | | |
| | | $cache = CacheManager::for(jvbNoBase($type))->invalidate(); |
| | | |
| | | // Clear related caches (taxonomies attached to this post) |
| | | $taxonomies = get_object_taxonomies($post->post_type); |
| | | foreach ($taxonomies as $taxonomy) { |
| | | $terms = wp_get_post_terms($post->ID, $taxonomy, ['fields' => 'ids']); |
| | | if (!empty($terms)) { |
| | | CacheManager::for(jvbNoBase($taxonomy))->invalidate(); |
| | | } |
| | | } |
| | | |
| | | // Trigger custom action for additional handling |
| | | do_action("jvb_cache_invalidated_{$type}", $post, $action); |
| | | } |
| | | |
| | | public function hideFromPublic(bool $is_viewable): bool |