| | |
| | | <?php |
| | | |
| | | use JVBase\managers\CacheManager; |
| | | use JVBase\meta\MetaManager; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | | } |
| | |
| | | */ |
| | | function jvbShareName(int $userID):string |
| | | { |
| | | $cache = new JVBase\managers\CacheManager('usernames'); |
| | | $cache = CacheManager::for('usernames'); |
| | | $cached = $cache->get($userID); |
| | | if ($cached) { |
| | | return $cached; |
| | |
| | | */ |
| | | 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)); |
| | | $cache = CacheManager::for('userFirstname')->connectTo('user'); |
| | | $cached = $cache->get($first_name)??false; |
| | | if ($cached) { |
| | | return get_userdata($cached); |
| | | } |
| | | $args = [ |
| | | 'post_type' => BASE . 'artist', |
| | |
| | | $user = get_userdata($user_id)?:false; |
| | | $cached[$user_id] = $first_name; |
| | | $cache->set('user_first_names', $cached); |
| | | 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 = CacheManager::for('user_displaynames')->connectTo('user'); |
| | | $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 = CacheManager::for('userNames', WEEK_IN_SECONDS)->connectTo('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 = CacheManager::for('artist', 3600)->connectTo('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); |
| | | |
| | | $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 = new MetaManager($id,'post'); |
| | | $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(JVB_USER, ['administrator'])), |
| | | array_map(function ($role) { |
| | | return jvbNoBase($role); |
| | | }, |