Jake Vanderwerf
5 days ago 75a097a018a0090f5902758353c578fce4aa2a25
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace JVBase\base;
use JVBase\registrar\Registrar;
 
/**
 * JVB_MEMBERSHIP defines the structure of the directory
 *         Options include:
 *         - membership_expires        = useful if members pay a yearly membership fee
 *         - hide_expired                = removes users once membership expired; only used if membership_expires is true
 *         - clip_expired                = keeps users once membership expires, but limits the information shown; only used if membership_expires is true
 *         - membership_approval        = verified users can approve other users
 *         - term_approval              = (bool) verified users can create new terms, but needs approval
 *          - member_only              = (array) if empty, open to any registered user. otherwise an array of registered user roles
 *
 *
 * Example:
 * [
 *    'member_content'  => true,
 *      'invitable'     => true,
 *    'can_invite'      => ['artist' => ['artist']],
 *    'member_verified' => true,
 *    'notifications'   => true,
 *    'forum'           => true,
 *    'member_only'     => [ 'artist' ],
 *    'member_expires'  => false,
 *    'hide_expired'    => false,
 *    'clip_expired'    => false,
 *    'term_approval'   => true,
 *    'can_approve'     => [ 'artist' ]
 * ]
 */
 
$membership = apply_filters('jvb_membership', [
    'can_invite' => [],
]);
define('JVB_MEMBERSHIP', $membership);
 
 
class Membership
{
    protected bool $member_content = true;
    protected bool $invitable = false;
    protected bool $member_verified = false;
    protected array $can_invite = [];
    protected bool $notifications = true;
    protected bool $member_expires = false;
    protected bool $hide_expired = false;
    protected bool $clip_expired = false;
    protected bool $term_approval = false;
    protected array $can_approve = [];
    protected bool $forum = true;
    protected array $member_only = [];
 
    public function __construct() {
 
    }
    public function set(string $property, null|bool|array $value = null):void
    {
        if (is_null($value)) {
            $value = true;
        }
        if (property_exists(self::class, $property)) {
            $this->$property = $value;
        } else {
            error_log('[Membership]::set Could not set property '.$property.', it does not exist.');
        }
    }
 
    public function setAll(array $properties):void
    {
        foreach ($properties as $property) {
            if (is_array($property)) {
                $this->set($property[0], $property[1]);
            } else {
                $this->set($property);
            }
        }
    }
 
    public function has(string $property):bool
    {
        return property_exists($this, $property) && $this->$property === true;
    }
 
    public function hasAny(array $properties):bool
    {
        foreach ($properties as $property) {
            if ($this->has($property)) {
                return true;
            }
        }
        return false;
    }
 
    public function setInvitable(array $invitable):void
    {
        $users = Registrar::getRegistered('user');
 
        $invitable = array_filter($invitable, function($role) use ($users) {
            if (!in_array($role, $users)) {
                error_log('[Membership]::set Invitable role '.$role.' does not exist.');
                return false;
            }
            return true;
        }, ARRAY_FILTER_USE_KEY);
        $allowed = [];
        foreach ($invitable as $inviter => $canInvites) {
            $canInvites = array_filter($canInvites, function($role) use ($users){
                if (!in_array($role, $users)) {
                    error_log('[Membership]::set Invitable role '.$role.' does not exist.');
                    return false;
                }
                return true;
            });
            if (!empty($canInvites)) {
                $allowed[$inviter] = $canInvites;
            }
        }
        if (!empty($allowed)) {
            $this->can_invite = $allowed;
        }
    }
 
    public function getInviters():array
    {
        return $this->can_invite;
    }
    public function canInvite(string $role):bool
    {
        return ($this->invitable) && array_key_exists($role, $this->can_invite);
    }
    public function getInvitableFor(string $role):array
    {
        return array_key_exists($role, $this->can_invite) ? $this->can_invite[$role] : [];
    }
 
    public function setApprovers(array $roles):void
    {
        $allowed = Registrar::getRegistered('users');
        $this->can_approve = array_filter($roles, function($role) use ($allowed){
            return in_array($role, $allowed);
        });
    }
    public function getApprovers():array
    {
        return $this->can_approve;
    }
 
    public function canApprove(string $role):bool
    {
        return ($this->term_approval) && in_array($role, $this->can_approve);
    }
 
 
    public function setCanSeeForum(array $roles):void
    {
        $allowed = Registrar::getRegistered('users');
        $this->member_only = array_filter($roles, function($role) use ($allowed){
            return in_array($role, $allowed);
        });
    }
    public function canSeeForum(string $role):bool
    {
        return $this->forum && (empty($this->member_only) || in_array($role, $this->member_only));
    }
 
}