PluginProbe
User Access Manager / 2.0.8
User Access Manager v2.0.8
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.8, at src/UserAccessManager/ObjectHandler/ObjectHandler.php

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