| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * UserGroupController.php | |
| 4 | + * | |
| 5 | + * The UserGroupController class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 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 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Controller\Backend; |
| @@ -17,33 +30,76 @@ | ||
| 17 | 30 | use UserAccessManager\Wrapper\Wordpress; |
| 18 | 31 | use WP_Post_Type; |
| 19 | 32 | use WP_Taxonomy; |
| 20 | 33 | |
| 34 | +/** | |
| 35 | + * Class UserGroupController | |
| 36 | + * | |
| 37 | + * @package UserAccessManager\Controller | |
| 38 | + */ | |
| 21 | 39 | class UserGroupController extends Controller |
| 22 | 40 | { |
| 23 | 41 | use ControllerTabNavigationTrait; |
| 24 | 42 | |
| 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'; | |
| 43 | + const INSERT_UPDATE_GROUP_NONCE = 'uamInsertUpdateGroup'; | |
| 44 | + const DELETE_GROUP_NONCE = 'uamDeleteGroup'; | |
| 45 | + const SET_DEFAULT_USER_GROUPS_NONCE = 'uamSetDefaultUserGroups'; | |
| 46 | + const GROUP_USER_GROUPS = 'user_groups'; | |
| 47 | + const GROUP_DEFAULT_USER_GROUPS = 'default_user_groups'; | |
| 48 | + const DEFAULT_USER_GROUPS_FORM_FIELD = 'default_user_groups'; | |
| 31 | 49 | |
| 32 | - protected ?string $template = 'AdminUserGroup.php'; | |
| 33 | - private ?UserGroup $userGroup = null; | |
| 50 | + /** | |
| 51 | + * @var string | |
| 52 | + */ | |
| 53 | + protected $template = 'AdminUserGroup.php'; | |
| 34 | 54 | |
| 55 | + /** | |
| 56 | + * @var UserGroupHandler | |
| 57 | + */ | |
| 58 | + private $userGroupHandler; | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * @var UserGroupFactory | |
| 62 | + */ | |
| 63 | + private $userGroupFactory; | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * @var FormHelper | |
| 67 | + */ | |
| 68 | + private $formHelper; | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * @var UserGroup | |
| 72 | + */ | |
| 73 | + private $userGroup = null; | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * UserGroupController constructor. | |
| 77 | + * @param Php $php | |
| 78 | + * @param Wordpress $wordpress | |
| 79 | + * @param WordpressConfig $wordpressConfig | |
| 80 | + * @param UserGroupHandler $userGroupHandler | |
| 81 | + * @param UserGroupFactory $userGroupFactory | |
| 82 | + * @param FormHelper $formHelper | |
| 83 | + */ | |
| 35 | 84 | public function __construct( |
| 36 | 85 | Php $php, |
| 37 | 86 | Wordpress $wordpress, |
| 38 | 87 | WordpressConfig $wordpressConfig, |
| 39 | - private UserGroupHandler $userGroupHandler, | |
| 40 | - private UserGroupFactory $userGroupFactory, | |
| 41 | - private FormHelper $formHelper | |
| 88 | + UserGroupHandler $userGroupHandler, | |
| 89 | + UserGroupFactory $userGroupFactory, | |
| 90 | + FormHelper $formHelper | |
| 42 | 91 | ) { |
| 43 | 92 | parent::__construct($php, $wordpress, $wordpressConfig); |
| 93 | + $this->userGroupHandler = $userGroupHandler; | |
| 94 | + $this->userGroupFactory = $userGroupFactory; | |
| 95 | + $this->formHelper = $formHelper; | |
| 44 | 96 | } |
| 45 | 97 | |
| 98 | + /** | |
| 99 | + * Returns the tab groups. | |
| 100 | + * @return array | |
| 101 | + */ | |
| 46 | 102 | public function getTabGroups(): array |
| 47 | 103 | { |
| 48 | 104 | return [ |
| 49 | 105 | self::GROUP_USER_GROUPS => ['user_groups'], |
| @@ -54,13 +110,23 @@ | ||
| 54 | 110 | ) |
| 55 | 111 | ]; |
| 56 | 112 | } |
| 57 | 113 | |
| 114 | + /** | |
| 115 | + * Returns the translated tag group name by the given key. | |
| 116 | + * @param string $key | |
| 117 | + * @return string | |
| 118 | + */ | |
| 58 | 119 | public function getGroupText(string $key): string |
| 59 | 120 | { |
| 60 | 121 | return $this->formHelper->getText($key); |
| 61 | 122 | } |
| 62 | 123 | |
| 124 | + /** | |
| 125 | + * Returns the translated tag group section name by the given key. | |
| 126 | + * @param string $key | |
| 127 | + * @return string | |
| 128 | + */ | |
| 63 | 129 | public function getGroupSectionText(string $key): string |
| 64 | 130 | { |
| 65 | 131 | $objects = $this->wordpress->getPostTypes(['public' => true], 'objects') |
| 66 | 132 | + $this->wordpress->getTaxonomies(['public' => true], 'objects'); |
| @@ -82,8 +148,10 @@ | ||
| 82 | 148 | return $objectName; |
| 83 | 149 | } |
| 84 | 150 | |
| 85 | 151 | /** |
| 152 | + * Returns the a user group object. | |
| 153 | + * @return UserGroup | |
| 86 | 154 | * @throws UserGroupTypeException |
| 87 | 155 | */ |
| 88 | 156 | public function getUserGroup(): UserGroup |
| 89 | 157 | { |
| @@ -94,19 +162,25 @@ | ||
| 94 | 162 | |
| 95 | 163 | return $this->userGroup; |
| 96 | 164 | } |
| 97 | 165 | |
| 166 | + /** | |
| 167 | + * Returns the sort url. | |
| 168 | + * @param string $sort | |
| 169 | + * @return string | |
| 170 | + */ | |
| 98 | 171 | public function getSortUrl(string $sort): string |
| 99 | 172 | { |
| 100 | 173 | $requestUrl = $this->getRequestUrl(); |
| 101 | 174 | $requestUrl = preg_replace('/&orderby[^&]*/i', '', $requestUrl); |
| 102 | 175 | $requestUrl = preg_replace('/&order[^&]*/i', '', $requestUrl); |
| 103 | - $divider = !str_contains($requestUrl, '?') ? '?' : '&'; | |
| 176 | + $divider = strpos($requestUrl, '?') === false ? '?' : '&'; | |
| 104 | 177 | $order = (string) $this->getRequestParameter('order') === 'asc' ? 'desc' : 'asc'; |
| 105 | - return "$requestUrl{$divider}orderby=$sort&order=$order"; | |
| 178 | + return "{$requestUrl}{$divider}orderby={$sort}&order={$order}"; | |
| 106 | 179 | } |
| 107 | 180 | |
| 108 | 181 | /** |
| 182 | + * Returns all user groups. | |
| 109 | 183 | * @return UserGroup[] |
| 110 | 184 | * @throws UserGroupTypeException |
| 111 | 185 | */ |
| 112 | 186 | public function getUserGroups(): array |
| @@ -136,8 +210,12 @@ | ||
| 136 | 210 | |
| 137 | 211 | return $userGroups; |
| 138 | 212 | } |
| 139 | 213 | |
| 214 | + /** | |
| 215 | + * Returns the wordpress role names. | |
| 216 | + * @return array | |
| 217 | + */ | |
| 140 | 218 | public function getRoleNames(): array |
| 141 | 219 | { |
| 142 | 220 | $roles = $this->wordpress->getRoles(); |
| 143 | 221 | return $roles->role_names; |
| @@ -143,12 +221,13 @@ | ||
| 143 | 221 | return $roles->role_names; |
| 144 | 222 | } |
| 145 | 223 | |
| 146 | 224 | /** |
| 225 | + * Action to insert or update a user group. | |
| 147 | 226 | * @throws UserGroupTypeException |
| 148 | 227 | * @throws Exception |
| 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'); |
| @@ -196,11 +275,12 @@ | ||
| 196 | 275 | } |
| 197 | 276 | } |
| 198 | 277 | |
| 199 | 278 | /** |
| 279 | + * Action to delete user groups. | |
| 200 | 280 | * @throws UserGroupTypeException |
| 201 | 281 | */ |
| 202 | - public function deleteUserGroupAction(): void | |
| 282 | + public function deleteUserGroupAction() | |
| 203 | 283 | { |
| 204 | 284 | $this->verifyNonce(self::DELETE_GROUP_NONCE); |
| 205 | 285 | $userGroups = $this->getRequestParameter('delete', []); |
| 206 | 286 | |
| @@ -210,17 +290,25 @@ | ||
| 210 | 290 | |
| 211 | 291 | $this->setUpdateMessage(TXT_UAM_DELETE_GROUP); |
| 212 | 292 | } |
| 213 | 293 | |
| 294 | + /** | |
| 295 | + * Checks if the default user group type should be added. | |
| 296 | + * @param array $defaultUserGroups | |
| 297 | + * @param string $userGroupId | |
| 298 | + * @param null|string $fromTime | |
| 299 | + * @param null|string $toTime | |
| 300 | + * @return bool | |
| 301 | + */ | |
| 214 | 302 | private function isDefaultTypeAdd( |
| 215 | 303 | array $defaultUserGroups, |
| 216 | 304 | string $userGroupId, |
| 217 | - ?string &$fromTime = null, | |
| 218 | - ?string &$toTime = null | |
| 305 | + &$fromTime = null, | |
| 306 | + &$toTime = null | |
| 219 | 307 | ): bool { |
| 220 | 308 | $userGroupInfo = isset($defaultUserGroups[$userGroupId]) === true ? $defaultUserGroups[$userGroupId] : []; |
| 221 | 309 | |
| 222 | - if (isset($userGroupInfo['id']) === true && (string) $userGroupInfo['id'] === $userGroupId) { | |
| 310 | + if (isset($userGroupInfo['id']) === true && (string) $userGroupInfo['id'] === (string) $userGroupId) { | |
| 223 | 311 | $fromTime = empty($userGroupInfo['fromTime']) === false ? $userGroupInfo['fromTime'] : null; |
| 224 | 312 | $toTime = empty($userGroupInfo['toTime']) === false ? $userGroupInfo['toTime'] : null; |
| 225 | 313 | return true; |
| 226 | 314 | } |
| @@ -228,12 +316,13 @@ | ||
| 228 | 316 | return false; |
| 229 | 317 | } |
| 230 | 318 | |
| 231 | 319 | /** |
| 320 | + * Action to set default user groups. | |
| 232 | 321 | * @throws UserGroupTypeException |
| 233 | 322 | * @throws Exception |
| 234 | 323 | */ |
| 235 | - public function setDefaultUserGroupsAction(): void | |
| 324 | + public function setDefaultUserGroupsAction() | |
| 236 | 325 | { |
| 237 | 326 | $this->verifyNonce(self::SET_DEFAULT_USER_GROUPS_NONCE); |
| 238 | 327 | $objectType = $this->getCurrentTabGroupSection(); |
| 239 | 328 | $defaultUserGroups = $this->getRequestParameter(self::DEFAULT_USER_GROUPS_FORM_FIELD, []); |