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 +194 -62 2.3.17 → 2.1.11 View file →
@@ -11,24 +11,15 @@
11 11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 12 * @version SVN: $id$
13 13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 14 */
15 -
16 -declare(strict_types=1);
17 -
18 15 namespace UserAccessManager\Object;
19 16
20 -use Exception;
21 17 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
22 18 use UserAccessManager\ObjectMembership\ObjectMembershipHandler;
23 19 use UserAccessManager\ObjectMembership\ObjectMembershipHandlerFactory;
24 20 use UserAccessManager\Wrapper\Php;
25 21 use UserAccessManager\Wrapper\Wordpress;
26 -use WP_Post;
27 -use WP_Post_Type;
28 -use WP_Taxonomy;
29 -use WP_Term;
30 -use WP_User;
31 22
32 23 /**
33 24 * Class ObjectHandler
34 25 *
@@ -35,40 +26,105 @@
35 26 * @package UserAccessManager\ObjectHandler
36 27 */
37 28 class ObjectHandler
38 29 {
39 - public const GENERAL_ROLE_OBJECT_TYPE = '_role_';
40 - public const GENERAL_USER_OBJECT_TYPE = '_user_';
41 - public const GENERAL_POST_OBJECT_TYPE = '_post_';
42 - public const GENERAL_TERM_OBJECT_TYPE = '_term_';
43 - public const ATTACHMENT_OBJECT_TYPE = 'attachment';
44 - public const POST_OBJECT_TYPE = 'post';
45 - public const PAGE_OBJECT_TYPE = 'page';
46 - 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';
47 38
48 - private ?array $postTypes = null;
49 - /** @var WP_Taxonomy[] */
50 - private ?array $taxonomies = null;
51 - /** @var WP_User[] */
52 - private array $users = [];
53 - /** @var WP_Post[] */
54 - private array $posts = [];
55 - /** @var WP_Term[] */
56 - private array $terms = [];
57 - private ?array $objectMembershipHandlers = null;
58 - private ?array $objectTypes = null;
59 - private ?array $allObjectTypesMap = null;
60 - private ?array $allObjectTypes = null;
61 - private array $validObjectTypes = [];
39 + /**
40 + * @var Php
41 + */
42 + private $php;
62 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 + */
63 111 public function __construct(
64 - private Php $php,
65 - private Wordpress $wordpress,
66 - private ObjectMembershipHandlerFactory $membershipHandlerFactory
112 + Php $php,
113 + Wordpress $wordpress,
114 + ObjectMembershipHandlerFactory $membershipHandlerFactory
67 115 ) {
116 + $this->php = $php;
117 + $this->wordpress = $wordpress;
118 + $this->membershipHandlerFactory = $membershipHandlerFactory;
68 119 }
69 120
70 - public function getPostTypes(): ?array
121 + /**
122 + * Returns all post types.
123 + *
124 + * @return array
125 + */
126 + public function getPostTypes()
71 127 {
72 128 if ($this->postTypes === null) {
73 129 $this->postTypes = $this->wordpress->getPostTypes(['public' => true]);
74 130 }
@@ -75,9 +131,14 @@
75 131
76 132 return $this->postTypes;
77 133 }
78 134
79 - public function getTaxonomies(): ?array
135 + /**
136 + * Returns the taxonomies.
137 + *
138 + * @return array
139 + */
140 + public function getTaxonomies()
80 141 {
81 142 if ($this->taxonomies === null) {
82 143 $this->taxonomies = $this->wordpress->getTaxonomies(['public' => true]);
83 144 }
@@ -84,9 +145,16 @@
84 145
85 146 return $this->taxonomies;
86 147 }
87 148
88 - 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)
89 157 {
90 158 if (isset($this->users[$id]) === false) {
91 159 $this->users[$id] = $this->wordpress->getUserData($id);
92 160 }
@@ -93,25 +161,40 @@
93 161
94 162 return $this->users[$id];
95 163 }
96 164
97 - 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)
98 173 {
99 174 if (isset($this->posts[$id]) === false) {
100 175 $post = $this->wordpress->getPost($id);
101 - $this->posts[$id] = ($post instanceof WP_Post) ? $post : false;
176 + $this->posts[$id] = ($post instanceof \WP_Post) ? $post : false;
102 177 }
103 178
104 179 return $this->posts[$id];
105 180 }
106 181
107 - 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 = '')
108 191 {
109 - $fullId = $id . '|' . $taxonomy;
192 + $fullId = $id.'|'.$taxonomy;
110 193
111 194 if (isset($this->terms[$fullId]) === false) {
112 195 $term = $this->wordpress->getTerm($id, $taxonomy);
113 - $this->terms[$fullId] = ($term instanceof WP_Term) ? $term : false;
196 + $this->terms[$fullId] = ($term instanceof \WP_Term) ? $term : false;
114 197 }
115 198
116 199 return $this->terms[$fullId];
117 200 }
@@ -116,13 +199,17 @@
116 199 return $this->terms[$fullId];
117 200 }
118 201
119 202 /**
203 + * Used for adding custom post types using the registered_post_type hook
120 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
121 208 */
122 - public function registeredPostType(string $postType, WP_Post_Type $arguments): void
209 + public function registeredPostType($postType, \WP_Post_Type $arguments)
123 210 {
124 - if ($arguments->public === true) {
211 + if ((bool)$arguments->public === true) {
125 212 $this->postTypes = $this->getPostTypes();
126 213 $this->postTypes[$postType] = $postType;
127 214 $this->objectTypes = null;
128 215 $this->allObjectTypes = null;
@@ -130,11 +217,18 @@
130 217 $this->validObjectTypes = [];
131 218 }
132 219 }
133 220
134 - 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)
135 229 {
136 - if ((bool) $arguments['public'] === true) {
230 + if ((bool)$arguments['public'] === true) {
137 231 $this->taxonomies = $this->getTaxonomies();
138 232 $this->taxonomies[$taxonomy] = $taxonomy;
139 233 $this->objectTypes = null;
140 234 $this->allObjectTypes = null;
@@ -142,21 +236,40 @@
142 236 $this->validObjectTypes = [];
143 237 }
144 238 }
145 239
146 - 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)
147 248 {
148 249 $postableTypes = $this->getPostTypes();
149 250 return isset($postableTypes[$type]);
150 251 }
151 252
152 - 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)
153 261 {
154 262 $taxonomies = $this->getTaxonomies();
155 263 return in_array($taxonomy, $taxonomies);
156 264 }
157 265
158 - public function getObjectTypes(): ?array
266 + /**
267 + * Returns the predefined object types.
268 + *
269 + * @return array
270 + */
271 + public function getObjectTypes()
159 272 {
160 273 if ($this->objectTypes === null) {
161 274 $this->objectTypes = array_merge(
162 275 $this->getPostTypes(),
@@ -167,11 +280,13 @@
167 280 return $this->objectTypes;
168 281 }
169 282
170 283 /**
171 - * @throws Exception
284 + * Returns the object types map.
285 + *
286 + * @return array
172 287 */
173 - private function getAllObjectsTypesMap(): ?array
288 + private function getAllObjectsTypesMap()
174 289 {
175 290 if ($this->allObjectTypesMap === null) {
176 291 $this->allObjectTypesMap = [];
177 292 $objectHandlers = $this->getObjectMembershipHandlers();
@@ -195,11 +310,13 @@
195 310 return $this->allObjectTypesMap;
196 311 }
197 312
198 313 /**
199 - * @throws Exception
314 + * Returns all objects types.
315 + *
316 + * @return array
200 317 */
201 - public function getAllObjectTypes(): ?array
318 + public function getAllObjectTypes()
202 319 {
203 320 if ($this->allObjectTypes === null) {
204 321 $objectTypes = array_keys($this->getAllObjectsTypesMap());
205 322 $this->allObjectTypes = array_combine($objectTypes, $objectTypes);
@@ -208,11 +325,15 @@
208 325 return $this->allObjectTypes;
209 326 }
210 327
211 328 /**
212 - * @throws Exception
329 + * Returns the general object type.
330 + *
331 + * @param string $objectType
332 + *
333 + * @return string
213 334 */
214 - public function getGeneralObjectType(?string $objectType): ?string
335 + public function getGeneralObjectType($objectType)
215 336 {
216 337 $objectsTypeMap = $this->getAllObjectsTypesMap();
217 338 return (isset($objectsTypeMap[$objectType]) === true) ? $objectsTypeMap[$objectType] : null;
218 339 }
@@ -217,11 +338,15 @@
217 338 return (isset($objectsTypeMap[$objectType]) === true) ? $objectsTypeMap[$objectType] : null;
218 339 }
219 340
220 341 /**
221 - * @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
222 347 */
223 - public function isValidObjectType(?string $objectType): bool
348 + public function isValidObjectType($objectType)
224 349 {
225 350 if (isset($this->validObjectTypes[$objectType]) === false) {
226 351 $objectTypesMap = $this->getAllObjectTypes();
227 352 $this->validObjectTypes[$objectType] = isset($objectTypesMap[$objectType]);
@@ -230,11 +355,13 @@
230 355 return $this->validObjectTypes[$objectType];
231 356 }
232 357
233 358 /**
234 - * @throws Exception
359 + * Returns the object membership handlers.
360 + *
361 + * @return ObjectMembershipHandler[]
235 362 */
236 - private function getObjectMembershipHandlers(): ?array
363 + private function getObjectMembershipHandlers()
237 364 {
238 365 if ($this->objectMembershipHandlers === null) {
239 366 $factory = $this->membershipHandlerFactory;
240 367
@@ -259,18 +386,23 @@
259 386 return $this->objectMembershipHandlers;
260 387 }
261 388
262 389 /**
390 + * Returns the membership handler for the given object type.
391 + *
392 + * @param string $objectType
393 + *
394 + * @return ObjectMembershipHandler
395 + *
263 396 * @throws MissingObjectMembershipHandlerException
264 - * @throws Exception
265 397 */
266 - public function getObjectMembershipHandler(?string $objectType): ObjectMembershipHandler
398 + public function getObjectMembershipHandler($objectType)
267 399 {
268 400 $objectMembershipHandlers = $this->getObjectMembershipHandlers();
269 401 $generalObjectType = $this->getGeneralObjectType($objectType);
270 402
271 403 if (isset($objectMembershipHandlers[$generalObjectType]) === false) {
272 - throw new MissingObjectMembershipHandlerException("Missing membership handler for '$objectType'.");
404 + throw new MissingObjectMembershipHandlerException("Missing membership handler for '{$objectType}'.");
273 405 }
274 406
275 407 return $objectMembershipHandlers[$generalObjectType];
276 408 }