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