@@ -16,133 +16,133 @@ |
||
| 16 | 16 | class EventManagers implements EventEditorDataInterface |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - private $event_managers = []; |
|
| 19 | + /** |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + private $event_managers = []; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var Utilities |
|
| 26 | - */ |
|
| 27 | - private $utilities; |
|
| 24 | + /** |
|
| 25 | + * @var Utilities |
|
| 26 | + */ |
|
| 27 | + private $utilities; |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * EventManagers constructor. |
|
| 32 | - * |
|
| 33 | - * @param Utilities $utilities |
|
| 34 | - */ |
|
| 35 | - public function __construct(Utilities $utilities) |
|
| 36 | - { |
|
| 37 | - $this->utilities = $utilities; |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * EventManagers constructor. |
|
| 32 | + * |
|
| 33 | + * @param Utilities $utilities |
|
| 34 | + */ |
|
| 35 | + public function __construct(Utilities $utilities) |
|
| 36 | + { |
|
| 37 | + $this->utilities = $utilities; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | 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(); |
|
| 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 | - } |
|
| 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(); |
|
| 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 | 64 | |
| 65 | 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 | - } |
|
| 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 | 80 | |
| 81 | 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) { |
|
| 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 | - } |
|
| 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) { |
|
| 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 | 108 | |
| 109 | 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) { |
|
| 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 | - } |
|
| 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) { |
|
| 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 | 148 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | $event_manager_users = $this->getEventManagerUsers($event_manager_roles, $capabilities); |
| 53 | 53 | // now convert to a format that's usable by GQL |
| 54 | 54 | foreach ($event_manager_users as $user) { |
| 55 | - $GUID = $this->utilities->convertToGlobalId('user', $user->ID); |
|
| 55 | + $GUID = $this->utilities->convertToGlobalId('user', $user->ID); |
|
| 56 | 56 | $this->event_managers[] = [ |
| 57 | 57 | 'id' => $GUID, |
| 58 | 58 | 'name' => $user->display_name, |
@@ -97,8 +97,8 @@ discard block |
||
| 97 | 97 | if ($role instanceof WP_Role) { |
| 98 | 98 | foreach ($capabilities as $capability) { |
| 99 | 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; |
|
| 100 | + if ( ! isset($event_manager_roles[$role->name]) && $role->has_cap($capability)) { |
|
| 101 | + $event_manager_roles[$role->name] = $role; |
|
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | 104 | } |
@@ -130,14 +130,14 @@ discard block |
||
| 130 | 130 | foreach ($event_manager_roles as $role) { |
| 131 | 131 | // for each role, add a WHERE clause |
| 132 | 132 | if ($role instanceof WP_Role) { |
| 133 | - $SQL .= $operator . ' u2.meta_value LIKE \'%"' . $role->name . '"%\' '; |
|
| 133 | + $SQL .= $operator.' u2.meta_value LIKE \'%"'.$role->name.'"%\' '; |
|
| 134 | 134 | // subsequent clauses will use OR so that any role is accepted |
| 135 | 135 | $operator = 'OR'; |
| 136 | 136 | } |
| 137 | 137 | } |
| 138 | 138 | foreach ($capabilities as $capability) { |
| 139 | 139 | // for each capability, add a WHERE clause |
| 140 | - $SQL .= $operator . ' u2.meta_value LIKE \'%"' . $capability . '";b:1;%\' '; |
|
| 140 | + $SQL .= $operator.' u2.meta_value LIKE \'%"'.$capability.'";b:1;%\' '; |
|
| 141 | 141 | // subsequent clauses will use OR so that any role is accepted |
| 142 | 142 | $operator = 'OR'; |
| 143 | 143 | } |