1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Trustalyze; |
11
|
|
|
use GeminiLabs\SiteReviews\Review; |
12
|
|
|
|
13
|
|
|
class TrustalyzeController extends Controller |
14
|
|
|
{ |
15
|
|
|
protected $apiKey = 'settings.general.trustalyze_serial'; |
16
|
|
|
protected $emailKey = 'settings.general.trustalyze_email'; |
17
|
|
|
protected $enabledKey = 'settings.general.trustalyze'; |
18
|
|
|
protected $trustalyzeKey = '_glsr_trustalyze'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return array |
22
|
|
|
* @filter site-reviews/settings/callback |
23
|
|
|
*/ |
24
|
|
|
public function filterSettingsCallback(array $settings) |
25
|
|
|
{ |
26
|
|
|
if ('yes' !== Arr::get($settings, $this->enabledKey)) { |
27
|
|
|
return $settings; |
28
|
|
|
} |
29
|
|
|
$isApiKeyModified = $this->isEmptyOrModified($this->apiKey, $settings); |
30
|
|
|
$isEmailModified = $this->isEmptyOrModified($this->emailKey, $settings); |
31
|
|
|
$isAccountVerified = glsr(OptionManager::class)->getWP($this->trustalyzeKey, false); |
32
|
|
|
if (!$isAccountVerified || $isApiKeyModified || $isEmailModified) { |
33
|
|
|
$settings = $this->sanitizeTrustalyzeSettings($settings); |
34
|
|
|
} |
35
|
|
|
return $settings; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $template |
40
|
|
|
* @return array |
41
|
|
|
* @filter site-reviews/interpolate/partials/form/table-row-multiple |
42
|
|
|
*/ |
43
|
|
|
public function filterSettingsTableRow(array $context, $template, array $data) |
44
|
|
|
{ |
45
|
|
|
if ($this->enabledKey !== Arr::get($data, 'field.path')) { |
46
|
|
|
return $context; |
47
|
|
|
} |
48
|
|
|
$trustalyzeProductType = glsr(OptionManager::class)->getWP($this->trustalyzeKey); |
49
|
|
|
if ('P' === $trustalyzeProductType) { |
50
|
|
|
return $context; |
51
|
|
|
} |
52
|
|
|
if ('F' === $trustalyzeProductType && 'yes' === glsr_get_option('general.trustalyze')) { |
53
|
|
|
$button = $this->buildUpgradeButton(); |
54
|
|
|
} else { |
55
|
|
|
$button = $this->buildCreateButton(); |
56
|
|
|
} |
57
|
|
|
$context['field'].= $button; |
58
|
|
|
return $context; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Triggered when a review is created. |
63
|
|
|
* @return void |
64
|
|
|
* @action site-reviews/review/created |
65
|
|
|
*/ |
66
|
1 |
|
public function onCreated(Review $review) |
67
|
|
|
{ |
68
|
1 |
|
if (!$this->canPostReview($review)) { |
69
|
1 |
|
return; |
70
|
|
|
} |
71
|
|
|
$trustalyze = glsr(Trustalyze::class)->sendReview($review); |
72
|
|
|
if ($trustalyze->success) { |
73
|
|
|
glsr(Database::class)->set($review->ID, 'trustalyze', $trustalyze->review_id); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Triggered when a review is reverted to its original title/content/date_timestamp. |
79
|
|
|
* @return void |
80
|
|
|
* @action site-reviews/review/reverted |
81
|
|
|
*/ |
82
|
|
|
public function onReverted(Review $review) |
83
|
|
|
{ |
84
|
|
|
if (!$this->canPostReview($review)) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
$trustalyze = glsr(Trustalyze::class)->sendReview($review); |
88
|
|
|
if ($trustalyze->success) { |
89
|
|
|
glsr(Database::class)->set($review->ID, 'trustalyze', $trustalyze->review_id); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Triggered when an existing review is updated. |
95
|
|
|
* @return void |
96
|
|
|
* @action site-reviews/review/saved |
97
|
|
|
*/ |
98
|
|
|
public function onSaved(Review $review) |
99
|
|
|
{ |
100
|
|
|
if (!$this->canPostReview($review)) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
$trustalyze = glsr(Trustalyze::class)->sendReview($review); |
104
|
|
|
if ($trustalyze->success) { |
105
|
|
|
glsr(Database::class)->set($review->ID, 'trustalyze', $trustalyze->review_id); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Triggered when a review's response is added or updated. |
111
|
|
|
* @param int $metaId |
112
|
|
|
* @param int $postId |
113
|
|
|
* @param string $metaKey |
114
|
|
|
* @return void |
115
|
|
|
* @action updated_postmeta |
116
|
|
|
*/ |
117
|
|
|
public function onUpdatedMeta($metaId, $postId, $metaKey) |
118
|
|
|
{ |
119
|
|
|
$review = glsr_get_review($postId); |
120
|
|
|
if (!$this->canPostResponse($review) || '_response' !== $metaKey) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
$trustalyze = glsr(Trustalyze::class)->sendReviewResponse($review); |
124
|
|
|
if ($trustalyze->success) { |
125
|
|
|
glsr(Database::class)->set($review->ID, 'trustalyze_response', true); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
protected function buildCreateButton() |
133
|
|
|
{ |
134
|
|
|
return glsr(Builder::class)->a(__('Create Your Trustalyze Account', 'site-reviews'), [ |
135
|
|
|
'class' => 'button', |
136
|
|
|
'href' => Trustalyze::WEB_URL, |
137
|
|
|
'target' => '_blank', |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function buildUpgradeButton() |
145
|
|
|
{ |
146
|
|
|
$build = glsr(Builder::class); |
147
|
|
|
$notice = $build->p(__('Free Trustalyze accounts are limited to 500 blockchain transactions per year.', 'site-reviews')); |
148
|
|
|
$button = $build->a(__('Upgrade Your Trustalyze Plan', 'site-reviews'), [ |
149
|
|
|
'class' => 'button', |
150
|
|
|
'href' => Trustalyze::WEB_URL, |
151
|
|
|
'target' => '_blank', |
152
|
|
|
]); |
153
|
|
|
return $build->div($notice.$button, [ |
154
|
|
|
'class' => 'glsr-notice-inline notice inline notice-info', |
155
|
|
|
]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
protected function canPostResponse(Review $review) |
162
|
|
|
{ |
163
|
|
|
$requiredValues = [ |
164
|
|
|
glsr(Database::class)->get($review->ID, 'trustalyze'), |
165
|
|
|
$review->response, |
166
|
|
|
$review->review_id, |
167
|
|
|
]; |
168
|
|
|
return $this->canProceed($review, 'trustalyze_response') |
169
|
|
|
&& 'publish' === $review->status |
170
|
|
|
&& 3 === count(array_filter($requiredValues)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
1 |
|
protected function canPostReview(Review $review) |
177
|
|
|
{ |
178
|
|
|
$requiredValues = [ |
179
|
1 |
|
$review->author, |
180
|
1 |
|
$review->content, |
181
|
1 |
|
$review->rating, |
182
|
1 |
|
$review->review_id, |
183
|
1 |
|
$review->title, |
184
|
|
|
]; |
185
|
1 |
|
return $this->canProceed($review) |
186
|
1 |
|
&& 'publish' === $review->status |
187
|
1 |
|
&& 5 === count(array_filter($requiredValues)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $metaKey |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
1 |
|
protected function canProceed(Review $review, $metaKey = 'trustalyze') |
195
|
|
|
{ |
196
|
1 |
|
return glsr(OptionManager::class)->getBool($this->enabledKey) |
197
|
1 |
|
&& $this->isReviewPostId($review->ID) |
198
|
1 |
|
&& !$this->hasMetaKey($review, $metaKey); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $metaKey |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
protected function hasMetaKey(Review $review, $metaKey = 'trustalyze') |
206
|
|
|
{ |
207
|
|
|
return '' !== glsr(Database::class)->get($review->ID, $metaKey); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $key |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
protected function isEmptyOrModified($key, array $settings) |
215
|
|
|
{ |
216
|
|
|
$oldValue = glsr_get_option($key); |
217
|
|
|
$newValue = Arr::get($settings, $key); |
218
|
|
|
return empty($newValue) || $newValue !== $oldValue; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
protected function sanitizeTrustalyzeSettings(array $settings) |
225
|
|
|
{ |
226
|
|
|
$trustalyze = glsr(Trustalyze::class)->activateKey( |
227
|
|
|
Arr::get($settings, $this->apiKey), |
228
|
|
|
Arr::get($settings, $this->emailKey) |
229
|
|
|
); |
230
|
|
|
if ($trustalyze->success) { |
231
|
|
|
update_option($this->trustalyzeKey, Arr::get($trustalyze->response, 'producttype')); |
232
|
|
|
} else { |
233
|
|
|
delete_option($this->trustalyzeKey); |
234
|
|
|
$settings = Arr::set($settings, $this->enabledKey, 'no'); |
235
|
|
|
glsr(Notice::class)->addError(sprintf( |
236
|
|
|
__('Your Trustalyze account details could not be verified, please try again. %s', 'site-reviews'), |
237
|
|
|
'('.$trustalyze->message.')' |
238
|
|
|
)); |
239
|
|
|
} |
240
|
|
|
return $settings; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|