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