| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * ObjectHandler.php | |
| 4 | + * | |
| 5 | + * The ObjectHandler class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Object; |
| @@ -4,9 +17,9 @@ | ||
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Object; |
| 6 | 19 | |
| 7 | 20 | use Exception; |
| 8 | -use UserAccessManager\ObjectMembership\Exception\MissingObjectMembershipHandlerException; | |
| 21 | +use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException; | |
| 9 | 22 | use UserAccessManager\ObjectMembership\ObjectMembershipHandler; |
| 10 | 23 | use UserAccessManager\ObjectMembership\ObjectMembershipHandlerFactory; |
| 11 | 24 | use UserAccessManager\Wrapper\Php; |
| 12 | 25 | use UserAccessManager\Wrapper\Wordpress; |
| @@ -11,46 +24,113 @@ | ||
| 11 | 24 | use UserAccessManager\Wrapper\Php; |
| 12 | 25 | use UserAccessManager\Wrapper\Wordpress; |
| 13 | 26 | use WP_Post; |
| 14 | 27 | use WP_Post_Type; |
| 15 | -use WP_Taxonomy; | |
| 16 | 28 | use WP_Term; |
| 17 | 29 | use WP_User; |
| 18 | 30 | |
| 31 | +/** | |
| 32 | + * Class ObjectHandler | |
| 33 | + * | |
| 34 | + * @package UserAccessManager\ObjectHandler | |
| 35 | + */ | |
| 19 | 36 | class ObjectHandler |
| 20 | 37 | { |
| 21 | - public const GENERAL_ROLE_OBJECT_TYPE = '_role_'; | |
| 22 | - public const GENERAL_USER_OBJECT_TYPE = '_user_'; | |
| 23 | - public const GENERAL_POST_OBJECT_TYPE = '_post_'; | |
| 24 | - public const GENERAL_TERM_OBJECT_TYPE = '_term_'; | |
| 25 | - public const ATTACHMENT_OBJECT_TYPE = 'attachment'; | |
| 26 | - public const POST_OBJECT_TYPE = 'post'; | |
| 27 | - public const PAGE_OBJECT_TYPE = 'page'; | |
| 28 | - public const POST_FORMAT_TYPE = 'post_format'; | |
| 38 | + const GENERAL_ROLE_OBJECT_TYPE = '_role_'; | |
| 39 | + const GENERAL_USER_OBJECT_TYPE = '_user_'; | |
| 40 | + const GENERAL_POST_OBJECT_TYPE = '_post_'; | |
| 41 | + const GENERAL_TERM_OBJECT_TYPE = '_term_'; | |
| 42 | + const ATTACHMENT_OBJECT_TYPE = 'attachment'; | |
| 43 | + const POST_OBJECT_TYPE = 'post'; | |
| 44 | + const PAGE_OBJECT_TYPE = 'page'; | |
| 45 | + const POST_FORMAT_TYPE = 'post_format'; | |
| 29 | 46 | |
| 30 | - private ?array $postTypes = null; | |
| 31 | - /** @var WP_Taxonomy[] */ | |
| 32 | - private ?array $taxonomies = null; | |
| 33 | - /** @var WP_User[] */ | |
| 34 | - private array $users = []; | |
| 35 | - /** @var WP_Post[] */ | |
| 36 | - private array $posts = []; | |
| 37 | - /** @var WP_Term[] */ | |
| 38 | - private array $terms = []; | |
| 39 | - private ?array $objectMembershipHandlers = null; | |
| 40 | - private ?array $objectTypes = null; | |
| 41 | - private ?array $allObjectTypesMap = null; | |
| 42 | - private ?array $allObjectTypes = null; | |
| 43 | - private array $validObjectTypes = []; | |
| 47 | + /** | |
| 48 | + * @var Php | |
| 49 | + */ | |
| 50 | + private $php; | |
| 44 | 51 | |
| 52 | + /** | |
| 53 | + * @var Wordpress | |
| 54 | + */ | |
| 55 | + private $wordpress; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @var ObjectMembershipHandlerFactory | |
| 59 | + */ | |
| 60 | + private $membershipHandlerFactory; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * @var null|array | |
| 64 | + */ | |
| 65 | + private $postTypes = null; | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * @var null|array | |
| 69 | + */ | |
| 70 | + private $taxonomies = null; | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * @var WP_User | |
| 74 | + */ | |
| 75 | + private $users = null; | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * @var WP_Post[] | |
| 79 | + */ | |
| 80 | + private $posts = null; | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * @var WP_Term[] | |
| 84 | + */ | |
| 85 | + private $terms = null; | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * @var null|array | |
| 89 | + */ | |
| 90 | + private $objectMembershipHandlers = null; | |
| 91 | + | |
| 92 | + /** | |
| 93 | + * @var null|array | |
| 94 | + */ | |
| 95 | + private $objectTypes = null; | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * @var null|array | |
| 99 | + */ | |
| 100 | + private $allObjectTypesMap = null; | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * @var null|array | |
| 104 | + */ | |
| 105 | + private $allObjectTypes = null; | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * @var array | |
| 109 | + */ | |
| 110 | + private $validObjectTypes = []; | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * ObjectHandler constructor. | |
| 114 | + * @param Php $php | |
| 115 | + * @param Wordpress $wordpress | |
| 116 | + * @param ObjectMembershipHandlerFactory $membershipHandlerFactory | |
| 117 | + */ | |
| 45 | 118 | public function __construct( |
| 46 | - private Php $php, | |
| 47 | - private Wordpress $wordpress, | |
| 48 | - private ObjectMembershipHandlerFactory $membershipHandlerFactory | |
| 119 | + Php $php, | |
| 120 | + Wordpress $wordpress, | |
| 121 | + ObjectMembershipHandlerFactory $membershipHandlerFactory | |
| 49 | 122 | ) { |
| 123 | + $this->php = $php; | |
| 124 | + $this->wordpress = $wordpress; | |
| 125 | + $this->membershipHandlerFactory = $membershipHandlerFactory; | |
| 50 | 126 | } |
| 51 | 127 | |
| 52 | - public function getPostTypes(): array | |
| 128 | + /** | |
| 129 | + * Returns all post types. | |
| 130 | + * @return array | |
| 131 | + */ | |
| 132 | + public function getPostTypes(): ?array | |
| 53 | 133 | { |
| 54 | 134 | if ($this->postTypes === null) { |
| 55 | 135 | $this->postTypes = $this->wordpress->getPostTypes(['public' => true]); |
| 56 | 136 | } |
| @@ -57,9 +137,13 @@ | ||
| 57 | 137 | |
| 58 | 138 | return $this->postTypes; |
| 59 | 139 | } |
| 60 | 140 | |
| 61 | - public function getTaxonomies(): array | |
| 141 | + /** | |
| 142 | + * Returns the taxonomies. | |
| 143 | + * @return array | |
| 144 | + */ | |
| 145 | + public function getTaxonomies(): ?array | |
| 62 | 146 | { |
| 63 | 147 | if ($this->taxonomies === null) { |
| 64 | 148 | $this->taxonomies = $this->wordpress->getTaxonomies(['public' => true]); |
| 65 | 149 | } |
| @@ -66,9 +150,14 @@ | ||
| 66 | 150 | |
| 67 | 151 | return $this->taxonomies; |
| 68 | 152 | } |
| 69 | 153 | |
| 70 | - public function getUser(int|string|null $id): WP_User|bool | |
| 154 | + /** | |
| 155 | + * Returns a user. | |
| 156 | + * @param int|string $id The user id. | |
| 157 | + * @return WP_User|false | |
| 158 | + */ | |
| 159 | + public function getUser($id) | |
| 71 | 160 | { |
| 72 | 161 | if (isset($this->users[$id]) === false) { |
| 73 | 162 | $this->users[$id] = $this->wordpress->getUserData($id); |
| 74 | 163 | } |
| @@ -75,9 +164,14 @@ | ||
| 75 | 164 | |
| 76 | 165 | return $this->users[$id]; |
| 77 | 166 | } |
| 78 | 167 | |
| 79 | - public function getPost(int|string|null $id): bool|WP_Post | |
| 168 | + /** | |
| 169 | + * Returns a post. | |
| 170 | + * @param int|string $id The post id. | |
| 171 | + * @return WP_Post|false | |
| 172 | + */ | |
| 173 | + public function getPost($id) | |
| 80 | 174 | { |
| 81 | 175 | if (isset($this->posts[$id]) === false) { |
| 82 | 176 | $post = $this->wordpress->getPost($id); |
| 83 | 177 | $this->posts[$id] = ($post instanceof WP_Post) ? $post : false; |
| @@ -85,9 +179,15 @@ | ||
| 85 | 179 | |
| 86 | 180 | return $this->posts[$id]; |
| 87 | 181 | } |
| 88 | 182 | |
| 89 | - public function getTerm(int|string|null $id, string $taxonomy = ''): WP_Term|bool | |
| 183 | + /** | |
| 184 | + * Returns a term. | |
| 185 | + * @param int|string $id The term id. | |
| 186 | + * @param string $taxonomy The taxonomy. | |
| 187 | + * @return false|WP_Term | |
| 188 | + */ | |
| 189 | + public function getTerm($id, $taxonomy = '') | |
| 90 | 190 | { |
| 91 | 191 | $fullId = $id . '|' . $taxonomy; |
| 92 | 192 | |
| 93 | 193 | if (isset($this->terms[$fullId]) === false) { |
| @@ -97,48 +197,71 @@ | ||
| 97 | 197 | |
| 98 | 198 | return $this->terms[$fullId]; |
| 99 | 199 | } |
| 100 | 200 | |
| 101 | - private function resetDerivedObjectTypeCaches(): void | |
| 102 | - { | |
| 103 | - $this->objectTypes = null; | |
| 104 | - $this->allObjectTypes = null; | |
| 105 | - $this->allObjectTypesMap = null; | |
| 106 | - $this->validObjectTypes = []; | |
| 107 | - } | |
| 108 | - | |
| 109 | 201 | /** |
| 202 | + * Used for adding custom post types using the registered_post_type hook | |
| 110 | 203 | * @see http://wordpress.org/support/topic/modifying-post-type-using-the-registered_post_type-hook |
| 204 | + * @param string $postType The string for the new post_type | |
| 205 | + * @param WP_Post_Type $arguments The array of arguments used to create the post_type | |
| 111 | 206 | */ |
| 112 | - public function registeredPostType(string $postType, WP_Post_Type $arguments): void | |
| 207 | + public function registeredPostType(string $postType, WP_Post_Type $arguments) | |
| 113 | 208 | { |
| 114 | - if ($arguments->public === true) { | |
| 209 | + if ((bool) $arguments->public === true) { | |
| 115 | 210 | $this->postTypes = $this->getPostTypes(); |
| 116 | 211 | $this->postTypes[$postType] = $postType; |
| 117 | - $this->resetDerivedObjectTypeCaches(); | |
| 212 | + $this->objectTypes = null; | |
| 213 | + $this->allObjectTypes = null; | |
| 214 | + $this->allObjectTypesMap = null; | |
| 215 | + $this->validObjectTypes = []; | |
| 118 | 216 | } |
| 119 | 217 | } |
| 120 | 218 | |
| 121 | - public function registeredTaxonomy(string $taxonomy, array|string|null $objectType, array $arguments): void | |
| 219 | + /** | |
| 220 | + * Adds an custom taxonomy. | |
| 221 | + * @param string $taxonomy | |
| 222 | + * @param array|string $objectType | |
| 223 | + * @param array $arguments | |
| 224 | + */ | |
| 225 | + public function registeredTaxonomy(string $taxonomy, $objectType, array $arguments) | |
| 122 | 226 | { |
| 123 | 227 | if ((bool) $arguments['public'] === true) { |
| 124 | 228 | $this->taxonomies = $this->getTaxonomies(); |
| 125 | 229 | $this->taxonomies[$taxonomy] = $taxonomy; |
| 126 | - $this->resetDerivedObjectTypeCaches(); | |
| 230 | + $this->objectTypes = null; | |
| 231 | + $this->allObjectTypes = null; | |
| 232 | + $this->allObjectTypesMap = null; | |
| 233 | + $this->validObjectTypes = []; | |
| 127 | 234 | } |
| 128 | 235 | } |
| 129 | 236 | |
| 237 | + /** | |
| 238 | + * Checks if type is postable. | |
| 239 | + * @param string $type | |
| 240 | + * @return bool | |
| 241 | + */ | |
| 130 | 242 | public function isPostType(string $type): bool |
| 131 | 243 | { |
| 132 | - return isset($this->getPostTypes()[$type]); | |
| 244 | + $postableTypes = $this->getPostTypes(); | |
| 245 | + return isset($postableTypes[$type]); | |
| 133 | 246 | } |
| 134 | 247 | |
| 248 | + /** | |
| 249 | + * Checks if the taxonomy is a valid one. | |
| 250 | + * @param string $taxonomy | |
| 251 | + * @return bool | |
| 252 | + */ | |
| 135 | 253 | public function isTaxonomy(string $taxonomy): bool |
| 136 | 254 | { |
| 137 | - return in_array($taxonomy, $this->getTaxonomies()); | |
| 255 | + $taxonomies = $this->getTaxonomies(); | |
| 256 | + return in_array($taxonomy, $taxonomies); | |
| 138 | 257 | } |
| 139 | 258 | |
| 140 | - public function getObjectTypes(): array | |
| 259 | + /** | |
| 260 | + * Returns the predefined object types. | |
| 261 | + * @return array | |
| 262 | + */ | |
| 263 | + public function getObjectTypes(): ?array | |
| 141 | 264 | { |
| 142 | 265 | if ($this->objectTypes === null) { |
| 143 | 266 | $this->objectTypes = array_merge( |
| 144 | 267 | $this->getPostTypes(), |
| @@ -149,26 +272,31 @@ | ||
| 149 | 272 | return $this->objectTypes; |
| 150 | 273 | } |
| 151 | 274 | |
| 152 | 275 | /** |
| 276 | + * Returns the object types map. | |
| 277 | + * @return array | |
| 153 | 278 | * @throws Exception |
| 154 | 279 | */ |
| 155 | - private function getAllObjectsTypesMap(): array | |
| 280 | + private function getAllObjectsTypesMap(): ?array | |
| 156 | 281 | { |
| 157 | 282 | if ($this->allObjectTypesMap === null) { |
| 158 | 283 | $this->allObjectTypesMap = []; |
| 284 | + $objectHandlers = $this->getObjectMembershipHandlers(); | |
| 159 | 285 | |
| 160 | - foreach ($this->getObjectMembershipHandlers() as $membershipHandler) { | |
| 161 | - $handledObjects = $membershipHandler->getHandledObjects(); | |
| 286 | + foreach ($objectHandlers as $objectHandler) { | |
| 287 | + $handledObjects = $objectHandler->getHandledObjects(); | |
| 162 | 288 | |
| 163 | 289 | if ($handledObjects === []) { |
| 164 | 290 | continue; |
| 165 | 291 | } |
| 166 | 292 | |
| 167 | - $this->allObjectTypesMap = array_merge($this->allObjectTypesMap, array_combine( | |
| 293 | + $handledObjectsMap = array_combine( | |
| 168 | 294 | $handledObjects, |
| 169 | - $this->php->arrayFill(0, count($handledObjects), $membershipHandler->getGeneralObjectType()) | |
| 170 | - )); | |
| 295 | + $this->php->arrayFill(0, count($handledObjects), $objectHandler->getGeneralObjectType()) | |
| 296 | + ); | |
| 297 | + | |
| 298 | + $this->allObjectTypesMap = array_merge($this->allObjectTypesMap, $handledObjectsMap); | |
| 171 | 299 | } |
| 172 | 300 | } |
| 173 | 301 | |
| 174 | 302 | return $this->allObjectTypesMap; |
| @@ -174,11 +302,13 @@ | ||
| 174 | 302 | return $this->allObjectTypesMap; |
| 175 | 303 | } |
| 176 | 304 | |
| 177 | 305 | /** |
| 306 | + * Returns all objects types. | |
| 307 | + * @return array | |
| 178 | 308 | * @throws Exception |
| 179 | 309 | */ |
| 180 | - public function getAllObjectTypes(): array | |
| 310 | + public function getAllObjectTypes(): ?array | |
| 181 | 311 | { |
| 182 | 312 | if ($this->allObjectTypes === null) { |
| 183 | 313 | $objectTypes = array_keys($this->getAllObjectsTypesMap()); |
| 184 | 314 | $this->allObjectTypes = array_combine($objectTypes, $objectTypes); |
| @@ -187,22 +317,30 @@ | ||
| 187 | 317 | return $this->allObjectTypes; |
| 188 | 318 | } |
| 189 | 319 | |
| 190 | 320 | /** |
| 321 | + * Returns the general object type. | |
| 322 | + * @param string|null $objectType | |
| 323 | + * @return string | |
| 191 | 324 | * @throws Exception |
| 192 | 325 | */ |
| 193 | 326 | public function getGeneralObjectType(?string $objectType): ?string |
| 194 | 327 | { |
| 195 | - return $this->getAllObjectsTypesMap()[$objectType] ?? null; | |
| 328 | + $objectsTypeMap = $this->getAllObjectsTypesMap(); | |
| 329 | + return (isset($objectsTypeMap[$objectType]) === true) ? $objectsTypeMap[$objectType] : null; | |
| 196 | 330 | } |
| 197 | 331 | |
| 198 | 332 | /** |
| 333 | + * Checks if the object type is a valid one. | |
| 334 | + * @param string|null $objectType The object type to check. | |
| 335 | + * @return bool | |
| 199 | 336 | * @throws Exception |
| 200 | 337 | */ |
| 201 | 338 | public function isValidObjectType(?string $objectType): bool |
| 202 | 339 | { |
| 203 | 340 | if (isset($this->validObjectTypes[$objectType]) === false) { |
| 204 | - $this->validObjectTypes[$objectType] = isset($this->getAllObjectTypes()[$objectType]); | |
| 341 | + $objectTypesMap = $this->getAllObjectTypes(); | |
| 342 | + $this->validObjectTypes[$objectType] = isset($objectTypesMap[$objectType]); | |
| 205 | 343 | } |
| 206 | 344 | |
| 207 | 345 | return $this->validObjectTypes[$objectType]; |
| 208 | 346 | } |
| @@ -207,30 +345,32 @@ | ||
| 207 | 345 | return $this->validObjectTypes[$objectType]; |
| 208 | 346 | } |
| 209 | 347 | |
| 210 | 348 | /** |
| 349 | + * Returns the object membership handlers. | |
| 211 | 350 | * @return ObjectMembershipHandler[] |
| 212 | 351 | * @throws Exception |
| 213 | 352 | */ |
| 214 | - private function getObjectMembershipHandlers(): array | |
| 353 | + private function getObjectMembershipHandlers(): ?array | |
| 215 | 354 | { |
| 216 | 355 | if ($this->objectMembershipHandlers === null) { |
| 217 | - $handlersByGeneralObjectType = []; | |
| 356 | + $factory = $this->membershipHandlerFactory; | |
| 218 | 357 | |
| 219 | - foreach ([ | |
| 220 | - $this->membershipHandlerFactory->createRoleMembershipHandler(), | |
| 221 | - $this->membershipHandlerFactory->createUserMembershipHandler($this), | |
| 222 | - $this->membershipHandlerFactory->createTermMembershipHandler($this), | |
| 223 | - $this->membershipHandlerFactory->createPostMembershipHandler($this) | |
| 224 | - ] as $membershipHandler) { | |
| 225 | - $handlersByGeneralObjectType[$membershipHandler->getGeneralObjectType()] = $membershipHandler; | |
| 226 | - } | |
| 358 | + $roleMembershipHandler = $factory->createRoleMembershipHandler(); | |
| 359 | + $userMembershipHandler = $factory->createUserMembershipHandler($this); | |
| 360 | + $termMembershipHandler = $factory->createTermMembershipHandler($this); | |
| 361 | + $postMembershipHandler = $factory->createPostMembershipHandler($this); | |
| 227 | 362 | |
| 228 | - // Published before the filter runs, so a callback that re-enters does not rebuild the handlers. | |
| 229 | - $this->objectMembershipHandlers = $handlersByGeneralObjectType; | |
| 363 | + $this->objectMembershipHandlers = [ | |
| 364 | + $roleMembershipHandler->getGeneralObjectType() => $roleMembershipHandler, | |
| 365 | + $userMembershipHandler->getGeneralObjectType() => $userMembershipHandler, | |
| 366 | + $termMembershipHandler->getGeneralObjectType() => $termMembershipHandler, | |
| 367 | + $postMembershipHandler->getGeneralObjectType() => $postMembershipHandler | |
| 368 | + ]; | |
| 369 | + | |
| 230 | 370 | $this->objectMembershipHandlers = $this->wordpress->applyFilters( |
| 231 | 371 | 'uam_register_object_membership_handler', |
| 232 | - $handlersByGeneralObjectType | |
| 372 | + $this->objectMembershipHandlers | |
| 233 | 373 | ); |
| 234 | 374 | } |
| 235 | 375 | |
| 236 | 376 | return $this->objectMembershipHandlers; |
| @@ -236,8 +376,11 @@ | ||
| 236 | 376 | return $this->objectMembershipHandlers; |
| 237 | 377 | } |
| 238 | 378 | |
| 239 | 379 | /** |
| 380 | + * Returns the membership handler for the given object type. | |
| 381 | + * @param null|string $objectType | |
| 382 | + * @return ObjectMembershipHandler | |
| 240 | 383 | * @throws MissingObjectMembershipHandlerException |
| 241 | 384 | * @throws Exception |
| 242 | 385 | */ |
| 243 | 386 | public function getObjectMembershipHandler(?string $objectType): ObjectMembershipHandler |
| @@ -245,9 +388,9 @@ | ||
| 245 | 388 | $objectMembershipHandlers = $this->getObjectMembershipHandlers(); |
| 246 | 389 | $generalObjectType = $this->getGeneralObjectType($objectType); |
| 247 | 390 | |
| 248 | 391 | if (isset($objectMembershipHandlers[$generalObjectType]) === false) { |
| 249 | - throw new MissingObjectMembershipHandlerException("Missing membership handler for '$objectType'."); | |
| 392 | + throw new MissingObjectMembershipHandlerException("Missing membership handler for '{$objectType}'."); | |
| 250 | 393 | } |
| 251 | 394 | |
| 252 | 395 | return $objectMembershipHandlers[$generalObjectType]; |
| 253 | 396 | } |