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