PluginProbe
User Access Manager / 2.3.13
User Access Manager v2.3.13
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
user-access-manager / src / Command / ObjectCommand.php

ObjectCommand.php in User Access Manager 2.3.13, at src/Command/ObjectCommand.php

166 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace UserAccessManager\Command;
4
5 use Exception;
6 use UserAccessManager\UserGroup\AbstractUserGroup;
7 use UserAccessManager\UserGroup\UserGroup;
8 use UserAccessManager\UserGroup\UserGroupHandler;
9 use UserAccessManager\UserGroup\UserGroupTypeException;
10 use UserAccessManager\Wrapper\WordpressCli;
11 use WP_CLI\ExitException;
12 use WP_CLI_Command;
13
14 class ObjectCommand extends WP_CLI_Command
15 {
16 const ACTION_ADD = 'add';
17 const ACTION_UPDATE = 'update';
18 const ACTION_REMOVE = 'remove';
19
20 /**
21 * ObjectCommand constructor.
22 * @param WordpressCli $wordpressCli
23 * @param UserGroupHandler $userGroupHandler
24 */
25 public function __construct(
26 private WordpressCli $wordpressCli,
27 private UserGroupHandler $userGroupHandler
28 ) {
29 }
30
31 /**
32 * Converts the string to and associative an array of index and group
33 * @param AbstractUserGroup[] $userGroups
34 * @return array
35 */
36 private function getUserGroupNameMap(array $userGroups): array
37 {
38 $userGroupNames = array_map(
39 function (UserGroup $userGroup) {
40 return $userGroup->getName();
41 },
42 $userGroups
43 );
44
45 return array_flip($userGroupNames);
46 }
47
48 private function getUserGroupIdAndType(array $namesMap, string $identifier, ?string &$type = ''): int|string
49 {
50 $type = (is_numeric($identifier) === true) ? 'id' : 'name';
51 return $namesMap[$identifier] ?? $identifier;
52 }
53
54 /**
55 * @throws ExitException
56 * @throws UserGroupTypeException
57 */
58 private function getAddRemoveUserGroups(
59 string $operation,
60 string $objectType,
61 int|string|null $objectId,
62 array $userGroupIds,
63 array $userGroups,
64 ?array &$addUserGroups = [],
65 ?array &$removeUserGroups = []
66 ): bool {
67 $addUserGroups = [];
68 $namesMap = $this->getUserGroupNameMap($userGroups);
69
70 // find the UserGroup object for the ids or strings given on the commandline
71 foreach ($userGroupIds as $identifier) {
72 $userGroupId = $this->getUserGroupIdAndType($namesMap, $identifier, $type);
73
74 if (isset($userGroups[$userGroupId]) !== true) {
75 $this->wordpressCli->error("There is no group with the $type: $identifier");
76 return false;
77 }
78
79 $addUserGroups[$userGroupId] = $userGroups[$userGroupId];
80 }
81
82 $removeUserGroups = ($operation === self::ACTION_UPDATE) ?
83 $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId) : [];
84
85 if ($operation === self::ACTION_REMOVE) {
86 $removeUserGroups = $addUserGroups;
87 $addUserGroups = [];
88 }
89
90 return true;
91 }
92
93 /**
94 * update groups for an object
95 * ## 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
100 * ## EXAMPLES
101 * wp uam object add user 1 fighters,losers
102 * wp uam object remove role author fighters
103 * wp uam object update category 5 controller
104 * @param array $arguments
105 * @param array $assocArguments
106 * @throws ExitException
107 * @throws UserGroupTypeException
108 * @throws Exception
109 */
110 public function __invoke(array $arguments, array $assocArguments): void
111 {
112 if (count($arguments) < 4) {
113 $this->wordpressCli->error('<operation>, <object_type>, <object_id> and <user_groups> are required');
114 return;
115 }
116
117 $operation = $arguments[0];
118 $messages = [
119 self::ACTION_ADD => 'Groups %1$s successfully added to %2$s %3$s',
120 self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s',
121 self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s'
122 ];
123
124 // check that an operation is valid
125 if (isset($messages[$operation]) === false) {
126 $this->wordpressCli->error("Operation is not valid: $operation");
127 return;
128 }
129
130 $objectType = $arguments[1];
131 $objectId = $arguments[2];
132 $userGroupIds = array_unique(explode(',', $arguments[3]));
133 $userGroups = $this->userGroupHandler->getUserGroups();
134
135 $success = $this->getAddRemoveUserGroups(
136 $operation,
137 $objectType,
138 $objectId,
139 $userGroupIds,
140 $userGroups,
141 $addUserGroups,
142 $removeUserGroups
143 );
144
145 if ($success === false) {
146 return;
147 }
148
149 foreach ($userGroups as $groupId => $userGroup) {
150 if (isset($removeUserGroups[$groupId]) === true) {
151 $userGroup->removeObject($objectType, $objectId);
152 }
153
154 if (isset($addUserGroups[$groupId]) === true) {
155 $userGroup->addObject($objectType, $objectId);
156 }
157
158 $userGroup->save();
159 }
160
161 $this->wordpressCli->success(
162 sprintf($messages[$operation], implode(', ', $userGroupIds), $objectType, $objectId)
163 );
164 }
165 }
166