PluginProbe
User Access Manager / 2.3.1
User Access Manager v2.3.1
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.1, at src/Command/ObjectCommand.php

167 lines 5.3 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 parent::__construct();
30 }
31
32 /**
33 * Converts the string to and associative an array of index and group
34 * @param AbstractUserGroup[] $userGroups
35 * @return array
36 */
37 private function getUserGroupNameMap(array $userGroups): array
38 {
39 $userGroupNames = array_map(
40 function (UserGroup $userGroup) {
41 return $userGroup->getName();
42 },
43 $userGroups
44 );
45
46 return array_flip($userGroupNames);
47 }
48
49 private function getUserGroupIdAndType(array $namesMap, string $identifier, ?string &$type = ''): int|string
50 {
51 $type = (is_numeric($identifier) === true) ? 'id' : 'name';
52 return $namesMap[$identifier] ?? $identifier;
53 }
54
55 /**
56 * @throws ExitException
57 * @throws UserGroupTypeException
58 */
59 private function getAddRemoveUserGroups(
60 string $operation,
61 string $objectType,
62 int|string $objectId,
63 array $userGroupIds,
64 array $userGroups,
65 ?array &$addUserGroups = [],
66 ?array &$removeUserGroups = []
67 ): bool {
68 $addUserGroups = [];
69 $namesMap = $this->getUserGroupNameMap($userGroups);
70
71 // find the UserGroup object for the ids or strings given on the commandline
72 foreach ($userGroupIds as $identifier) {
73 $userGroupId = $this->getUserGroupIdAndType($namesMap, $identifier, $type);
74
75 if (isset($userGroups[$userGroupId]) !== true) {
76 $this->wordpressCli->error("There is no group with the $type: $identifier");
77 return false;
78 }
79
80 $addUserGroups[$userGroupId] = $userGroups[$userGroupId];
81 }
82
83 $removeUserGroups = ($operation === self::ACTION_UPDATE) ?
84 $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId) : [];
85
86 if ($operation === self::ACTION_REMOVE) {
87 $removeUserGroups = $addUserGroups;
88 $addUserGroups = [];
89 }
90
91 return true;
92 }
93
94 /**
95 * update groups for an object
96 * ## OPTIONS
97 * <operation>: 'add', 'remove' or 'update'
98 * <object_type>: 'page', 'post', 'user', 'role', 'category' or any other term type
99 * <object_id>: the id of the object (string for role)
100 * <user_groups>: comma separated list of group names or ids to add, remove of update to for the object
101 * ## EXAMPLES
102 * wp uam object add user 1 fighters,losers
103 * wp uam object remove role author fighters
104 * wp uam object update category 5 controller
105 * @param array $arguments
106 * @param array $assocArguments
107 * @throws ExitException
108 * @throws UserGroupTypeException
109 * @throws Exception
110 */
111 public function __invoke(array $arguments, array $assocArguments): void
112 {
113 if (count($arguments) < 4) {
114 $this->wordpressCli->error('<operation>, <object_type>, <object_id> and <user_groups> are required');
115 return;
116 }
117
118 $operation = $arguments[0];
119 $messages = [
120 self::ACTION_ADD => 'Groups %1$s successfully added to %2$s %3$s',
121 self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s',
122 self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s'
123 ];
124
125 // check that an operation is valid
126 if (isset($messages[$operation]) === false) {
127 $this->wordpressCli->error("Operation is not valid: $operation");
128 return;
129 }
130
131 $objectType = $arguments[1];
132 $objectId = $arguments[2];
133 $userGroupIds = array_unique(explode(',', $arguments[3]));
134 $userGroups = $this->userGroupHandler->getUserGroups();
135
136 $success = $this->getAddRemoveUserGroups(
137 $operation,
138 $objectType,
139 $objectId,
140 $userGroupIds,
141 $userGroups,
142 $addUserGroups,
143 $removeUserGroups
144 );
145
146 if ($success === false) {
147 return;
148 }
149
150 foreach ($userGroups as $groupId => $userGroup) {
151 if (isset($removeUserGroups[$groupId]) === true) {
152 $userGroup->removeObject($objectType, $objectId);
153 }
154
155 if (isset($addUserGroups[$groupId]) === true) {
156 $userGroup->addObject($objectType, $objectId);
157 }
158
159 $userGroup->save();
160 }
161
162 $this->wordpressCli->success(
163 sprintf($messages[$operation], implode(', ', $userGroupIds), $objectType, $objectId)
164 );
165 }
166 }
167