| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\SiteReviews\Controllers; |
| 4 |
|
| 5 |
use GeminiLabs\SiteReviews\Commands\CreateReview; |
| 6 |
use GeminiLabs\SiteReviews\Commands\SendVerificationEmail; |
| 7 |
use GeminiLabs\SiteReviews\Commands\ToggleVerified; |
| 8 |
use GeminiLabs\SiteReviews\Commands\VerifyReview; |
| 9 |
use GeminiLabs\SiteReviews\Database\OptionManager; |
| 10 |
use GeminiLabs\SiteReviews\Database\PostMeta; |
| 11 |
use GeminiLabs\SiteReviews\Database\ReviewManager; |
| 12 |
use GeminiLabs\SiteReviews\Helpers\Arr; |
| 13 |
use GeminiLabs\SiteReviews\Helpers\Cast; |
| 14 |
use GeminiLabs\SiteReviews\Modules\Encryption; |
| 15 |
use GeminiLabs\SiteReviews\Modules\Html\Template; |
| 16 |
use GeminiLabs\SiteReviews\Request; |
| 17 |
use GeminiLabs\SiteReviews\Review; |
| 18 |
|
| 19 |
class VerificationController extends AbstractController |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @action post_submitbox_misc_actions |
| 23 |
*/ |
| 24 |
public function renderVerifyAction(\WP_Post $post): void |
| 25 |
{ |
| 26 |
if (!Review::isReview($post)) { |
| 27 |
return; |
| 28 |
} |
| 29 |
$review = glsr(ReviewManager::class)->get($post->ID); |
| 30 |
if (!$review->isValid()) { |
| 31 |
return; |
| 32 |
} |
| 33 |
$text = glsr(PostMeta::class)->get($review->ID, 'verified_requested', 'bool') |
| 34 |
? esc_html_x('Resend Verification Request', 'admin-text', 'site-reviews') |
| 35 |
: esc_html_x('Send Verification Request', 'admin-text', 'site-reviews'); |
| 36 |
glsr(Template::class)->render('partials/editor/verified', [ |
| 37 |
'is_verification_enabled' => glsr(OptionManager::class)->getBool('settings.general.request_verification', false), |
| 38 |
'is_verified' => $review->is_verified, |
| 39 |
'text' => $text, |
| 40 |
]); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @action site-reviews/route/ajax/request-verification |
| 45 |
*/ |
| 46 |
public function resendVerificationEmailAjax(Request $request): void |
| 47 |
{ |
| 48 |
$review = glsr(ReviewManager::class)->get($request->cast('post_id', 'int')); |
| 49 |
$pathPostId = $review->meta()->cast('_submitted._post_id', 'int'); |
| 50 |
$path = Cast::toString(wp_parse_url((string) get_permalink($pathPostId), \PHP_URL_PATH)); |
| 51 |
$command = $this->execute(new SendVerificationEmail($review, $review->verifyUrl($path))); |
| 52 |
wp_send_json_success($command->response()); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @action site-reviews/review/created |
| 57 |
*/ |
| 58 |
public function sendVerificationEmail(Review $review, CreateReview $command): void |
| 59 |
{ |
| 60 |
if (defined('WP_IMPORTING')) { |
| 61 |
return; |
| 62 |
} |
| 63 |
if (!in_array($review->status, ['pending', 'publish'])) { |
| 64 |
return; // this review is likely a draft made in the wp-admin |
| 65 |
} |
| 66 |
$path = Cast::toString(wp_parse_url((string) get_permalink($command->post_id), \PHP_URL_PATH)); |
| 67 |
$verifyUrl = $review->verifyUrl($path); |
| 68 |
if (!empty($verifyUrl)) { |
| 69 |
$this->execute(new SendVerificationEmail($review, $verifyUrl)); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @action site-reviews/route/ajax/toggle-verified |
| 75 |
*/ |
| 76 |
public function toggleVerifiedAjax(Request $request): void |
| 77 |
{ |
| 78 |
$this->execute(new ToggleVerified($request))->sendJsonResponse(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @action site-reviews/route/ajax/verified-review |
| 83 |
*/ |
| 84 |
public function verifiedReviewAjax(Request $request): void |
| 85 |
{ |
| 86 |
$reviewId = $request->cast('review_id', 'int'); |
| 87 |
$token = sanitize_text_field($request->get('verified')); |
| 88 |
$token = (int) glsr(Encryption::class)->decrypt($token); |
| 89 |
if (empty($reviewId) || $reviewId !== $token) { |
| 90 |
wp_send_json_error(); |
| 91 |
} |
| 92 |
$review = glsr_get_review($reviewId); |
| 93 |
if ($review->isValid()) { |
| 94 |
$html = $review->build($request->toArray()); |
| 95 |
$message = $review->is_approved |
| 96 |
? __('Thank you, your review has been verified.', 'site-reviews') |
| 97 |
: __('Thank you, your review has been verified and is awaiting approval.', 'site-reviews'); |
| 98 |
wp_send_json_success([ |
| 99 |
'attributes' => $html->attributes(), |
| 100 |
'message' => $message, |
| 101 |
'review' => (string) $html, |
| 102 |
]); |
| 103 |
} |
| 104 |
wp_send_json_error(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* @action site-reviews/route/get/public/verify |
| 109 |
*/ |
| 110 |
public function verifyReview(Request $request): void |
| 111 |
{ |
| 112 |
$postId = Arr::getAs('int', $request->data, 0); |
| 113 |
$redirectUrl = get_home_url(); |
| 114 |
$review = glsr_get_review($postId); |
| 115 |
if ($review->isValid()) { |
| 116 |
$command = $this->execute(new VerifyReview($review)); |
| 117 |
$path = Arr::get($request->data, 1); |
| 118 |
$redirectUrl .= $path; |
| 119 |
$redirectUrl = add_query_arg('review_id', $review->ID, $redirectUrl); |
| 120 |
if ($command->successful()) { |
| 121 |
$token = glsr(Encryption::class)->encrypt($review->ID); |
| 122 |
$redirectUrl = add_query_arg('verified', $token, $redirectUrl); |
| 123 |
} |
| 124 |
} |
| 125 |
wp_safe_redirect($redirectUrl); |
| 126 |
glsr_exit(); |
| 127 |
} |
| 128 |
} |
| 129 |
|