ObjectHandler.php
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* ObjectHandler.php
*
* The ObjectHandler class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\Object;
use Exception;
use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
use UserAccessManager\ObjectMembership\ObjectMembershipHandler;
use UserAccessManager\ObjectMembership\ObjectMembershipHandlerFactory;
use UserAccessManager\Wrapper\Php;
use UserAccessManager\Wrapper\Wordpress;
use WP_Post;
use WP_Post_Type;
use WP_Term;
use WP_User;
/**
* Class ObjectHandler
*
* @package UserAccessManager\ObjectHandler
*/
class ObjectHandler
{
const GENERAL_ROLE_OBJECT_TYPE = '_role_';
const GENERAL_USER_OBJECT_TYPE = '_user_';
const GENERAL_POST_OBJECT_TYPE = '_post_';
const GENERAL_TERM_OBJECT_TYPE = '_term_';
const ATTACHMENT_OBJECT_TYPE = 'attachment';
const POST_OBJECT_TYPE = 'post';
const PAGE_OBJECT_TYPE = 'page';
const POST_FORMAT_TYPE = 'post_format';
/**
* @var Php
*/
private $php;
/**
* @var Wordpress
*/
private $wordpress;
/**
* @var ObjectMembershipHandlerFactory
*/
private $membershipHandlerFactory;
/**
* @var null|array
*/
private $postTypes = null;
/**
* @var null|array
*/
private $taxonomies = null;
/**
* @var WP_User
*/
private $users = null;
/**
* @var WP_Post[]
*/
private $posts = null;
/**
* @var WP_Term[]
*/
private $terms = null;
/**
* @var null|array
*/
private $objectMembershipHandlers = null;
/**
* @var null|array
*/
private $objectTypes = null;
/**
* @var null|array
*/
private $allObjectTypesMap = null;
/**
* @var null|array
*/
private $allObjectTypes = null;
/**
* @var array
*/
private $validObjectTypes = [];
/**
* ObjectHandler constructor.
* @param Php $php
* @param Wordpress $wordpress
* @param ObjectMembershipHandlerFactory $membershipHandlerFactory
*/
public function __construct(
Php $php,
Wordpress $wordpress,
ObjectMembershipHandlerFactory $membershipHandlerFactory
) {
$this->php = $php;
$this->wordpress = $wordpress;
$this->membershipHandlerFactory = $membershipHandlerFactory;
}
/**
* Returns all post types.
* @return array
*/
public function getPostTypes(): ?array
{
if ($this->postTypes === null) {
$this->postTypes = $this->wordpress->getPostTypes(['public' => true]);
}
return $this->postTypes;
}
/**
* Returns the taxonomies.
* @return array
*/
public function getTaxonomies(): ?array
{
if ($this->taxonomies === null) {
$this->taxonomies = $this->wordpress->getTaxonomies(['public' => true]);
}
return $this->taxonomies;
}
/**
* Returns a user.
* @param int|string $id The user id.
* @return WP_User|false
*/
public function getUser($id)
{
if (isset($this->users[$id]) === false) {
$this->users[$id] = $this->wordpress->getUserData($id);
}
return $this->users[$id];
}
/**
* Returns a post.
* @param int|string $id The post id.
* @return WP_Post|false
*/
public function getPost($id)
{
if (isset($this->posts[$id]) === false) {
$post = $this->wordpress->getPost($id);
$this->posts[$id] = ($post instanceof WP_Post) ? $post : false;
}
return $this->posts[$id];
}
/**
* Returns a term.
* @param int|string $id The term id.
* @param string $taxonomy The taxonomy.
* @return false|WP_Term
*/
public function getTerm($id, $taxonomy = '')
{
$fullId = $id . '|' . $taxonomy;
if (isset($this->terms[$fullId]) === false) {
$term = $this->wordpress->getTerm($id, $taxonomy);
$this->terms[$fullId] = ($term instanceof WP_Term) ? $term : false;
}
return $this->terms[$fullId];
}
/**
* Used for adding custom post types using the registered_post_type hook
* @see http://wordpress.org/support/topic/modifying-post-type-using-the-registered_post_type-hook
* @param string $postType The string for the new post_type
* @param WP_Post_Type $arguments The array of arguments used to create the post_type
*/
public function registeredPostType(string $postType, WP_Post_Type $arguments)
{
if ((bool) $arguments->public === true) {
$this->postTypes = $this->getPostTypes();
$this->postTypes[$postType] = $postType;
$this->objectTypes = null;
$this->allObjectTypes = null;
$this->allObjectTypesMap = null;
$this->validObjectTypes = [];
}
}
/**
* Adds an custom taxonomy.
* @param string $taxonomy
* @param array|string $objectType
* @param array $arguments
*/
public function registeredTaxonomy(string $taxonomy, $objectType, array $arguments)
{
if ((bool) $arguments['public'] === true) {
$this->taxonomies = $this->getTaxonomies();
$this->taxonomies[$taxonomy] = $taxonomy;
$this->objectTypes = null;
$this->allObjectTypes = null;
$this->allObjectTypesMap = null;
$this->validObjectTypes = [];
}
}
/**
* Checks if type is postable.
* @param string $type
* @return bool
*/
public function isPostType(string $type): bool
{
$postableTypes = $this->getPostTypes();
return isset($postableTypes[$type]);
}
/**
* Checks if the taxonomy is a valid one.
* @param string $taxonomy
* @return bool
*/
public function isTaxonomy(string $taxonomy): bool
{
$taxonomies = $this->getTaxonomies();
return in_array($taxonomy, $taxonomies);
}
/**
* Returns the predefined object types.
* @return array
*/
public function getObjectTypes(): ?array
{
if ($this->objectTypes === null) {
$this->objectTypes = array_merge(
$this->getPostTypes(),
$this->getTaxonomies()
);
}
return $this->objectTypes;
}
/**
* Returns the object types map.
* @return array
* @throws Exception
*/
private function getAllObjectsTypesMap(): ?array
{
if ($this->allObjectTypesMap === null) {
$this->allObjectTypesMap = [];
$objectHandlers = $this->getObjectMembershipHandlers();
foreach ($objectHandlers as $objectHandler) {
$handledObjects = $objectHandler->getHandledObjects();
if ($handledObjects === []) {
continue;
}
$handledObjectsMap = array_combine(
$handledObjects,
$this->php->arrayFill(0, count($handledObjects), $objectHandler->getGeneralObjectType())
);
$this->allObjectTypesMap = array_merge($this->allObjectTypesMap, $handledObjectsMap);
}
}
return $this->allObjectTypesMap;
}
/**
* Returns all objects types.
* @return array
* @throws Exception
*/
public function getAllObjectTypes(): ?array
{
if ($this->allObjectTypes === null) {
$objectTypes = array_keys($this->getAllObjectsTypesMap());
$this->allObjectTypes = array_combine($objectTypes, $objectTypes);
}
return $this->allObjectTypes;
}
/**
* Returns the general object type.
* @param string|null $objectType
* @return string
* @throws Exception
*/
public function getGeneralObjectType(?string $objectType): ?string
{
$objectsTypeMap = $this->getAllObjectsTypesMap();
return (isset($objectsTypeMap[$objectType]) === true) ? $objectsTypeMap[$objectType] : null;
}
/**
* Checks if the object type is a valid one.
* @param string|null $objectType The object type to check.
* @return bool
* @throws Exception
*/
public function isValidObjectType(?string $objectType): bool
{
if (isset($this->validObjectTypes[$objectType]) === false) {
$objectTypesMap = $this->getAllObjectTypes();
$this->validObjectTypes[$objectType] = isset($objectTypesMap[$objectType]);
}
return $this->validObjectTypes[$objectType];
}
/**
* Returns the object membership handlers.
* @return ObjectMembershipHandler[]
* @throws Exception
*/
private function getObjectMembershipHandlers(): ?array
{
if ($this->objectMembershipHandlers === null) {
$factory = $this->membershipHandlerFactory;
$roleMembershipHandler = $factory->createRoleMembershipHandler();
$userMembershipHandler = $factory->createUserMembershipHandler($this);
$termMembershipHandler = $factory->createTermMembershipHandler($this);
$postMembershipHandler = $factory->createPostMembershipHandler($this);
$this->objectMembershipHandlers = [
$roleMembershipHandler->getGeneralObjectType() => $roleMembershipHandler,
$userMembershipHandler->getGeneralObjectType() => $userMembershipHandler,
$termMembershipHandler->getGeneralObjectType() => $termMembershipHandler,
$postMembershipHandler->getGeneralObjectType() => $postMembershipHandler
];
$this->objectMembershipHandlers = $this->wordpress->applyFilters(
'uam_register_object_membership_handler',
$this->objectMembershipHandlers
);
}
return $this->objectMembershipHandlers;
}
/**
* Returns the membership handler for the given object type.
* @param null|string $objectType
* @return ObjectMembershipHandler
* @throws MissingObjectMembershipHandlerException
* @throws Exception
*/
public function getObjectMembershipHandler(?string $objectType): ObjectMembershipHandler
{
$objectMembershipHandlers = $this->getObjectMembershipHandlers();
$generalObjectType = $this->getGeneralObjectType($objectType);
if (isset($objectMembershipHandlers[$generalObjectType]) === false) {
throw new MissingObjectMembershipHandlerException("Missing membership handler for '{$objectType}'.");
}
return $objectMembershipHandlers[$generalObjectType];
}
}