Jake Vanderwerf
2025-09-30 2cb91676044ecd0abd9c45b4835abb8b0d042312
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace JVBase\registry;
 
use WP_Post;
use JVBase\meta\MetaRegistry;
use JVBase\managers\CacheManager;
if (!defined('ABSPATH')) {
    exit;
}
class PostTypeRegistrar
{
    private string $slug;
    private array $config;
    protected array $fields;
    private string $post_type;
    private FieldRegistry $fieldRegistry;
 
    public function __construct(string $slug, array $config)
    {
        $this->slug = $slug;
        $this->config = $config;
        $this->post_type = BASE . $slug;
        $this->fieldRegistry = FieldRegistry::getInstance();
        $this->fields = $this->fieldRegistry->getFields($slug, 'post');
 
        $this->registerHooks();
    }
 
    public function register(): void
    {
        $singular = $this->config['singular'] ?? ucfirst($this->slug);
        $plural = $this->config['plural'] ?? $singular . 's';
        $loweredPlural = strtolower($plural);
        $args = [
            'labels'              => $this->buildLabels($singular, $plural),
            'public'              => $this->config['public'] ?? true,
            'publicly_queryable'  => $this->config['publicly_queryable'] ?? true,
            'show_ui'             => $this->config['show_ui'] ?? true,
//          'show_in_menu'        => false,
            'show_in_menu'        => $this->config['show_in_menu'] ?? true,
            'query_var'           => $this->config['query_var'] ?? true,
            'rewrite'             => $this->config['rewrite'] ?? ['slug' => $this->slug, 'with_front' => false],
            'capability_type'     => [$this->slug, $loweredPlural],
            'capabilities'    => [
                'edit_post'          => "edit_{$this->slug}",
                'read_post'          => "read_{$this->slug}",
                'delete_post'        => "delete_{$this->slug}",
                'edit_posts'         => "edit_{$loweredPlural}",
                'edit_others_posts'  => "edit_others_{$loweredPlural}",
                'publish_posts'      => "publish_{$loweredPlural}",
                'read_private_posts' => "read_private_{$loweredPlural}",
                'create_posts'       => "edit_{$loweredPlural}",
            ],
            'has_archive'         => $this->config['has_archive'] ?? true,
            'hierarchical'        => $this->config['hierarchical'] ?? false,
            'menu_position'       => $this->config['menu_position'] ?? null,
            'supports'            => $this->config['supports'] ??  ['title', 'author', 'thumbnail', 'editor', 'revisions', 'custom-fields', 'excerpt', 'content'],
            'show_in_rest'        => $this->config['show_in_rest'] ?? true,
        ];
 
        if (jvbCheck('is_calendar', $this->config)) {
            $args['rewrite']['slug'] = $args['rewrite']['slug']??$this->slug.'/%eyear%/%emonth%/%eday%';
        }
        if (isset($this->config['icon'])) {
            $args['menu_icon'] = jvbCSSIcon($this->config['icon']);
        }
 
        register_post_type($this->post_type, $args);
 
        if (!empty($this->fields)) {
            $meta_registry = new MetaRegistry($this->fields, $this->slug, 'post');
            $meta_registry->registerMetaFields();
        }
    }
 
    private function buildLabels(string $singular, string $plural): array
    {
        return [
            'name'               => $plural,
            'singular_name'      => $singular,
            'menu_name'          => $plural,
            'name_admin_bar'     => $singular,
            'add_new'            => "Add New",
            'add_new_item'       => "Add New {$singular}",
            'new_item'           => "New {$singular}",
            'edit_item'          => "Edit {$singular}",
            'view_item'          => "View {$singular}",
            'all_items'          => "All {$plural}",
            'search_items'       => "Search {$plural}",
            'parent_item_colon'  => "Parent {$plural}:",
            'not_found'          => "No {$plural} found.",
            'not_found_in_trash' => "No {$plural} found in Trash.",
        ];
    }
 
    public function registerHooks() {
        if ($this->config['hide_single'] ?? false) {
            add_filter('is_post_type_viewable', [$this, 'hideFromPublic']);
            if (jvbCheck('redirectToAuthor',$this->config)) {
                add_filter('post_type_link', [$this, 'redirectSingleToAuthor'], 15, 2);
            } else {
                add_filter('post_type_link', [$this, 'redirectSingleToArchive'], 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 = new CacheManager(jvbNoBase($type));
        $cache->delete($post->ID);
 
        // Clear specific cache groups
        CacheManager::invalidateGroup($type);
 
        CacheManager::invalidateGroup("user_content_{$post->post_author}");
 
        // 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::invalidateGroup($taxonomy);
            }
        }
 
        // Trigger custom action for additional handling
        do_action("jvb_cache_invalidated_{$type}", $post, $action);
    }
 
    public function hideFromPublic(bool $is_viewable): bool
    {
        if (!is_admin() && is_singular($this->post_type)) {
            return false;
        }
        return $is_viewable;
    }
 
    public function redirectSingleToAuthor(string $url, \WP_Post $post): string
    {
        if ($post->post_type !== $this->post_type) {
            return $url;
        }
 
        // Redirect to author page or archive
        $user_link = get_user_meta($post->post_author, BASE . 'link', true);
        if ($user_link) {
            $query_var = str_replace(BASE, '', $post->post_type);
            return add_query_arg($query_var, $post->ID, get_permalink($user_link));
        }
 
        return get_post_type_archive_link($post->post_type);
    }
    public function redirectSingleToArchive(string $url, \WP_Post $post): string
    {
        if ($post->post_type !== $this->post_type) {
            return $url;
        }
 
        return get_post_type_archive_link($post->post_type);
    }
}