PluginProbe
User Access Manager / 2.3.18
User Access Manager v2.3.18
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.18, at src/Command/ObjectCommand.php

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