PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
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 +149 -48 2.3.20 → 2.1.11 View file →
@@ -1,34 +1,92 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * ObjectMapHandler.php
4 + *
5 + * The ObjectMapHandler class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <[email protected]>
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 + */
5 15 namespace UserAccessManager\Object;
6 16
7 17 use UserAccessManager\Cache\Cache;
8 18 use UserAccessManager\Database\Database;
9 19
20 +/**
21 + * Class ObjectMapHandler
22 + *
23 + * @package UserAccessManager\Object
24 + */
10 25 class ObjectMapHandler
11 26 {
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';
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';
18 33
19 - private ?array $termPostMap = null;
20 - private ?array $postTermMap = null;
21 - private ?array $termTreeMap = null;
22 - private ?array $postTreeMap = null;
34 + /**
35 + * @var Database
36 + */
37 + private $database;
23 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 + */
24 71 public function __construct(
25 - private Database $database,
26 - private Cache $cache
72 + Database $database,
73 + Cache $cache
27 74 ) {
75 + $this->database = $database;
76 + $this->cache = $cache;
28 77 }
29 78
30 - private function processTreeMapElements(array &$map, ?array $subMap = null, array &$processed = []): array
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 = [])
31 89 {
32 90 $processMap = ($subMap === null) ? $map : $subMap;
33 91
34 92 foreach ($processMap as $id => $subIds) {
@@ -42,9 +100,17 @@
42 100
43 101 return $map;
44 102 }
45 103
46 - private function getTreeMap(string $select, string $generalType): array
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)
47 113 {
48 114 $treeMap = [
49 115 self::TREE_MAP_CHILDREN => [
50 116 $generalType => []
@@ -52,9 +118,9 @@
52 118 self::TREE_MAP_PARENTS => [
53 119 $generalType => []
54 120 ]
55 121 ];
56 - $results = (array) $this->database->getResults($select);
122 + $results = (array)$this->database->getResults($select);
57 123
58 124 foreach ($results as $result) {
59 125 $treeMap[self::TREE_MAP_CHILDREN][$generalType][$result->parentId][$result->id] = $result->type;
60 126 $treeMap[self::TREE_MAP_CHILDREN][$result->type][$result->parentId][$result->id] = $result->type;
@@ -61,11 +127,12 @@
61 127 $treeMap[self::TREE_MAP_PARENTS][$generalType][$result->id][$result->parentId] = $result->type;
62 128 $treeMap[self::TREE_MAP_PARENTS][$result->type][$result->id][$result->parentId] = $result->type;
63 129 }
64 130
65 - foreach ($treeMap as $mapType => $mapsByObjectType) {
66 - foreach ($mapsByObjectType as $objectType => $map) {
67 - $treeMap[$mapType][(string) $objectType] = $this->processTreeMapElements($map);
131 + //Process elements
132 + foreach ($treeMap as $mapType => $mayTypeMap) {
133 + foreach ($mayTypeMap as $objectType => $map) {
134 + $treeMap[$mapType][$objectType] = $this->processTreeMapElements($map);
68 135 }
69 136 }
70 137
71 138 return $treeMap;
@@ -70,9 +137,18 @@
70 137
71 138 return $treeMap;
72 139 }
73 140
74 - private function getCachedTreeMap(string $cacheKey, string $generalType, string $query): array
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)
75 151 {
76 152 $map = $this->cache->get($cacheKey);
77 153
78 154 if ($map === null) {
@@ -79,22 +155,26 @@
79 155 $map = $this->getTreeMap($query, $generalType);
80 156 $this->cache->add($cacheKey, $map);
81 157 }
82 158
159 +
83 160 return $map;
84 161 }
85 162
86 - public function getPostTreeMap(): array
163 + /**
164 + * Returns the post tree map.
165 + *
166 + * @return array
167 + */
168 + public function getPostTreeMap()
87 169 {
88 170 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'";
92 -
93 171 $this->postTreeMap = $this->getCachedTreeMap(
94 172 self::POST_TREE_MAP_CACHE_KEY,
95 173 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
96 - $query
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'"
97 177 );
98 178 }
99 179
100 180 return $this->postTreeMap;
@@ -99,19 +179,22 @@
99 179
100 180 return $this->postTreeMap;
101 181 }
102 182
103 - public function getTermTreeMap(): array
183 + /**
184 + * Returns the term tree map.
185 + *
186 + * @return array
187 + */
188 + public function getTermTreeMap()
104 189 {
105 190 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";
109 -
110 191 $this->termTreeMap = $this->getCachedTreeMap(
111 192 self::TERM_TREE_MAP_CACHE_KEY,
112 193 ObjectHandler::GENERAL_TERM_OBJECT_TYPE,
113 - $query
194 + "SELECT term_id AS id, parent AS parentId, taxonomy AS type
195 + FROM {$this->database->getTermTaxonomyTable()}
196 + WHERE parent != 0"
114 197 );
115 198 }
116 199
117 200 return $this->termTreeMap;
@@ -116,15 +199,23 @@
116 199
117 200 return $this->termTreeMap;
118 201 }
119 202
120 - private function getCachedMap(string $cacheKey, string $query): array
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)
121 212 {
122 213 $map = $this->cache->get($cacheKey);
123 214
124 215 if ($map === null) {
125 216 $map = [];
126 - $results = (array) $this->database->getResults($query);
217 + $results = (array)$this->database->getResults($query);
127 218
128 219 foreach ($results as $result) {
129 220 $map[$result->parentId][$result->objectId] = $result->type;
130 221 }
@@ -134,18 +225,23 @@
134 225
135 226 return $map;
136 227 }
137 228
138 - public function getTermPostMap(): array
229 + /**
230 + * Returns the term post map.
231 + *
232 + * @return array
233 + */
234 + public function getTermPostMap()
139 235 {
140 236 if ($this->termPostMap === null) {
141 237 $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`)";
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)";
148 244
149 245 $this->termPostMap = $this->getCachedMap(self::TERM_POST_MAP_CACHE_KEY, $select);
150 246 }
151 247
@@ -151,16 +247,21 @@
151 247
152 248 return $this->termPostMap;
153 249 }
154 250
155 - public function getPostTermMap(): array
251 + /**
252 + * Returns the post term map.
253 + *
254 + * @return array
255 + */
256 + public function getPostTermMap()
156 257 {
157 258 if ($this->postTermMap === null) {
158 259 $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`)";
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)";
163 264
164 265 $this->postTermMap = $this->getCachedMap(self::POST_TERM_MAP_CACHE_KEY, $select);
165 266 }
166 267