Jake Vanderwerf
2026-07-12 c204185ae86a98994f80010abf35a190c9406739
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace JVBase\integrations;
 
use JVBase\meta\Meta;
use JVBase\registrar\Registrar;
 
if (!defined('ABSPATH')) {
    exit;
}
 
trait UserConnection {
    protected function determineUser():void
    {
        if (current_user_can('manage_options')) {
            $this->switchUser();
        }
        $this->switchUser(get_current_user_id());
    }
    public function switchUser(?int $userID = null):bool
    {
        if (!$this->userCanConnect($userID)) {
            return false;
        }
        $this->clearUser();
        $this->userID = $userID;
        $this->initializeRateLimiters();
        return true;
    }
    protected function clearUser():void
    {
        $this->userID = false;
        $this->credentials = [];
        if (property_exists($this, 'rateLimiter')) {
            $this->requestLimiter?->saveHistory();
            $this->requestLimiter = null;
        }
        if (property_exists($this, 'oathLimiter')) {
            $this->oauthLimiter?->saveHistory();
            $this->oauthLimiter = null;
        }
    }
 
    public function userCanConnect(?int $userID):bool
    {
        if (current_user_can('manage_options')) {
            return true;
        }
 
        $user = get_userdata($userID);
        if (!$user || is_wp_error($user)) {
            return false;
        }
        $role = jvbUserRole($userID);
        $registrar = Registrar::getInstance($role);
        if (!$registrar || !$registrar->hasIntegration($this->service_name)) {
            return false;
        }
 
        return (bool) Meta::forUser($userID)->get('integration_'.$this->service_name.'_connected');
    }
 
}