PluginProbe
User Access Manager / 2.1.7
User Access Manager v2.1.7
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 / Object / ObjectMapHandler.php

ObjectMapHandler.php in User Access Manager 2.1.7, at src/Object/ObjectMapHandler.php

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