| @@ -1,6 +1,22 @@ | ||
| 1 | -<?php declare(strict_types=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 | |
| 17 | +declare(strict_types=1); | |
| 18 | + | |
| 3 | 19 | namespace UserAccessManager\Command; |
| 4 | 20 | |
| 5 | 21 | use Exception; |
| 6 | 22 | use UserAccessManager\UserGroup\AbstractUserGroup; |
| @@ -10,8 +26,13 @@ | ||
| 10 | 26 | use UserAccessManager\Wrapper\WordpressCli; |
| 11 | 27 | use WP_CLI\ExitException; |
| 12 | 28 | use WP_CLI_Command; |
| 13 | 29 | |
| 30 | +/** | |
| 31 | + * Class ObjectCommand | |
| 32 | + * | |
| 33 | + * @package UserAccessManager\Command | |
| 34 | + */ | |
| 14 | 35 | class ObjectCommand extends WP_CLI_Command |
| 15 | 36 | { |
| 16 | 37 | const ACTION_ADD = 'add'; |
| 17 | 38 | const ACTION_UPDATE = 'update'; |
| @@ -17,20 +38,31 @@ | ||
| 17 | 38 | const ACTION_UPDATE = 'update'; |
| 18 | 39 | const ACTION_REMOVE = 'remove'; |
| 19 | 40 | |
| 20 | 41 | /** |
| 42 | + * @var WordpressCli | |
| 43 | + */ | |
| 44 | + private $wordpressCli; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * @var UserGroupHandler | |
| 48 | + */ | |
| 49 | + private $userGroupHandler; | |
| 50 | + | |
| 51 | + /** | |
| 21 | 52 | * ObjectCommand constructor. |
| 22 | 53 | * @param WordpressCli $wordpressCli |
| 23 | 54 | * @param UserGroupHandler $userGroupHandler |
| 24 | 55 | */ |
| 25 | - public function __construct( | |
| 26 | - private WordpressCli $wordpressCli, | |
| 27 | - private UserGroupHandler $userGroupHandler | |
| 28 | - ) { | |
| 56 | + public function __construct(WordpressCli $wordpressCli, UserGroupHandler $userGroupHandler) | |
| 57 | + { | |
| 58 | + $this->wordpressCli = $wordpressCli; | |
| 59 | + $this->userGroupHandler = $userGroupHandler; | |
| 60 | + parent::__construct(); | |
| 29 | 61 | } |
| 30 | 62 | |
| 31 | 63 | /** |
| 32 | - * Converts the string to and associative an array of index and group | |
| 64 | + * Converts the string to and associative array of index and group | |
| 33 | 65 | * @param AbstractUserGroup[] $userGroups |
| 34 | 66 | * @return array |
| 35 | 67 | */ |
| 36 | 68 | private function getUserGroupNameMap(array $userGroups): array |
| @@ -41,12 +73,20 @@ | ||
| 41 | 73 | }, |
| 42 | 74 | $userGroups |
| 43 | 75 | ); |
| 44 | 76 | |
| 45 | - return array_flip($userGroupNames); | |
| 77 | + $userGroupNames = array_flip($userGroupNames); | |
| 78 | + return (is_array($userGroupNames) === true) ? $userGroupNames : []; | |
| 46 | 79 | } |
| 47 | 80 | |
| 48 | - 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 = '') | |
| 49 | 89 | { |
| 50 | 90 | $type = (is_numeric($identifier) === true) ? 'id' : 'name'; |
| 51 | 91 | return $namesMap[$identifier] ?? $identifier; |
| 52 | 92 | } |
| @@ -51,8 +91,17 @@ | ||
| 51 | 91 | return $namesMap[$identifier] ?? $identifier; |
| 52 | 92 | } |
| 53 | 93 | |
| 54 | 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 | |
| 55 | 104 | * @throws ExitException |
| 56 | 105 | * @throws UserGroupTypeException |
| 57 | 106 | */ |
| 58 | 107 | private function getAddRemoveUserGroups( |
| @@ -57,9 +106,9 @@ | ||
| 57 | 106 | */ |
| 58 | 107 | private function getAddRemoveUserGroups( |
| 59 | 108 | string $operation, |
| 60 | 109 | string $objectType, |
| 61 | - int|string|null $objectId, | |
| 110 | + $objectId, | |
| 62 | 111 | array $userGroupIds, |
| 63 | 112 | array $userGroups, |
| 64 | 113 | ?array &$addUserGroups = [], |
| 65 | 114 | ?array &$removeUserGroups = [] |
| @@ -71,9 +120,9 @@ | ||
| 71 | 120 | foreach ($userGroupIds as $identifier) { |
| 72 | 121 | $userGroupId = $this->getUserGroupIdAndType($namesMap, $identifier, $type); |
| 73 | 122 | |
| 74 | 123 | if (isset($userGroups[$userGroupId]) !== true) { |
| 75 | - $this->wordpressCli->error("There is no group with the $type: $identifier"); | |
| 124 | + $this->wordpressCli->error("There is no group with the {$type}: {$identifier}"); | |
| 76 | 125 | return false; |
| 77 | 126 | } |
| 78 | 127 | |
| 79 | 128 | $addUserGroups[$userGroupId] = $userGroups[$userGroupId]; |
| @@ -92,12 +141,16 @@ | ||
| 92 | 141 | |
| 93 | 142 | /** |
| 94 | 143 | * update groups for an object |
| 95 | 144 | * ## 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 | |
| 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 | |
| 100 | 153 | * ## EXAMPLES |
| 101 | 154 | * wp uam object add user 1 fighters,losers |
| 102 | 155 | * wp uam object remove role author fighters |
| 103 | 156 | * wp uam object update category 5 controller |
| @@ -106,9 +159,9 @@ | ||
| 106 | 159 | * @throws ExitException |
| 107 | 160 | * @throws UserGroupTypeException |
| 108 | 161 | * @throws Exception |
| 109 | 162 | */ |
| 110 | - public function __invoke(array $arguments, array $assocArguments): void | |
| 163 | + public function __invoke(array $arguments, array $assocArguments) | |
| 111 | 164 | { |
| 112 | 165 | if (count($arguments) < 4) { |
| 113 | 166 | $this->wordpressCli->error('<operation>, <object_type>, <object_id> and <user_groups> are required'); |
| 114 | 167 | return; |
| @@ -120,11 +173,11 @@ | ||
| 120 | 173 | self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s', |
| 121 | 174 | self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s' |
| 122 | 175 | ]; |
| 123 | 176 | |
| 124 | - // check that an operation is valid | |
| 177 | + // check that operation is valid | |
| 125 | 178 | if (isset($messages[$operation]) === false) { |
| 126 | - $this->wordpressCli->error("Operation is not valid: $operation"); | |
| 179 | + $this->wordpressCli->error("Operation is not valid: {$operation}"); | |
| 127 | 180 | return; |
| 128 | 181 | } |
| 129 | 182 | |
| 130 | 183 | $objectType = $arguments[1]; |