Completed
Branch dev (1399ad)
by
unknown
60:04 queued 50:49
created

EventManagers::roles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\entities\users;
4
5
use stdClass;
6
use WP_Role;
7
8
/**
9
 * Class EventManagers
10
 * compiles and stores information about event manager capabilities, roles, and users
11
 *
12
 * @author  Brent Christensen
13
 * @package EventEspresso\core\domain\entities\users
14
 * @since   $VID:$
15
 */
16
class EventManagers
17
{
18
19
    /**
20
     * @var string[]
21
     */
22
    private $capabilities = [];
23
24
    /**
25
     * @var WP_Role[]
26
     */
27
    private $roles = [];
28
29
    /**
30
     * @var array
31
     */
32
    private $user_list = [];
33
34
    /**
35
     * @var WP_Role[]
36
     */
37
    private $wp_roles;
38
39
40
    /**
41
     * EventManagerRoles constructor.
42
     */
43
    public function __construct()
44
    {
45
        global $wp_roles;
46
        // first let's grab ALL of the WP_Role objects
47
        $this->wp_roles = $wp_roles->role_objects;
48
        $this->setCapabilities();
49
        $this->buildRolesArray();
50
        $this->buildUserList();
51
    }
52
53
54
    private function setCapabilities(): void
55
    {
56
        // filter a list of capabilities we want to use to define an event manager
57
        $capabilities = (array) apply_filters(
58
            'FHEE__EventEspresso_core_domain_services_capabilities_EventManagers__setCapabilities',
59
            ['ee_edit_events', 'ee_edit_event'],
60
            $this->wp_roles
61
        );
62
        $this->capabilities = array_map('sanitize_text_field', $capabilities);
63
    }
64
65
66
    private function buildRolesArray(): void
67
    {
68
        // we'll use this array to capture all of the WP_Role objects that have any of the caps we are targeting
69
        $event_manager_roles = [];
70
        foreach ($this->wp_roles as $role) {
71
            if ($role instanceof WP_Role) {
0 ignored issues
show
Bug introduced by
The class WP_Role does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
72
                foreach ($this->capabilities as $capability) {
73
                    // we're using the role name as the array index to prevent duplicates
74
                    if (! isset($event_manager_roles[ $role->name ]) && $role->has_cap($capability)) {
75
                        $event_manager_roles[ $role->name ] = $role;
76
                    }
77
                }
78
            }
79
        }
80
        $this->roles = $event_manager_roles;
81
    }
82
83
84
    private function buildUserList(): void
85
    {
86
        global $wpdb;
87
        // no roles ?!!? then nothing to query for
88
        if (empty($this->roles)) {
89
            $this->user_list = [];
90
        }
91
        // begin to build our query
92
        $SQL      = "SELECT u1.ID, u1.display_name FROM $wpdb->users AS u1 "
93
                    . "INNER JOIN $wpdb->usermeta AS u2 ON u1.ID = u2.user_id "
94
                    . "AND u2.meta_key='{$wpdb->prefix}capabilities' "
95
                    . 'WHERE';
96
        $operator = '';
97
        foreach ($this->roles as $role) {
98
            // for each role, add a WHERE clause
99
            if ($role instanceof WP_Role) {
0 ignored issues
show
Bug introduced by
The class WP_Role does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
100
                $SQL .= $operator . ' u2.meta_value LIKE \'%"' . $role->name . '"%\' ';
101
                // subsequent clauses will use OR so that any role is accepted
102
                $operator = 'OR';
103
            }
104
        }
105
        foreach ($this->capabilities as $capability) {
106
            // for each capability, add a WHERE clause
107
            $SQL .= $operator . ' u2.meta_value LIKE \'%"' . $capability . '";b:1;%\' ';
108
            // subsequent clauses will use OR so that any role is accepted
109
            $operator = 'OR';
110
        }
111
        $SQL   .= 'ORDER BY user_id ASC';
112
        $users = $wpdb->get_results($SQL);
113
114
        $this->user_list = ! empty($users) ? $users : [];
115
    }
116
117
118
    /**
119
     * @return array
120
     */
121
    public function capabilities(): array
122
    {
123
        return $this->capabilities;
124
    }
125
126
127
    /**
128
     * Returns a list of WP_Role objects that have "event manager" capabilities
129
     * The list of "event manager" capabilities is filtered but defaults to:
130
     *      - 'ee_edit_events'
131
     *      - 'ee_edit_event'
132
     *
133
     * @return WP_Role[]
134
     */
135
    public function roles(): array
136
    {
137
        return $this->roles;
138
    }
139
140
141
    /**
142
     * Returns a list of users that have any of the Event Manager roles
143
     *
144
     * @return stdClass[]
145
     */
146
    public function userList(): array
147
    {
148
        return $this->user_list;
149
    }
150
}
151