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