|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\CommandContract as Contract; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\ReviewManager; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Defaults\CustomFieldsDefaults; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Avatar; |
|
12
|
|
|
use GeminiLabs\SiteReviews\Modules\Notification; |
|
13
|
|
|
use GeminiLabs\SiteReviews\Modules\Validator\DefaultValidator; |
|
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Validator\ValidateReview; |
|
15
|
|
|
use GeminiLabs\SiteReviews\Request; |
|
16
|
|
|
|
|
17
|
|
|
class CreateReview implements Contract |
|
18
|
|
|
{ |
|
19
|
|
|
public $assigned_posts; |
|
20
|
|
|
public $assigned_terms; |
|
21
|
|
|
public $assigned_users; |
|
22
|
|
|
public $avatar; |
|
23
|
|
|
public $blacklisted; |
|
24
|
|
|
public $content; |
|
25
|
|
|
public $custom; |
|
26
|
|
|
public $date; |
|
27
|
|
|
public $email; |
|
28
|
|
|
public $form_id; |
|
29
|
|
|
public $ip_address; |
|
30
|
|
|
public $is_pinned; |
|
31
|
|
|
public $name; |
|
32
|
|
|
public $post_id; |
|
33
|
|
|
public $rating; |
|
34
|
|
|
public $referer; |
|
35
|
|
|
public $request; |
|
36
|
|
|
public $response; |
|
37
|
|
|
public $title; |
|
38
|
|
|
public $type; |
|
39
|
|
|
public $url; |
|
40
|
|
|
|
|
41
|
|
|
protected $errors; |
|
42
|
|
|
protected $message; |
|
43
|
|
|
protected $recaptcha; |
|
44
|
|
|
protected $review; |
|
45
|
|
|
|
|
46
|
14 |
|
public function __construct(Request $request) |
|
47
|
|
|
{ |
|
48
|
14 |
|
$request->set('ip_address', Helper::getIpAddress()); // required for Akismet and Blacklist validation |
|
49
|
14 |
|
$this->request = $request; |
|
50
|
14 |
|
$this->sanitize(); |
|
51
|
14 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return static |
|
55
|
|
|
*/ |
|
56
|
7 |
|
public function handle() |
|
57
|
|
|
{ |
|
58
|
7 |
|
if ($this->validate()) { |
|
59
|
6 |
|
$this->create(); |
|
60
|
|
|
} |
|
61
|
7 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function referer() |
|
68
|
|
|
{ |
|
69
|
|
|
if ($referer = $this->redirect($this->referer)) { |
|
70
|
|
|
return $referer; |
|
71
|
|
|
} |
|
72
|
|
|
glsr_log()->warning('The form referer ($_SERVER[REQUEST_URI]) was empty.')->debug($this); |
|
73
|
|
|
return home_url(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
7 |
|
public function response() |
|
80
|
|
|
{ |
|
81
|
|
|
return [ |
|
82
|
7 |
|
'errors' => $this->errors, |
|
83
|
7 |
|
'html' => (string) $this->review, |
|
84
|
7 |
|
'message' => $this->message, |
|
85
|
7 |
|
'recaptcha' => $this->recaptcha, |
|
86
|
7 |
|
'redirect' => $this->redirect(), |
|
87
|
7 |
|
'review' => Cast::toArray($this->review), |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
7 |
|
public function success() |
|
95
|
|
|
{ |
|
96
|
7 |
|
if (false === $this->errors) { |
|
97
|
6 |
|
glsr()->sessionClear(); |
|
98
|
6 |
|
return true; |
|
99
|
|
|
} |
|
100
|
7 |
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
13 |
|
public function toArray() |
|
107
|
|
|
{ |
|
108
|
13 |
|
$values = get_object_vars($this); |
|
109
|
13 |
|
$values = glsr()->filterArray('create/review-values', $values, $this); |
|
110
|
13 |
|
return glsr(CreateReviewDefaults::class)->merge($values); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return bool |
|
115
|
|
|
*/ |
|
116
|
7 |
|
public function validate() |
|
117
|
|
|
{ |
|
118
|
7 |
|
$validator = glsr(ValidateReview::class)->validate($this->request); |
|
119
|
7 |
|
$this->blacklisted = $validator->blacklisted; |
|
120
|
7 |
|
$this->errors = $validator->errors; |
|
121
|
7 |
|
$this->message = $validator->message; |
|
122
|
7 |
|
$this->recaptcha = $validator->recaptcha; |
|
123
|
7 |
|
return $validator->isValid(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* This only validates the provided values in the Request |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function isValid() |
|
131
|
|
|
{ |
|
132
|
|
|
return glsr(DefaultValidator::class, ['request' => $this->request])->isValidRequest(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
14 |
|
protected function avatar() |
|
139
|
|
|
{ |
|
140
|
14 |
|
if (!empty($this->avatar)) { |
|
141
|
|
|
return $this->avatar; |
|
142
|
|
|
} |
|
143
|
14 |
|
$userField = empty($this->email) |
|
144
|
7 |
|
? get_current_user_id() |
|
145
|
14 |
|
: $this->email; |
|
146
|
14 |
|
return glsr(Avatar::class)->generate($userField); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
6 |
|
protected function create() |
|
153
|
|
|
{ |
|
154
|
6 |
|
if ($this->review = glsr(ReviewManager::class)->create($this)) { |
|
155
|
6 |
|
$this->message = __('Your review has been submitted!', 'site-reviews'); |
|
156
|
6 |
|
glsr(Notification::class)->send($this->review); |
|
157
|
6 |
|
return; |
|
158
|
|
|
} |
|
159
|
|
|
$this->errors = []; |
|
160
|
|
|
$this->message = __('Your review could not be submitted and the error has been logged. Please notify the site admin.', 'site-reviews'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
14 |
|
protected function custom() |
|
167
|
|
|
{ |
|
168
|
14 |
|
return glsr(CustomFieldsDefaults::class)->filter($this->request->toArray()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
7 |
|
protected function redirect($fallback = '') |
|
175
|
|
|
{ |
|
176
|
7 |
|
$redirect = trim(strval(get_post_meta($this->post_id, 'redirect_to', true))); |
|
177
|
7 |
|
$redirect = glsr()->filterString('review/redirect', $redirect, $this); |
|
178
|
7 |
|
if (empty($redirect)) { |
|
179
|
7 |
|
$redirect = $fallback; |
|
180
|
|
|
} |
|
181
|
7 |
|
return sanitize_text_field($redirect); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return void |
|
186
|
|
|
*/ |
|
187
|
14 |
|
protected function sanitize() |
|
188
|
|
|
{ |
|
189
|
14 |
|
$values = glsr(CreateReviewDefaults::class)->restrict($this->request->toArray()); |
|
190
|
14 |
|
foreach ($values as $key => $value) { |
|
191
|
14 |
|
if (property_exists($this, $key)) { |
|
192
|
14 |
|
$this->{$key} = $value; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
14 |
|
$this->avatar = $this->avatar(); |
|
196
|
14 |
|
$this->custom = $this->custom(); |
|
197
|
14 |
|
$this->type = $this->type(); |
|
198
|
14 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return string |
|
202
|
|
|
*/ |
|
203
|
14 |
|
protected function type() |
|
204
|
|
|
{ |
|
205
|
14 |
|
return array_key_exists($this->type, glsr()->reviewTypes) ? $this->type : 'local'; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|