| @@ -1,41 +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 | 79 | /** |
| 31 | 80 | * Resolves all tree map elements |
| 81 | + * | |
| 32 | 82 | * @param array $map |
| 33 | - * @param array|null $subMap | |
| 83 | + * @param array $subMap | |
| 34 | 84 | * @param array $processed |
| 85 | + * | |
| 35 | 86 | * @return array |
| 36 | 87 | */ |
| 37 | - private function processTreeMapElements(array &$map, ?array $subMap = null, array &$processed = []): array | |
| 88 | + private function processTreeMapElements(array &$map, array $subMap = null, array &$processed = []) | |
| 38 | 89 | { |
| 39 | 90 | $processMap = ($subMap === null) ? $map : $subMap; |
| 40 | 91 | |
| 41 | 92 | foreach ($processMap as $id => $subIds) { |
| @@ -49,9 +100,17 @@ | ||
| 49 | 100 | |
| 50 | 101 | return $map; |
| 51 | 102 | } |
| 52 | 103 | |
| 53 | - 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) | |
| 54 | 113 | { |
| 55 | 114 | $treeMap = [ |
| 56 | 115 | self::TREE_MAP_CHILDREN => [ |
| 57 | 116 | $generalType => [] |
| @@ -59,9 +118,9 @@ | ||
| 59 | 118 | self::TREE_MAP_PARENTS => [ |
| 60 | 119 | $generalType => [] |
| 61 | 120 | ] |
| 62 | 121 | ]; |
| 63 | - $results = (array) $this->database->getResults($select); | |
| 122 | + $results = (array)$this->database->getResults($select); | |
| 64 | 123 | |
| 65 | 124 | foreach ($results as $result) { |
| 66 | 125 | $treeMap[self::TREE_MAP_CHILDREN][$generalType][$result->parentId][$result->id] = $result->type; |
| 67 | 126 | $treeMap[self::TREE_MAP_CHILDREN][$result->type][$result->parentId][$result->id] = $result->type; |
| @@ -71,9 +130,9 @@ | ||
| 71 | 130 | |
| 72 | 131 | //Process elements |
| 73 | 132 | foreach ($treeMap as $mapType => $mayTypeMap) { |
| 74 | 133 | foreach ($mayTypeMap as $objectType => $map) { |
| 75 | - $treeMap[$mapType][(string) $objectType] = $this->processTreeMapElements($map); | |
| 134 | + $treeMap[$mapType][$objectType] = $this->processTreeMapElements($map); | |
| 76 | 135 | } |
| 77 | 136 | } |
| 78 | 137 | |
| 79 | 138 | return $treeMap; |
| @@ -78,9 +137,18 @@ | ||
| 78 | 137 | |
| 79 | 138 | return $treeMap; |
| 80 | 139 | } |
| 81 | 140 | |
| 82 | - 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) | |
| 83 | 151 | { |
| 84 | 152 | $map = $this->cache->get($cacheKey); |
| 85 | 153 | |
| 86 | 154 | if ($map === null) { |
| @@ -91,19 +159,22 @@ | ||
| 91 | 159 | |
| 92 | 160 | return $map; |
| 93 | 161 | } |
| 94 | 162 | |
| 95 | - public function getPostTreeMap(): ?array | |
| 163 | + /** | |
| 164 | + * Returns the post tree map. | |
| 165 | + * | |
| 166 | + * @return array | |
| 167 | + */ | |
| 168 | + public function getPostTreeMap() | |
| 96 | 169 | { |
| 97 | 170 | if ($this->postTreeMap === null) { |
| 98 | - $query = "SELECT ID AS id, post_parent AS parentId, post_type AS type | |
| 99 | - FROM {$this->database->getPostsTable()} | |
| 100 | - WHERE post_parent != 0 AND post_type != 'revision'"; | |
| 101 | - | |
| 102 | 171 | $this->postTreeMap = $this->getCachedTreeMap( |
| 103 | 172 | self::POST_TREE_MAP_CACHE_KEY, |
| 104 | 173 | ObjectHandler::GENERAL_POST_OBJECT_TYPE, |
| 105 | - $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'" | |
| 106 | 177 | ); |
| 107 | 178 | } |
| 108 | 179 | |
| 109 | 180 | return $this->postTreeMap; |
| @@ -108,19 +179,22 @@ | ||
| 108 | 179 | |
| 109 | 180 | return $this->postTreeMap; |
| 110 | 181 | } |
| 111 | 182 | |
| 112 | - public function getTermTreeMap(): ?array | |
| 183 | + /** | |
| 184 | + * Returns the term tree map. | |
| 185 | + * | |
| 186 | + * @return array | |
| 187 | + */ | |
| 188 | + public function getTermTreeMap() | |
| 113 | 189 | { |
| 114 | 190 | if ($this->termTreeMap === null) { |
| 115 | - $query = "SELECT term_id AS id, parent AS parentId, taxonomy AS type | |
| 116 | - FROM {$this->database->getTermTaxonomyTable()} | |
| 117 | - WHERE parent != 0"; | |
| 118 | - | |
| 119 | 191 | $this->termTreeMap = $this->getCachedTreeMap( |
| 120 | 192 | self::TERM_TREE_MAP_CACHE_KEY, |
| 121 | 193 | ObjectHandler::GENERAL_TERM_OBJECT_TYPE, |
| 122 | - $query | |
| 194 | + "SELECT term_id AS id, parent AS parentId, taxonomy AS type | |
| 195 | + FROM {$this->database->getTermTaxonomyTable()} | |
| 196 | + WHERE parent != 0" | |
| 123 | 197 | ); |
| 124 | 198 | } |
| 125 | 199 | |
| 126 | 200 | return $this->termTreeMap; |
| @@ -125,15 +199,23 @@ | ||
| 125 | 199 | |
| 126 | 200 | return $this->termTreeMap; |
| 127 | 201 | } |
| 128 | 202 | |
| 129 | - 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) | |
| 130 | 212 | { |
| 131 | 213 | $map = $this->cache->get($cacheKey); |
| 132 | 214 | |
| 133 | 215 | if ($map === null) { |
| 134 | 216 | $map = []; |
| 135 | - $results = (array) $this->database->getResults($query); | |
| 217 | + $results = (array)$this->database->getResults($query); | |
| 136 | 218 | |
| 137 | 219 | foreach ($results as $result) { |
| 138 | 220 | $map[$result->parentId][$result->objectId] = $result->type; |
| 139 | 221 | } |
| @@ -143,9 +225,14 @@ | ||
| 143 | 225 | |
| 144 | 226 | return $map; |
| 145 | 227 | } |
| 146 | 228 | |
| 147 | - public function getTermPostMap(): ?array | |
| 229 | + /** | |
| 230 | + * Returns the term post map. | |
| 231 | + * | |
| 232 | + * @return array | |
| 233 | + */ | |
| 234 | + public function getTermPostMap() | |
| 148 | 235 | { |
| 149 | 236 | if ($this->termPostMap === null) { |
| 150 | 237 | $select = " |
| 151 | 238 | SELECT tr.object_id AS objectId, tt.term_id AS parentId, p.post_type AS type |
| @@ -160,9 +247,14 @@ | ||
| 160 | 247 | |
| 161 | 248 | return $this->termPostMap; |
| 162 | 249 | } |
| 163 | 250 | |
| 164 | - public function getPostTermMap(): ?array | |
| 251 | + /** | |
| 252 | + * Returns the post term map. | |
| 253 | + * | |
| 254 | + * @return array | |
| 255 | + */ | |
| 256 | + public function getPostTermMap() | |
| 165 | 257 | { |
| 166 | 258 | if ($this->postTermMap === null) { |
| 167 | 259 | $select = " |
| 168 | 260 | SELECT tr.object_id AS parentId, tt.term_id AS objectId, tt.taxonomy AS type |