Jake Vanderwerf
2026-04-15 c4aa5cdb5e90ad4b420e22772797d16980232a2b
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
 
use JVBase\managers\Cache;
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Outputs the name according to whether they wish to share it or not
 * DUPLICATE. See which method we like better
 * @param int $userID
 *
 * @return string
 */
function jvbShareName(int $userID):string
{
    $cache = Cache::for('usernames')->connect('user');
    return $cache->remember(
        $userID,
        function() use ($userID) {
            $check = get_user_meta($userID, BASE.'notify', true);
            return ($check) ? get_userdata($userID)->display_name : 'Someone';
        }
    );
}
 
/**
 * @param string $first_name
 *
 * @return WP_User|false
 */
function jvbGetUserByFirstName(string $first_name):WP_User|false
{
    $cache = Cache::for('userFirstname')->connect('user', true);
    return $cache->remember(
        $first_name,
        function() use ($first_name) {
            $args = [
                'post_type' => BASE . 'artist',
                'posts_per_page' => 1,
                'fields' => 'ids',
                'meta_query' => [
                    [
                        'key' => BASE . 'first_name',
                        'value' => $first_name,
                        'compare' => '='
                    ]
                ]
            ];
            $query = new WP_Query($args);
 
            if ($query->have_posts()) {
                $post_id = $query->posts[0];
                $user_id = get_post_meta($post_id, BASE . 'link', true);
                $user = get_userdata($user_id)?:false;
                if ($user) {
                    wp_reset_postdata();
                    return $user;
                }
            }
            wp_reset_postdata();
            return false;
        }
    );
}
 
/**
 * @param string $display_name
 *
 * @return WP_User|false
 */
function jvbGetUserByDisplayName(string $display_name):WP_User|false
{
    $cache = Cache::for('displayNames')->connect('user', true);
    $cached = $cache->get($display_name)??false;
 
    if ($cached && is_int($cached)) {
        return get_userdata($cached);
    }
 
    $args = [
        'post_type' => BASE . 'artist',
        'title' => $display_name,
        'posts_per_page' => 1,
        'fields' => 'ids'
    ];
 
    $query = new WP_Query($args);
 
    if ($query->have_posts()) {
        $post_id = $query->posts[0];
        $user_id = get_post_meta($post_id, BASE . 'link', true);
 
        $user = get_userdata($user_id)?:false;
 
        $cache->set($display_name, ($user) ? $user->ID : false);
        return $user;
    }
 
    return false;
}
 
/**
 * Get user name, generally for notification messages
 *
 * @param int $user_id User ID
 *
 * @return string User display name or "Someone" for anonymous
 */
function jvbGetUsername(int $user_id):string
{
    $key = 'user_display_names';
    $cache = Cache::for('userNames', WEEK_IN_SECONDS)->connect('user');
    $cached = $cache->get($user_id);
 
    if ($cached) {
        return $cached;
    }
 
    $permission = get_user_meta($user_id, BASE.'notify', true);
 
    $display_name = (!$permission) ? 'Someone' : false;
    $user = get_userdata($user_id);
    $display_name = (!$display_name && $user) ? $user->display_name : 'Someone';
    $cache->set($user_id, $display_name);
    return $display_name;
}
 
/**
 * Gets artist data from user_id. Can return everything, or a specific item
 * @param int $userID
 * @param string $return
 *
 * @return array|string
 */
function jvbContentFromUser(int $userID, string $return = 'all'):array|string
{
    if (!in_array(
        $return,
        [
            'all',
            'id',
            'name',
            'display_name',
            'url',
            'type',
            'city',
            'shop'
        ]
    )) {
        return false;
    }
 
    $cache = Cache::for('artist', 3600)->connect('post');
    $cached = $cache->get($userID);
    if ($cached) {
        return match ($return) {
            'id' => $cache['id'],
            'first_name', 'name' => $cache['first_name'],
            'display_name' => $cache['display_name'],
            'url' => $cache['url'],
            'type' => $cache['type'],
            'shop' => $cache['shop'],
            'city' => $cache['city'],
            default => $cache,
        };
    }
 
    $user = get_userdata($userID);
    if (!$user) {
        return [];
    }
    $id = (int) get_user_meta($userID, BASE.'link', true);
 
    $meta = Meta::forPost($id);
    $artist = $meta->getAll(['first_name','type','city','shop']);
    $artist['id'] = $id;
    $artist['display_name'] = $user->display_name;
    $artist['url'] = get_the_permalink($id);
 
 
    $cache->set($userID, $artist);
 
    return match ($return) {
        'id' => $cache['id'],
        'first_name', 'name' => $cache['first_name'],
        'display_name' => $cache['display_name'],
        'url' => $cache['url'],
        'type' => $cache['type'],
        'shop' => $cache['shop'],
        'city' => $cache['city'],
        default => $cache,
    };
}
 
function jvbUserRole(int $ID = 0):string
{
    if (!is_user_logged_in()) {
        return '';
    }
    if ($ID === 0) {
        if (current_user_can('manage_options')) {
            return 'admin';
        }
    }
    if ($ID > 0 && user_can($ID, 'manage_options')) {
        return 'admin';
    }
    $user = ($ID === 0) ? wp_get_current_user() : get_userdata($ID);
    return array_values(array_intersect(
        array_keys(array_merge(JVB_USER, ['administrator'])),
        array_map(function ($role) {
            return jvbNoBase($role);
        },
        $user->roles)
    ))[0];
}
 
function  jvbUserProfileLink(int $userID):string|false
{
    $cache = Cache::for('userLink')->connect('user');
    return $cache->remember(
        $userID,
        function() use ($userID) {
            $user = get_userdata($userID);
            if (!$user) {
                return false;
            }
            $role = jvbUserRole($userID);
            $registrar = Registrar::getInstance($role);
            if (!$registrar || !$registrar->profile_link) {
                return false;
            }
            $link = get_user_meta($userID, BASE.'profile_link', true);
            //Try to create it
            if (empty($link)) {
                $link = JVB()->roles()->addUserLink($user, $role);
                if (!$link) {
                    return false;
                }
            }
            $status = get_post_status($link);
            return ($status === 'publish') ? get_the_permalink($link) : false;
        }
    );
}