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