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