<?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;
|
}
|
);
|
}
|