PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Controller/Backend/UserGroupController.php +137 -53 2.3.17 → 2.1.11 View file →
@@ -1,11 +1,20 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * UserGroupController.php
4 + *
5 + * The UserGroupController class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <[email protected]>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
5 15 namespace UserAccessManager\Controller\Backend;
6 16
7 -use Exception;
8 17 use UserAccessManager\Config\WordpressConfig;
9 18 use UserAccessManager\Controller\Controller;
10 19 use UserAccessManager\Form\FormHelper;
11 20 use UserAccessManager\Object\ObjectHandler;
@@ -11,40 +20,82 @@
11 20 use UserAccessManager\Object\ObjectHandler;
12 21 use UserAccessManager\UserGroup\UserGroup;
13 22 use UserAccessManager\UserGroup\UserGroupFactory;
14 23 use UserAccessManager\UserGroup\UserGroupHandler;
15 -use UserAccessManager\UserGroup\UserGroupTypeException;
16 24 use UserAccessManager\Wrapper\Php;
17 25 use UserAccessManager\Wrapper\Wordpress;
18 -use WP_Post_Type;
19 -use WP_Taxonomy;
20 26
27 +/**
28 + * Class UserGroupController
29 + *
30 + * @package UserAccessManager\Controller
31 + */
21 32 class UserGroupController extends Controller
22 33 {
23 34 use ControllerTabNavigationTrait;
24 35
25 - public const INSERT_UPDATE_GROUP_NONCE = 'uamInsertUpdateGroup';
26 - public const DELETE_GROUP_NONCE = 'uamDeleteGroup';
27 - public const SET_DEFAULT_USER_GROUPS_NONCE = 'uamSetDefaultUserGroups';
28 - public const GROUP_USER_GROUPS = 'user_groups';
29 - public const GROUP_DEFAULT_USER_GROUPS = 'default_user_groups';
30 - public const DEFAULT_USER_GROUPS_FORM_FIELD = 'default_user_groups';
36 + const INSERT_UPDATE_GROUP_NONCE = 'uamInsertUpdateGroup';
37 + const DELETE_GROUP_NONCE = 'uamDeleteGroup';
38 + const SET_DEFAULT_USER_GROUPS_NONCE = 'uamSetDefaultUserGroups';
39 + const GROUP_USER_GROUPS = 'user_groups';
40 + const GROUP_DEFAULT_USER_GROUPS = 'default_user_groups';
41 + const DEFAULT_USER_GROUPS_FORM_FIELD = 'default_user_groups';
31 42
32 - protected ?string $template = 'AdminUserGroup.php';
33 - private ?UserGroup $userGroup = null;
43 + /**
44 + * @var string
45 + */
46 + protected $template = 'AdminUserGroup.php';
34 47
48 + /**
49 + * @var UserGroupHandler
50 + */
51 + private $userGroupHandler;
52 +
53 + /**
54 + * @var UserGroupFactory
55 + */
56 + private $userGroupFactory;
57 +
58 + /**
59 + * @var FormHelper
60 + */
61 + private $formHelper;
62 +
63 + /**
64 + * @var \UserAccessManager\UserGroup\UserGroup
65 + */
66 + private $userGroup = null;
67 +
68 + /**
69 + * UserGroupController constructor.
70 + *
71 + * @param Php $php
72 + * @param Wordpress $wordpress
73 + * @param WordpressConfig $wordpressConfig
74 + * @param UserGroupHandler $userGroupHandler
75 + * @param UserGroupFactory $userGroupFactory
76 + * @param FormHelper $formHelper
77 + */
35 78 public function __construct(
36 79 Php $php,
37 80 Wordpress $wordpress,
38 81 WordpressConfig $wordpressConfig,
39 - private UserGroupHandler $userGroupHandler,
40 - private UserGroupFactory $userGroupFactory,
41 - private FormHelper $formHelper
82 + UserGroupHandler $userGroupHandler,
83 + UserGroupFactory $userGroupFactory,
84 + FormHelper $formHelper
42 85 ) {
43 86 parent::__construct($php, $wordpress, $wordpressConfig);
87 + $this->userGroupHandler = $userGroupHandler;
88 + $this->userGroupFactory = $userGroupFactory;
89 + $this->formHelper = $formHelper;
44 90 }
45 91
46 - public function getTabGroups(): array
92 + /**
93 + * Returns the tab groups.
94 + *
95 + * @return array
96 + */
97 + public function getTabGroups()
47 98 {
48 99 return [
49 100 self::GROUP_USER_GROUPS => ['user_groups'],
50 101 self::GROUP_DEFAULT_USER_GROUPS => array_merge(
@@ -54,14 +105,28 @@
54 105 )
55 106 ];
56 107 }
57 108
58 - public function getGroupText(string $key): string
109 + /**
110 + * Returns the translated tag group name by the given key.
111 + *
112 + * @param string $key
113 + *
114 + * @return string
115 + */
116 + public function getGroupText($key)
59 117 {
60 118 return $this->formHelper->getText($key);
61 119 }
62 120
63 - public function getGroupSectionText(string $key): string
121 + /**
122 + * Returns the translated tag group section name by the given key.
123 + *
124 + * @param string $key
125 + *
126 + * @return string
127 + */
128 + public function getGroupSectionText($key)
64 129 {
65 130 $objects = $this->wordpress->getPostTypes(['public' => true], 'objects')
66 131 + $this->wordpress->getTaxonomies(['public' => true], 'objects');
67 132
@@ -71,12 +136,12 @@
71 136 $objectName = TXT_UAM_USER;
72 137 } elseif (isset($objects[$key]) === true) {
73 138 $objectName = $objects[$key]->labels->name;
74 139
75 - if ($objects[$key] instanceof WP_Post_Type) {
76 - $objectName .= ' (' . TXT_UAM_POST_TYPE . ')';
77 - } elseif ($objects[$key] instanceof WP_Taxonomy) {
78 - $objectName .= ' (' . TXT_UAM_TAXONOMY_TYPE . ')';
140 + if ($objects[$key] instanceof \WP_Post_Type) {
141 + $objectName .= ' ('.TXT_UAM_POST_TYPE.')';
142 + } elseif ($objects[$key] instanceof \WP_Taxonomy) {
143 + $objectName .= ' ('.TXT_UAM_TAXONOMY_TYPE.')';
79 144 }
80 145 }
81 146
82 147 return $objectName;
@@ -82,11 +147,13 @@
82 147 return $objectName;
83 148 }
84 149
85 150 /**
86 - * @throws UserGroupTypeException
151 + * Returns the a user group object.
152 + *
153 + * @return \UserAccessManager\UserGroup\UserGroup
87 154 */
88 - public function getUserGroup(): UserGroup
155 + public function getUserGroup()
89 156 {
90 157 if ($this->userGroup === null) {
91 158 $userGroupId = $this->getRequestParameter('userGroupId');
92 159 $this->userGroup = $this->userGroupFactory->createUserGroup($userGroupId);
@@ -94,23 +161,31 @@
94 161
95 162 return $this->userGroup;
96 163 }
97 164
98 - public function getSortUrl(string $sort): string
165 + /**
166 + * Returns the sort url.
167 + *
168 + * @param string $sort
169 + *
170 + * @return string
171 + */
172 + public function getSortUrl($sort)
99 173 {
100 174 $requestUrl = $this->getRequestUrl();
101 175 $requestUrl = preg_replace('/&amp;orderby[^&]*/i', '', $requestUrl);
102 176 $requestUrl = preg_replace('/&amp;order[^&]*/i', '', $requestUrl);
103 - $divider = !str_contains($requestUrl, '?') ? '?' : '&amp;';
177 + $divider = strpos($requestUrl, '?') === false ? '?' : '&amp;';
104 178 $order = (string) $this->getRequestParameter('order') === 'asc' ? 'desc' : 'asc';
105 - return "$requestUrl{$divider}orderby=$sort&amp;order=$order";
179 + return "{$requestUrl}{$divider}orderby={$sort}&amp;order={$order}";
106 180 }
107 181
108 182 /**
109 - * @return UserGroup[]
110 - * @throws UserGroupTypeException
183 + * Returns all user groups.
184 + *
185 + * @return \UserAccessManager\UserGroup\UserGroup[]
111 186 */
112 - public function getUserGroups(): array
187 + public function getUserGroups()
113 188 {
114 189 $userGroups = $this->userGroupHandler->getUserGroups();
115 190
116 191 $sort = $this->getRequestParameter('orderby');
@@ -115,18 +190,18 @@
115 190
116 191 $sort = $this->getRequestParameter('orderby');
117 192
118 193 if ($sort !== null) {
119 - $reverse = (string) $this->getRequestParameter('order') === 'desc';
194 + $reverse = (string) $this->getRequestParameter('order') === 'desc' ? true : false;
120 195
121 196 uasort(
122 197 $userGroups,
123 198 function (UserGroup $userGroupOne, UserGroup $userGroupTwo) use ($sort, $reverse) {
124 199 $values = ['', ''];
125 - $method = 'get' . ucfirst($sort);
200 + $method = 'get'.ucfirst($sort);
126 201
127 202 if (method_exists($userGroupOne, $method) === true) {
128 - $values = [(string) $userGroupOne->{$method}(), (string) $userGroupTwo->{$method}()];
203 + $values = [(string)$userGroupOne->{$method}(), (string)$userGroupTwo->{$method}()];
129 204 $values = ($reverse === true) ? array_reverse($values) : $values;
130 205 }
131 206
132 207 return strnatcasecmp($values[0], $values[1]);
@@ -136,9 +211,14 @@
136 211
137 212 return $userGroups;
138 213 }
139 214
140 - public function getRoleNames(): array
215 + /**
216 + * Returns the wordpress role names.
217 + *
218 + * @return array
219 + */
220 + public function getRoleNames()
141 221 {
142 222 $roles = $this->wordpress->getRoles();
143 223 return $roles->role_names;
144 224 }
@@ -143,12 +223,11 @@
143 223 return $roles->role_names;
144 224 }
145 225
146 226 /**
147 - * @throws UserGroupTypeException
148 - * @throws Exception
227 + * Action to insert or update a user group.
149 228 */
150 - public function insertUpdateUserGroupAction(): void
229 + public function insertUpdateUserGroupAction()
151 230 {
152 231 $this->verifyNonce(self::INSERT_UPDATE_GROUP_NONCE);
153 232
154 233 $userGroupId = $this->getRequestParameter('userGroupId');
@@ -155,9 +234,9 @@
155 234
156 235 $userGroup = $this->userGroupFactory->createUserGroup($userGroupId);
157 236
158 237 // Assign parameters
159 - $groupName = (string) $this->getRequestParameter('userGroupName');
238 + $groupName = $this->getRequestParameter('userGroupName');
160 239
161 240 if (trim($groupName) === '') {
162 241 $this->setUpdateMessage(TXT_UAM_GROUP_NAME_ERROR);
163 242 return;
@@ -196,11 +275,11 @@
196 275 }
197 276 }
198 277
199 278 /**
200 - * @throws UserGroupTypeException
279 + * Action to delete user groups.
201 280 */
202 - public function deleteUserGroupAction(): void
281 + public function deleteUserGroupAction()
203 282 {
204 283 $this->verifyNonce(self::DELETE_GROUP_NONCE);
205 284 $userGroups = $this->getRequestParameter('delete', []);
206 285
@@ -210,17 +289,23 @@
210 289
211 290 $this->setUpdateMessage(TXT_UAM_DELETE_GROUP);
212 291 }
213 292
214 - private function isDefaultTypeAdd(
215 - array $defaultUserGroups,
216 - string $userGroupId,
217 - ?string &$fromTime = null,
218 - ?string &$toTime = null
219 - ): bool {
293 + /**
294 + * Checks if the default user group type should be added.
295 + *
296 + * @param array $defaultUserGroups
297 + * @param string $userGroupId
298 + * @param null|string $fromTime
299 + * @param null|string $toTime
300 + *
301 + * @return bool
302 + */
303 + private function isDefaultTypeAdd(array $defaultUserGroups, $userGroupId, &$fromTime = null, &$toTime = null)
304 + {
220 305 $userGroupInfo = isset($defaultUserGroups[$userGroupId]) === true ? $defaultUserGroups[$userGroupId] : [];
221 306
222 - if (isset($userGroupInfo['id']) === true && (string) $userGroupInfo['id'] === $userGroupId) {
307 + if (isset($userGroupInfo['id']) === true && (string)$userGroupInfo['id'] === (string)$userGroupId) {
223 308 $fromTime = empty($userGroupInfo['fromTime']) === false ? $userGroupInfo['fromTime'] : null;
224 309 $toTime = empty($userGroupInfo['toTime']) === false ? $userGroupInfo['toTime'] : null;
225 310 return true;
226 311 }
@@ -228,12 +313,11 @@
228 313 return false;
229 314 }
230 315
231 316 /**
232 - * @throws UserGroupTypeException
233 - * @throws Exception
317 + * Action to set default user groups.
234 318 */
235 - public function setDefaultUserGroupsAction(): void
319 + public function setDefaultUserGroupsAction()
236 320 {
237 321 $this->verifyNonce(self::SET_DEFAULT_USER_GROUPS_NONCE);
238 322 $objectType = $this->getCurrentTabGroupSection();
239 323 $defaultUserGroups = $this->getRequestParameter(self::DEFAULT_USER_GROUPS_FORM_FIELD, []);