PluginProbe
User Access Manager / 2.2.23
User Access Manager v2.2.23
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 +153 -33 2.3.132.2.23 View file →
@@ -24,9 +24,8 @@
24 24 use UserAccessManager\Wrapper\Php;
25 25 use UserAccessManager\Wrapper\Wordpress;
26 26 use WP_Post;
27 27 use WP_Post_Type;
28 -use WP_Taxonomy;
29 28 use WP_Term;
30 29 use WP_User;
31 30
32 31 /**
@@ -35,39 +34,102 @@
35 34 * @package UserAccessManager\ObjectHandler
36 35 */
37 36 class ObjectHandler
38 37 {
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';
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';
47 46
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 = [];
47 + /**
48 + * @var Php
49 + */
50 + private $php;
62 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 + */
63 118 public function __construct(
64 - private Php $php,
65 - private Wordpress $wordpress,
66 - private ObjectMembershipHandlerFactory $membershipHandlerFactory
119 + Php $php,
120 + Wordpress $wordpress,
121 + ObjectMembershipHandlerFactory $membershipHandlerFactory
67 122 ) {
123 + $this->php = $php;
124 + $this->wordpress = $wordpress;
125 + $this->membershipHandlerFactory = $membershipHandlerFactory;
68 126 }
69 127
128 + /**
129 + * Returns all post types.
130 + * @return array
131 + */
70 132 public function getPostTypes(): ?array
71 133 {
72 134 if ($this->postTypes === null) {
73 135 $this->postTypes = $this->wordpress->getPostTypes(['public' => true]);
@@ -75,8 +137,12 @@
75 137
76 138 return $this->postTypes;
77 139 }
78 140
141 + /**
142 + * Returns the taxonomies.
143 + * @return array
144 + */
79 145 public function getTaxonomies(): ?array
80 146 {
81 147 if ($this->taxonomies === null) {
82 148 $this->taxonomies = $this->wordpress->getTaxonomies(['public' => true]);
@@ -84,9 +150,14 @@
84 150
85 151 return $this->taxonomies;
86 152 }
87 153
88 - 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)
89 160 {
90 161 if (isset($this->users[$id]) === false) {
91 162 $this->users[$id] = $this->wordpress->getUserData($id);
92 163 }
@@ -93,9 +164,14 @@
93 164
94 165 return $this->users[$id];
95 166 }
96 167
97 - 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)
98 174 {
99 175 if (isset($this->posts[$id]) === false) {
100 176 $post = $this->wordpress->getPost($id);
101 177 $this->posts[$id] = ($post instanceof WP_Post) ? $post : false;
@@ -103,9 +179,15 @@
103 179
104 180 return $this->posts[$id];
105 181 }
106 182
107 - 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 = '')
108 190 {
109 191 $fullId = $id . '|' . $taxonomy;
110 192
111 193 if (isset($this->terms[$fullId]) === false) {
@@ -116,13 +198,16 @@
116 198 return $this->terms[$fullId];
117 199 }
118 200
119 201 /**
202 + * Used for adding custom post types using the registered_post_type hook
120 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
121 206 */
122 - public function registeredPostType(string $postType, WP_Post_Type $arguments): void
207 + public function registeredPostType(string $postType, WP_Post_Type $arguments)
123 208 {
124 - if ($arguments->public === true) {
209 + if ((bool) $arguments->public === true) {
125 210 $this->postTypes = $this->getPostTypes();
126 211 $this->postTypes[$postType] = $postType;
127 212 $this->objectTypes = null;
128 213 $this->allObjectTypes = null;
@@ -130,9 +215,15 @@
130 215 $this->validObjectTypes = [];
131 216 }
132 217 }
133 218
134 - 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)
135 226 {
136 227 if ((bool) $arguments['public'] === true) {
137 228 $this->taxonomies = $this->getTaxonomies();
138 229 $this->taxonomies[$taxonomy] = $taxonomy;
@@ -142,8 +233,13 @@
142 233 $this->validObjectTypes = [];
143 234 }
144 235 }
145 236
237 + /**
238 + * Checks if type is postable.
239 + * @param string $type
240 + * @return bool
241 + */
146 242 public function isPostType(string $type): bool
147 243 {
148 244 $postableTypes = $this->getPostTypes();
149 245 return isset($postableTypes[$type]);
@@ -148,8 +244,13 @@
148 244 $postableTypes = $this->getPostTypes();
149 245 return isset($postableTypes[$type]);
150 246 }
151 247
248 + /**
249 + * Checks if the taxonomy is a valid one.
250 + * @param string $taxonomy
251 + * @return bool
252 + */
152 253 public function isTaxonomy(string $taxonomy): bool
153 254 {
154 255 $taxonomies = $this->getTaxonomies();
155 256 return in_array($taxonomy, $taxonomies);
@@ -154,8 +255,12 @@
154 255 $taxonomies = $this->getTaxonomies();
155 256 return in_array($taxonomy, $taxonomies);
156 257 }
157 258
259 + /**
260 + * Returns the predefined object types.
261 + * @return array
262 + */
158 263 public function getObjectTypes(): ?array
159 264 {
160 265 if ($this->objectTypes === null) {
161 266 $this->objectTypes = array_merge(
@@ -167,8 +272,10 @@
167 272 return $this->objectTypes;
168 273 }
169 274
170 275 /**
276 + * Returns the object types map.
277 + * @return array
171 278 * @throws Exception
172 279 */
173 280 private function getAllObjectsTypesMap(): ?array
174 281 {
@@ -195,8 +302,10 @@
195 302 return $this->allObjectTypesMap;
196 303 }
197 304
198 305 /**
306 + * Returns all objects types.
307 + * @return array
199 308 * @throws Exception
200 309 */
201 310 public function getAllObjectTypes(): ?array
202 311 {
@@ -208,8 +317,11 @@
208 317 return $this->allObjectTypes;
209 318 }
210 319
211 320 /**
321 + * Returns the general object type.
322 + * @param string|null $objectType
323 + * @return string
212 324 * @throws Exception
213 325 */
214 326 public function getGeneralObjectType(?string $objectType): ?string
215 327 {
@@ -217,8 +329,11 @@
217 329 return (isset($objectsTypeMap[$objectType]) === true) ? $objectsTypeMap[$objectType] : null;
218 330 }
219 331
220 332 /**
333 + * Checks if the object type is a valid one.
334 + * @param string|null $objectType The object type to check.
335 + * @return bool
221 336 * @throws Exception
222 337 */
223 338 public function isValidObjectType(?string $objectType): bool
224 339 {
@@ -230,8 +345,10 @@
230 345 return $this->validObjectTypes[$objectType];
231 346 }
232 347
233 348 /**
349 + * Returns the object membership handlers.
350 + * @return ObjectMembershipHandler[]
234 351 * @throws Exception
235 352 */
236 353 private function getObjectMembershipHandlers(): ?array
237 354 {
@@ -259,8 +376,11 @@
259 376 return $this->objectMembershipHandlers;
260 377 }
261 378
262 379 /**
380 + * Returns the membership handler for the given object type.
381 + * @param null|string $objectType
382 + * @return ObjectMembershipHandler
263 383 * @throws MissingObjectMembershipHandlerException
264 384 * @throws Exception
265 385 */
266 386 public function getObjectMembershipHandler(?string $objectType): ObjectMembershipHandler
@@ -268,9 +388,9 @@
268 388 $objectMembershipHandlers = $this->getObjectMembershipHandlers();
269 389 $generalObjectType = $this->getGeneralObjectType($objectType);
270 390
271 391 if (isset($objectMembershipHandlers[$generalObjectType]) === false) {
272 - throw new MissingObjectMembershipHandlerException("Missing membership handler for '$objectType'.");
392 + throw new MissingObjectMembershipHandlerException("Missing membership handler for '{$objectType}'.");
273 393 }
274 394
275 395 return $objectMembershipHandlers[$generalObjectType];
276 396 }