Completed
Branch barista (4dd19e)
by
unknown
38:55 queued 28:23
created

EventManagers::getData()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\admin\events\editor;
4
5
use EventEspresso\core\domain\services\graphql\Utilities;
6
use stdClass;
7
use WP_Role;
8
9
/**
10
 * Class EventManagers
11
 *
12
 * @author  Brent Christensen
13
 * @package EventEspresso\core\domain\services\admin\events\editor
14
 * @since   $VID:$
15
 */
16
class EventManagers implements EventEditorDataInterface
17
{
18
19
    /**
20
     * @var array
21
     */
22
    private $event_managers = [];
23
24
    /**
25
     * @var Utilities
26
     */
27
    private $utilities;
28
29
30
    /**
31
     * EventManagers constructor.
32
     *
33
     * @param Utilities $utilities
34
     */
35
    public function __construct(Utilities $utilities)
36
    {
37
        $this->utilities = $utilities;
38
    }
39
40
41
    /**
42
     * @param int $eventId
43
     * @return array
44
     */
45
    public function getData(int $eventId)
46
    {
47
        if (empty($this->event_managers)) {
48
            [$roles, $capabilities] = $this->getRoleAndCapabilities();
0 ignored issues
show
Bug introduced by
The variable $roles does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $capabilities does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
            // first get a list of WP_Roles that have "event manager" capabilities
50
            $event_manager_roles = $this->getEventManagerRoles($roles, $capabilities);
51
            // then get a list of WP Users that have any of those roles
52
            $event_manager_users = $this->getEventManagerUsers($event_manager_roles, $capabilities);
53
            // now convert to a format that's usable by GQL
54
            foreach ($event_manager_users as $user) {
55
                $GUID             = $this->utilities->convertToGlobalId('user', $user->ID);
56
                $this->event_managers[] = [
57
                    'id'   => $GUID,
58
                    'name' => $user->display_name,
59
                ];
60
            }
61
        }
62
        return $this->event_managers;
63
    }
64
65
66
    private function getRoleAndCapabilities()
67
    {
68
        global $wp_roles;
69
        // first let's grab all of the WP_Role objects
70
        $roles = $wp_roles->role_objects;
71
        // then filter a list of capabilities we want to use to define an event manager
72
        $capabilities = (array) apply_filters(
73
            'FHEE__EventEspresso_core_domain_services_admin_events_editor_EventManagers__getRoleAndCapabilities__capabilities',
74
            ['ee_edit_events', 'ee_edit_event'],
75
            $roles
76
        );
77
        $capabilities = array_map('sanitize_text_field', $capabilities);
78
        return [$roles, $capabilities];
79
    }
80
81
82
    /**
83
     * Returns a list of WP_Role that have "event manager" capabilities
84
     * The list of "event manager" capabilities is filtered but defaults to:
85
     *      - 'ee_edit_events'
86
     *      - 'ee_edit_event'
87
     *
88
     * @param WP_Role[] $roles
89
     * @param string[]  $capabilities
90
     * @return WP_Role[]
91
     */
92
    private function getEventManagerRoles(array $roles, array $capabilities = [])
93
    {
94
        // we'll use this array to capture all of the WP_Role objects that have any of the caps we are targeting
95
        $event_manager_roles = [];
96
        foreach ($roles as $role) {
97
            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...
98
                foreach ($capabilities as $capability) {
99
                    // we're using the role name as the array index to prevent duplicates
100
                    if (! isset($event_manager_roles[ $role->name ]) && $role->has_cap($capability)) {
101
                        $event_manager_roles[ $role->name ] = $role;
102
                    }
103
                }
104
            }
105
        }
106
        return $event_manager_roles;
107
    }
108
109
110
    /**
111
     * Returns a list of users that have any of the supplied roles
112
     *
113
     * @param WP_Role[] $event_manager_roles
114
     * @param string[]  $capabilities
115
     * @return stdClass[]
116
     */
117
    private function getEventManagerUsers(array $event_manager_roles, array $capabilities)
118
    {
119
        global $wpdb;
120
        // no roles ?!!? then nothing to query for
121
        if (empty($event_manager_roles)) {
122
            return [];
123
        }
124
        // begin to build our query
125
        $SQL = "SELECT u1.ID, u1.display_name FROM {$wpdb->users} AS u1 "
126
             . "INNER JOIN {$wpdb->usermeta} AS u2 ON u1.ID = u2.user_id "
127
             . "AND u2.meta_key='{$wpdb->prefix}capabilities' "
128
             . 'WHERE';
129
        $operator = '';
130
        foreach ($event_manager_roles as $role) {
131
            // for each role, add a WHERE clause
132
            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...
133
                $SQL     .= $operator . ' u2.meta_value LIKE \'%"' . $role->name . '"%\' ';
134
                // subsequent clauses will use OR so that any role is accepted
135
                $operator = 'OR';
136
            }
137
        }
138
        foreach ($capabilities as $capability) {
139
            // for each capability, add a WHERE clause
140
            $SQL     .= $operator . ' u2.meta_value LIKE \'%"' . $capability . '";b:1;%\' ';
141
            // subsequent clauses will use OR so that any role is accepted
142
            $operator = 'OR';
143
        }
144
        $SQL  .= "ORDER BY user_id ASC";
145
        $users = $wpdb->get_results($SQL);
146
        return ! empty($users) ? $users : [];
147
    }
148
}
149