| 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 |
|
| 16 |
declare(strict_types=1); |
| 17 |
|
| 18 |
namespace UserAccessManager\Object; |
| 19 |
|
| 20 |
use UserAccessManager\Cache\Cache; |
| 21 |
use UserAccessManager\Database\Database; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class ObjectMapHandler |
| 25 |
* |
| 26 |
* @package UserAccessManager\Object |
| 27 |
*/ |
| 28 |
class ObjectMapHandler |
| 29 |
{ |
| 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'; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var Database |
| 39 |
*/ |
| 40 |
private $database; |
| 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 |
*/ |
| 73 |
public function __construct( |
| 74 |
Database $database, |
| 75 |
Cache $cache |
| 76 |
) { |
| 77 |
$this->database = $database; |
| 78 |
$this->cache = $cache; |
| 79 |
} |
| 80 |
|
| 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 |
| 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 |
* @param string $select |
| 107 |
* @param string $generalType |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
private function getTreeMap(string $select, string $generalType): array |
| 111 |
{ |
| 112 |
$treeMap = [ |
| 113 |
self::TREE_MAP_CHILDREN => [ |
| 114 |
$generalType => [] |
| 115 |
], |
| 116 |
self::TREE_MAP_PARENTS => [ |
| 117 |
$generalType => [] |
| 118 |
] |
| 119 |
]; |
| 120 |
$results = (array) $this->database->getResults($select); |
| 121 |
|
| 122 |
foreach ($results as $result) { |
| 123 |
$treeMap[self::TREE_MAP_CHILDREN][$generalType][$result->parentId][$result->id] = $result->type; |
| 124 |
$treeMap[self::TREE_MAP_CHILDREN][$result->type][$result->parentId][$result->id] = $result->type; |
| 125 |
$treeMap[self::TREE_MAP_PARENTS][$generalType][$result->id][$result->parentId] = $result->type; |
| 126 |
$treeMap[self::TREE_MAP_PARENTS][$result->type][$result->id][$result->parentId] = $result->type; |
| 127 |
} |
| 128 |
|
| 129 |
//Process elements |
| 130 |
foreach ($treeMap as $mapType => $mayTypeMap) { |
| 131 |
foreach ($mayTypeMap as $objectType => $map) { |
| 132 |
$treeMap[$mapType][$objectType] = $this->processTreeMapElements($map); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return $treeMap; |
| 137 |
} |
| 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 |
*/ |
| 146 |
private function getCachedTreeMap(string $cacheKey, string $generalType, string $query): array |
| 147 |
{ |
| 148 |
$map = $this->cache->get($cacheKey); |
| 149 |
|
| 150 |
if ($map === null) { |
| 151 |
$map = $this->getTreeMap($query, $generalType); |
| 152 |
$this->cache->add($cacheKey, $map); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
return $map; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Returns the post tree map. |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
public function getPostTreeMap(): ?array |
| 164 |
{ |
| 165 |
if ($this->postTreeMap === null) { |
| 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'"; |
| 169 |
|
| 170 |
$this->postTreeMap = $this->getCachedTreeMap( |
| 171 |
self::POST_TREE_MAP_CACHE_KEY, |
| 172 |
ObjectHandler::GENERAL_POST_OBJECT_TYPE, |
| 173 |
$query |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
return $this->postTreeMap; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns the term tree map. |
| 182 |
* @return array |
| 183 |
*/ |
| 184 |
public function getTermTreeMap(): ?array |
| 185 |
{ |
| 186 |
if ($this->termTreeMap === null) { |
| 187 |
$query = "SELECT term_id AS id, parent AS parentId, taxonomy AS type |
| 188 |
FROM {$this->database->getTermTaxonomyTable()} |
| 189 |
WHERE parent != 0"; |
| 190 |
|
| 191 |
$this->termTreeMap = $this->getCachedTreeMap( |
| 192 |
self::TERM_TREE_MAP_CACHE_KEY, |
| 193 |
ObjectHandler::GENERAL_TERM_OBJECT_TYPE, |
| 194 |
$query |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
return $this->termTreeMap; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Returns the cached map. |
| 203 |
* @param string $cacheKey |
| 204 |
* @param string $query |
| 205 |
* @return array |
| 206 |
*/ |
| 207 |
private function getCachedMap(string $cacheKey, string $query): array |
| 208 |
{ |
| 209 |
$map = $this->cache->get($cacheKey); |
| 210 |
|
| 211 |
if ($map === null) { |
| 212 |
$map = []; |
| 213 |
$results = (array) $this->database->getResults($query); |
| 214 |
|
| 215 |
foreach ($results as $result) { |
| 216 |
$map[$result->parentId][$result->objectId] = $result->type; |
| 217 |
} |
| 218 |
|
| 219 |
$this->cache->add($cacheKey, $map); |
| 220 |
} |
| 221 |
|
| 222 |
return $map; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Returns the term post map. |
| 227 |
* @return array |
| 228 |
*/ |
| 229 |
public function getTermPostMap(): ?array |
| 230 |
{ |
| 231 |
if ($this->termPostMap === null) { |
| 232 |
$select = " |
| 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)"; |
| 239 |
|
| 240 |
$this->termPostMap = $this->getCachedMap(self::TERM_POST_MAP_CACHE_KEY, $select); |
| 241 |
} |
| 242 |
|
| 243 |
return $this->termPostMap; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the post term map. |
| 248 |
* @return array |
| 249 |
*/ |
| 250 |
public function getPostTermMap(): ?array |
| 251 |
{ |
| 252 |
if ($this->postTermMap === null) { |
| 253 |
$select = " |
| 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)"; |
| 258 |
|
| 259 |
$this->postTermMap = $this->getCachedMap(self::POST_TERM_MAP_CACHE_KEY, $select); |
| 260 |
} |
| 261 |
|
| 262 |
return $this->postTermMap; |
| 263 |
} |
| 264 |
} |
| 265 |
|