<?php
|
|
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 = new JVBase\managers\CacheManager('usernames');
|
$cached = $cache->get($userID);
|
if ($cached) {
|
return $cached;
|
}
|
$check = get_user_meta($userID, BASE.'notify', true);
|
$name = ($check) ? get_userdata($userID)->display_name : 'Someone';
|
$cache->set($userID, $name);
|
|
return $name;
|
}
|
|
/**
|
* @param string $first_name
|
*
|
* @return WP_User|false
|
*/
|
function jvbGetUserByFirstName(string $first_name):WP_User|false
|
{
|
$cache = new JVBase\managers\CacheManager;
|
$cached = $cache->get('user_first_names')??[];
|
if (in_array($first_name, $cached)) {
|
return get_userdata(array_search($first_name, $cached));
|
}
|
$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;
|
$cached[$user_id] = $first_name;
|
$cache->set('user_first_names', $cached);
|
return $user;
|
}
|
|
return false;
|
}
|
|
/**
|
* @param string $display_name
|
*
|
* @return WP_User|false
|
*/
|
function jvbGetUserByDisplayName(string $display_name):WP_User|false
|
{
|
$cache = new JVBase\managers\CacheManager('users');
|
$cached = $cache->get('user_display_names')??[];
|
|
if (in_array($display_name, $cached)) {
|
return get_userdata(array_search($display_name, $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;
|
$cached[$user_id] = $display_name;
|
$cache->set('user_display_names', $cached);
|
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 = new JVBase\managers\CacheManager('users', WEEK_IN_SECONDS);
|
$cached_names = $cache->get($key, 'user_data');
|
$cached_names = $cached_names ?: [];
|
|
if (array_key_exists($user_id, $cached_names)) {
|
return $cached_names[$user_id];
|
}
|
|
$permission = get_user_meta($user_id, BASE.'notify', true);
|
if ($permission === false) {
|
$cached_names[$user_id] = 'Someone';
|
$cache->set($key, $cached_names, 'user_data');
|
return 'Someone';
|
}
|
|
$display_name = get_userdata($user_id)?->display_name;
|
if ($display_name) {
|
$cached_names[$user_id] = $display_name;
|
$cache->set($key, $cached_names, 'user_data');
|
return $display_name;
|
}
|
return false;
|
}
|
|
/**
|
* 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;
|
}
|
|
$handler = new JVBase\managers\CacheManager('artist', 3600);
|
$handler->invalidateGroup('artist');
|
$key = $userID;
|
|
$cache = $handler->get($key);
|
$cache = false;
|
if ($cache) {
|
return match ($return) {
|
'id' => $cache['id'],
|
'name' => $cache['name'],
|
'display_name' => $cache['display_name'],
|
'url' => $cache['url'],
|
'type' => $cache['type'],
|
'shop' => $cache['shop'],
|
'city' => $cache['city'],
|
default => $cache,
|
};
|
}
|
|
if (!get_userdata($userID)) {
|
return [];
|
}
|
$id = (int) get_user_meta($userID, BASE.'link', true);
|
|
$artist = [
|
'id' => $id,
|
'name' => get_post_meta($id, BASE.'first_name', true),
|
'display_name' => get_userdata($userID)->display_name,
|
'url' => get_the_permalink($id),
|
'type' => jvbGetArtistTerm($id, 'type'),
|
'city' => jvbGetArtistTerm($id, 'city'),
|
'shop' => jvbGetArtistTerm($id, 'shop'),
|
];
|
|
$handler->set($key, $artist);
|
|
return match ($return) {
|
'id' => $artist['id'],
|
'name' => $artist['name'],
|
'display_name' => $artist['display_name'],
|
'url' => $artist['url'],
|
'type' => $artist['type'],
|
'shop' => $artist['shop'],
|
'city' => $artist['city'],
|
default => $artist,
|
};
|
}
|
|
function jvbUserRole(int $ID = 0):string
|
{
|
if (!is_user_logged_in()) {
|
return '';
|
}
|
if ($ID === 0) {
|
if (current_user_can('manage_options')) {
|
return 'admin';
|
}
|
}
|
$user = ($ID === 0) ? wp_get_current_user() : get_userdata($ID);
|
|
if (user_can($user, 'manage_options')) {
|
return 'admin';
|
}
|
return array_values(array_intersect(
|
array_keys(JVB_USER),
|
array_map(function ($role) {
|
return jvbNoBase($role);
|
},
|
$user->roles)
|
))[0];
|
}
|