PostMembershipHandler.php
6.12 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
<?php
/**
* PostMembershipHandler.php
*
* The PostMembershipHandler 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\ObjectMembership;
use Exception;
use UserAccessManager\Object\ObjectHandler;
use UserAccessManager\Object\ObjectMapHandler;
use UserAccessManager\UserGroup\AbstractUserGroup;
use UserAccessManager\UserGroup\AssignmentInformation;
use UserAccessManager\UserGroup\AssignmentInformationFactory;
use UserAccessManager\Wrapper\Wordpress;
/**
* Class PostMembershipHandler
*
* @package UserAccessManager\UserGroup
*/
class PostMembershipHandler extends ObjectMembershipWithMapHandler
{
/**
* @var string
*/
protected $generalObjectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
/**
* @var Wordpress
*/
private $wordpress;
/**
* @var ObjectHandler
*/
private $objectHandler;
/**
* @var ObjectMapHandler
*/
private $objectMapHandler;
/**
* PostMembershipHandler constructor.
* @param AssignmentInformationFactory $assignmentInformationFactory
* @param Wordpress $wordpress
* @param ObjectHandler $objectHandler
* @param ObjectMapHandler $objectMapHandler
* @throws Exception
*/
public function __construct(
AssignmentInformationFactory $assignmentInformationFactory,
Wordpress $wordpress,
ObjectHandler $objectHandler,
ObjectMapHandler $objectMapHandler
) {
parent::__construct($assignmentInformationFactory);
$this->wordpress = $wordpress;
$this->objectHandler = $objectHandler;
$this->objectMapHandler = $objectMapHandler;
}
/**
* Returns the object and type name.
* @param int|string $objectId
* @param string $typeName
* @return int|string
*/
public function getObjectName($objectId, &$typeName = '')
{
$post = $this->objectHandler->getPost($objectId);
if ($post !== false) {
$postTypeObject = $this->wordpress->getPostTypeObject($post->post_type);
$typeName = ($postTypeObject !== null) ? $postTypeObject->labels->name : $typeName;
return $post->post_title;
}
return $objectId;
}
/**
* Returns the handled objects.
* @return array
*/
public function getHandledObjects(): array
{
$keys = array_keys($this->objectHandler->getPostTypes());
return array_merge(parent::getHandledObjects(), array_combine($keys, $keys));
}
/**
* Returns the map.
* @return array
*/
protected function getMap(): array
{
return $this->objectMapHandler->getPostTreeMap();
}
/**
* Assigns the recursive member ship by the group terms.
* @param AbstractUserGroup $userGroup
* @param int|string $objectId
* @param array $recursiveMembership
* @throws Exception
*/
private function assignRecursiveMembershipByTerm(
AbstractUserGroup $userGroup,
$objectId,
array &$recursiveMembership
) {
$postTermMap = $this->objectMapHandler->getPostTermMap();
if (isset($postTermMap[$objectId]) === true) {
foreach ($postTermMap[$objectId] as $termId => $type) {
if ($userGroup->isTermMember($termId, $rmAssignmentInformation) === true) {
$recursiveMembership[ObjectHandler::GENERAL_TERM_OBJECT_TYPE][$termId] = $rmAssignmentInformation;
}
}
}
}
/**
* Checks if the post is a member of the user group.
* @param AbstractUserGroup $userGroup
* @param bool $lockRecursive
* @param int|string $objectId
* @param null|AssignmentInformation $assignmentInformation
* @return bool
* @throws Exception
*/
public function isMember(
AbstractUserGroup $userGroup,
bool $lockRecursive,
$objectId,
?AssignmentInformation &$assignmentInformation = null
): bool {
$isMember = $this->getMembershipByMap($userGroup, $lockRecursive, $objectId, $assignmentInformation);
if ($lockRecursive === true) {
$recursiveMembership = ($assignmentInformation !== null) ?
$assignmentInformation->getRecursiveMembership() : [];
$this->assignRecursiveMembershipByTerm($userGroup, $objectId, $recursiveMembership);
$this->assignRecursiveMembership($assignmentInformation, $recursiveMembership);
$isMember = $isMember || count($recursiveMembership) > 0;
}
return $isMember;
}
/**
* Returns the full post objects.
* @param AbstractUserGroup $userGroup
* @param bool $lockRecursive
* @param null|string $objectType
* @return array
* @throws Exception
*/
public function getFullObjects(AbstractUserGroup $userGroup, bool $lockRecursive, $objectType = null): array
{
$objectType = ($objectType === null) ? $this->generalObjectType : $objectType;
$posts = $this->getFullObjectsByMap($userGroup, $lockRecursive, $objectType);
if ($lockRecursive === true) {
$termsPostMap = $this->objectMapHandler->getTermPostMap();
$terms = $userGroup->getFullTerms();
foreach ($terms as $termId => $term) {
if (isset($termsPostMap[$termId]) === true) {
$map = $termsPostMap[$termId];
if ($objectType !== $this->generalObjectType) {
$map = array_filter(
$map,
function ($element) use ($objectType) {
return ($element === $objectType);
}
);
}
$posts += $map;
}
}
}
return $posts;
}
}