| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Object; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\ObjectMembership\Exception\MissingObjectMembershipHandlerException; |
| 9 |
use UserAccessManager\ObjectMembership\ObjectMembershipHandler; |
| 10 |
use UserAccessManager\ObjectMembership\ObjectMembershipHandlerFactory; |
| 11 |
use UserAccessManager\Wrapper\Php; |
| 12 |
use UserAccessManager\Wrapper\Wordpress; |
| 13 |
use WP_Post; |
| 14 |
use WP_Post_Type; |
| 15 |
use WP_Taxonomy; |
| 16 |
use WP_Term; |
| 17 |
use WP_User; |
| 18 |
|
| 19 |
class ObjectHandler |
| 20 |
{ |
| 21 |
public const GENERAL_ROLE_OBJECT_TYPE = '_role_'; |
| 22 |
public const GENERAL_USER_OBJECT_TYPE = '_user_'; |
| 23 |
public const GENERAL_POST_OBJECT_TYPE = '_post_'; |
| 24 |
public const GENERAL_TERM_OBJECT_TYPE = '_term_'; |
| 25 |
public const ATTACHMENT_OBJECT_TYPE = 'attachment'; |
| 26 |
public const POST_OBJECT_TYPE = 'post'; |
| 27 |
public const PAGE_OBJECT_TYPE = 'page'; |
| 28 |
public const POST_FORMAT_TYPE = 'post_format'; |
| 29 |
|
| 30 |
private ?array $postTypes = null; |
| 31 |
/** @var WP_Taxonomy[] */ |
| 32 |
private ?array $taxonomies = null; |
| 33 |
/** @var WP_User[] */ |
| 34 |
private array $users = []; |
| 35 |
/** @var WP_Post[] */ |
| 36 |
private array $posts = []; |
| 37 |
/** @var WP_Term[] */ |
| 38 |
private array $terms = []; |
| 39 |
private ?array $objectMembershipHandlers = null; |
| 40 |
private ?array $objectTypes = null; |
| 41 |
private ?array $allObjectTypesMap = null; |
| 42 |
private ?array $allObjectTypes = null; |
| 43 |
private array $validObjectTypes = []; |
| 44 |
|
| 45 |
public function __construct( |
| 46 |
private Php $php, |
| 47 |
private Wordpress $wordpress, |
| 48 |
private ObjectMembershipHandlerFactory $membershipHandlerFactory |
| 49 |
) { |
| 50 |
} |
| 51 |
|
| 52 |
public function getPostTypes(): array |
| 53 |
{ |
| 54 |
if ($this->postTypes === null) { |
| 55 |
$this->postTypes = $this->wordpress->getPostTypes(['public' => true]); |
| 56 |
} |
| 57 |
|
| 58 |
return $this->postTypes; |
| 59 |
} |
| 60 |
|
| 61 |
public function getTaxonomies(): array |
| 62 |
{ |
| 63 |
if ($this->taxonomies === null) { |
| 64 |
$this->taxonomies = $this->wordpress->getTaxonomies(['public' => true]); |
| 65 |
} |
| 66 |
|
| 67 |
return $this->taxonomies; |
| 68 |
} |
| 69 |
|
| 70 |
public function getUser(int|string|null $id): WP_User|bool |
| 71 |
{ |
| 72 |
if (isset($this->users[$id]) === false) { |
| 73 |
$this->users[$id] = $this->wordpress->getUserData($id); |
| 74 |
} |
| 75 |
|
| 76 |
return $this->users[$id]; |
| 77 |
} |
| 78 |
|
| 79 |
public function getPost(int|string|null $id): bool|WP_Post |
| 80 |
{ |
| 81 |
if (isset($this->posts[$id]) === false) { |
| 82 |
$post = $this->wordpress->getPost($id); |
| 83 |
$this->posts[$id] = ($post instanceof WP_Post) ? $post : false; |
| 84 |
} |
| 85 |
|
| 86 |
return $this->posts[$id]; |
| 87 |
} |
| 88 |
|
| 89 |
public function getTerm(int|string|null $id, string $taxonomy = ''): WP_Term|bool |
| 90 |
{ |
| 91 |
$fullId = $id . '|' . $taxonomy; |
| 92 |
|
| 93 |
if (isset($this->terms[$fullId]) === false) { |
| 94 |
$term = $this->wordpress->getTerm($id, $taxonomy); |
| 95 |
$this->terms[$fullId] = ($term instanceof WP_Term) ? $term : false; |
| 96 |
} |
| 97 |
|
| 98 |
return $this->terms[$fullId]; |
| 99 |
} |
| 100 |
|
| 101 |
private function resetDerivedObjectTypeCaches(): void |
| 102 |
{ |
| 103 |
$this->objectTypes = null; |
| 104 |
$this->allObjectTypes = null; |
| 105 |
$this->allObjectTypesMap = null; |
| 106 |
$this->validObjectTypes = []; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @see http://wordpress.org/support/topic/modifying-post-type-using-the-registered_post_type-hook |
| 111 |
*/ |
| 112 |
public function registeredPostType(string $postType, WP_Post_Type $arguments): void |
| 113 |
{ |
| 114 |
if ($arguments->public === true) { |
| 115 |
$this->postTypes = $this->getPostTypes(); |
| 116 |
$this->postTypes[$postType] = $postType; |
| 117 |
$this->resetDerivedObjectTypeCaches(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public function registeredTaxonomy(string $taxonomy, array|string|null $objectType, array $arguments): void |
| 122 |
{ |
| 123 |
if ((bool) $arguments['public'] === true) { |
| 124 |
$this->taxonomies = $this->getTaxonomies(); |
| 125 |
$this->taxonomies[$taxonomy] = $taxonomy; |
| 126 |
$this->resetDerivedObjectTypeCaches(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public function isPostType(string $type): bool |
| 131 |
{ |
| 132 |
return isset($this->getPostTypes()[$type]); |
| 133 |
} |
| 134 |
|
| 135 |
public function isTaxonomy(string $taxonomy): bool |
| 136 |
{ |
| 137 |
return in_array($taxonomy, $this->getTaxonomies()); |
| 138 |
} |
| 139 |
|
| 140 |
public function getObjectTypes(): array |
| 141 |
{ |
| 142 |
if ($this->objectTypes === null) { |
| 143 |
$this->objectTypes = array_merge( |
| 144 |
$this->getPostTypes(), |
| 145 |
$this->getTaxonomies() |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
return $this->objectTypes; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @throws Exception |
| 154 |
*/ |
| 155 |
private function getAllObjectsTypesMap(): array |
| 156 |
{ |
| 157 |
if ($this->allObjectTypesMap === null) { |
| 158 |
$this->allObjectTypesMap = []; |
| 159 |
|
| 160 |
foreach ($this->getObjectMembershipHandlers() as $membershipHandler) { |
| 161 |
$handledObjects = $membershipHandler->getHandledObjects(); |
| 162 |
|
| 163 |
if ($handledObjects === []) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
$this->allObjectTypesMap = array_merge($this->allObjectTypesMap, array_combine( |
| 168 |
$handledObjects, |
| 169 |
$this->php->arrayFill(0, count($handledObjects), $membershipHandler->getGeneralObjectType()) |
| 170 |
)); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $this->allObjectTypesMap; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* @throws Exception |
| 179 |
*/ |
| 180 |
public function getAllObjectTypes(): array |
| 181 |
{ |
| 182 |
if ($this->allObjectTypes === null) { |
| 183 |
$objectTypes = array_keys($this->getAllObjectsTypesMap()); |
| 184 |
$this->allObjectTypes = array_combine($objectTypes, $objectTypes); |
| 185 |
} |
| 186 |
|
| 187 |
return $this->allObjectTypes; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @throws Exception |
| 192 |
*/ |
| 193 |
public function getGeneralObjectType(?string $objectType): ?string |
| 194 |
{ |
| 195 |
return $this->getAllObjectsTypesMap()[$objectType] ?? null; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* @throws Exception |
| 200 |
*/ |
| 201 |
public function isValidObjectType(?string $objectType): bool |
| 202 |
{ |
| 203 |
if (isset($this->validObjectTypes[$objectType]) === false) { |
| 204 |
$this->validObjectTypes[$objectType] = isset($this->getAllObjectTypes()[$objectType]); |
| 205 |
} |
| 206 |
|
| 207 |
return $this->validObjectTypes[$objectType]; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @return ObjectMembershipHandler[] |
| 212 |
* @throws Exception |
| 213 |
*/ |
| 214 |
private function getObjectMembershipHandlers(): array |
| 215 |
{ |
| 216 |
if ($this->objectMembershipHandlers === null) { |
| 217 |
$handlersByGeneralObjectType = []; |
| 218 |
|
| 219 |
foreach ([ |
| 220 |
$this->membershipHandlerFactory->createRoleMembershipHandler(), |
| 221 |
$this->membershipHandlerFactory->createUserMembershipHandler($this), |
| 222 |
$this->membershipHandlerFactory->createTermMembershipHandler($this), |
| 223 |
$this->membershipHandlerFactory->createPostMembershipHandler($this) |
| 224 |
] as $membershipHandler) { |
| 225 |
$handlersByGeneralObjectType[$membershipHandler->getGeneralObjectType()] = $membershipHandler; |
| 226 |
} |
| 227 |
|
| 228 |
// Published before the filter runs, so a callback that re-enters does not rebuild the handlers. |
| 229 |
$this->objectMembershipHandlers = $handlersByGeneralObjectType; |
| 230 |
$this->objectMembershipHandlers = $this->wordpress->applyFilters( |
| 231 |
'uam_register_object_membership_handler', |
| 232 |
$handlersByGeneralObjectType |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
return $this->objectMembershipHandlers; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* @throws MissingObjectMembershipHandlerException |
| 241 |
* @throws Exception |
| 242 |
*/ |
| 243 |
public function getObjectMembershipHandler(?string $objectType): ObjectMembershipHandler |
| 244 |
{ |
| 245 |
$objectMembershipHandlers = $this->getObjectMembershipHandlers(); |
| 246 |
$generalObjectType = $this->getGeneralObjectType($objectType); |
| 247 |
|
| 248 |
if (isset($objectMembershipHandlers[$generalObjectType]) === false) { |
| 249 |
throw new MissingObjectMembershipHandlerException("Missing membership handler for '$objectType'."); |
| 250 |
} |
| 251 |
|
| 252 |
return $objectMembershipHandlers[$generalObjectType]; |
| 253 |
} |
| 254 |
} |
| 255 |
|