1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\Query; |
6
|
|
|
use GeminiLabs\SiteReviews\Defaults\ReviewDefaults; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
9
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Avatar; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\ReviewHtml; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @property bool $approved This property is mapped to $is_approved |
15
|
|
|
* @property array $assigned_posts |
16
|
|
|
* @property array $assigned_terms |
17
|
|
|
* @property array $assigned_users |
18
|
|
|
* @property string $author |
19
|
|
|
* @property int $author_id |
20
|
|
|
* @property string $avatar |
21
|
|
|
* @property string $content |
22
|
|
|
* @property Arguments $custom |
23
|
|
|
* @property string $date |
24
|
|
|
* @property string $date_gmt |
25
|
|
|
* @property string $name This property is mapped to $author |
26
|
|
|
* @property string $email |
27
|
|
|
* @property bool $has_revisions This property is mapped to $is_modified |
28
|
|
|
* @property int $ID |
29
|
|
|
* @property string $ip_address |
30
|
|
|
* @property bool $is_approved |
31
|
|
|
* @property bool $is_modified |
32
|
|
|
* @property bool $is_pinned |
33
|
|
|
* @property bool $modified This property is mapped to $is_modified |
34
|
|
|
* @property bool $pinned This property is mapped to $is_pinned |
35
|
|
|
* @property int $rating |
36
|
|
|
* @property int $rating_id |
37
|
|
|
* @property string $response |
38
|
|
|
* @property string $status |
39
|
|
|
* @property bool $terms |
40
|
|
|
* @property string $title |
41
|
|
|
* @property string $type |
42
|
|
|
* @property string $url |
43
|
|
|
* @property int $user_id This property is mapped to $author_id |
44
|
|
|
*/ |
45
|
|
|
class Review extends Arguments |
46
|
|
|
{ |
47
|
|
|
/** |
48
|
|
|
* @var Arguments |
49
|
|
|
*/ |
50
|
|
|
protected $_meta; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \WP_Post |
54
|
|
|
*/ |
55
|
|
|
protected $_post; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $has_checked_revisions; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
protected $id; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array|object $values |
69
|
|
|
*/ |
70
|
14 |
|
public function __construct($values) |
71
|
|
|
{ |
72
|
14 |
|
$values = glsr()->args($values); |
73
|
14 |
|
$this->id = Cast::toInt($values->review_id); |
74
|
14 |
|
$args = glsr(ReviewDefaults::class)->restrict($values->toArray()); |
75
|
14 |
|
$args['avatar'] = glsr(Avatar::class)->url($values->avatar); |
76
|
14 |
|
$args['custom'] = $this->custom(); |
77
|
14 |
|
$args['ID'] = $this->id; |
78
|
14 |
|
$args['response'] = $this->meta()->_response; |
79
|
14 |
|
parent::__construct($args); |
80
|
14 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function __call($method, $args) |
86
|
|
|
{ |
87
|
|
|
array_unshift($args, $this); |
88
|
|
|
$result = apply_filters_ref_array(glsr()->id.'/review/call/'.$method, $args); |
89
|
|
|
if (!is_a($result, get_class($this))) { |
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
6 |
|
public function __toString() |
98
|
|
|
{ |
99
|
6 |
|
return (string) $this->build(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function assignedPosts() |
106
|
|
|
{ |
107
|
|
|
if (empty($this->assigned_posts)) { |
108
|
|
|
return $this->assigned_posts; |
109
|
|
|
} |
110
|
|
|
return get_posts([ |
111
|
|
|
'post__in' => $this->assigned_posts, |
112
|
|
|
'post_type' => 'any', |
113
|
|
|
'posts_per_page' => -1, |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
6 |
|
public function assignedTerms() |
121
|
|
|
{ |
122
|
6 |
|
if (empty($this->assigned_terms)) { |
123
|
6 |
|
return $this->assigned_terms; |
124
|
|
|
} |
125
|
|
|
$terms = get_terms(glsr()->taxonomy, ['include' => $this->assigned_terms]); |
126
|
|
|
if (is_wp_error($terms)) { |
127
|
|
|
return $this->assigned_terms; |
128
|
|
|
} |
129
|
|
|
return $terms; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
6 |
|
public function assignedUsers() |
136
|
|
|
{ |
137
|
6 |
|
if (empty($this->assigned_users)) { |
138
|
6 |
|
return $this->assigned_users; |
139
|
|
|
} |
140
|
|
|
return get_users([ |
141
|
|
|
'fields' => ['display_name', 'ID', 'user_email', 'user_nicename', 'user_url'], |
142
|
|
|
'include' => $this->assigned_users, |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
6 |
|
public function author() |
150
|
|
|
{ |
151
|
6 |
|
return Str::convertName($this->get('author'), |
152
|
6 |
|
glsr_get_option('reviews.name.format'), |
153
|
6 |
|
glsr_get_option('reviews.name.initial') |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param int $size |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function avatar($size = null) |
162
|
|
|
{ |
163
|
|
|
return glsr(Avatar::class)->img($this, $size); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return ReviewHtml |
168
|
|
|
*/ |
169
|
6 |
|
public function build(array $args = []) |
170
|
|
|
{ |
171
|
6 |
|
return new ReviewHtml($this, $args); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return Arguments |
176
|
|
|
*/ |
177
|
14 |
|
public function custom() |
178
|
|
|
{ |
179
|
|
|
$custom = array_filter($this->meta()->toArray(), function ($key) { |
180
|
14 |
|
return Str::startsWith('_custom', $key); |
181
|
14 |
|
}, ARRAY_FILTER_USE_KEY); |
182
|
14 |
|
$custom = Arr::unprefixKeys($custom, '_custom_'); |
183
|
14 |
|
$custom = Arr::unprefixKeys($custom, '_'); |
184
|
14 |
|
return glsr()->args($custom); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function date($format = 'F j, Y') |
191
|
|
|
{ |
192
|
|
|
return get_date_from_gmt($this->get('date'), $format); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param int|\WP_Post $post |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public static function isEditable($post) |
200
|
|
|
{ |
201
|
|
|
$postId = Helper::getPostId($post); |
202
|
|
|
return static::isReview($postId) |
203
|
|
|
&& in_array(glsr(Query::class)->review($postId)->type, ['', 'local']); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param \WP_Post|int|false $post |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
5 |
|
public static function isReview($post) |
211
|
|
|
{ |
212
|
5 |
|
return glsr()->post_type === get_post_type($post); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
14 |
|
public function isValid() |
219
|
|
|
{ |
220
|
14 |
|
return !empty($this->id) && !empty($this->get('rating_id')); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return Arguments |
225
|
|
|
*/ |
226
|
14 |
|
public function meta() |
227
|
|
|
{ |
228
|
14 |
|
if (!$this->_meta instanceof Arguments) { |
229
|
14 |
|
$meta = Arr::consolidate(get_post_meta($this->id)); |
230
|
|
|
$meta = array_map(function ($item) { |
231
|
14 |
|
return array_shift($item); |
232
|
14 |
|
}, array_filter($meta)); |
233
|
14 |
|
$meta = array_filter($meta, 'strlen'); |
234
|
14 |
|
$meta = array_map('maybe_unserialize', $meta); |
235
|
14 |
|
$this->_meta = glsr()->args($meta); |
236
|
|
|
} |
237
|
14 |
|
return $this->_meta; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param mixed $key |
242
|
|
|
* @return bool |
243
|
|
|
*/ |
244
|
6 |
|
public function offsetExists($key) |
245
|
|
|
{ |
246
|
6 |
|
return parent::offsetExists($key) || !is_null($this->custom()->$key); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param mixed $key |
251
|
|
|
* @return mixed |
252
|
|
|
*/ |
253
|
14 |
|
public function offsetGet($key) |
254
|
|
|
{ |
255
|
|
|
$alternateKeys = [ |
256
|
14 |
|
'approved' => 'is_approved', |
257
|
|
|
'has_revisions' => 'is_modified', |
258
|
|
|
'modified' => 'is_modified', |
259
|
|
|
'name' => 'author', |
260
|
|
|
'pinned' => 'is_pinned', |
261
|
|
|
'user_id' => 'author_id', |
262
|
|
|
]; |
263
|
14 |
|
if (array_key_exists($key, $alternateKeys)) { |
264
|
|
|
return $this->offsetGet($alternateKeys[$key]); |
265
|
|
|
} |
266
|
14 |
|
if ('is_modified' === $key) { |
267
|
|
|
return $this->hasRevisions(); |
268
|
|
|
} |
269
|
14 |
|
if (is_null($value = parent::offsetGet($key))) { |
270
|
|
|
return $this->custom()->$key; |
271
|
|
|
} |
272
|
14 |
|
return $value; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param mixed $key |
277
|
|
|
* @return void |
278
|
|
|
*/ |
279
|
|
|
public function offsetSet($key, $value) |
280
|
|
|
{ |
281
|
|
|
// This class is read-only, except for custom fields |
282
|
|
|
if ('custom' === $key) { |
283
|
|
|
$value = Arr::consolidate($value); |
284
|
|
|
$value = Arr::prefixKeys($value, '_custom_'); |
285
|
|
|
$meta = wp_parse_args($this->_meta->toArray(), $value); |
286
|
|
|
$this->_meta = glsr()->args($meta); |
287
|
|
|
parent::offsetSet($key, $this->custom()); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param mixed $key |
293
|
|
|
* @return void |
294
|
|
|
*/ |
295
|
|
|
public function offsetUnset($key) |
296
|
|
|
{ |
297
|
|
|
// This class is read-only |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @return \WP_Post|null |
302
|
|
|
*/ |
303
|
|
|
public function post() |
304
|
|
|
{ |
305
|
|
|
if (!$this->_post instanceof \WP_Post) { |
306
|
|
|
$this->_post = get_post($this->id); |
307
|
|
|
} |
308
|
|
|
return $this->_post; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
public function render() |
315
|
|
|
{ |
316
|
|
|
echo $this->build(); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @return string |
321
|
|
|
*/ |
322
|
|
|
public function rating() |
323
|
|
|
{ |
324
|
|
|
return glsr_star_rating($this->get('rating')); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
|
|
public function type() |
331
|
|
|
{ |
332
|
|
|
$type = $this->get('type'); |
333
|
|
|
$reviewTypes = glsr()->retrieveAs('array', 'review_types'); |
334
|
|
|
return Arr::get($reviewTypes, $type, _x('Unknown', 'admin-text', 'site-reviews')); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @return bool |
339
|
|
|
*/ |
340
|
|
|
protected function hasRevisions() |
341
|
|
|
{ |
342
|
|
|
if (!$this->has_checked_revisions) { |
343
|
|
|
$modified = glsr(Query::class)->hasRevisions($this->ID); |
344
|
|
|
$this->set('is_modified', $modified); |
345
|
|
|
$this->has_checked_revisions = true; |
346
|
|
|
} |
347
|
|
|
return $this->get('is_modified'); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|