|
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; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
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
|
24 |
|
|
|
19
|
|
|
class VerificationController extends AbstractController |
|
20
|
24 |
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @action post_submitbox_misc_actions |
|
23
|
24 |
|
*/ |
|
24
|
|
|
public function renderVerifyAction(\WP_Post $post): void |
|
25
|
|
|
{ |
|
26
|
24 |
|
if (!Review::isReview($post)) { |
|
27
|
24 |
|
return; |
|
28
|
24 |
|
} |
|
29
|
24 |
|
$review = glsr(ReviewManager::class)->get($post->ID); |
|
30
|
|
|
if (!$review->isValid()) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
$text = empty(glsr(Database::class)->meta($review->ID, 'verified_requested')) |
|
34
|
|
|
? esc_html_x('Send Verification Request', 'admin-text', 'site-reviews') |
|
35
|
|
|
: esc_html_x('Resend 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 = 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 = 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
|
|
|
$command = $this->execute(new ToggleVerified($request)); |
|
79
|
|
|
glsr()->action('cache/flush', $command->review); // @phpstan-ignore-line |
|
80
|
|
|
wp_send_json_success($command->response()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @action site-reviews/route/ajax/verified-review |
|
85
|
|
|
*/ |
|
86
|
|
|
public function verifiedReviewAjax(Request $request): void |
|
87
|
|
|
{ |
|
88
|
|
|
$reviewId = $request->cast('review_id', 'int'); |
|
89
|
|
|
$token = sanitize_text_field($request->get('verified')); |
|
90
|
|
|
$token = (int) glsr(Encryption::class)->decrypt($token); |
|
91
|
|
|
if (empty($reviewId) || $reviewId !== $token) { |
|
92
|
|
|
wp_send_json_error(); |
|
93
|
|
|
} |
|
94
|
|
|
$review = glsr_get_review($reviewId); |
|
95
|
|
|
if ($review->isValid()) { |
|
96
|
|
|
$html = $review->build($request->toArray()); |
|
97
|
|
|
$message = $review->is_approved |
|
98
|
|
|
? __('Thank you, your review has been verified.', 'site-reviews') |
|
99
|
|
|
: __('Thank you, your review has been verified and is awaiting approval.', 'site-reviews'); |
|
100
|
|
|
wp_send_json_success([ |
|
101
|
|
|
'attributes' => $html->attributes(), |
|
102
|
|
|
'message' => $message, |
|
103
|
|
|
'review' => (string) $html, |
|
104
|
|
|
]); |
|
105
|
|
|
} |
|
106
|
|
|
wp_send_json_error(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @action site-reviews/route/get/public/verify |
|
111
|
|
|
*/ |
|
112
|
|
|
public function verifyReview(Request $request): void |
|
113
|
|
|
{ |
|
114
|
|
|
$postId = Arr::getAs('int', $request->data, 0); |
|
115
|
|
|
$redirectUrl = get_home_url(); |
|
116
|
|
|
$review = glsr_get_review($postId); |
|
117
|
|
|
if ($review->isValid()) { |
|
118
|
|
|
$command = $this->execute(new VerifyReview($review)); |
|
119
|
|
|
$path = Arr::get($request->data, 1); |
|
120
|
|
|
$redirectUrl .= $path; |
|
121
|
|
|
$redirectUrl = add_query_arg('review_id', $review->ID, $redirectUrl); |
|
122
|
|
|
if ($command->successful()) { |
|
123
|
|
|
glsr()->action('cache/flush', $review); |
|
124
|
|
|
$token = glsr(Encryption::class)->encrypt($review->ID); |
|
125
|
|
|
$redirectUrl = add_query_arg('verified', $token, $redirectUrl); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
wp_redirect($redirectUrl); |
|
129
|
|
|
exit; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|