PluginProbe
User Access Manager / 2.0.0
User Access Manager v2.0.0
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 / UserAccessManager / ObjectHandler / ObjectHandler.php

ObjectHandler.php in User Access Manager 2.0.0, at src/UserAccessManager/ObjectHandler/ObjectHandler.php

572 lines 15.1 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\ObjectHandler;
16
17 use UserAccessManager\Database\Database;
18 use UserAccessManager\Wrapper\Wordpress;
19
20 /**
21 * Class ObjectHandler
22 *
23 * @package UserAccessManager\ObjectHandler
24 */
25 class ObjectHandler
26 {
27 const TREE_MAP_PARENTS = 'PARENT';
28 const TREE_MAP_CHILDREN = 'CHILDREN';
29 const GENERAL_ROLE_OBJECT_TYPE = '_role_';
30 const GENERAL_USER_OBJECT_TYPE = '_user_';
31 const GENERAL_POST_OBJECT_TYPE = '_post_';
32 const GENERAL_TERM_OBJECT_TYPE = '_term_';
33 const ATTACHMENT_OBJECT_TYPE = 'attachment';
34 const POST_OBJECT_TYPE = 'post';
35 const PAGE_OBJECT_TYPE = 'page';
36 /**
37 * @var Wordpress
38 */
39 private $wordpress;
40
41 /**
42 * @var null|array
43 */
44 private $postTypes = null;
45
46 /**
47 * @var null|array
48 */
49 private $taxonomies = null;
50
51 /**
52 * @var \WP_User
53 */
54 private $users = null;
55
56 /**
57 * @var \WP_Post[]
58 */
59 private $posts = null;
60
61 /**
62 * @var \WP_Term[]
63 */
64 private $terms = null;
65
66 /**
67 * @var array
68 */
69 private $termPostMap = null;
70
71 /**
72 * @var array
73 */
74 private $postTermMap = null;
75
76 /**
77 * @var array
78 */
79 private $termTreeMap = null;
80
81 /**
82 * @var array
83 */
84 private $postTreeMap = null;
85
86 /**
87 * @var array
88 */
89 private $pluggableObjects = [];
90
91 /**
92 * @var array
93 */
94 private $objectTypes = null;
95
96 /**
97 * @var array
98 */
99 private $allObjectTypes = null;
100
101 /**
102 * @var array
103 */
104 private $validObjectTypes = [];
105
106 /**
107 * Cache constructor.
108 *
109 * @param Wordpress $wordpress
110 * @param Database $database
111 */
112 public function __construct(Wordpress $wordpress, Database $database)
113 {
114 $this->wordpress = $wordpress;
115 $this->database = $database;
116 }
117
118 /**
119 * Returns all post types.
120 *
121 * @return array
122 */
123 public function getPostTypes()
124 {
125 if ($this->postTypes === null) {
126 $this->postTypes = $this->wordpress->getPostTypes(['public' => true]);
127 }
128
129 return $this->postTypes;
130 }
131
132 /**
133 * Returns the taxonomies.
134 *
135 * @return array
136 */
137 public function getTaxonomies()
138 {
139 if ($this->taxonomies === null) {
140 $this->taxonomies = $this->wordpress->getTaxonomies(['public' => true]);
141 }
142
143 return $this->taxonomies;
144 }
145
146 /**
147 * Returns a user.
148 *
149 * @param int|string $id The user id.
150 *
151 * @return \WP_User|false
152 */
153 public function getUser($id)
154 {
155 if (isset($this->users[$id]) === false) {
156 $this->users[$id] = $this->wordpress->getUserData($id);
157 }
158
159 return $this->users[$id];
160 }
161
162 /**
163 * Returns a post.
164 *
165 * @param int $id The post id.
166 *
167 * @return \WP_Post|array|false
168 */
169 public function getPost($id)
170 {
171 if (isset($this->posts[$id]) === false) {
172 $post = $this->wordpress->getPost($id);
173 $this->posts[$id] = ($post === null) ? false : $post;
174 }
175
176 return $this->posts[$id];
177 }
178
179 /**
180 * Returns a term.
181 *
182 * @param int $id The term id.
183 * @param string $taxonomy The taxonomy.
184 *
185 * @return false|\WP_Error|\WP_Term
186 */
187 public function getTerm($id, $taxonomy = '')
188 {
189 $fullId = $id.'|'.$taxonomy;
190
191 if (isset($this->terms[$fullId]) === false) {
192 $term = $this->wordpress->getTerm($id, $taxonomy);
193 $this->terms[$fullId] = ($term === null) ? false : $term;
194 }
195
196 return $this->terms[$fullId];
197 }
198
199 /**
200 * Resolves all tree map elements
201 *
202 * @param array $map
203 * @param array $subMap
204 *
205 * @return array
206 */
207 private function processTreeMapElements(array &$map, array $subMap = null)
208 {
209 $processMap = ($subMap === null) ? $map : $subMap;
210
211 foreach ($processMap as $id => $subIds) {
212 foreach ($subIds as $subId => $type) {
213 if (isset($map[$subId]) === true) {
214 $map[$id] += $this->processTreeMapElements($map, [$subId => $map[$subId]])[$subId];
215 }
216 }
217 }
218
219 return $map;
220 }
221
222 /**
223 * Returns the tree map for the query.
224 *
225 * @param string $select
226 * @param string $generalType
227 *
228 * @return array
229 */
230 private function getTreeMap($select, $generalType)
231 {
232 $treeMap = [
233 self::TREE_MAP_CHILDREN => [
234 $generalType => []
235 ],
236 self::TREE_MAP_PARENTS => [
237 $generalType => []
238 ]
239 ];
240 $results = $this->database->getResults($select);
241
242 foreach ($results as $result) {
243 if (isset($treeMap[self::TREE_MAP_CHILDREN][$result->type]) === false) {
244 $treeMap[self::TREE_MAP_CHILDREN][$result->type] = [];
245 }
246
247 if (isset($treeMap[self::TREE_MAP_PARENTS][$result->type]) === false) {
248 $treeMap[self::TREE_MAP_PARENTS][$result->type] = [];
249 }
250
251 if (isset($treeMap[self::TREE_MAP_CHILDREN][$result->type][$result->parentId]) === false) {
252 $treeMap[self::TREE_MAP_CHILDREN][$result->type][$result->parentId] = [];
253 }
254
255 if (isset($treeMap[self::TREE_MAP_PARENTS][$result->type][$result->id]) === false) {
256 $treeMap[self::TREE_MAP_PARENTS][$result->type][$result->id] = [];
257 }
258
259 $treeMap[self::TREE_MAP_CHILDREN][$generalType][$result->parentId][$result->id] = $result->type;
260 $treeMap[self::TREE_MAP_CHILDREN][$result->type][$result->parentId][$result->id] = $result->type;
261 $treeMap[self::TREE_MAP_PARENTS][$generalType][$result->id][$result->parentId] = $result->type;
262 $treeMap[self::TREE_MAP_PARENTS][$result->type][$result->id][$result->parentId] = $result->type;
263 }
264
265 //Process elements
266 foreach ($treeMap as $mapType => $mayTypeMap) {
267 foreach ($mayTypeMap as $objectType => $map) {
268 $treeMap[$mapType][$objectType] = $this->processTreeMapElements($map);
269 }
270 }
271
272 return $treeMap;
273 }
274
275 /**
276 * Returns the post tree map.
277 *
278 * @return array
279 */
280 public function getPostTreeMap()
281 {
282 if ($this->postTreeMap === null) {
283 $select = "
284 SELECT ID AS id, post_parent AS parentId, post_type AS type
285 FROM {$this->database->getPostsTable()}
286 WHERE post_parent != 0";
287
288 $this->postTreeMap = $this->getTreeMap($select, self::GENERAL_POST_OBJECT_TYPE);
289 }
290
291 return $this->postTreeMap;
292 }
293
294 /**
295 * Returns the term tree map.
296 *
297 * @return array
298 */
299 public function getTermTreeMap()
300 {
301 if ($this->termTreeMap === null) {
302 $select = "
303 SELECT term_id AS id, parent AS parentId, taxonomy AS type
304 FROM {$this->database->getTermTaxonomyTable()}
305 WHERE parent != 0";
306
307 $this->termTreeMap = $this->getTreeMap($select, self::GENERAL_TERM_OBJECT_TYPE);
308 }
309
310 return $this->termTreeMap;
311 }
312
313 /**
314 * Returns the term post map.
315 *
316 * @return array
317 */
318 public function getTermPostMap()
319 {
320 if ($this->termPostMap === null) {
321 $this->termPostMap = [];
322
323 $select = "
324 SELECT tr.object_id AS objectId, tt.term_id AS termId, p.post_type AS postType
325 FROM {$this->database->getTermRelationshipsTable()} AS tr
326 LEFT JOIN {$this->database->getPostsTable()} AS p
327 ON (tr.object_id = p.ID)
328 LEFT JOIN {$this->database->getTermTaxonomyTable()} AS tt
329 ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
330
331 $results = $this->database->getResults($select);
332
333 foreach ($results as $result) {
334 if (isset($this->termPostMap[$result->termId]) === false) {
335 $this->termPostMap[$result->termId] = [];
336 }
337
338 $this->termPostMap[$result->termId][$result->objectId] = $result->postType;
339 }
340 }
341
342 return $this->termPostMap;
343 }
344
345 /**
346 * Returns the post term map.
347 *
348 * @return array
349 */
350 public function getPostTermMap()
351 {
352 if ($this->postTermMap === null) {
353 $this->postTermMap = [];
354
355 $select = "
356 SELECT tr.object_id AS objectId, tt.term_id AS termId, tt.taxonomy AS termType
357 FROM {$this->database->getTermRelationshipsTable()} AS tr
358 LEFT JOIN {$this->database->getTermTaxonomyTable()} AS tt
359 ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
360
361 $results = $this->database->getResults($select);
362
363 foreach ($results as $result) {
364 if (isset($this->postTermMap[$result->objectId]) === false) {
365 $this->postTermMap[$result->objectId] = [];
366 }
367
368 $this->postTermMap[$result->objectId][$result->termId] = $result->termType;
369 }
370 }
371
372 return $this->postTermMap;
373 }
374
375 /**
376 * Used for adding custom post types using the registered_post_type hook
377 * @see http://wordpress.org/support/topic/modifying-post-type-using-the-registered_post_type-hook
378 *
379 * @param string $postType The string for the new post_type
380 * @param \WP_Post_Type $arguments The array of arguments used to create the post_type
381 */
382 public function registeredPostType($postType, \WP_Post_Type $arguments)
383 {
384 if ((bool)$arguments->public === true) {
385 $this->postTypes = $this->getPostTypes();
386 $this->postTypes[$postType] = $postType;
387 $this->objectTypes = null;
388 $this->allObjectTypes = null;
389 $this->validObjectTypes = null;
390 }
391 }
392
393 /**
394 * Adds an custom taxonomy.
395 *
396 * @param string $taxonomy
397 * @param string $objectType
398 * @param array $arguments
399 */
400 public function registeredTaxonomy($taxonomy, $objectType, array $arguments)
401 {
402 if ((bool)$arguments['public'] === true) {
403 $this->taxonomies = $this->getTaxonomies();
404 $this->taxonomies[$taxonomy] = $taxonomy;
405 $this->objectTypes = null;
406 $this->allObjectTypes = null;
407 $this->validObjectTypes = null;
408 }
409 }
410
411 /**
412 * Checks if type is postable.
413 *
414 * @param string $type
415 *
416 * @return bool
417 */
418 public function isPostType($type)
419 {
420 $postableTypes = $this->getPostTypes();
421 return isset($postableTypes[$type]);
422 }
423
424 /**
425 * Checks if the taxonomy is a valid one.
426 *
427 * @param string $taxonomy
428 *
429 * @return bool
430 */
431 public function isTaxonomy($taxonomy)
432 {
433 $taxonomies = $this->getTaxonomies();
434 return in_array($taxonomy, $taxonomies);
435 }
436
437 /**
438 * Registers object that should be handel by the user access manager.
439 *
440 * @param PluggableObject $object The object which you want to register.
441 */
442 public function registerPluggableObject(PluggableObject $object)
443 {
444 $this->pluggableObjects[$object->getObjectType()] = $object;
445 }
446
447 /**
448 * Returns a registered pluggable object.
449 *
450 * @param string $objectName The name of the object which should be returned.
451 *
452 * @return PluggableObject
453 */
454 public function getPluggableObject($objectName)
455 {
456 if (isset($this->pluggableObjects[$objectName]) === true) {
457 return $this->pluggableObjects[$objectName];
458 }
459
460 return null;
461 }
462
463 /**
464 * Returns true if the object is a pluggable object.
465 *
466 * @param string $objectName
467 *
468 * @return bool
469 */
470 public function isPluggableObject($objectName)
471 {
472 return isset($this->pluggableObjects[$objectName]);
473 }
474
475 /**
476 * Returns all registered pluggable objects.
477 *
478 * @return PluggableObject[]
479 */
480 public function getPluggableObjects()
481 {
482 return $this->pluggableObjects;
483 }
484
485 /**
486 * Returns the predefined object types.
487 *
488 * @return array
489 */
490 public function getObjectTypes()
491 {
492 if ($this->objectTypes === null) {
493 $this->objectTypes = array_merge(
494 $this->getPostTypes(),
495 $this->getTaxonomies()
496 );
497 }
498
499 return $this->objectTypes;
500 }
501
502 /**
503 * Returns all objects types.
504 *
505 * @return array
506 */
507 public function getAllObjectTypes()
508 {
509 if ($this->allObjectTypes === null) {
510 $objectTypes = $this->getObjectTypes();
511 $pluggableObjects = $this->getPluggableObjects();
512 $pluggableObjectKeys = array_keys($pluggableObjects);
513 $pluggableObjectKeys = array_combine($pluggableObjectKeys, $pluggableObjectKeys);
514
515 $this->allObjectTypes = array_merge(
516 [
517 self::GENERAL_ROLE_OBJECT_TYPE => self::GENERAL_ROLE_OBJECT_TYPE,
518 self::GENERAL_USER_OBJECT_TYPE => self::GENERAL_USER_OBJECT_TYPE,
519 self::GENERAL_POST_OBJECT_TYPE => self::GENERAL_POST_OBJECT_TYPE,
520 self::GENERAL_TERM_OBJECT_TYPE => self::GENERAL_TERM_OBJECT_TYPE
521 ],
522 $objectTypes,
523 $pluggableObjectKeys
524 );
525 }
526
527 return $this->allObjectTypes;
528 }
529
530 /**
531 * Returns the general object type.
532 *
533 * @param string $objectType
534 *
535 * @return string
536 */
537 public function getGeneralObjectType($objectType)
538 {
539 if ($objectType === self::GENERAL_USER_OBJECT_TYPE
540 || $objectType === self::GENERAL_ROLE_OBJECT_TYPE
541 || $objectType === self::GENERAL_TERM_OBJECT_TYPE
542 || $objectType === self::GENERAL_POST_OBJECT_TYPE
543 || $this->isPluggableObject($objectType) === true
544 ) {
545 return $objectType;
546 } elseif ($this->isPostType($objectType) === true) {
547 return self::GENERAL_POST_OBJECT_TYPE;
548 } elseif ($this->isTaxonomy($objectType) === true) {
549 return self::GENERAL_TERM_OBJECT_TYPE;
550 }
551
552 return null;
553 }
554
555 /**
556 * Checks if the object type is a valid one.
557 *
558 * @param string $objectType The object type to check.
559 *
560 * @return bool
561 */
562 public function isValidObjectType($objectType)
563 {
564 if (isset($this->validObjectTypes[$objectType]) === false) {
565 $objectTypesMap = $this->getAllObjectTypes();
566 $this->validObjectTypes[$objectType] = isset($objectTypesMap[$objectType]);
567 }
568
569 return $this->validObjectTypes[$objectType];
570 }
571 }
572