| | |
| | | <?php |
| | | |
| | | use JVBase\managers\Cache; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\registrar\Registrar; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | | } |
| | |
| | | */ |
| | | 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; |
| | | $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'; |
| | | } |
| | | ); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | 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' => '=' |
| | | ] |
| | | ] |
| | | ]; |
| | | $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); |
| | | |
| | | $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; |
| | | 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; |
| | | } |
| | | ); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | function jvbGetUserByDisplayName(string $display_name):WP_User|false |
| | | { |
| | | $cache = new JVBase\managers\CacheManager('users'); |
| | | $cached = $cache->get('user_display_names')??[]; |
| | | $cache = Cache::for('displayNames')->connect('user', true); |
| | | $cached = $cache->get($display_name)??false; |
| | | |
| | | if (in_array($display_name, $cached)) { |
| | | return get_userdata(array_search($display_name, $cached)); |
| | | if ($cached && is_int($cached)) { |
| | | return get_userdata($cached); |
| | | } |
| | | |
| | | $args = [ |
| | |
| | | $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); |
| | | |
| | | $cache->set($display_name, ($user) ? $user->ID : false); |
| | | return $user; |
| | | } |
| | | |
| | |
| | | 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 ?: []; |
| | | $cache = Cache::for('userNames', WEEK_IN_SECONDS)->connect('user'); |
| | | $cached = $cache->get($user_id); |
| | | |
| | | if (array_key_exists($user_id, $cached_names)) { |
| | | return $cached_names[$user_id]; |
| | | if ($cached) { |
| | | return $cached; |
| | | } |
| | | |
| | | $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; |
| | | $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; |
| | | } |
| | | |
| | | /** |
| | |
| | | return false; |
| | | } |
| | | |
| | | $handler = new JVBase\managers\CacheManager('artist', 3600); |
| | | $handler->invalidateGroup('artist'); |
| | | $key = $userID; |
| | | $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, |
| | | }; |
| | | } |
| | | |
| | | $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)) { |
| | | $user = get_userdata($userID); |
| | | if (!$user) { |
| | | return []; |
| | | } |
| | | $id = (int) get_user_meta($userID, BASE.'link', true); |
| | | $id = (int) get_user_meta($userID, BASE.'profile_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'), |
| | | ]; |
| | | $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); |
| | | |
| | | $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, |
| | | }; |
| | | $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 |
| | |
| | | return 'admin'; |
| | | } |
| | | } |
| | | $user = ($ID === 0) ? wp_get_current_user() : get_userdata($ID); |
| | | |
| | | if (user_can($user, 'manage_options')) { |
| | | 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(JVB_USER), |
| | | array_keys(array_merge(Registrar::getRegistered('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; |
| | | } |
| | | ); |
| | | } |