| @@ -1,53 +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_Error; | |
| 23 | -use WP_Hook; | |
| 24 | -use WP_Post; | |
| 25 | -use WP_Query; | |
| 26 | -use WP_REST_Request; | |
| 27 | -use WP_REST_Response; | |
| 28 | 28 | |
| 29 | +/** | |
| 30 | + * Class FrontendPostController | |
| 31 | + * | |
| 32 | + * @package UserAccessManager\Controller | |
| 33 | + */ | |
| 29 | 34 | class PostController extends ContentController |
| 30 | 35 | { |
| 31 | - private const REST_OBJECT_ROUTE_PATTERN = '#^/[^/]+/v\d+/([^/]+)/(\d+)(?:/(\w+))?#'; | |
| 36 | + /** | |
| 37 | + * @var Database | |
| 38 | + */ | |
| 39 | + private $database; | |
| 32 | 40 | |
| 33 | - private array $wordpressFilters = []; | |
| 34 | - private stdClass|array|null $cachedCounts = []; | |
| 35 | - private ?array $restBaseToPostTypeMap = null; | |
| 41 | + /** | |
| 42 | + * @var array | |
| 43 | + */ | |
| 44 | + private $wordpressFilters = []; | |
| 36 | 45 | |
| 37 | - private WeakMap $posts; | |
| 46 | + /** | |
| 47 | + * @var null|\stdClass | |
| 48 | + */ | |
| 49 | + private $cachedCounts = []; | |
| 38 | 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 | + */ | |
| 39 | 65 | public function __construct( |
| 40 | 66 | Php $php, |
| 41 | 67 | Wordpress $wordpress, |
| 42 | 68 | WordpressConfig $wordpressConfig, |
| 43 | 69 | MainConfig $mainConfig, |
| 70 | + Database $database, | |
| 44 | 71 | Util $util, |
| 45 | 72 | ObjectHandler $objectHandler, |
| 46 | 73 | UserHandler $userHandler, |
| 47 | 74 | UserGroupHandler $userGroupHandler, |
| 48 | - AccessHandler $accessHandler, | |
| 49 | - private Database $database | |
| 75 | + AccessHandler $accessHandler | |
| 50 | 76 | ) { |
| 51 | 77 | parent::__construct( |
| 52 | 78 | $php, |
| 53 | 79 | $wordpress, |
| @@ -58,40 +84,42 @@ | ||
| 58 | 84 | $userHandler, |
| 59 | 85 | $userGroupHandler, |
| 60 | 86 | $accessHandler |
| 61 | 87 | ); |
| 88 | + $this->database = $database; | |
| 89 | + } | |
| 62 | 90 | |
| 63 | - $this->posts = new WeakMap(); | |
| 64 | - } | |
| 65 | 91 | |
| 66 | - public function getWordpressFilters(): array | |
| 67 | - { | |
| 68 | - return $this->wordpressFilters; | |
| 69 | - } | |
| 70 | 92 | |
| 71 | - 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) | |
| 72 | 101 | { |
| 73 | 102 | return isset($wpQuery->query_vars['suppress_filters']) === true |
| 74 | 103 | && $wpQuery->query_vars['suppress_filters'] === true; |
| 75 | 104 | } |
| 76 | 105 | |
| 77 | - private function addExcludedPosts(mixed $postsNotIn, array $excludedPosts): array | |
| 78 | - { | |
| 79 | - return array_unique(array_merge((array) $postsNotIn, $excludedPosts)); | |
| 80 | - } | |
| 81 | - | |
| 82 | 106 | /** |
| 83 | - * @throws UserGroupTypeException | |
| 107 | + * Manipulates the wordpress query object to filter content. | |
| 108 | + * | |
| 109 | + * @param \WP_Query $wpQuery The wordpress query object. | |
| 84 | 110 | */ |
| 85 | - public function parseQuery(WP_Query $wpQuery): void | |
| 111 | + public function parseQuery($wpQuery) | |
| 86 | 112 | { |
| 87 | 113 | if ($this->filtersSuppressed($wpQuery) === true) { |
| 88 | 114 | $excludedPosts = $this->accessHandler->getExcludedPosts(); |
| 89 | 115 | |
| 90 | 116 | if ($excludedPosts !== []) { |
| 91 | - $wpQuery->query_vars['post__not_in'] = $this->addExcludedPosts( | |
| 92 | - $wpQuery->query_vars['post__not_in'] ?? [], | |
| 93 | - $excludedPosts | |
| 117 | + $postsNotIn = (isset($wpQuery->query_vars['post__not_in']) === true) ? | |
| 118 | + $wpQuery->query_vars['post__not_in'] : []; | |
| 119 | + | |
| 120 | + $wpQuery->query_vars['post__not_in'] = array_unique( | |
| 121 | + array_merge($postsNotIn, $excludedPosts) | |
| 94 | 122 | ); |
| 95 | 123 | } |
| 96 | 124 | } |
| 97 | 125 | } |
| @@ -96,11 +124,15 @@ | ||
| 96 | 124 | } |
| 97 | 125 | } |
| 98 | 126 | |
| 99 | 127 | /** |
| 100 | - * @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 | |
| 101 | 133 | */ |
| 102 | - private function extractOwnFilters(array $filters): bool | |
| 134 | + private function extractOwnFilters(array &$filters) | |
| 103 | 135 | { |
| 104 | 136 | if (isset($filters['the_posts']->callbacks[10]) === true) { |
| 105 | 137 | foreach ($filters['the_posts']->callbacks[10] as $postFilter) { |
| 106 | 138 | if (is_array($postFilter['function']) === true |
| @@ -116,9 +148,19 @@ | ||
| 116 | 148 | |
| 117 | 149 | return false; |
| 118 | 150 | } |
| 119 | 151 | |
| 120 | - 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) | |
| 121 | 163 | { |
| 122 | 164 | if ($this->filtersSuppressed($query) === true) { |
| 123 | 165 | $filters = $this->wordpress->getFilters(); |
| 124 | 166 | |
| @@ -137,9 +179,12 @@ | ||
| 137 | 179 | |
| 138 | 180 | return $posts; |
| 139 | 181 | } |
| 140 | 182 | |
| 141 | - private function restoreFilters(): void | |
| 183 | + /** | |
| 184 | + * Restores the filters to normal. | |
| 185 | + */ | |
| 186 | + private function restoreFilters() | |
| 142 | 187 | { |
| 143 | 188 | if (count($this->wordpressFilters) > 0) { |
| 144 | 189 | $filters = $this->wordpress->getFilters(); |
| 145 | 190 | |
| @@ -151,11 +196,18 @@ | ||
| 151 | 196 | $this->wordpressFilters = []; |
| 152 | 197 | } |
| 153 | 198 | } |
| 154 | 199 | |
| 155 | - 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) | |
| 156 | 208 | { |
| 157 | - if ($post instanceof WP_post) { | |
| 209 | + if ($post instanceof \WP_post) { | |
| 158 | 210 | return $post; |
| 159 | 211 | } elseif (is_int($post) === true) { |
| 160 | 212 | return $this->objectHandler->getPost($post); |
| 161 | 213 | } elseif (isset($post->ID) === true) { |
| @@ -164,9 +216,16 @@ | ||
| 164 | 216 | |
| 165 | 217 | return false; |
| 166 | 218 | } |
| 167 | 219 | |
| 168 | - 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) | |
| 169 | 228 | { |
| 170 | 229 | $uamPostContent = htmlspecialchars_decode($this->mainConfig->getPostTypeContent($post->post_type)); |
| 171 | 230 | |
| 172 | 231 | if ($this->mainConfig->showPostTypeContentBeforeMore($post->post_type) === true |
| @@ -171,9 +230,9 @@ | ||
| 171 | 230 | |
| 172 | 231 | if ($this->mainConfig->showPostTypeContentBeforeMore($post->post_type) === true |
| 173 | 232 | && preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches) |
| 174 | 233 | ) { |
| 175 | - $uamPostContent = explode($matches[0], $post->post_content)[0] . ' ' . $uamPostContent; | |
| 234 | + $uamPostContent = explode($matches[0], $post->post_content)[0].' '.$uamPostContent; | |
| 176 | 235 | } |
| 177 | 236 | |
| 178 | 237 | return stripslashes($uamPostContent); |
| 179 | 238 | } |
| @@ -178,17 +237,21 @@ | ||
| 178 | 237 | return stripslashes($uamPostContent); |
| 179 | 238 | } |
| 180 | 239 | |
| 181 | 240 | /** |
| 182 | - * @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 | |
| 183 | 246 | */ |
| 184 | - private function processPost(WP_Post $post): WP_Post|bool | |
| 247 | + private function processPost(\WP_Post $post) | |
| 185 | 248 | { |
| 186 | 249 | $post->post_title .= $this->adminOutput($post->post_type, $post->ID); |
| 187 | 250 | |
| 188 | 251 | if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === false) { |
| 189 | 252 | if ($this->removePostFromList($post->post_type) === true) { |
| 190 | - return false; | |
| 253 | + return null; | |
| 191 | 254 | } |
| 192 | 255 | |
| 193 | 256 | $post->post_content = $this->processPostContent($post); |
| 194 | 257 | |
| @@ -204,21 +267,16 @@ | ||
| 204 | 267 | return $post; |
| 205 | 268 | } |
| 206 | 269 | |
| 207 | 270 | /** |
| 208 | - * @throws UserGroupTypeException | |
| 271 | + * Filters the raw posts. | |
| 272 | + * | |
| 273 | + * @param array $rawPosts | |
| 274 | + * | |
| 275 | + * @return array | |
| 209 | 276 | */ |
| 210 | - private function getProcessedPost(WP_Post $post): ?WP_Post | |
| 277 | + private function filterRawPosts(array $rawPosts) | |
| 211 | 278 | { |
| 212 | - $post = $this->posts[$post] ??= $this->processPost($post); | |
| 213 | - return $post === false ? null : $post; | |
| 214 | - } | |
| 215 | - | |
| 216 | - /** | |
| 217 | - * @throws UserGroupTypeException | |
| 218 | - */ | |
| 219 | - private function filterRawPosts(array $rawPosts): array | |
| 220 | - { | |
| 221 | 279 | $filteredPosts = []; |
| 222 | 280 | |
| 223 | 281 | foreach ($rawPosts as $rawPost) { |
| 224 | 282 | $post = $this->getPost($rawPost); |
| @@ -223,9 +281,9 @@ | ||
| 223 | 281 | foreach ($rawPosts as $rawPost) { |
| 224 | 282 | $post = $this->getPost($rawPost); |
| 225 | 283 | |
| 226 | 284 | if ($post !== false) { |
| 227 | - $post = $this->getProcessedPost($post); | |
| 285 | + $post = $this->processPost($post); | |
| 228 | 286 | |
| 229 | 287 | if ($post !== null) { |
| 230 | 288 | $filteredPosts[] = $post; |
| 231 | 289 | } |
| @@ -237,14 +295,18 @@ | ||
| 237 | 295 | return $filteredPosts; |
| 238 | 296 | } |
| 239 | 297 | |
| 240 | 298 | /** |
| 241 | - * @throws UserGroupTypeException | |
| 299 | + * The function for the the_posts filter. | |
| 300 | + * | |
| 301 | + * @param array $showPosts The posts. | |
| 302 | + * | |
| 303 | + * @return array | |
| 242 | 304 | */ |
| 243 | - public function showPosts(?array $showPosts = []): ?array | |
| 305 | + public function showPosts($showPosts = []) | |
| 244 | 306 | { |
| 245 | 307 | if ($this->wordpress->isFeed() === false || $this->mainConfig->protectFeed() === true) { |
| 246 | - $showPosts = $this->filterRawPosts((array) $showPosts); | |
| 308 | + $showPosts = $this->filterRawPosts($showPosts); | |
| 247 | 309 | } |
| 248 | 310 | |
| 249 | 311 | $this->restoreFilters(); |
| 250 | 312 | |
| @@ -251,179 +313,32 @@ | ||
| 251 | 313 | return $showPosts; |
| 252 | 314 | } |
| 253 | 315 | |
| 254 | 316 | /** |
| 255 | - * @param WP_Post[] $rawPages The pages. | |
| 256 | - * @throws UserGroupTypeException | |
| 317 | + * The function for the get_pages filter. | |
| 318 | + * | |
| 319 | + * @param \WP_Post[] $rawPages The pages. | |
| 320 | + * | |
| 321 | + * @return array | |
| 257 | 322 | */ |
| 258 | - public function showPages(array $rawPages = []): array | |
| 323 | + public function showPages($rawPages = []) | |
| 259 | 324 | { |
| 260 | 325 | return $this->filterRawPosts($rawPages); |
| 261 | 326 | } |
| 262 | 327 | |
| 263 | - private function getRestAccessDeniedError(): WP_Error | |
| 264 | - { | |
| 265 | - return $this->wordpress->getWpError( | |
| 266 | - 'uam_rest_access_denied', | |
| 267 | - TXT_UAM_REST_ACCESS_DENIED, | |
| 268 | - ['status' => $this->wordpress->isUserLoggedIn() === true ? 403 : 401] | |
| 269 | - ); | |
| 270 | - } | |
| 271 | - | |
| 272 | - private function setRestField(array &$data, string $field, string $value): void | |
| 273 | - { | |
| 274 | - if (array_key_exists($field, $data) === false) { | |
| 275 | - return; | |
| 276 | - } | |
| 277 | - | |
| 278 | - if (is_array($data[$field]) === false) { | |
| 279 | - $data[$field] = $value; | |
| 280 | - | |
| 281 | - return; | |
| 282 | - } | |
| 283 | - | |
| 284 | - $restrictedValues = ['rendered' => $value, 'raw' => $value, 'protected' => false]; | |
| 285 | - | |
| 286 | - foreach ($restrictedValues as $key => $restrictedValue) { | |
| 287 | - if (array_key_exists($key, $data[$field]) === true) { | |
| 288 | - $data[$field][$key] = $restrictedValue; | |
| 289 | - } | |
| 290 | - } | |
| 291 | - } | |
| 292 | - | |
| 293 | 328 | /** |
| 294 | - * @throws UserGroupTypeException | |
| 329 | + * Checks the access of the attached file. | |
| 330 | + * | |
| 331 | + * @param string $file | |
| 332 | + * @param int $attachmentId | |
| 333 | + * | |
| 334 | + * @return string|false | |
| 295 | 335 | */ |
| 296 | - public function restrictRestResponse(mixed $response, mixed $post = null, mixed $request = null): mixed | |
| 336 | + public function getAttachedFile($file, $attachmentId) | |
| 297 | 337 | { |
| 298 | - if (($response instanceof WP_REST_Response) === false | |
| 299 | - || ($post instanceof WP_Post) === false | |
| 300 | - || $this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === true | |
| 301 | - ) { | |
| 302 | - return $response; | |
| 303 | - } | |
| 304 | - | |
| 305 | - $restrictedContent = $this->processPostContent($post); | |
| 306 | - $data = (array) $response->get_data(); | |
| 307 | - | |
| 308 | - $this->setRestField($data, 'content', $restrictedContent); | |
| 309 | - $this->setRestField($data, 'excerpt', $restrictedContent); | |
| 310 | - | |
| 311 | - if ($this->mainConfig->hidePostTypeTitle($post->post_type) === true) { | |
| 312 | - $this->setRestField($data, 'title', $this->mainConfig->getPostTypeTitle($post->post_type)); | |
| 313 | - } | |
| 314 | - | |
| 315 | - $response->set_data($data); | |
| 316 | - | |
| 317 | - return $response; | |
| 318 | - } | |
| 319 | - | |
| 320 | - /** | |
| 321 | - * @throws UserGroupTypeException | |
| 322 | - */ | |
| 323 | - public function excludeRestrictedPostsFromRestQuery(array $queryArgs): array | |
| 324 | - { | |
| 325 | - $excludedPosts = $this->accessHandler->getExcludedPosts(); | |
| 326 | - | |
| 327 | - if ($excludedPosts !== []) { | |
| 328 | - $queryArgs['post__not_in'] = $this->addExcludedPosts($queryArgs['post__not_in'] ?? [], $excludedPosts); | |
| 329 | - } | |
| 330 | - | |
| 331 | - return $queryArgs; | |
| 332 | - } | |
| 333 | - | |
| 334 | - private function getRestBaseToPostTypeMap(): array | |
| 335 | - { | |
| 336 | - if ($this->restBaseToPostTypeMap !== null) { | |
| 337 | - return $this->restBaseToPostTypeMap; | |
| 338 | - } | |
| 339 | - | |
| 340 | - $this->restBaseToPostTypeMap = []; | |
| 341 | - | |
| 342 | - foreach ((array) $this->objectHandler->getPostTypes() as $postType) { | |
| 343 | - $restBase = $this->wordpress->getPostTypeObject($postType)?->rest_base; | |
| 344 | - $this->restBaseToPostTypeMap[empty($restBase) === true ? $postType : $restBase] = $postType; | |
| 345 | - } | |
| 346 | - | |
| 347 | - return $this->restBaseToPostTypeMap; | |
| 348 | - } | |
| 349 | - | |
| 350 | - /** | |
| 351 | - * @return null|array{type: string, id: int, addressesSubResource: bool} | |
| 352 | - */ | |
| 353 | - private function getRestRouteTarget(WP_REST_Request $request): ?array | |
| 354 | - { | |
| 355 | - if (preg_match(self::REST_OBJECT_ROUTE_PATTERN, (string) $request->get_route(), $matches) !== 1) { | |
| 356 | - return null; | |
| 357 | - } | |
| 358 | - | |
| 359 | - $postType = $this->getRestBaseToPostTypeMap()[$matches[1]] ?? null; | |
| 360 | - | |
| 361 | - return $postType === null ? null : [ | |
| 362 | - 'type' => $postType, | |
| 363 | - 'id' => (int) $matches[2], | |
| 364 | - 'addressesSubResource' => ($matches[3] ?? '') !== '' | |
| 365 | - ]; | |
| 366 | - } | |
| 367 | - | |
| 368 | - private function isReadingRestRequest(WP_REST_Request $request): bool | |
| 369 | - { | |
| 370 | - return in_array(strtoupper((string) $request->get_method()), Wordpress::REST_READING_METHODS, true); | |
| 371 | - } | |
| 372 | - | |
| 373 | - private function isEditingRestRoute(bool $addressesSubResource, WP_REST_Request $request): bool | |
| 374 | - { | |
| 375 | - return $this->isReadingRestRequest($request) === false || $addressesSubResource === true; | |
| 376 | - } | |
| 377 | - | |
| 378 | - /** | |
| 379 | - * @throws UserGroupTypeException | |
| 380 | - */ | |
| 381 | - private function hasRestRouteAccess(string $objectType, int $objectId, bool $isEditingRoute): bool | |
| 382 | - { | |
| 383 | - if ($isEditingRoute === true) { | |
| 384 | - return $this->accessHandler->checkObjectAccess($objectType, $objectId, true); | |
| 385 | - } | |
| 386 | - | |
| 387 | - return $this->removePostFromList($objectType) === false | |
| 388 | - || $this->accessHandler->checkObjectAccess($objectType, $objectId); | |
| 389 | - } | |
| 390 | - | |
| 391 | - /** | |
| 392 | - * @throws UserGroupTypeException | |
| 393 | - */ | |
| 394 | - public function restrictRestRequest(mixed $result, mixed $server = null, mixed $request = null): mixed | |
| 395 | - { | |
| 396 | - if (($request instanceof WP_REST_Request) === false) { | |
| 397 | - return $result; | |
| 398 | - } | |
| 399 | - | |
| 400 | - $routeTarget = $this->getRestRouteTarget($request); | |
| 401 | - $isEditingRoute = $routeTarget !== null | |
| 402 | - && $this->isEditingRestRoute($routeTarget['addressesSubResource'], $request); | |
| 403 | - | |
| 404 | - $this->wordpress->setRestRequestContext( | |
| 405 | - $isEditingRoute === true || $request->get_param('context') === 'edit' | |
| 406 | - ); | |
| 407 | - | |
| 408 | - if ($result !== null || $routeTarget === null) { | |
| 409 | - return $result; | |
| 410 | - } | |
| 411 | - | |
| 412 | - ['type' => $type, 'id' => $id] = $routeTarget; | |
| 413 | - | |
| 414 | - return $this->hasRestRouteAccess($type, $id, $isEditingRoute) === true ? | |
| 415 | - $result : $this->getRestAccessDeniedError(); | |
| 416 | - } | |
| 417 | - | |
| 418 | - /** | |
| 419 | - * @throws UserGroupTypeException | |
| 420 | - */ | |
| 421 | - public function getAttachedFile(string $file, int|string|null $attachmentId): bool|string | |
| 422 | - { | |
| 423 | 338 | $isImage = (bool) preg_match('/(?i)\.(jpg|jpeg|jpe|png|gif)$/', $file); |
| 424 | 339 | |
| 425 | - if ($isImage === false && $this->mainConfig->lockFile() === true) { | |
| 340 | + if ($this->mainConfig->lockFile() === true && $isImage === false) { | |
| 426 | 341 | $hasAccess = $this->accessHandler->checkObjectAccess(ObjectHandler::ATTACHMENT_OBJECT_TYPE, $attachmentId); |
| 427 | 342 | return ($hasAccess === true) ? $file : false; |
| 428 | 343 | } |
| 429 | 344 | |
| @@ -430,17 +345,22 @@ | ||
| 430 | 345 | return $file; |
| 431 | 346 | } |
| 432 | 347 | |
| 433 | 348 | /** |
| 434 | - * @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 | |
| 435 | 355 | */ |
| 436 | - private function addQueryExcludedPostFilter(string $query, string $table): string | |
| 356 | + private function addQueryExcludedPostFilter($query, $table) | |
| 437 | 357 | { |
| 438 | 358 | $excludedPosts = $this->accessHandler->getExcludedPosts(); |
| 439 | 359 | |
| 440 | 360 | if ($excludedPosts !== []) { |
| 441 | - $excludedPostsStr = implode(', ', array_map('intval', $excludedPosts)); | |
| 442 | - $query .= " AND $table.ID NOT IN ($excludedPostsStr) "; | |
| 361 | + $excludedPostsStr = implode(', ', $excludedPosts); | |
| 362 | + $query .= " AND {$table}.ID NOT IN ($excludedPostsStr) "; | |
| 443 | 363 | } |
| 444 | 364 | |
| 445 | 365 | return $query; |
| 446 | 366 | } |
| @@ -445,30 +365,48 @@ | ||
| 445 | 365 | return $query; |
| 446 | 366 | } |
| 447 | 367 | |
| 448 | 368 | /** |
| 449 | - * @throws UserGroupTypeException | |
| 369 | + * The function for the posts_where_paged filter. | |
| 370 | + * | |
| 371 | + * @param string $query The where sql statement. | |
| 372 | + * | |
| 373 | + * @return string | |
| 450 | 374 | */ |
| 451 | - public function showPostSql(string $query): string | |
| 375 | + public function showPostSql($query) | |
| 452 | 376 | { |
| 453 | 377 | return $this->addQueryExcludedPostFilter($query, $this->database->getPostsTable()); |
| 454 | 378 | } |
| 455 | 379 | |
| 456 | 380 | /** |
| 457 | - * @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 | |
| 458 | 387 | */ |
| 459 | - public function showNextPreviousPost(string $query): string | |
| 388 | + public function showNextPreviousPost($query) | |
| 460 | 389 | { |
| 461 | 390 | return $this->addQueryExcludedPostFilter($query, 'p'); |
| 462 | 391 | } |
| 463 | 392 | |
| 464 | - 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) | |
| 465 | 403 | { |
| 466 | - $excludedPosts = implode(', ', array_map('intval', $excludedPosts)); | |
| 467 | - $query = "SELECT post_status, COUNT(*) AS num_posts | |
| 468 | - FROM {$this->database->getPostsTable()} | |
| 404 | + $excludedPosts = implode('\', \'', $excludedPosts); | |
| 405 | + $query = "SELECT post_status, COUNT(*) AS num_posts | |
| 406 | + FROM {$this->database->getPostsTable()} | |
| 469 | 407 | WHERE post_type = %s |
| 470 | - AND ID NOT IN ($excludedPosts)"; | |
| 408 | + AND ID NOT IN ('{$excludedPosts}')"; | |
| 471 | 409 | |
| 472 | 410 | if ('readable' === $perm |
| 473 | 411 | && $this->wordpress->isUserLoggedIn() === true |
| 474 | 412 | && $this->wordpress->currentUserCan( |
| @@ -485,11 +423,17 @@ | ||
| 485 | 423 | return $query; |
| 486 | 424 | } |
| 487 | 425 | |
| 488 | 426 | /** |
| 489 | - * @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 | |
| 490 | 434 | */ |
| 491 | - public function showPostCount(stdClass $counts, string $type, string $perm): stdClass | |
| 435 | + public function showPostCount($counts, $type, $perm) | |
| 492 | 436 | { |
| 493 | 437 | if (isset($this->cachedCounts[$type]) === false) { |
| 494 | 438 | $excludedPosts = $this->accessHandler->getExcludedPosts(); |
| 495 | 439 | |
| @@ -494,9 +438,9 @@ | ||
| 494 | 438 | $excludedPosts = $this->accessHandler->getExcludedPosts(); |
| 495 | 439 | |
| 496 | 440 | if ($excludedPosts !== []) { |
| 497 | 441 | $query = $this->getPostCountQuery($excludedPosts, $type, $perm); |
| 498 | - $results = (array) $this->database->getResults( | |
| 442 | + $results = (array)$this->database->getResults( | |
| 499 | 443 | $this->database->prepare($query, $type), |
| 500 | 444 | ARRAY_A |
| 501 | 445 | ); |
| 502 | 446 | |
| @@ -512,9 +456,16 @@ | ||
| 512 | 456 | |
| 513 | 457 | return $this->cachedCounts[$type]; |
| 514 | 458 | } |
| 515 | 459 | |
| 516 | - 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) | |
| 517 | 468 | { |
| 518 | 469 | return $this->mainConfig->lockPostTypeComments($postType) === true |
| 519 | 470 | || $this->mainConfig->hidePostType($postType) === true |
| 520 | 471 | || $this->wordpressConfig->atAdminPanel() === true; |
| @@ -520,12 +471,15 @@ | ||
| 520 | 471 | || $this->wordpressConfig->atAdminPanel() === true; |
| 521 | 472 | } |
| 522 | 473 | |
| 523 | 474 | /** |
| 524 | - * @param WP_Comment[] $comments The comments. | |
| 525 | - * @throws UserGroupTypeException | |
| 475 | + * The function for the comments_array filter. | |
| 476 | + * | |
| 477 | + * @param \WP_Comment[] $comments The comments. | |
| 478 | + * | |
| 479 | + * @return array | |
| 526 | 480 | */ |
| 527 | - public function showComment(array $comments = []): array | |
| 481 | + public function showComment($comments = []) | |
| 528 | 482 | { |
| 529 | 483 | $showComments = []; |
| 530 | 484 | |
| 531 | 485 | foreach ($comments as $comment) { |
| @@ -549,11 +503,16 @@ | ||
| 549 | 503 | return $showComments; |
| 550 | 504 | } |
| 551 | 505 | |
| 552 | 506 | /** |
| 553 | - * @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 | |
| 554 | 513 | */ |
| 555 | - public function showEditLink(?string $link, int|string|null $postId): string | |
| 514 | + public function showEditLink($link, $postId) | |
| 556 | 515 | { |
| 557 | 516 | if ($this->mainConfig->hideEditLinkOnNoAccess() === true |
| 558 | 517 | && $this->accessHandler->checkObjectAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, true) === false |
| 559 | 518 | ) { |
| @@ -574,11 +533,11 @@ | ||
| 574 | 533 | $userGroups |
| 575 | 534 | ); |
| 576 | 535 | |
| 577 | 536 | $link .= $link !== '' ? ' | ' : ' '; |
| 578 | - $link .= TXT_UAM_ASSIGNED_GROUPS . ': ' . implode(', ', $escapedGroups); | |
| 537 | + $link .= TXT_UAM_ASSIGNED_GROUPS.': '.implode(', ', $escapedGroups); | |
| 579 | 538 | } |
| 580 | 539 | } |
| 581 | 540 | |
| 582 | - return (string) $link; | |
| 541 | + return $link; | |
| 583 | 542 | } |
| 584 | 543 | } |