Completed
Branch barista (126f7d)
by
unknown
53:38 queued 34:59
created

EventManagers::getEventManagerRoles()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 23
rs 9.2408
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 Utilities
21
     */
22
    private $utilities;
23
24
25
    /**
26
     * EventManagers constructor.
27
     *
28
     * @param Utilities $utilities
29
     */
30
    public function __construct(Utilities $utilities)
31
    {
32
        $this->utilities = $utilities;
33
    }
34
35
36
    /**
37
     * @param int $eventId
38
     * @return array
39
     */
40
    public function getData(int $eventId)
41
    {
42
        // first get a list of WP_Roles that have "event manager" capabilities
43
        $event_manager_roles = $this->getEventManagerRoles();
44
        // then get a list of WP Users that have any of those roles
45
        $event_manager_users = $this->getEventManagerUsers($event_manager_roles);
46
        // now convert to a format that's usable by GQL
47
        $event_managers = [];
48
        foreach ($event_manager_users as $user) {
49
            $GUID             = $this->utilities->convertToGlobalId('User', $user->ID);
50
            $event_managers[] = [
51
                'id'   => $GUID,
52
                'name' => $user->display_name,
53
            ];
54
        }
55
        return $event_managers;
56
    }
57
58
59
    /**
60
     * Returns a list of WP_Role that have "event manager" capabilities
61
     * The list of "event manager" capabilities is filtered but defaults to:
62
     *      - 'ee_edit_events'
63
     *      - 'ee_edit_event'
64
     *
65
     * @return WP_Role[]
66
     */
67
    private function getEventManagerRoles()
68
    {
69
        global $wp_roles;
70
        // first let's grab all of the WP_Role objects
71
        $roles = $wp_roles->role_objects;
72
        // then filter a list of capabilities we want to use to define an event manager
73
        $capabilities = (array) apply_filters(
74
            'FHEE__EventEspresso_core_domain_services_admin_events_editor_EventManagers__getData__capabilities',
75
            ['ee_edit_events', 'ee_edit_event'],
76
            $roles
77
        );
78
        // we'll use this array to capture all of the WP_Role objects that have any of the caps we are targeting
79
        $event_manager_roles = [];
80
        foreach ($roles as $role) {
81
            foreach ($capabilities as $capability) {
82
                // we're using the role name as the array index to prevent duplicates
83
                if (! isset($event_manager_roles[ $role->name ]) && $role->has_cap($capability)) {
84
                    $event_manager_roles[ $role->name ] = $role;
85
                }
86
            }
87
        }
88
        return $event_manager_roles;
89
    }
90
91
92
    /**
93
     * Returns a list of users that have any of the supplied roles
94
     *
95
     * @param WP_Role[] $event_manager_roles
96
     * @return stdClass[]
97
     */
98
    private function getEventManagerUsers(array $event_manager_roles)
99
    {
100
        global $wpdb;
101
        // no roles ?!!? then nothing to query for
102
        if (empty($event_manager_roles)) {
103
            return [];
104
        }
105
        // begin to build our query
106
        $SQL = "SELECT u1.ID, u1.display_name FROM {$wpdb->users} AS u1 "
107
             . "INNER JOIN {$wpdb->usermeta} AS u2 ON u1.ID = u2.user_id "
108
             . "AND u2.meta_key='{$wpdb->prefix}capabilities' "
109
             . 'WHERE';
110
        $operator = '';
111
        foreach ($event_manager_roles as $role) {
112
            // for each role, add a WHERE clause
113
            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...
114
                $SQL     .= $operator . ' u2.meta_value LIKE \'%"' . $role->name . '"%\' ';
115
                // subsequent clauses will use OR so that any role is accepted
116
                $operator = 'OR';
117
            }
118
        }
119
        $SQL  .= "ORDER BY user_id ASC";
120
        $users = $wpdb->get_results($SQL);
121
        return ! empty($users) ? $users : [];
122
    }
123
}
124