| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * FrontendPostController.php | |
| 4 | + * | |
| 5 | + * The FrontendPostController 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 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Controller\Frontend; |
| @@ -16,32 +29,59 @@ | ||
| 16 | 29 | use UserAccessManager\UserGroup\UserGroupTypeException; |
| 17 | 30 | use UserAccessManager\Util\Util; |
| 18 | 31 | use UserAccessManager\Wrapper\Php; |
| 19 | 32 | use UserAccessManager\Wrapper\Wordpress; |
| 20 | -use WeakMap; | |
| 21 | 33 | use WP_Comment; |
| 22 | 34 | use WP_Hook; |
| 23 | 35 | use WP_Post; |
| 24 | 36 | use WP_Query; |
| 25 | 37 | |
| 38 | +/** | |
| 39 | + * Class FrontendPostController | |
| 40 | + * | |
| 41 | + * @package UserAccessManager\Controller | |
| 42 | + */ | |
| 26 | 43 | class PostController extends ContentController |
| 27 | 44 | { |
| 28 | - private array $wordpressFilters = []; | |
| 29 | - private stdClass|array|null $cachedCounts = []; | |
| 45 | + /** | |
| 46 | + * @var Database | |
| 47 | + */ | |
| 48 | + private $database; | |
| 30 | 49 | |
| 31 | - private WeakMap $posts; | |
| 50 | + /** | |
| 51 | + * @var array | |
| 52 | + */ | |
| 53 | + private $wordpressFilters = []; | |
| 32 | 54 | |
| 55 | + /** | |
| 56 | + * @var null|stdClass | |
| 57 | + */ | |
| 58 | + private $cachedCounts = []; | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * PostController constructor. | |
| 62 | + * @param Php $php | |
| 63 | + * @param Wordpress $wordpress | |
| 64 | + * @param WordpressConfig $wordpressConfig | |
| 65 | + * @param MainConfig $mainConfig | |
| 66 | + * @param Database $database | |
| 67 | + * @param Util $util | |
| 68 | + * @param ObjectHandler $objectHandler | |
| 69 | + * @param UserHandler $userHandler | |
| 70 | + * @param UserGroupHandler $userGroupHandler | |
| 71 | + * @param AccessHandler $accessHandler | |
| 72 | + */ | |
| 33 | 73 | public function __construct( |
| 34 | 74 | Php $php, |
| 35 | 75 | Wordpress $wordpress, |
| 36 | 76 | WordpressConfig $wordpressConfig, |
| 37 | 77 | MainConfig $mainConfig, |
| 78 | + Database $database, | |
| 38 | 79 | Util $util, |
| 39 | 80 | ObjectHandler $objectHandler, |
| 40 | 81 | UserHandler $userHandler, |
| 41 | 82 | UserGroupHandler $userGroupHandler, |
| 42 | - AccessHandler $accessHandler, | |
| 43 | - private Database $database | |
| 83 | + AccessHandler $accessHandler | |
| 44 | 84 | ) { |
| 45 | 85 | parent::__construct( |
| 46 | 86 | $php, |
| 47 | 87 | $wordpress, |
| @@ -52,17 +92,25 @@ | ||
| 52 | 92 | $userHandler, |
| 53 | 93 | $userGroupHandler, |
| 54 | 94 | $accessHandler |
| 55 | 95 | ); |
| 56 | - | |
| 57 | - $this->posts = new WeakMap(); | |
| 96 | + $this->database = $database; | |
| 58 | 97 | } |
| 59 | 98 | |
| 99 | + /** | |
| 100 | + * Return the wordpress filters. | |
| 101 | + * @return array | |
| 102 | + */ | |
| 60 | 103 | public function getWordpressFilters(): array |
| 61 | 104 | { |
| 62 | 105 | return $this->wordpressFilters; |
| 63 | 106 | } |
| 64 | 107 | |
| 108 | + /** | |
| 109 | + * Returns true if the filters are suppressed. | |
| 110 | + * @param WP_Query $wpQuery | |
| 111 | + * @return bool | |
| 112 | + */ | |
| 65 | 113 | private function filtersSuppressed(WP_Query $wpQuery): bool |
| 66 | 114 | { |
| 67 | 115 | return isset($wpQuery->query_vars['suppress_filters']) === true |
| 68 | 116 | && $wpQuery->query_vars['suppress_filters'] === true; |
| @@ -68,11 +116,13 @@ | ||
| 68 | 116 | && $wpQuery->query_vars['suppress_filters'] === true; |
| 69 | 117 | } |
| 70 | 118 | |
| 71 | 119 | /** |
| 120 | + * Manipulates the wordpress query object to filter content. | |
| 121 | + * @param WP_Query $wpQuery The wordpress query object. | |
| 72 | 122 | * @throws UserGroupTypeException |
| 73 | 123 | */ |
| 74 | - public function parseQuery(WP_Query $wpQuery): void | |
| 124 | + public function parseQuery(WP_Query $wpQuery) | |
| 75 | 125 | { |
| 76 | 126 | if ($this->filtersSuppressed($wpQuery) === true) { |
| 77 | 127 | $excludedPosts = $this->accessHandler->getExcludedPosts(); |
| 78 | 128 | |
| @@ -87,9 +137,11 @@ | ||
| 87 | 137 | } |
| 88 | 138 | } |
| 89 | 139 | |
| 90 | 140 | /** |
| 141 | + * Extracts the user access manager filters and returns true if it was successful. | |
| 91 | 142 | * @param WP_Hook[] $filters |
| 143 | + * @return bool | |
| 92 | 144 | */ |
| 93 | 145 | private function extractOwnFilters(array $filters): bool |
| 94 | 146 | { |
| 95 | 147 | if (isset($filters['the_posts']->callbacks[10]) === true) { |
| @@ -107,8 +159,16 @@ | ||
| 107 | 159 | |
| 108 | 160 | return false; |
| 109 | 161 | } |
| 110 | 162 | |
| 163 | + /** | |
| 164 | + * If filters are suppressed we still want to filter posts, so we have to turn the suppression off, | |
| 165 | + * remove all other filters than the ones from the user access manager and store them to restore | |
| 166 | + * them later. | |
| 167 | + * @param array|null $posts | |
| 168 | + * @param WP_Query $query | |
| 169 | + * @return null|array | |
| 170 | + */ | |
| 111 | 171 | public function postsPreQuery(?array $posts, WP_Query $query): ?array |
| 112 | 172 | { |
| 113 | 173 | if ($this->filtersSuppressed($query) === true) { |
| 114 | 174 | $filters = $this->wordpress->getFilters(); |
| @@ -128,9 +188,12 @@ | ||
| 128 | 188 | |
| 129 | 189 | return $posts; |
| 130 | 190 | } |
| 131 | 191 | |
| 132 | - private function restoreFilters(): void | |
| 192 | + /** | |
| 193 | + * Restores the filters to normal. | |
| 194 | + */ | |
| 195 | + private function restoreFilters() | |
| 133 | 196 | { |
| 134 | 197 | if (count($this->wordpressFilters) > 0) { |
| 135 | 198 | $filters = $this->wordpress->getFilters(); |
| 136 | 199 | |
| @@ -142,9 +205,14 @@ | ||
| 142 | 205 | $this->wordpressFilters = []; |
| 143 | 206 | } |
| 144 | 207 | } |
| 145 | 208 | |
| 146 | - private function getPost(mixed $post): bool|WP_Post | |
| 209 | + /** | |
| 210 | + * Tries to get the post from the given mixed data. | |
| 211 | + * @param mixed $post | |
| 212 | + * @return false|WP_Post | |
| 213 | + */ | |
| 214 | + private function getPost($post) | |
| 147 | 215 | { |
| 148 | 216 | if ($post instanceof WP_post) { |
| 149 | 217 | return $post; |
| 150 | 218 | } elseif (is_int($post) === true) { |
| @@ -155,8 +223,13 @@ | ||
| 155 | 223 | |
| 156 | 224 | return false; |
| 157 | 225 | } |
| 158 | 226 | |
| 227 | + /** | |
| 228 | + * Processes the post content and searches for the more tag. | |
| 229 | + * @param WP_Post $post | |
| 230 | + * @return string | |
| 231 | + */ | |
| 159 | 232 | private function processPostContent(WP_Post $post): string |
| 160 | 233 | { |
| 161 | 234 | $uamPostContent = htmlspecialchars_decode($this->mainConfig->getPostTypeContent($post->post_type)); |
| 162 | 235 | |
| @@ -169,17 +242,20 @@ | ||
| 169 | 242 | return stripslashes($uamPostContent); |
| 170 | 243 | } |
| 171 | 244 | |
| 172 | 245 | /** |
| 246 | + * Modifies the content of the post by the given settings. | |
| 247 | + * @param WP_Post $post The current post. | |
| 248 | + * @return null|WP_Post | |
| 173 | 249 | * @throws UserGroupTypeException |
| 174 | 250 | */ |
| 175 | - private function processPost(WP_Post $post): WP_Post|bool | |
| 251 | + private function processPost(WP_Post $post): ?WP_Post | |
| 176 | 252 | { |
| 177 | - $post->post_title .= $this->adminOutput($post->post_type, $post->ID); | |
| 253 | + $post->post_title .= $this->adminOutput((string) $post->post_type, $post->ID); | |
| 178 | 254 | |
| 179 | 255 | if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === false) { |
| 180 | 256 | if ($this->removePostFromList($post->post_type) === true) { |
| 181 | - return false; | |
| 257 | + return null; | |
| 182 | 258 | } |
| 183 | 259 | |
| 184 | 260 | $post->post_content = $this->processPostContent($post); |
| 185 | 261 | |
| @@ -195,19 +271,13 @@ | ||
| 195 | 271 | return $post; |
| 196 | 272 | } |
| 197 | 273 | |
| 198 | 274 | /** |
| 275 | + * Filters the raw posts. | |
| 276 | + * @param array $rawPosts | |
| 277 | + * @return array | |
| 199 | 278 | * @throws UserGroupTypeException |
| 200 | 279 | */ |
| 201 | - private function getProcessedPost(WP_Post $post): ?WP_Post | |
| 202 | - { | |
| 203 | - $post = $this->posts[$post] ??= $this->processPost($post); | |
| 204 | - return $post === false ? null : $post; | |
| 205 | - } | |
| 206 | - | |
| 207 | - /** | |
| 208 | - * @throws UserGroupTypeException | |
| 209 | - */ | |
| 210 | 280 | private function filterRawPosts(array $rawPosts): array |
| 211 | 281 | { |
| 212 | 282 | $filteredPosts = []; |
| 213 | 283 | |
| @@ -214,9 +284,9 @@ | ||
| 214 | 284 | foreach ($rawPosts as $rawPost) { |
| 215 | 285 | $post = $this->getPost($rawPost); |
| 216 | 286 | |
| 217 | 287 | if ($post !== false) { |
| 218 | - $post = $this->getProcessedPost($post); | |
| 288 | + $post = $this->processPost($post); | |
| 219 | 289 | |
| 220 | 290 | if ($post !== null) { |
| 221 | 291 | $filteredPosts[] = $post; |
| 222 | 292 | } |
| @@ -228,8 +298,11 @@ | ||
| 228 | 298 | return $filteredPosts; |
| 229 | 299 | } |
| 230 | 300 | |
| 231 | 301 | /** |
| 302 | + * The function for the the_posts filter. | |
| 303 | + * @param null|array $showPosts The posts. | |
| 304 | + * @return array | |
| 232 | 305 | * @throws UserGroupTypeException |
| 233 | 306 | */ |
| 234 | 307 | public function showPosts(?array $showPosts = []): ?array |
| 235 | 308 | { |
| @@ -242,20 +315,26 @@ | ||
| 242 | 315 | return $showPosts; |
| 243 | 316 | } |
| 244 | 317 | |
| 245 | 318 | /** |
| 319 | + * The function for the get_pages filter. | |
| 246 | 320 | * @param WP_Post[] $rawPages The pages. |
| 321 | + * @return array | |
| 247 | 322 | * @throws UserGroupTypeException |
| 248 | 323 | */ |
| 249 | - public function showPages(array $rawPages = []): array | |
| 324 | + public function showPages($rawPages = []): array | |
| 250 | 325 | { |
| 251 | - return $this->filterRawPosts($rawPages); | |
| 326 | + return $this->filterRawPosts((array) $rawPages); | |
| 252 | 327 | } |
| 253 | 328 | |
| 254 | 329 | /** |
| 330 | + * Checks the access of the attached file. | |
| 331 | + * @param string $file | |
| 332 | + * @param int|string $attachmentId | |
| 333 | + * @return string|false | |
| 255 | 334 | * @throws UserGroupTypeException |
| 256 | 335 | */ |
| 257 | - public function getAttachedFile(string $file, int|string|null $attachmentId): bool|string | |
| 336 | + public function getAttachedFile(string $file, $attachmentId) | |
| 258 | 337 | { |
| 259 | 338 | $isImage = (bool) preg_match('/(?i)\.(jpg|jpeg|jpe|png|gif)$/', $file); |
| 260 | 339 | |
| 261 | 340 | if ($isImage === false && $this->mainConfig->lockFile() === true) { |
| @@ -266,8 +345,12 @@ | ||
| 266 | 345 | return $file; |
| 267 | 346 | } |
| 268 | 347 | |
| 269 | 348 | /** |
| 349 | + * Adds the excluded posts filter to the given query. | |
| 350 | + * @param string $query | |
| 351 | + * @param string $table | |
| 352 | + * @return string | |
| 270 | 353 | * @throws UserGroupTypeException |
| 271 | 354 | */ |
| 272 | 355 | private function addQueryExcludedPostFilter(string $query, string $table): string |
| 273 | 356 | { |
| @@ -273,10 +356,10 @@ | ||
| 273 | 356 | { |
| 274 | 357 | $excludedPosts = $this->accessHandler->getExcludedPosts(); |
| 275 | 358 | |
| 276 | 359 | if ($excludedPosts !== []) { |
| 277 | - $excludedPostsStr = implode(', ', array_map('intval', $excludedPosts)); | |
| 278 | - $query .= " AND $table.ID NOT IN ($excludedPostsStr) "; | |
| 360 | + $excludedPostsStr = implode(', ', $excludedPosts); | |
| 361 | + $query .= " AND {$table}.ID NOT IN ($excludedPostsStr) "; | |
| 279 | 362 | } |
| 280 | 363 | |
| 281 | 364 | return $query; |
| 282 | 365 | } |
| @@ -281,8 +364,11 @@ | ||
| 281 | 364 | return $query; |
| 282 | 365 | } |
| 283 | 366 | |
| 284 | 367 | /** |
| 368 | + * The function for the posts_where_paged filter. | |
| 369 | + * @param string $query The where sql statement. | |
| 370 | + * @return string | |
| 285 | 371 | * @throws UserGroupTypeException |
| 286 | 372 | */ |
| 287 | 373 | public function showPostSql(string $query): string |
| 288 | 374 | { |
| @@ -289,8 +375,12 @@ | ||
| 289 | 375 | return $this->addQueryExcludedPostFilter($query, $this->database->getPostsTable()); |
| 290 | 376 | } |
| 291 | 377 | |
| 292 | 378 | /** |
| 379 | + * The function for the get_previous_post_where and | |
| 380 | + * the get_next_post_where filter. | |
| 381 | + * @param string $query The current sql string. | |
| 382 | + * @return string | |
| 293 | 383 | * @throws UserGroupTypeException |
| 294 | 384 | */ |
| 295 | 385 | public function showNextPreviousPost(string $query): string |
| 296 | 386 | { |
| @@ -296,15 +386,22 @@ | ||
| 296 | 386 | { |
| 297 | 387 | return $this->addQueryExcludedPostFilter($query, 'p'); |
| 298 | 388 | } |
| 299 | 389 | |
| 390 | + /** | |
| 391 | + * Returns the post count query. | |
| 392 | + * @param array $excludedPosts | |
| 393 | + * @param string $type | |
| 394 | + * @param string $perm | |
| 395 | + * @return string | |
| 396 | + */ | |
| 300 | 397 | private function getPostCountQuery(array $excludedPosts, string $type, string $perm): string |
| 301 | 398 | { |
| 302 | - $excludedPosts = implode(', ', array_map('intval', $excludedPosts)); | |
| 303 | - $query = "SELECT post_status, COUNT(*) AS num_posts | |
| 304 | - FROM {$this->database->getPostsTable()} | |
| 399 | + $excludedPosts = implode('\', \'', $excludedPosts); | |
| 400 | + $query = "SELECT post_status, COUNT(*) AS num_posts | |
| 401 | + FROM {$this->database->getPostsTable()} | |
| 305 | 402 | WHERE post_type = %s |
| 306 | - AND ID NOT IN ($excludedPosts)"; | |
| 403 | + AND ID NOT IN ('{$excludedPosts}')"; | |
| 307 | 404 | |
| 308 | 405 | if ('readable' === $perm |
| 309 | 406 | && $this->wordpress->isUserLoggedIn() === true |
| 310 | 407 | && $this->wordpress->currentUserCan( |
| @@ -321,8 +418,13 @@ | ||
| 321 | 418 | return $query; |
| 322 | 419 | } |
| 323 | 420 | |
| 324 | 421 | /** |
| 422 | + * Function for the wp_count_posts filter. | |
| 423 | + * @param stdClass $counts | |
| 424 | + * @param string $type | |
| 425 | + * @param string $perm | |
| 426 | + * @return stdClass | |
| 325 | 427 | * @throws UserGroupTypeException |
| 326 | 428 | */ |
| 327 | 429 | public function showPostCount(stdClass $counts, string $type, string $perm): stdClass |
| 328 | 430 | { |
| @@ -348,8 +450,13 @@ | ||
| 348 | 450 | |
| 349 | 451 | return $this->cachedCounts[$type]; |
| 350 | 452 | } |
| 351 | 453 | |
| 454 | + /** | |
| 455 | + * Checks if the post comment should be completely hidden. | |
| 456 | + * @param string $postType | |
| 457 | + * @return bool | |
| 458 | + */ | |
| 352 | 459 | private function hidePostComment(string $postType): bool |
| 353 | 460 | { |
| 354 | 461 | return $this->mainConfig->lockPostTypeComments($postType) === true |
| 355 | 462 | || $this->mainConfig->hidePostType($postType) === true |
| @@ -356,12 +463,14 @@ | ||
| 356 | 463 | || $this->wordpressConfig->atAdminPanel() === true; |
| 357 | 464 | } |
| 358 | 465 | |
| 359 | 466 | /** |
| 467 | + * The function for the comments_array filter. | |
| 360 | 468 | * @param WP_Comment[] $comments The comments. |
| 469 | + * @return array | |
| 361 | 470 | * @throws UserGroupTypeException |
| 362 | 471 | */ |
| 363 | - public function showComment(array $comments = []): array | |
| 472 | + public function showComment($comments = []): array | |
| 364 | 473 | { |
| 365 | 474 | $showComments = []; |
| 366 | 475 | |
| 367 | 476 | foreach ($comments as $comment) { |
| @@ -385,11 +494,15 @@ | ||
| 385 | 494 | return $showComments; |
| 386 | 495 | } |
| 387 | 496 | |
| 388 | 497 | /** |
| 498 | + * The function for the edit_post_link filter. | |
| 499 | + * @param null|string $link The edit link. | |
| 500 | + * @param int|string $postId The _iId of the post. | |
| 501 | + * @return string | |
| 389 | 502 | * @throws UserGroupTypeException |
| 390 | 503 | */ |
| 391 | - public function showEditLink(?string $link, int|string|null $postId): string | |
| 504 | + public function showEditLink(?string $link, $postId): string | |
| 392 | 505 | { |
| 393 | 506 | if ($this->mainConfig->hideEditLinkOnNoAccess() === true |
| 394 | 507 | && $this->accessHandler->checkObjectAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, true) === false |
| 395 | 508 | ) { |