Jake Vanderwerf
2025-11-10 e9967fa22781d922ba4eb8fb44fe72d200ac4b14
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
184
185
186
187
188
189
190
191
192
193
<?php
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * @param string $term
 * @param int|false $ID
 * @param string $classes
 *
 * @return string
 */
function jvbGetTheTerms(string $term, int|false $ID = false, string $classes = ''):string
{
    $taxonomy = jvbCheckBase($term);
    if (!taxonomy_exists($taxonomy)) {
        return '';
    }
    if (!str_starts_with($classes, ' ')) {
        $classes = ' '.$classes;
    }
 
    if (!$ID) {
        $ID = get_the_ID();
    }
 
    $classes .= $term;
 
    $terms = get_the_terms($ID, $taxonomy);
    $out = '';
    if ($terms && !is_wp_error($terms)) {
        $out = '<ul class="'.trim('term-list '.$classes).'">';
        foreach ($terms as $t) {
            $url = get_term_link($t->term_id, BASE.$term);
            $out .= '<li><a href="'.$url.'" title="View more in '.$t->name.'" rel="tag">'.$t->name.'</a></li>';
        }
        $out .= '</ul>';
    }
 
    return $out;
}
 
/**
 * Clear taxonomy list caches when terms are saved
 * @param int $term_id
 * @param int $tt_id
 * @param string $taxonomy
 *
 * @return void
 */
add_action('edited_term', 'jvbClearTermListCache', 10, 3);
add_action('created_term', 'jvbClearTermListCache', 10, 3);
add_action('delete_term', 'jvbClearTermListCache', 10, 3);
 
function jvbClearTermListCache(int $term_id, int $tt_id, string $taxonomy):void
{
    // Check if this taxonomy has a corresponding list
    $types = jvbGlobalDirectoryInfo();
    foreach ($types as $type) {
        if ($type['slug'] === $taxonomy) {
            // Delete the option to force regeneration
            delete_option(BASE . $type['slug'] . '_list');
            break;
        }
    }
 
    // If this is a city and it's linked to other taxonomies, clear those too
    if ($taxonomy === BASE.'city') {
        foreach ($types as $type) {
            if (!empty($type['links']) && in_array(BASE.'_city', $type['links'])) {
                delete_option(BASE . $type['slug'] . '_list');
            }
        }
    }
}
 
/**
 * Clear list caches when post types are saved
 */
add_action('save_post', 'jvbClearListCache', 10, 2);
function jvbClearListCache(int $post_id, \WP_Post $post):void
{
    // SAFETY: Skip attachments and other non-content post types
    if (in_array($post->post_type, jvbIgnoredPostTypes())) {
        return;
    }
    if (jvbNoSaveIt($post_id, $post)) {
        return;
    }
 
    // Get post type
    $post_type = get_post_type($post_id);
 
    // Check if this post type has a corresponding list
    $types = jvbGlobalDirectoryInfo();
    foreach ($types as $type) {
        if ($type['slug'] === $post_type) {
            // Delete the option to force regeneration
            delete_option(BASE . $type['slug'] . '_list');
            break;
        }
    }
}
 
 
/**
 * @param int $artistID
 * @param string $taxonomy
 *
 * @return array
 */
function jvbGetArtistTerm(int $artistID, string $taxonomy):array
{
    $taxonomy = (str_starts_with($taxonomy, BASE)) ? $taxonomy : BASE.$taxonomy;
    $terms = get_the_terms($artistID, $taxonomy);
    if ($terms && !is_wp_error($terms)) {
        $ID = $terms[0]->term_id;
        $name = $terms[0]->name;
        return [
            'id'    => $ID,
            'name'    => $name,
            'url'    => get_term_link($ID, $taxonomy)
        ];
    }
    return [];
}
 
/**
 * Gets the artist type taxonomy name
 * @param int $ID
 *
 * @return string
 */
function jvbArtistType(int $ID):string
{
    $types = get_the_terms($ID, BASE.'type');
    $type = '';
    if ($types && !is_wp_error($types)) {
        $type = $types[0]->term_id;
        $type = get_term_meta($type, BASE.'singular', true);
    }
 
    return ($type == '') ? 'Tattoo Artist' : $type;
}
 
/**
 * Gets the first name of city listed
 * @param int $ID
 *
 * @return string
 */
function jvbArtistCity(int $ID):string
{
    $cities = get_the_terms($ID, 'jvb_city');
    if ($cities && !is_wp_error($cities)) {
        return $cities[0]->name;
    }
    return 'Edmonton';
}
 
 
function jvbGetContentUsers(int $termID, $user = BASE.'artist'):array
{
    $users = get_term_meta($termID, BASE.'users', true);
    if ($users === '') {
        $term = get_term($termID);
        $taxonomy = $term->taxonomy;
        if (taxIsJVBContentTax($taxonomy)) {
            $posts = new WP_Query([
                'post_type'    => jvbCheckBase($user),
                'posts_per_page'    => -1,
                'tax_query' => [
                    [
                        'taxonomy'    => $taxonomy,
                        'terms'    => $term
                    ]
                ]
            ]);
            if (!empty($posts->posts)) {
                $users = array_map(
                    function ($post) {
                        return $post->post_author;
                    },
                    $posts->posts
                );
            }
            $users = implode(',', $users);
            update_post_meta($termID, BASE.'users', $users);
        }
    }
    return explode(',', $users);
}