PluginProbe
User Access Manager / 2.2.21
User Access Manager v2.2.21
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
user-access-manager / src / Object / ObjectHandler.php

ObjectHandler.php in User Access Manager 2.2.21, at src/Object/ObjectHandler.php

398 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\Object;
19
20 use Exception;
21 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
22 use UserAccessManager\ObjectMembership\ObjectMembershipHandler;
23 use UserAccessManager\ObjectMembership\ObjectMembershipHandlerFactory;
24 use UserAccessManager\Wrapper\Php;
25 use UserAccessManager\Wrapper\Wordpress;
26 use WP_Post;
27 use WP_Post_Type;
28 use WP_Term;
29 use WP_User;
30
31 /**
32 * Class ObjectHandler
33 *
34 * @package UserAccessManager\ObjectHandler
35 */
36 class ObjectHandler
37 {
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';
46
47 /**
48 * @var Php
49 */
50 private $php;
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 */
118 public function __construct(
119 Php $php,
120 Wordpress $wordpress,
121 ObjectMembershipHandlerFactory $membershipHandlerFactory
122 ) {
123 $this->php = $php;
124 $this->wordpress = $wordpress;
125 $this->membershipHandlerFactory = $membershipHandlerFactory;
126 }
127
128 /**
129 * Returns all post types.
130 * @return array
131 */
132 public function getPostTypes(): ?array
133 {
134 if ($this->postTypes === null) {
135 $this->postTypes = $this->wordpress->getPostTypes(['public' => true]);
136 }
137
138 return $this->postTypes;
139 }
140
141 /**
142 * Returns the taxonomies.
143 * @return array
144 */
145 public function getTaxonomies(): ?array
146 {
147 if ($this->taxonomies === null) {
148 $this->taxonomies = $this->wordpress->getTaxonomies(['public' => true]);
149 }
150
151 return $this->taxonomies;
152 }
153
154 /**
155 * Returns a user.
156 * @param int|string $id The user id.
157 * @return WP_User|false
158 */
159 public function getUser($id)
160 {
161 if (isset($this->users[$id]) === false) {
162 $this->users[$id] = $this->wordpress->getUserData($id);
163 }
164
165 return $this->users[$id];
166 }
167
168 /**
169 * Returns a post.
170 * @param int|string $id The post id.
171 * @return WP_Post|false
172 */
173 public function getPost($id)
174 {
175 if (isset($this->posts[$id]) === false) {
176 $post = $this->wordpress->getPost($id);
177 $this->posts[$id] = ($post instanceof WP_Post) ? $post : false;
178 }
179
180 return $this->posts[$id];
181 }
182
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 = '')
190 {
191 $fullId = $id . '|' . $taxonomy;
192
193 if (isset($this->terms[$fullId]) === false) {
194 $term = $this->wordpress->getTerm($id, $taxonomy);
195 $this->terms[$fullId] = ($term instanceof WP_Term) ? $term : false;
196 }
197
198 return $this->terms[$fullId];
199 }
200
201 /**
202 * Used for adding custom post types using the registered_post_type hook
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
206 */
207 public function registeredPostType(string $postType, WP_Post_Type $arguments)
208 {
209 if ((bool) $arguments->public === true) {
210 $this->postTypes = $this->getPostTypes();
211 $this->postTypes[$postType] = $postType;
212 $this->objectTypes = null;
213 $this->allObjectTypes = null;
214 $this->allObjectTypesMap = null;
215 $this->validObjectTypes = [];
216 }
217 }
218
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)
226 {
227 if ((bool) $arguments['public'] === true) {
228 $this->taxonomies = $this->getTaxonomies();
229 $this->taxonomies[$taxonomy] = $taxonomy;
230 $this->objectTypes = null;
231 $this->allObjectTypes = null;
232 $this->allObjectTypesMap = null;
233 $this->validObjectTypes = [];
234 }
235 }
236
237 /**
238 * Checks if type is postable.
239 * @param string $type
240 * @return bool
241 */
242 public function isPostType(string $type): bool
243 {
244 $postableTypes = $this->getPostTypes();
245 return isset($postableTypes[$type]);
246 }
247
248 /**
249 * Checks if the taxonomy is a valid one.
250 * @param string $taxonomy
251 * @return bool
252 */
253 public function isTaxonomy(string $taxonomy): bool
254 {
255 $taxonomies = $this->getTaxonomies();
256 return in_array($taxonomy, $taxonomies);
257 }
258
259 /**
260 * Returns the predefined object types.
261 * @return array
262 */
263 public function getObjectTypes(): ?array
264 {
265 if ($this->objectTypes === null) {
266 $this->objectTypes = array_merge(
267 $this->getPostTypes(),
268 $this->getTaxonomies()
269 );
270 }
271
272 return $this->objectTypes;
273 }
274
275 /**
276 * Returns the object types map.
277 * @return array
278 * @throws Exception
279 */
280 private function getAllObjectsTypesMap(): ?array
281 {
282 if ($this->allObjectTypesMap === null) {
283 $this->allObjectTypesMap = [];
284 $objectHandlers = $this->getObjectMembershipHandlers();
285
286 foreach ($objectHandlers as $objectHandler) {
287 $handledObjects = $objectHandler->getHandledObjects();
288
289 if ($handledObjects === []) {
290 continue;
291 }
292
293 $handledObjectsMap = array_combine(
294 $handledObjects,
295 $this->php->arrayFill(0, count($handledObjects), $objectHandler->getGeneralObjectType())
296 );
297
298 $this->allObjectTypesMap = array_merge($this->allObjectTypesMap, $handledObjectsMap);
299 }
300 }
301
302 return $this->allObjectTypesMap;
303 }
304
305 /**
306 * Returns all objects types.
307 * @return array
308 * @throws Exception
309 */
310 public function getAllObjectTypes(): ?array
311 {
312 if ($this->allObjectTypes === null) {
313 $objectTypes = array_keys($this->getAllObjectsTypesMap());
314 $this->allObjectTypes = array_combine($objectTypes, $objectTypes);
315 }
316
317 return $this->allObjectTypes;
318 }
319
320 /**
321 * Returns the general object type.
322 * @param string|null $objectType
323 * @return string
324 * @throws Exception
325 */
326 public function getGeneralObjectType(?string $objectType): ?string
327 {
328 $objectsTypeMap = $this->getAllObjectsTypesMap();
329 return (isset($objectsTypeMap[$objectType]) === true) ? $objectsTypeMap[$objectType] : null;
330 }
331
332 /**
333 * Checks if the object type is a valid one.
334 * @param string|null $objectType The object type to check.
335 * @return bool
336 * @throws Exception
337 */
338 public function isValidObjectType(?string $objectType): bool
339 {
340 if (isset($this->validObjectTypes[$objectType]) === false) {
341 $objectTypesMap = $this->getAllObjectTypes();
342 $this->validObjectTypes[$objectType] = isset($objectTypesMap[$objectType]);
343 }
344
345 return $this->validObjectTypes[$objectType];
346 }
347
348 /**
349 * Returns the object membership handlers.
350 * @return ObjectMembershipHandler[]
351 * @throws Exception
352 */
353 private function getObjectMembershipHandlers(): ?array
354 {
355 if ($this->objectMembershipHandlers === null) {
356 $factory = $this->membershipHandlerFactory;
357
358 $roleMembershipHandler = $factory->createRoleMembershipHandler();
359 $userMembershipHandler = $factory->createUserMembershipHandler($this);
360 $termMembershipHandler = $factory->createTermMembershipHandler($this);
361 $postMembershipHandler = $factory->createPostMembershipHandler($this);
362
363 $this->objectMembershipHandlers = [
364 $roleMembershipHandler->getGeneralObjectType() => $roleMembershipHandler,
365 $userMembershipHandler->getGeneralObjectType() => $userMembershipHandler,
366 $termMembershipHandler->getGeneralObjectType() => $termMembershipHandler,
367 $postMembershipHandler->getGeneralObjectType() => $postMembershipHandler
368 ];
369
370 $this->objectMembershipHandlers = $this->wordpress->applyFilters(
371 'uam_register_object_membership_handler',
372 $this->objectMembershipHandlers
373 );
374 }
375
376 return $this->objectMembershipHandlers;
377 }
378
379 /**
380 * Returns the membership handler for the given object type.
381 * @param null|string $objectType
382 * @return ObjectMembershipHandler
383 * @throws MissingObjectMembershipHandlerException
384 * @throws Exception
385 */
386 public function getObjectMembershipHandler(?string $objectType): ObjectMembershipHandler
387 {
388 $objectMembershipHandlers = $this->getObjectMembershipHandlers();
389 $generalObjectType = $this->getGeneralObjectType($objectType);
390
391 if (isset($objectMembershipHandlers[$generalObjectType]) === false) {
392 throw new MissingObjectMembershipHandlerException("Missing membership handler for '{$objectType}'.");
393 }
394
395 return $objectMembershipHandlers[$generalObjectType];
396 }
397 }
398