PluginProbe
User Access Manager / 2.0.6
User Access Manager v2.0.6
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.6, at src/UserAccessManager/ObjectHandler/ObjectHandler.php

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