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/Command/ObjectCommand.php +122 -49 2.3.20 → 2.1.11 View file →
@@ -1,75 +1,133 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * ObjectsCommand.php
4 + *
5 + * The ObjectCommand class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <[email protected]>
10 + * @author Nils Woetzel [email protected]
11 + * @copyright 2008-2017 Alexander Schneider
12 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
13 + * @version SVN: $id$
14 + * @link http://wordpress.org/extend/plugins/user-access-manager/
15 + */
5 16 namespace UserAccessManager\Command;
6 17
7 -use Exception;
8 18 use UserAccessManager\UserGroup\AbstractUserGroup;
19 +use UserAccessManager\UserGroup\UserGroup;
9 20 use UserAccessManager\UserGroup\UserGroupHandler;
10 -use UserAccessManager\UserGroup\UserGroupTypeException;
11 21 use UserAccessManager\Wrapper\WordpressCli;
12 22 use WP_CLI\ExitException;
13 -use WP_CLI_Command;
14 23
15 -class ObjectCommand extends WP_CLI_Command
24 +/**
25 + * Class ObjectCommand
26 + *
27 + * @package UserAccessManager\Command
28 + */
29 +class ObjectCommand extends \WP_CLI_Command
16 30 {
17 - public const ACTION_ADD = 'add';
18 - public const ACTION_UPDATE = 'update';
19 - public const ACTION_REMOVE = 'remove';
31 + const ACTION_ADD = 'add';
32 + const ACTION_UPDATE = 'update';
33 + const ACTION_REMOVE = 'remove';
20 34
21 - private const SUCCESS_MESSAGES = [
22 - self::ACTION_ADD => 'Groups %1$s successfully added to %2$s %3$s',
23 - self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s',
24 - self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s'
25 - ];
35 + /**
36 + * @var WordpressCli
37 + */
38 + private $wordpressCli;
26 39
27 - public function __construct(
28 - private WordpressCli $wordpressCli,
29 - private UserGroupHandler $userGroupHandler
30 - ) {
40 + /**
41 + * @var UserGroupHandler
42 + */
43 + private $userGroupHandler;
44 +
45 + /**
46 + * ObjectCommand constructor.
47 + *
48 + * @param WordpressCli $wordpressCli
49 + * @param UserGroupHandler $userGroupHandler
50 + */
51 + public function __construct(WordpressCli $wordpressCli, UserGroupHandler $userGroupHandler)
52 + {
53 + $this->wordpressCli = $wordpressCli;
54 + $this->userGroupHandler = $userGroupHandler;
31 55 }
32 56
33 57 /**
58 + * Converts the string to and associative array of index and group
59 + *
34 60 * @param AbstractUserGroup[] $userGroups
35 - * @return array<string, int|string> group name to group id
61 + *
62 + * @return array
36 63 */
37 - private function getUserGroupNameMap(array $userGroups): array
64 + private function getUserGroupNameMap(array $userGroups)
38 65 {
39 - return array_flip(array_map(
40 - fn(AbstractUserGroup $userGroup) => $userGroup->getName(),
66 + $userGroupNames = array_map(
67 + function (UserGroup $userGroup) {
68 + return $userGroup->getName();
69 + },
41 70 $userGroups
42 - ));
71 + );
72 +
73 + foreach ($userGroups as $userGroup) {
74 + $userGroupNames[$userGroup->getId()] = $userGroup->getName();
75 + }
76 +
77 + $userGroupNames = array_flip($userGroupNames);
78 + return (is_array($userGroupNames) === true) ? $userGroupNames : [];
43 79 }
44 80
45 - private function getUserGroupIdAndType(array $namesMap, string $identifier, ?string &$type = ''): int|string
81 + /**
82 + * Returns the user group id and the type of the id.
83 + *
84 + * @param array $namesMap
85 + * @param string $identifier
86 + * @param string $type
87 + *
88 + * @return mixed
89 + */
90 + private function getUserGroupIdAndType(array $namesMap, $identifier, &$type)
46 91 {
47 92 $type = (is_numeric($identifier) === true) ? 'id' : 'name';
48 - return $namesMap[$identifier] ?? $identifier;
93 + return isset($namesMap[$identifier]) ? $namesMap[$identifier] : $identifier;
49 94 }
50 95
51 96 /**
97 + * Returns the add and remove user groups by reference.
98 + *
99 + * @param string $operation
100 + * @param string $objectType
101 + * @param string $objectId
102 + * @param string $userGroupsArgument
103 + * @param AbstractUserGroup[] $userGroups
104 + * @param array $addUserGroups
105 + * @param array $removeUserGroups
106 + *
107 + * @return bool
108 + *
52 109 * @throws ExitException
53 - * @throws UserGroupTypeException
54 110 */
55 111 private function getAddRemoveUserGroups(
56 - string $operation,
57 - string $objectType,
58 - int|string|null $objectId,
59 - array $userGroupIds,
112 + $operation,
113 + $objectType,
114 + $objectId,
115 + $userGroupsArgument,
60 116 array $userGroups,
61 - ?array &$addUserGroups = [],
62 - ?array &$removeUserGroups = []
63 - ): bool {
117 + &$addUserGroups,
118 + &$removeUserGroups
119 + ) {
64 120 $addUserGroups = [];
121 + $userGroupIds = array_unique(explode(',', $userGroupsArgument));
65 122 $namesMap = $this->getUserGroupNameMap($userGroups);
66 123
124 + // find the UserGroup object for the ids or strings given on the commandline
67 125 foreach ($userGroupIds as $identifier) {
68 126 $userGroupId = $this->getUserGroupIdAndType($namesMap, $identifier, $type);
69 127
70 128 if (isset($userGroups[$userGroupId]) !== true) {
71 - $this->wordpressCli->error("There is no group with the $type: $identifier");
129 + $this->wordpressCli->error("There is no group with the {$type}: {$identifier}");
72 130 return false;
73 131 }
74 132
75 133 $addUserGroups[$userGroupId] = $userGroups[$userGroupId];
@@ -87,24 +145,35 @@
87 145 }
88 146
89 147 /**
90 148 * update groups for an object
149 + *
91 150 * ## OPTIONS
92 - * <operation>: 'add', 'remove' or 'update'
93 - * <object_type>: 'page', 'post', 'user', 'role', 'category' or any other term type
94 - * <object_id>: the id of the object (string for role)
95 - * <user_groups>: comma separated list of group names or ids to add, remove of update to for the object
151 + *
152 + * <operation>
153 + * : 'add', 'remove' or 'update'
154 + *
155 + * <object_type>
156 + * : 'page', 'post', 'user', 'role', 'category' or any other term type
157 + *
158 + * <object_id>
159 + * : the id of the object (string for role)
160 + *
161 + * <user_groups>
162 + * : comma separated list of group names or ids to add, remove of update to for the object
163 + *
96 164 * ## EXAMPLES
165 + *
97 166 * wp uam object add user 1 fighters,losers
98 167 * wp uam object remove role author fighters
99 168 * wp uam object update category 5 controller
169 + *
100 170 * @param array $arguments
101 171 * @param array $assocArguments
172 + *
102 173 * @throws ExitException
103 - * @throws UserGroupTypeException
104 - * @throws Exception
105 174 */
106 - public function __invoke(array $arguments, array $assocArguments): void
175 + public function __invoke(array $arguments, array $assocArguments)
107 176 {
108 177 if (count($arguments) < 4) {
109 178 $this->wordpressCli->error('<operation>, <object_type>, <object_id> and <user_groups> are required');
110 179 return;
@@ -110,17 +179,23 @@
110 179 return;
111 180 }
112 181
113 182 $operation = $arguments[0];
183 + $messages = [
184 + self::ACTION_ADD => 'Groups %1$s successfully added to %2$s %3$s',
185 + self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s',
186 + self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s'
187 + ];
114 188
115 - if (isset(self::SUCCESS_MESSAGES[$operation]) === false) {
116 - $this->wordpressCli->error("Operation is not valid: $operation");
189 + // check that operation is valid
190 + if (isset($messages[$operation]) === false) {
191 + $this->wordpressCli->error("Operation is not valid: {$operation}");
117 192 return;
118 193 }
119 194
120 195 $objectType = $arguments[1];
121 196 $objectId = $arguments[2];
122 - $userGroupIds = array_unique(explode(',', $arguments[3]));
197 + $userGroupsArgument = $arguments[3];
123 198 $userGroups = $this->userGroupHandler->getUserGroups();
124 199
125 200 $success = $this->getAddRemoveUserGroups(
126 201 $operation,
@@ -125,9 +200,9 @@
125 200 $success = $this->getAddRemoveUserGroups(
126 201 $operation,
127 202 $objectType,
128 203 $objectId,
129 - $userGroupIds,
204 + $userGroupsArgument,
130 205 $userGroups,
131 206 $addUserGroups,
132 207 $removeUserGroups
133 208 );
@@ -147,9 +222,7 @@
147 222
148 223 $userGroup->save();
149 224 }
150 225
151 - $this->wordpressCli->success(
152 - sprintf(self::SUCCESS_MESSAGES[$operation], implode(', ', $userGroupIds), $objectType, $objectId)
153 - );
226 + $this->wordpressCli->success(sprintf($messages[$operation], $userGroupsArgument, $objectType, $objectId));
154 227 }
155 228 }