1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html\Partials; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database; |
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
7
|
|
|
use GeminiLabs\SiteReviews\Helper; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Date; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Partial; |
12
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
13
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Schema; |
15
|
|
|
use WP_Post; |
16
|
|
|
|
17
|
|
|
class SiteReviews |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $args; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $current; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $options; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var object |
36
|
|
|
*/ |
37
|
|
|
protected $reviews; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return void|string |
41
|
|
|
*/ |
42
|
|
|
public function build( array $args = [] ) |
43
|
|
|
{ |
44
|
|
|
$this->args = $args; |
45
|
|
|
$this->options = glsr( Helper::class )->flattenArray( glsr( OptionManager::class )->all() ); |
46
|
|
|
$this->reviews = glsr( Database::class )->getReviews( $args ); |
47
|
|
|
$this->generateSchema(); |
48
|
|
|
$navigation = wp_validate_boolean( $this->args['pagination'] ) |
49
|
|
|
? glsr( Partial::class )->build( 'pagination', ['total' => $this->reviews->max_num_pages] ) |
50
|
|
|
: ''; |
51
|
|
|
return glsr( Template::class )->build( 'templates/reviews', [ |
52
|
|
|
'context' => [ |
53
|
|
|
'class' => $this->getClass(), |
54
|
|
|
'id' => $this->args['id'], |
55
|
|
|
'navigation' => $navigation, |
56
|
|
|
], |
57
|
|
|
'reviews' => $this->buildReviews(), |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function buildReviews() |
65
|
|
|
{ |
66
|
|
|
$reviews = []; |
67
|
|
|
foreach( $this->reviews->results as $index => $review ) { |
68
|
|
|
$this->current = $index; |
69
|
|
|
$review = apply_filters( 'site-reviews/review/build/before', $review ); |
70
|
|
|
$review = $this->buildReview( $review ); |
71
|
|
|
$review = apply_filters( 'site-reviews/review/build/after', $review ); |
72
|
|
|
$reviews[] = $review; |
73
|
|
|
} |
74
|
|
|
return $reviews; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param object $review |
79
|
|
|
* @return object |
80
|
|
|
*/ |
81
|
|
|
protected function buildReview( $review ) |
82
|
|
|
{ |
83
|
|
|
$generatedReview = []; |
84
|
|
|
foreach( $review as $key => $value ) { |
85
|
|
|
$method = glsr( Helper::class )->buildMethodName( $key, 'buildOption' ); |
86
|
|
|
if( !method_exists( $this, $method ))continue; |
87
|
|
|
$generatedReview[$key] = $this->$method( $key, $value ); |
88
|
|
|
} |
89
|
|
|
return (object)$generatedReview; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $key |
94
|
|
|
* @param string $value |
95
|
|
|
* @return void|string |
96
|
|
|
*/ |
97
|
|
|
protected function buildOptionAssignedTo( $key, $value ) |
98
|
|
|
{ |
99
|
|
|
if( $this->isHiddenOrEmpty( $key, 'settings.reviews.assigned_links.enabled' ))return; |
100
|
|
|
$post = get_post( intval( $value )); |
101
|
|
|
if( !( $post instanceof WP_Post ))return; |
102
|
|
|
$permalink = glsr( Builder::class )->a( get_the_title( $post->ID ), [ |
103
|
|
|
'href' => get_the_permalink( $post->ID ), |
104
|
|
|
]); |
105
|
|
|
$assignedTo = sprintf( __( 'Review of %s', 'site-reviews' ), $permalink ); |
106
|
|
|
return $this->wrap( $key, '<span>'.$assignedTo.'</span>' ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $key |
111
|
|
|
* @param string $value |
112
|
|
|
* @return void|string |
113
|
|
|
*/ |
114
|
|
|
protected function buildOptionAuthor( $key, $value ) |
115
|
|
|
{ |
116
|
|
|
if( $this->isHidden( $key ))return; |
117
|
|
|
$prefix = !$this->isOptionEnabled( 'settings.reviews.avatars.enabled' ) |
118
|
|
|
? apply_filters( 'site-reviews/review/author/prefix', '—' ) |
119
|
|
|
: ''; |
120
|
|
|
return $this->wrap( $key, $prefix.'<span>'.$value.'</span>' ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $key |
125
|
|
|
* @param string $value |
126
|
|
|
* @return void|string |
127
|
|
|
*/ |
128
|
|
|
protected function buildOptionAvatar( $key, $value ) |
129
|
|
|
{ |
130
|
|
|
if( $this->isHidden( $key, 'settings.reviews.avatars.enabled' ))return; |
131
|
|
|
$size = $this->getOption( 'settings.reviews.avatars.size', 40 ); |
132
|
|
|
return $this->wrap( $key, glsr( Builder::class )->img([ |
133
|
|
|
'src' => $this->generateAvatar( $value ), |
134
|
|
|
'height' => $size, |
135
|
|
|
'width' => $size, |
136
|
|
|
])); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $key |
141
|
|
|
* @param string $value |
142
|
|
|
* @return void|string |
143
|
|
|
*/ |
144
|
|
|
protected function buildOptionContent( $key, $value ) |
145
|
|
|
{ |
146
|
|
|
$text = $this->normalizeText( $value ); |
147
|
|
|
if( $this->isHiddenOrEmpty( $key, $text ))return; |
148
|
|
|
return $this->wrap( $key, '<p>'.$text.'</p>' ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $key |
153
|
|
|
* @param string $value |
154
|
|
|
* @return void|string |
155
|
|
|
*/ |
156
|
|
|
protected function buildOptionDate( $key, $value ) |
157
|
|
|
{ |
158
|
|
|
if( $this->isHidden( $key ))return; |
159
|
|
|
$dateFormat = $this->getOption( 'settings.reviews.date.format', 'default' ); |
160
|
|
|
if( $dateFormat == 'relative' ) { |
161
|
|
|
$date = glsr( Date::class )->relative( $value ); |
162
|
|
|
} |
163
|
|
|
else { |
164
|
|
|
$format = $dateFormat == 'custom' |
165
|
|
|
? $this->getOption( 'settings.reviews.date.custom', 'M j, Y' ) |
166
|
|
|
: (string)get_option( 'date_format' ); |
167
|
|
|
$date = date_i18n( $format, strtotime( $value )); |
168
|
|
|
} |
169
|
|
|
return $this->wrap( $key, '<span>'.$date.'</span>' ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $key |
174
|
|
|
* @param string $value |
175
|
|
|
* @return void|string |
176
|
|
|
*/ |
177
|
|
|
protected function buildOptionRating( $key, $value ) |
178
|
|
|
{ |
179
|
|
|
if( $this->isHiddenOrEmpty( $key, $value ))return; |
180
|
|
|
$rating = glsr( Html::class )->buildPartial( 'star-rating', [ |
181
|
|
|
'rating' => $value, |
182
|
|
|
]); |
183
|
|
|
return $this->wrap( $key, $rating ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $key |
188
|
|
|
* @param string $value |
189
|
|
|
* @return void|string |
190
|
|
|
*/ |
191
|
|
|
protected function buildOptionResponse( $key, $value ) |
192
|
|
|
{ |
193
|
|
|
if( $this->isHiddenOrEmpty( $key, $value ))return; |
194
|
|
|
$title = sprintf( __( 'Response from %s', 'site-reviews' ), get_bloginfo( 'name' )); |
195
|
|
|
$text = $this->normalizeText( $value ); |
196
|
|
|
$text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
197
|
|
|
return $this->wrap( $key, |
198
|
|
|
glsr( Builder::class )->div( $text, ['class' => 'glsr-review-response-inner'] ). |
199
|
|
|
glsr( Builder::class )->div( ['class' => 'glsr-review-response-background'] ) |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $key |
205
|
|
|
* @param string $value |
206
|
|
|
* @return void|string |
207
|
|
|
*/ |
208
|
|
|
protected function buildOptionTitle( $key, $value ) |
209
|
|
|
{ |
210
|
|
|
if( $this->isHidden( $key ))return; |
211
|
|
|
if( empty( $value )) { |
212
|
|
|
$value = __( 'No Title', 'site-reviews' ); |
213
|
|
|
} |
214
|
|
|
return $this->wrap( $key, '<h3>'.$value.'</h3>' ); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param string $avatarUrl |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
protected function generateAvatar( $avatarUrl ) |
222
|
|
|
{ |
223
|
|
|
$review = $this->reviews->results[$this->current]; |
224
|
|
|
if( !$this->isOptionEnabled( 'settings.reviews.avatars.regenerate' ) |
225
|
|
|
|| $review->review_type != 'local' ) { |
226
|
|
|
return $avatarUrl; |
227
|
|
|
} |
228
|
|
|
$authorIdOrEmail = get_the_author_meta( 'ID', $review->user_id ); |
229
|
|
|
if( empty( $authorIdOrEmail )) { |
230
|
|
|
$authorIdOrEmail = $review->email; |
231
|
|
|
} |
232
|
|
|
return get_avatar_url( $authorIdOrEmail ); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return void |
237
|
|
|
*/ |
238
|
|
|
protected function generateSchema() |
239
|
|
|
{ |
240
|
|
|
if( !wp_validate_boolean( $this->args['schema'] ))return; |
241
|
|
|
glsr( Schema::class )->store( |
242
|
|
|
glsr( Schema::class )->build( $this->args ) |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
protected function getClass() |
250
|
|
|
{ |
251
|
|
|
$style = apply_filters( 'site-reviews/style', 'glsr-style' ); |
252
|
|
|
$pagination = $this->args['pagination'] == 'ajax' |
253
|
|
|
? 'glsr-ajax-pagination' |
254
|
|
|
: ''; |
255
|
|
|
return trim( 'glsr-reviews '.$style.' '.$pagination.' '.$this->args['class'] ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $text |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
protected function getExcerpt( $text ) |
263
|
|
|
{ |
264
|
|
|
$limit = $this->getOption( 'settings.reviews.excerpt.length', 55 ); |
265
|
|
|
if( str_word_count( $text, 0 ) > $limit ) { |
266
|
|
|
$words = array_keys( str_word_count( $text, 2 )); |
267
|
|
|
$excerpt = ltrim( substr( $text, 0, $words[$limit] )); |
268
|
|
|
$hiddenText = substr( $text, strlen( $excerpt )); |
269
|
|
|
$showMore = glsr( Builder::class )->span( $hiddenText, [ |
270
|
|
|
'class' => 'glsr-hidden glsr-hidden-text', |
271
|
|
|
'data-show-less' => __( 'Show less', 'site-reviews' ), |
272
|
|
|
'data-show-more' => __( 'Show more', 'site-reviews' ), |
273
|
|
|
]); |
274
|
|
|
$text = $excerpt.$showMore; |
275
|
|
|
} |
276
|
|
|
return nl2br( $text ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param string $path |
281
|
|
|
* @param mixed $fallback |
282
|
|
|
* @return mixed |
283
|
|
|
*/ |
284
|
|
|
protected function getOption( $path, $fallback = '' ) |
285
|
|
|
{ |
286
|
|
|
if( array_key_exists( $path, $this->options )) { |
287
|
|
|
return $this->options[$path]; |
288
|
|
|
} |
289
|
|
|
return $fallback; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param string $key |
294
|
|
|
* @param string $path |
295
|
|
|
* @return bool |
296
|
|
|
*/ |
297
|
|
|
protected function isHidden( $key, $path = '' ) |
298
|
|
|
{ |
299
|
|
|
$isOptionEnabled = !empty( $path ) |
300
|
|
|
? $this->isOptionEnabled( $path ) |
301
|
|
|
: true; |
302
|
|
|
return in_array( $key, $this->args['hide'] ) || !$isOptionEnabled; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param string $key |
307
|
|
|
* @param string $value |
308
|
|
|
* @return bool |
309
|
|
|
*/ |
310
|
|
|
protected function isHiddenOrEmpty( $key, $value ) |
311
|
|
|
{ |
312
|
|
|
return $this->isHidden( $key ) || empty( $value ); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param string $path |
317
|
|
|
* @return bool |
318
|
|
|
*/ |
319
|
|
|
protected function isOptionEnabled( $path ) |
320
|
|
|
{ |
321
|
|
|
return $this->getOption( $path ) == 'yes'; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param string $text |
326
|
|
|
* @return string |
327
|
|
|
*/ |
328
|
|
|
protected function normalizeText( $text ) |
329
|
|
|
{ |
330
|
|
|
$text = wp_kses( $text, wp_kses_allowed_html() ); |
331
|
|
|
$text = convert_smilies( wptexturize( strip_shortcodes( $text ))); |
332
|
|
|
$text = str_replace( ']]>', ']]>', $text ); |
333
|
|
|
$text = preg_replace( '/(\R){2,}/', '$1', $text ); |
334
|
|
|
return $this->isOptionEnabled( 'settings.reviews.excerpt.enabled' ) |
335
|
|
|
? $this->getExcerpt( $text ) |
336
|
|
|
: $text; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param string $key |
341
|
|
|
* @param string $value |
342
|
|
|
* @return string |
343
|
|
|
*/ |
344
|
|
|
protected function wrap( $key, $value ) |
345
|
|
|
{ |
346
|
|
|
return glsr( Builder::class )->div( $value, [ |
347
|
|
|
'class' => 'glsr-review-'.$key, |
348
|
|
|
]); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.