| 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 |
|