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