Jake Vanderwerf
4 days ago 0afb2c0046b55c123eafb4ab9ee77efa68d12463
inc/helpers/members.php
@@ -1,7 +1,8 @@
<?php
use JVBase\managers\CacheManager;
use JVBase\meta\MetaManager;
use JVBase\managers\Cache;
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
if (!defined('ABSPATH')) {
   exit;
@@ -16,16 +17,14 @@
 */
function jvbShareName(int $userID):string
{
    $cache = CacheManager::for('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';
      }
   );
}
/**
@@ -35,38 +34,37 @@
 */
function jvbGetUserByFirstName(string $first_name):WP_User|false
{
    $cache = CacheManager::for('userFirstname');
    $cached = $cache->get($first_name)??false;
    if ($cached) {
        return get_userdata($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);
      wp_reset_postdata();
        return $user;
    }
   wp_reset_postdata();
    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;
      }
   );
}
/**
@@ -76,7 +74,7 @@
 */
function jvbGetUserByDisplayName(string $display_name):WP_User|false
{
    $cache = CacheManager::for('user_displaynames');
    $cache = Cache::for('displayNames')->connect('user', true);
    $cached = $cache->get($display_name)??false;
    if ($cached && is_int($cached)) {
@@ -115,7 +113,7 @@
function jvbGetUsername(int $user_id):string
{
    $key = 'user_display_names';
    $cache = CacheManager::for('userNames', WEEK_IN_SECONDS);
    $cache = Cache::for('userNames', WEEK_IN_SECONDS)->connect('user');
    $cached = $cache->get($user_id);
    if ($cached) {
@@ -156,7 +154,7 @@
        return false;
    }
    $cache = CacheManager::for('artist', 3600);
    $cache = Cache::for('artist', 3600)->connect('post');
   $cached = $cache->get($userID);
   if ($cached) {
      return match ($return) {
@@ -175,9 +173,9 @@
    if (!$user) {
        return [];
    }
    $id = (int) get_user_meta($userID, BASE.'link', true);
    $id = (int) get_user_meta($userID, BASE.'profile_link', true);
   $meta = new MetaManager($id,'post');
   $meta = Meta::forPost($id);
   $artist = $meta->getAll(['first_name','type','city','shop']);
   $artist['id'] = $id;
   $artist['display_name'] = $user->display_name;
@@ -212,12 +210,40 @@
      return 'admin';
   }
    $user = ($ID === 0) ? wp_get_current_user() : get_userdata($ID);
   error_log('Current User: '.print_r($user, true));
    return array_values(array_intersect(
        array_keys(array_merge(JVB_USER, ['administrator'])),
        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;
      }
   );
}