PluginProbe
User Access Manager / 2.2.20
User Access Manager v2.2.20
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
← All changes | src/Object/ObjectMapHandler.php +131 -36 trunk2.2.20 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * ObjectMapHandler.php
4 + *
5 + * The ObjectMapHandler 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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Object;
@@ -6,29 +19,74 @@
6 19
7 20 use UserAccessManager\Cache\Cache;
8 21 use UserAccessManager\Database\Database;
9 22
23 +/**
24 + * Class ObjectMapHandler
25 + *
26 + * @package UserAccessManager\Object
27 + */
10 28 class ObjectMapHandler
11 29 {
12 - public const TREE_MAP_PARENTS = 'PARENT';
13 - public const TREE_MAP_CHILDREN = 'CHILDREN';
14 - public const POST_TREE_MAP_CACHE_KEY = 'uamPostTreeMap';
15 - public const TERM_TREE_MAP_CACHE_KEY = 'uamTermTreeMap';
16 - public const TERM_POST_MAP_CACHE_KEY = 'uamTermPostMap';
17 - public const POST_TERM_MAP_CACHE_KEY = 'uamPostTermMap';
30 + const TREE_MAP_PARENTS = 'PARENT';
31 + const TREE_MAP_CHILDREN = 'CHILDREN';
32 + const POST_TREE_MAP_CACHE_KEY = 'uamPostTreeMap';
33 + const TERM_TREE_MAP_CACHE_KEY = 'uamTermTreeMap';
34 + const TERM_POST_MAP_CACHE_KEY = 'uamTermPostMap';
35 + const POST_TERM_MAP_CACHE_KEY = 'uamPostTermMap';
18 36
19 - private ?array $termPostMap = null;
20 - private ?array $postTermMap = null;
21 - private ?array $termTreeMap = null;
22 - private ?array $postTreeMap = null;
37 + /**
38 + * @var Database
39 + */
40 + private $database;
23 41
42 + /**
43 + * @var Cache
44 + */
45 + private $cache;
46 +
47 + /**
48 + * @var null|array
49 + */
50 + private $termPostMap = null;
51 +
52 + /**
53 + * @var null|array
54 + */
55 + private $postTermMap = null;
56 +
57 + /**
58 + * @var null|array
59 + */
60 + private $termTreeMap = null;
61 +
62 + /**
63 + * @var null|array
64 + */
65 + private $postTreeMap = null;
66 +
67 +
68 + /**
69 + * ObjectMapHandler constructor.
70 + * @param Database $database
71 + * @param Cache $cache
72 + */
24 73 public function __construct(
25 - private Database $database,
26 - private Cache $cache
74 + Database $database,
75 + Cache $cache
27 76 ) {
77 + $this->database = $database;
78 + $this->cache = $cache;
28 79 }
29 80
30 - private function processTreeMapElements(array &$map, ?array $subMap = null, array &$processed = []): array
81 + /**
82 + * Resolves all tree map elements
83 + * @param array $map
84 + * @param array|null $subMap
85 + * @param array $processed
86 + * @return array
87 + */
88 + private function processTreeMapElements(array &$map, array $subMap = null, array &$processed = []): array
31 89 {
32 90 $processMap = ($subMap === null) ? $map : $subMap;
33 91
34 92 foreach ($processMap as $id => $subIds) {
@@ -42,8 +100,14 @@
42 100
43 101 return $map;
44 102 }
45 103
104 + /**
105 + * Returns the tree map for the query.
106 + * @param string $select
107 + * @param string $generalType
108 + * @return array
109 + */
46 110 private function getTreeMap(string $select, string $generalType): array
47 111 {
48 112 $treeMap = [
49 113 self::TREE_MAP_CHILDREN => [
@@ -61,11 +125,12 @@
61 125 $treeMap[self::TREE_MAP_PARENTS][$generalType][$result->id][$result->parentId] = $result->type;
62 126 $treeMap[self::TREE_MAP_PARENTS][$result->type][$result->id][$result->parentId] = $result->type;
63 127 }
64 128
65 - foreach ($treeMap as $mapType => $mapsByObjectType) {
66 - foreach ($mapsByObjectType as $objectType => $map) {
67 - $treeMap[$mapType][(string) $objectType] = $this->processTreeMapElements($map);
129 + //Process elements
130 + foreach ($treeMap as $mapType => $mayTypeMap) {
131 + foreach ($mayTypeMap as $objectType => $map) {
132 + $treeMap[$mapType][$objectType] = $this->processTreeMapElements($map);
68 133 }
69 134 }
70 135
71 136 return $treeMap;
@@ -70,8 +135,15 @@
70 135
71 136 return $treeMap;
72 137 }
73 138
139 + /**
140 + * Checks if a cache key exists and returns the map.
141 + * @param string $cacheKey
142 + * @param string $generalType
143 + * @param string $query
144 + * @return array
145 + */
74 146 private function getCachedTreeMap(string $cacheKey, string $generalType, string $query): array
75 147 {
76 148 $map = $this->cache->get($cacheKey);
77 149
@@ -79,17 +151,22 @@
79 151 $map = $this->getTreeMap($query, $generalType);
80 152 $this->cache->add($cacheKey, $map);
81 153 }
82 154
155 +
83 156 return $map;
84 157 }
85 158
86 - public function getPostTreeMap(): array
159 + /**
160 + * Returns the post tree map.
161 + * @return array
162 + */
163 + public function getPostTreeMap(): ?array
87 164 {
88 165 if ($this->postTreeMap === null) {
89 - $query = "SELECT `ID` AS `id`, `post_parent` AS `parentId`, `post_type` AS `type`
90 - FROM `{$this->database->getPostsTable()}`
91 - WHERE `post_parent` != 0 AND `post_type` != 'revision'";
166 + $query = "SELECT ID AS id, post_parent AS parentId, post_type AS type
167 + FROM {$this->database->getPostsTable()}
168 + WHERE post_parent != 0 AND post_type != 'revision'";
92 169
93 170 $this->postTreeMap = $this->getCachedTreeMap(
94 171 self::POST_TREE_MAP_CACHE_KEY,
95 172 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
@@ -99,14 +176,18 @@
99 176
100 177 return $this->postTreeMap;
101 178 }
102 179
103 - public function getTermTreeMap(): array
180 + /**
181 + * Returns the term tree map.
182 + * @return array
183 + */
184 + public function getTermTreeMap(): ?array
104 185 {
105 186 if ($this->termTreeMap === null) {
106 - $query = "SELECT `term_id` AS `id`, `parent` AS `parentId`, `taxonomy` AS `type`
107 - FROM `{$this->database->getTermTaxonomyTable()}`
108 - WHERE `parent` != 0";
187 + $query = "SELECT term_id AS id, parent AS parentId, taxonomy AS type
188 + FROM {$this->database->getTermTaxonomyTable()}
189 + WHERE parent != 0";
109 190
110 191 $this->termTreeMap = $this->getCachedTreeMap(
111 192 self::TERM_TREE_MAP_CACHE_KEY,
112 193 ObjectHandler::GENERAL_TERM_OBJECT_TYPE,
@@ -116,8 +197,14 @@
116 197
117 198 return $this->termTreeMap;
118 199 }
119 200
201 + /**
202 + * Returns the cached map.
203 + * @param string $cacheKey
204 + * @param string $query
205 + * @return array
206 + */
120 207 private function getCachedMap(string $cacheKey, string $query): array
121 208 {
122 209 $map = $this->cache->get($cacheKey);
123 210
@@ -134,18 +221,22 @@
134 221
135 222 return $map;
136 223 }
137 224
138 - public function getTermPostMap(): array
225 + /**
226 + * Returns the term post map.
227 + * @return array
228 + */
229 + public function getTermPostMap(): ?array
139 230 {
140 231 if ($this->termPostMap === null) {
141 232 $select = "
142 - SELECT `tr`.`object_id` AS `objectId`, `tt`.`term_id` AS `parentId`, `p`.`post_type` AS `type`
143 - FROM `{$this->database->getTermRelationshipsTable()}` AS `tr`
144 - LEFT JOIN `{$this->database->getPostsTable()}` AS `p`
145 - ON (`tr`.`object_id` = `p`.`ID`)
146 - LEFT JOIN `{$this->database->getTermTaxonomyTable()}` AS `tt`
147 - ON (`tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`)";
233 + SELECT tr.object_id AS objectId, tt.term_id AS parentId, p.post_type AS type
234 + FROM {$this->database->getTermRelationshipsTable()} AS tr
235 + LEFT JOIN {$this->database->getPostsTable()} AS p
236 + ON (tr.object_id = p.ID)
237 + LEFT JOIN {$this->database->getTermTaxonomyTable()} AS tt
238 + ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
148 239
149 240 $this->termPostMap = $this->getCachedMap(self::TERM_POST_MAP_CACHE_KEY, $select);
150 241 }
151 242
@@ -151,16 +242,20 @@
151 242
152 243 return $this->termPostMap;
153 244 }
154 245
155 - public function getPostTermMap(): array
246 + /**
247 + * Returns the post term map.
248 + * @return array
249 + */
250 + public function getPostTermMap(): ?array
156 251 {
157 252 if ($this->postTermMap === null) {
158 253 $select = "
159 - SELECT `tr`.`object_id` AS `parentId`, `tt`.`term_id` AS `objectId`, `tt`.`taxonomy` AS `type`
160 - FROM `{$this->database->getTermRelationshipsTable()}` AS `tr`
161 - LEFT JOIN `{$this->database->getTermTaxonomyTable()}` AS `tt`
162 - ON (`tr`.`term_taxonomy_id` = `tt`.`term_taxonomy_id`)";
254 + SELECT tr.object_id AS parentId, tt.term_id AS objectId, tt.taxonomy AS type
255 + FROM {$this->database->getTermRelationshipsTable()} AS tr
256 + LEFT JOIN {$this->database->getTermTaxonomyTable()} AS tt
257 + ON (tr.term_taxonomy_id = tt.term_taxonomy_id)";
163 258
164 259 $this->postTermMap = $this->getCachedMap(self::POST_TERM_MAP_CACHE_KEY, $select);
165 260 }
166 261