PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Controller/Frontend/PostController.php +204 -81 2.3.13 → 2.1.11 View file →
@@ -1,47 +1,79 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * FrontendPostController.php
4 + *
5 + * The FrontendPostController 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\Controller\Frontend;
6 16
7 -use stdClass;
8 17 use UserAccessManager\Access\AccessHandler;
9 18 use UserAccessManager\Config\MainConfig;
10 19 use UserAccessManager\Config\WordpressConfig;
11 20 use UserAccessManager\Database\Database;
12 21 use UserAccessManager\Object\ObjectHandler;
22 +use UserAccessManager\UserGroup\AbstractUserGroup;
13 23 use UserAccessManager\User\UserHandler;
14 -use UserAccessManager\UserGroup\AbstractUserGroup;
15 24 use UserAccessManager\UserGroup\UserGroupHandler;
16 -use UserAccessManager\UserGroup\UserGroupTypeException;
17 25 use UserAccessManager\Util\Util;
18 26 use UserAccessManager\Wrapper\Php;
19 27 use UserAccessManager\Wrapper\Wordpress;
20 -use WeakMap;
21 -use WP_Comment;
22 -use WP_Hook;
23 -use WP_Post;
24 -use WP_Query;
25 28
29 +/**
30 + * Class FrontendPostController
31 + *
32 + * @package UserAccessManager\Controller
33 + */
26 34 class PostController extends ContentController
27 35 {
28 - private array $wordpressFilters = [];
29 - private stdClass|array|null $cachedCounts = [];
36 + /**
37 + * @var Database
38 + */
39 + private $database;
30 40
31 - private WeakMap $posts;
41 + /**
42 + * @var array
43 + */
44 + private $wordpressFilters = [];
32 45
46 + /**
47 + * @var null|\stdClass
48 + */
49 + private $cachedCounts = [];
50 +
51 + /**
52 + * PostController constructor.
53 + *
54 + * @param Php $php
55 + * @param Wordpress $wordpress
56 + * @param WordpressConfig $wordpressConfig
57 + * @param MainConfig $mainConfig
58 + * @param Database $database
59 + * @param Util $util
60 + * @param ObjectHandler $objectHandler
61 + * @param UserHandler $userHandler
62 + * @param UserGroupHandler $userGroupHandler
63 + * @param AccessHandler $accessHandler
64 + */
33 65 public function __construct(
34 66 Php $php,
35 67 Wordpress $wordpress,
36 68 WordpressConfig $wordpressConfig,
37 69 MainConfig $mainConfig,
70 + Database $database,
38 71 Util $util,
39 72 ObjectHandler $objectHandler,
40 73 UserHandler $userHandler,
41 74 UserGroupHandler $userGroupHandler,
42 - AccessHandler $accessHandler,
43 - private Database $database
75 + AccessHandler $accessHandler
44 76 ) {
45 77 parent::__construct(
46 78 $php,
47 79 $wordpress,
@@ -52,18 +84,21 @@
52 84 $userHandler,
53 85 $userGroupHandler,
54 86 $accessHandler
55 87 );
88 + $this->database = $database;
89 + }
56 90
57 - $this->posts = new WeakMap();
58 - }
59 91
60 - public function getWordpressFilters(): array
61 - {
62 - return $this->wordpressFilters;
63 - }
64 92
65 - private function filtersSuppressed(WP_Query $wpQuery): bool
93 + /**
94 + * Returns true if the filters are suppressed.
95 + *
96 + * @param \WP_Query $wpQuery
97 + *
98 + * @return bool
99 + */
100 + private function filtersSuppressed($wpQuery)
66 101 {
67 102 return isset($wpQuery->query_vars['suppress_filters']) === true
68 103 && $wpQuery->query_vars['suppress_filters'] === true;
69 104 }
@@ -68,11 +103,13 @@
68 103 && $wpQuery->query_vars['suppress_filters'] === true;
69 104 }
70 105
71 106 /**
72 - * @throws UserGroupTypeException
107 + * Manipulates the wordpress query object to filter content.
108 + *
109 + * @param \WP_Query $wpQuery The wordpress query object.
73 110 */
74 - public function parseQuery(WP_Query $wpQuery): void
111 + public function parseQuery($wpQuery)
75 112 {
76 113 if ($this->filtersSuppressed($wpQuery) === true) {
77 114 $excludedPosts = $this->accessHandler->getExcludedPosts();
78 115
@@ -87,11 +124,15 @@
87 124 }
88 125 }
89 126
90 127 /**
91 - * @param WP_Hook[] $filters
128 + * Extracts the user access manager filters and returns true if it was successful.
129 + *
130 + * @param \WP_Hook[] $filters
131 + *
132 + * @return bool
92 133 */
93 - private function extractOwnFilters(array $filters): bool
134 + private function extractOwnFilters(array &$filters)
94 135 {
95 136 if (isset($filters['the_posts']->callbacks[10]) === true) {
96 137 foreach ($filters['the_posts']->callbacks[10] as $postFilter) {
97 138 if (is_array($postFilter['function']) === true
@@ -107,9 +148,19 @@
107 148
108 149 return false;
109 150 }
110 151
111 - public function postsPreQuery(?array $posts, WP_Query $query): ?array
152 + /**
153 + * If filters are suppressed we still want to filter posts, so we have to turn the suppression off,
154 + * remove all other filters than the ones from the user access manager and store them to restore
155 + * them later.
156 + *
157 + * @param array $posts
158 + * @param \WP_Query $query
159 + *
160 + * @return mixed
161 + */
162 + public function postsPreQuery($posts, \WP_Query $query)
112 163 {
113 164 if ($this->filtersSuppressed($query) === true) {
114 165 $filters = $this->wordpress->getFilters();
115 166
@@ -128,9 +179,12 @@
128 179
129 180 return $posts;
130 181 }
131 182
132 - private function restoreFilters(): void
183 + /**
184 + * Restores the filters to normal.
185 + */
186 + private function restoreFilters()
133 187 {
134 188 if (count($this->wordpressFilters) > 0) {
135 189 $filters = $this->wordpress->getFilters();
136 190
@@ -142,11 +196,18 @@
142 196 $this->wordpressFilters = [];
143 197 }
144 198 }
145 199
146 - private function getPost(mixed $post): bool|WP_Post
200 + /**
201 + * Tries to get the post from the given mixed data.
202 + *
203 + * @param mixed $post
204 + *
205 + * @return false|\WP_Post
206 + */
207 + private function getPost($post)
147 208 {
148 - if ($post instanceof WP_post) {
209 + if ($post instanceof \WP_post) {
149 210 return $post;
150 211 } elseif (is_int($post) === true) {
151 212 return $this->objectHandler->getPost($post);
152 213 } elseif (isset($post->ID) === true) {
@@ -155,9 +216,16 @@
155 216
156 217 return false;
157 218 }
158 219
159 - private function processPostContent(WP_Post $post): string
220 + /**
221 + * Processes the post content and searches for the more tag.
222 + *
223 + * @param \WP_Post $post
224 + *
225 + * @return string
226 + */
227 + private function processPostContent(\WP_Post $post)
160 228 {
161 229 $uamPostContent = htmlspecialchars_decode($this->mainConfig->getPostTypeContent($post->post_type));
162 230
163 231 if ($this->mainConfig->showPostTypeContentBeforeMore($post->post_type) === true
@@ -162,9 +230,9 @@
162 230
163 231 if ($this->mainConfig->showPostTypeContentBeforeMore($post->post_type) === true
164 232 && preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches)
165 233 ) {
166 - $uamPostContent = explode($matches[0], $post->post_content)[0] . ' ' . $uamPostContent;
234 + $uamPostContent = explode($matches[0], $post->post_content)[0].' '.$uamPostContent;
167 235 }
168 236
169 237 return stripslashes($uamPostContent);
170 238 }
@@ -169,17 +237,21 @@
169 237 return stripslashes($uamPostContent);
170 238 }
171 239
172 240 /**
173 - * @throws UserGroupTypeException
241 + * Modifies the content of the post by the given settings.
242 + *
243 + * @param \WP_Post $post The current post.
244 + *
245 + * @return null|\WP_Post
174 246 */
175 - private function processPost(WP_Post $post): WP_Post|bool
247 + private function processPost(\WP_Post $post)
176 248 {
177 249 $post->post_title .= $this->adminOutput($post->post_type, $post->ID);
178 250
179 251 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === false) {
180 252 if ($this->removePostFromList($post->post_type) === true) {
181 - return false;
253 + return null;
182 254 }
183 255
184 256 $post->post_content = $this->processPostContent($post);
185 257
@@ -195,21 +267,16 @@
195 267 return $post;
196 268 }
197 269
198 270 /**
199 - * @throws UserGroupTypeException
271 + * Filters the raw posts.
272 + *
273 + * @param array $rawPosts
274 + *
275 + * @return array
200 276 */
201 - private function getProcessedPost(WP_Post $post): ?WP_Post
277 + private function filterRawPosts(array $rawPosts)
202 278 {
203 - $post = $this->posts[$post] ??= $this->processPost($post);
204 - return $post === false ? null : $post;
205 - }
206 -
207 - /**
208 - * @throws UserGroupTypeException
209 - */
210 - private function filterRawPosts(array $rawPosts): array
211 - {
212 279 $filteredPosts = [];
213 280
214 281 foreach ($rawPosts as $rawPost) {
215 282 $post = $this->getPost($rawPost);
@@ -214,9 +281,9 @@
214 281 foreach ($rawPosts as $rawPost) {
215 282 $post = $this->getPost($rawPost);
216 283
217 284 if ($post !== false) {
218 - $post = $this->getProcessedPost($post);
285 + $post = $this->processPost($post);
219 286
220 287 if ($post !== null) {
221 288 $filteredPosts[] = $post;
222 289 }
@@ -228,14 +295,18 @@
228 295 return $filteredPosts;
229 296 }
230 297
231 298 /**
232 - * @throws UserGroupTypeException
299 + * The function for the the_posts filter.
300 + *
301 + * @param array $showPosts The posts.
302 + *
303 + * @return array
233 304 */
234 - public function showPosts(?array $showPosts = []): ?array
305 + public function showPosts($showPosts = [])
235 306 {
236 307 if ($this->wordpress->isFeed() === false || $this->mainConfig->protectFeed() === true) {
237 - $showPosts = $this->filterRawPosts((array) $showPosts);
308 + $showPosts = $this->filterRawPosts($showPosts);
238 309 }
239 310
240 311 $this->restoreFilters();
241 312
@@ -242,24 +313,32 @@
242 313 return $showPosts;
243 314 }
244 315
245 316 /**
246 - * @param WP_Post[] $rawPages The pages.
247 - * @throws UserGroupTypeException
317 + * The function for the get_pages filter.
318 + *
319 + * @param \WP_Post[] $rawPages The pages.
320 + *
321 + * @return array
248 322 */
249 - public function showPages(array $rawPages = []): array
323 + public function showPages($rawPages = [])
250 324 {
251 325 return $this->filterRawPosts($rawPages);
252 326 }
253 327
254 328 /**
255 - * @throws UserGroupTypeException
329 + * Checks the access of the attached file.
330 + *
331 + * @param string $file
332 + * @param int $attachmentId
333 + *
334 + * @return string|false
256 335 */
257 - public function getAttachedFile(string $file, int|string|null $attachmentId): bool|string
336 + public function getAttachedFile($file, $attachmentId)
258 337 {
259 338 $isImage = (bool) preg_match('/(?i)\.(jpg|jpeg|jpe|png|gif)$/', $file);
260 339
261 - if ($isImage === false && $this->mainConfig->lockFile() === true) {
340 + if ($this->mainConfig->lockFile() === true && $isImage === false) {
262 341 $hasAccess = $this->accessHandler->checkObjectAccess(ObjectHandler::ATTACHMENT_OBJECT_TYPE, $attachmentId);
263 342 return ($hasAccess === true) ? $file : false;
264 343 }
265 344
@@ -266,17 +345,22 @@
266 345 return $file;
267 346 }
268 347
269 348 /**
270 - * @throws UserGroupTypeException
349 + * Adds the excluded posts filter to the given query.
350 + *
351 + * @param string $query
352 + * @param string $table
353 + *
354 + * @return string
271 355 */
272 - private function addQueryExcludedPostFilter(string $query, string $table): string
356 + private function addQueryExcludedPostFilter($query, $table)
273 357 {
274 358 $excludedPosts = $this->accessHandler->getExcludedPosts();
275 359
276 360 if ($excludedPosts !== []) {
277 - $excludedPostsStr = implode(', ', array_map('intval', $excludedPosts));
278 - $query .= " AND $table.ID NOT IN ($excludedPostsStr) ";
361 + $excludedPostsStr = implode(', ', $excludedPosts);
362 + $query .= " AND {$table}.ID NOT IN ($excludedPostsStr) ";
279 363 }
280 364
281 365 return $query;
282 366 }
@@ -281,30 +365,48 @@
281 365 return $query;
282 366 }
283 367
284 368 /**
285 - * @throws UserGroupTypeException
369 + * The function for the posts_where_paged filter.
370 + *
371 + * @param string $query The where sql statement.
372 + *
373 + * @return string
286 374 */
287 - public function showPostSql(string $query): string
375 + public function showPostSql($query)
288 376 {
289 377 return $this->addQueryExcludedPostFilter($query, $this->database->getPostsTable());
290 378 }
291 379
292 380 /**
293 - * @throws UserGroupTypeException
381 + * The function for the get_previous_post_where and
382 + * the get_next_post_where filter.
383 + *
384 + * @param string $query The current sql string.
385 + *
386 + * @return string
294 387 */
295 - public function showNextPreviousPost(string $query): string
388 + public function showNextPreviousPost($query)
296 389 {
297 390 return $this->addQueryExcludedPostFilter($query, 'p');
298 391 }
299 392
300 - private function getPostCountQuery(array $excludedPosts, string $type, string $perm): string
393 + /**
394 + * Returns the post count query.
395 + *
396 + * @param array $excludedPosts
397 + * @param string $type
398 + * @param string $perm
399 + *
400 + * @return string
401 + */
402 + private function getPostCountQuery(array $excludedPosts, $type, $perm)
301 403 {
302 - $excludedPosts = implode(', ', array_map('intval', $excludedPosts));
303 - $query = "SELECT post_status, COUNT(*) AS num_posts
304 - FROM {$this->database->getPostsTable()}
404 + $excludedPosts = implode('\', \'', $excludedPosts);
405 + $query = "SELECT post_status, COUNT(*) AS num_posts
406 + FROM {$this->database->getPostsTable()}
305 407 WHERE post_type = %s
306 - AND ID NOT IN ($excludedPosts)";
408 + AND ID NOT IN ('{$excludedPosts}')";
307 409
308 410 if ('readable' === $perm
309 411 && $this->wordpress->isUserLoggedIn() === true
310 412 && $this->wordpress->currentUserCan(
@@ -321,11 +423,17 @@
321 423 return $query;
322 424 }
323 425
324 426 /**
325 - * @throws UserGroupTypeException
427 + * Function for the wp_count_posts filter.
428 + *
429 + * @param \stdClass $counts
430 + * @param string $type
431 + * @param string $perm
432 + *
433 + * @return \stdClass
326 434 */
327 - public function showPostCount(stdClass $counts, string $type, string $perm): stdClass
435 + public function showPostCount($counts, $type, $perm)
328 436 {
329 437 if (isset($this->cachedCounts[$type]) === false) {
330 438 $excludedPosts = $this->accessHandler->getExcludedPosts();
331 439
@@ -330,9 +438,9 @@
330 438 $excludedPosts = $this->accessHandler->getExcludedPosts();
331 439
332 440 if ($excludedPosts !== []) {
333 441 $query = $this->getPostCountQuery($excludedPosts, $type, $perm);
334 - $results = (array) $this->database->getResults(
442 + $results = (array)$this->database->getResults(
335 443 $this->database->prepare($query, $type),
336 444 ARRAY_A
337 445 );
338 446
@@ -348,9 +456,16 @@
348 456
349 457 return $this->cachedCounts[$type];
350 458 }
351 459
352 - private function hidePostComment(string $postType): bool
460 + /**
461 + * Checks if the post comment should be completely hidden.
462 + *
463 + * @param string $postType
464 + *
465 + * @return bool
466 + */
467 + private function hidePostComment($postType)
353 468 {
354 469 return $this->mainConfig->lockPostTypeComments($postType) === true
355 470 || $this->mainConfig->hidePostType($postType) === true
356 471 || $this->wordpressConfig->atAdminPanel() === true;
@@ -356,12 +471,15 @@
356 471 || $this->wordpressConfig->atAdminPanel() === true;
357 472 }
358 473
359 474 /**
360 - * @param WP_Comment[] $comments The comments.
361 - * @throws UserGroupTypeException
475 + * The function for the comments_array filter.
476 + *
477 + * @param \WP_Comment[] $comments The comments.
478 + *
479 + * @return array
362 480 */
363 - public function showComment(array $comments = []): array
481 + public function showComment($comments = [])
364 482 {
365 483 $showComments = [];
366 484
367 485 foreach ($comments as $comment) {
@@ -385,11 +503,16 @@
385 503 return $showComments;
386 504 }
387 505
388 506 /**
389 - * @throws UserGroupTypeException
507 + * The function for the edit_post_link filter.
508 + *
509 + * @param string $link The edit link.
510 + * @param integer $postId The _iId of the post.
511 + *
512 + * @return string
390 513 */
391 - public function showEditLink(?string $link, int|string|null $postId): string
514 + public function showEditLink($link, $postId)
392 515 {
393 516 if ($this->mainConfig->hideEditLinkOnNoAccess() === true
394 517 && $this->accessHandler->checkObjectAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, true) === false
395 518 ) {
@@ -410,11 +533,11 @@
410 533 $userGroups
411 534 );
412 535
413 536 $link .= $link !== '' ? ' | ' : ' ';
414 - $link .= TXT_UAM_ASSIGNED_GROUPS . ': ' . implode(', ', $escapedGroups);
537 + $link .= TXT_UAM_ASSIGNED_GROUPS.': '.implode(', ', $escapedGroups);
415 538 }
416 539 }
417 540
418 - return (string) $link;
541 + return $link;
419 542 }
420 543 }