PluginProbe
User Access Manager / 2.1.2
User Access Manager v2.1.2
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.1.2, at src/Object/ObjectHandler.php

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