|
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) { |
|
|
|
|
|
|
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
|
|
|
|