PluginProbe
Site Reviews / trunk
Site Reviews vtrunk
8.3.1 8.3.0 8.2.2 8.2.1 8.2.0 8.1.0 8.0.13 8.0.12 8.0.11 trunk 1.2.2 2.17.1 3.5.4 4.7.0 5.25.1 6.11.8 7.0.10 7.0.11 7.0.12 7.0.13 7.0.14 7.0.15 7.0.16 7.0.17 7.0.18 All 54 releases
site-reviews / plugin / Controllers / UserController.php

UserController.php in Site Reviews trunk, at plugin/Controllers/UserController.php

73 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GeminiLabs\SiteReviews\Controllers;
4
5 use GeminiLabs\SiteReviews\Helpers\Arr;
6 use GeminiLabs\SiteReviews\Role;
7
8 class UserController extends AbstractController
9 {
10 /**
11 * @param string[] $caps
12 *
13 * @return string[]
14 *
15 * @filter map_meta_cap
16 */
17 public function filterMapMetaCap(array $caps, string $cap, int $userId, array $args): array
18 {
19 if ('respond_to_'.glsr()->post_type !== $cap) {
20 return $caps;
21 }
22 $review = glsr_get_review(Arr::get($args, 0));
23 if (!$review->isValid()) {
24 return ['do_not_allow'];
25 }
26 $caps = [];
27 $respondToReviews = glsr(Role::class)->capability('respond_to_posts');
28 if ($userId == $review->author_id) {
29 $caps[] = $respondToReviews; // they are the author of the review
30 }
31 foreach ($review->assignedPosts() as $assignedPost) {
32 if ($userId == $assignedPost->post_author) {
33 $caps[] = $respondToReviews; // they are the author of the post that the review is assigned to
34 break;
35 }
36 }
37 if (!in_array($respondToReviews, $caps)) {
38 $caps[] = glsr(Role::class)->capability('respond_to_others_posts');
39 }
40 return array_unique($caps);
41 }
42
43 /**
44 * @param bool[] $allcaps
45 * @param string[] $caps
46 *
47 * @return bool[]
48 *
49 * @filter user_has_cap
50 */
51 public function filterUserHasCap(array $allcaps, array $caps, array $args): array
52 {
53 $capability = Arr::get($args, 0);
54 if (!in_array($capability, ['assign_post', 'unassign_post'])) {
55 return $allcaps;
56 }
57 $postId = Arr::getAs('int', $args, 2);
58 $status = get_post_status_object((string) get_post_status($postId));
59 if (!$status) {
60 return $allcaps;
61 }
62 $allcaps[$capability] = true;
63 if (!$status->public && !$status->private) {
64 unset($allcaps[$capability]);
65 } elseif ('private' === $status->name && !current_user_can('read_post', $postId)) {
66 unset($allcaps[$capability]);
67 } elseif (post_password_required($postId) && !current_user_can('edit_post', $postId)) {
68 unset($allcaps[$capability]);
69 }
70 return $allcaps;
71 }
72 }
73