PluginProbe ʕ •ᴥ•ʔ
Rich Showcase for Google Reviews / 6.4
Rich Showcase for Google Reviews v6.4
6.9.8 6.9.7 6.9.6 trunk 1.4 1.42 1.43 1.44 1.45 1.46 1.47 1.48 1.49 1.5 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.7 1.6.8 1.6.9 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3 2.4 2.4.1 2.4.2 2.5 2.5.1 2.6 2.6.1 2.6.2 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.6.1 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.8.1 4.8.2 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.7.1 5.8 5.9 5.9.1 5.9.2 5.9.3 5.9.7 6.0 6.1 6.2 6.3 6.4 6.4.1 6.5 6.6 6.6.1 6.6.2 6.7 6.8 6.8.1 6.8.2 6.9 6.9.1 6.9.2 6.9.3 6.9.4 6.9.4.1 6.9.4.2 6.9.4.3 6.9.4.4 6.9.5
widget-google-reviews / includes / core / class-core.php
widget-google-reviews / includes / core Last commit date
class-connect-helper.php 1 year ago class-core.php 10 months ago class-database.php 10 months ago class-google-api-new.php 1 year ago class-google-api-old.php 11 months ago class-google-connect.php 11 months ago class-google-dao.php 10 months ago class-google-utils.php 1 year ago
class-core.php
471 lines
1 <?php
2
3 namespace WP_Rplg_Google_Reviews\Includes\Core;
4
5 class Core {
6
7 public function __construct() {
8 }
9
10 public static function get_default_options() {
11 return array(
12 'view_mode' => 'list',
13 'pagination' => '10',
14 'text_size' => '',
15 'min_letter' => '',
16 'disable_user_link' => false,
17 'hide_based_on' => false,
18 'hide_writereview' => false,
19 'hide_reviews' => false,
20 'hide_avatar' => false,
21 'hide_backgnd' => false,
22 'show_round' => false,
23 'show_shadow' => false,
24 'short_last_name' => false,
25 'media' => true,
26 'reply' => true,
27
28 'slider_autoplay' => true,
29 'slider_hide_border' => false,
30 'slider_hide_prevnext' => false,
31 'slider_hide_dots' => false,
32 'slider_text_height' => '',
33 'slider_speed' => 3,
34 'slider_mousestop' => true,
35 'slider_breakpoints' => '',
36
37 'header_merge_social' => false,
38 'header_hide_social' => false,
39 'header_center' => false,
40 'header_hide_photo' => false,
41 'header_hide_name' => false,
42
43 'dark_theme' => false,
44 'centered' => false,
45 'max_width' => '',
46 'max_height' => '',
47 'style_vars' => '',
48
49 'open_link' => true,
50 'nofollow_link' => true,
51 'lazy_load_img' => true,
52 'aria_label' => false,
53 'google_def_rev_link' => false,
54 'reviewer_avatar_size' => 56,
55 'reviews_limit' => '',
56 'cache' => 12,
57 );
58 }
59
60 public function get_reviews($feed, $is_admin = false) {
61 $connection = json_decode($feed->post_content);
62
63 if ($is_admin) {
64 return $this->get_data($connection, $is_admin);
65 }
66
67 $cache_time = isset($connection->options->cache) ? $connection->options->cache : null;
68 $data_cache_key = 'grw_feed_' . GRW_VERSION . '_' . $feed->ID . '_reviews';
69 $connection_cache_key = 'grw_feed_' . GRW_VERSION . '_' . $feed->ID . '_options';
70
71 $data = get_transient($data_cache_key);
72 $cached_connection = get_transient($connection_cache_key);
73 $serialized_connection = serialize($connection);
74
75 if ($data === false || $serialized_connection !== $cached_connection || !$cache_time) {
76 $expiration = $cache_time;
77 switch ($expiration) {
78 case '1':
79 $expiration = 3600;
80 break;
81 case '3':
82 $expiration = 3600 * 3;
83 break;
84 case '6':
85 $expiration = 3600 * 6;
86 break;
87 case '12':
88 $expiration = 3600 * 12;
89 break;
90 case '24':
91 $expiration = 3600 * 24;
92 break;
93 case '48':
94 $expiration = 3600 * 48;
95 break;
96 case '168':
97 $expiration = 3600 * 168;
98 break;
99 default:
100 $expiration = 3600 * 24;
101 }
102 $data = $this->get_data($connection, $is_admin);
103 set_transient($data_cache_key, $data, $expiration);
104 set_transient($connection_cache_key, $serialized_connection, $expiration);
105 }
106 return $data;
107 }
108
109 public function get_data($connection, $is_admin = false) {
110
111 if ($connection == null) {
112 return null;
113 }
114
115 foreach ($this->get_default_options() as $field => $value) {
116 $connection->options->{$field} = isset($connection->options->{$field}) ? esc_attr($connection->options->{$field}) : $value;
117 }
118 $options = $connection->options;
119
120 if (isset($connection->connections) && is_array($connection->connections)) {
121 $google_business = null;
122 foreach ($connection->connections as $conn) {
123 switch ($conn->platform) {
124 case 'google':
125 if (!$google_business) $google_business = array();
126 array_push($google_business, $conn);
127 break;
128 }
129 }
130 } else {
131 $google_business = isset($connection->google) ? $connection->google : null;
132 }
133
134 $google_biz = array();
135 $google_reviews = array();
136
137 if ($google_business != null) {
138 foreach ($google_business as $biz) {
139 $result = $this->get_google_reviews($biz, $options, $is_admin);
140 array_push($google_biz, $result['business']);
141 $google_reviews = array_merge($google_reviews, $result['reviews']);
142 }
143 }
144
145 $social_biz = array();
146 if ($options->header_merge_social) {
147 if (count($google_biz) > 0) {
148 array_push($social_biz, $this->merge_biz($google_biz));
149 }
150 } else {
151 $social_biz = array_merge($google_biz);
152 }
153
154 $businesses = array();
155 if (!$options->header_hide_social) {
156 $businesses = $social_biz;
157 }
158
159 $reviews = array();
160 if (!$options->hide_reviews) {
161
162 $revs = array();
163 if (count($google_reviews) > 0) {
164 array_push($revs, $google_reviews);
165 }
166
167 // Sorting
168 while (count($revs) > 0) {
169 foreach ($revs as $i => $value) {
170 $review = array_shift($revs[$i]);
171 if (strlen($review->text) > 0) {
172 $review->text = nl2br($review->text);
173 }
174 array_push($reviews, $review);
175 if (count($revs[$i]) < 1) {
176 unset($revs[$i]);
177 }
178 }
179 }
180
181 // Normalize reviews array indexes after unset filter above
182 $reviews = array_values($reviews);
183
184 // Trim reviews limit
185 if ($options->reviews_limit > 0) {
186 $reviews = array_slice($google_reviews, 0, $options->reviews_limit);
187 }
188 }
189
190 return array('businesses' => $businesses, 'reviews' => $reviews, 'options' => $options);
191 }
192
193 public function get_google_reviews($google_biz, $options, $is_admin = false) {
194 global $wpdb;
195
196 $rating = 0;
197 $review_count = 0;
198 $reviews = [];
199
200 // Get Google place
201 $place = $wpdb->get_row(
202 $wpdb->prepare(
203 "SELECT * FROM " . $wpdb->prefix . Database::BUSINESS_TABLE .
204 " WHERE place_id = %s", $google_biz->id
205 )
206 );
207
208 if ($place) {
209
210 // Get Google reviews
211 $reviews_where = $is_admin ? '' : ' AND hide = \'\'';
212
213 $reviews_lang = strlen($google_biz->lang) > 0 ? $google_biz->lang : 'en';
214 $reviews_where = $reviews_where . ' AND (language = \'' . $reviews_lang . '\' OR language = \'\' OR language IS NULL)';
215
216 $reviews = $wpdb->get_results(
217 $wpdb->prepare(
218 "SELECT * FROM " . $wpdb->prefix . Database::REVIEW_TABLE .
219 " WHERE google_place_id = %d" . $reviews_where . " ORDER BY time DESC", $place->id
220 )
221 );
222
223 // Setup Google photo
224 $place->photo = strlen($google_biz->photo) > 0 ? $google_biz->photo : (strlen($place->photo) > 0 ? $place->photo : GRW_GOOGLE_BIZ);
225
226 // Calculate Google reviews count
227 if (isset($place->review_count) && $place->review_count > 0) {
228 $review_count = $place->review_count;
229 } else {
230 $review_count = $wpdb->get_var(
231 $wpdb->prepare(
232 "SELECT count(*) FROM " . $wpdb->prefix . Database::REVIEW_TABLE .
233 " WHERE google_place_id = %d", $place->id
234 )
235 );
236 }
237
238 // Calculate Google rating
239 $rating = 0;
240 if ($place->rating > 0) {
241 $rating = $place->rating;
242 } else if (count($reviews) > 0) {
243 foreach ($reviews as $review) {
244 $rating = $rating + $review->rating;
245 }
246 $rating = round($rating / count($reviews), 1);
247 }
248 $rating = number_format((float)$rating, 1, '.', '');
249 }
250
251 $business = json_decode(json_encode(
252 array(
253 'id' => $google_biz->id,
254 'name' => $google_biz->name ? $google_biz->name : $place->name,
255 'url' => isset($place->url) ? $place->url : null,
256 'photo' => isset($place->photo) ? $place->photo : GRW_GOOGLE_BIZ,
257 'address' => isset($place->address) ? $place->address : null,
258 'rating' => $rating,
259 'review_count' => $review_count,
260 'provider' => 'google'
261 )
262 ));
263
264 $google_reviews = array();
265 foreach ($reviews as $rev) {
266 if (isset($options->min_letter) && isset($rev->text) && strlen($rev->text) < $options->min_letter) {
267 continue;
268 }
269 $review = json_decode(json_encode(
270 array(
271 'id' => $rev->id,
272 'hide' => $rev->hide,
273 'biz_id' => $google_biz->id,
274 'biz_url' => $place->url,
275 'rating' => $rev->rating,
276 'text' => wp_encode_emoji($rev->text),
277 'author_avatar' => $rev->profile_photo_url,
278 'author_url' => $rev->author_url,
279 'author_name' => isset($options->short_last_name) && $options->short_last_name ?
280 $this->get_short_name($rev->author_name) : $rev->author_name,
281 'time' => $rev->time,
282 'images' => isset($rev->images) ? $rev->images : null,
283 'reply' => isset($rev->reply) ? $rev->reply : null,
284 'reply_time' => isset($rev->reply_time) ? $rev->reply_time : null,
285 'url' => isset($rev->url) ? $rev->url : null,
286 'provider' => isset($rev->provider) ? $rev->provider : 'google'
287 )
288 ));
289 array_push($google_reviews, $review);
290 }
291
292 return array('business' => $business, 'reviews' => $google_reviews);
293 }
294
295 public function get_overview($place_id = 0) {
296 global $wpdb;
297
298 // -------------- Get Google place --------------
299 $place_sql = "SELECT id, place_id, name, rating, review_count, updated" .
300 " FROM " . $wpdb->prefix . Database::BUSINESS_TABLE .
301 " WHERE rating > 0 AND review_count > 0" . ($place_id > 0 ? ' AND id = %d' : '') .
302 " ORDER BY id DESC";
303
304 $places = $place_id > 0 ?
305 // Query for specific Google place
306 $wpdb->get_results($wpdb->prepare($place_sql, sanitize_text_field(wp_unslash($place_id)))) :
307 // Query for summary (all places)
308 $wpdb->get_results($place_sql);
309
310 $count = count($places);
311 if ($count < 1) {
312 return null;
313 }
314
315 $rating = 0;
316 $review_count = 0;
317 $google_places = array();
318 $google_place_ids = array();
319
320 foreach ($places as $place) {
321 $id = $place->id;
322 $name = $place->name;
323 $rating += $place->rating;
324 $review_count += $place->review_count;
325
326 array_push($google_place_ids, $place->id);
327 array_push($google_places, json_decode(json_encode(array('id' => $place->id, 'name' => $place->name, 'updated' => $place->updated))));
328 }
329
330 if ($count > 1) {
331 $rating = round($rating / $count, 1);
332 $rating = number_format((float)$rating, 1, '.', '');
333 array_unshift($google_places, json_decode(json_encode(array('id' => 0, 'name' => 'Summary for all places'))));
334 }
335
336 // -------------- Get Google reviews --------------
337 $google_reviews = array();
338
339 $reviews = $wpdb->get_results(
340 $wpdb->prepare(
341 "SELECT * FROM " . $wpdb->prefix . Database::REVIEW_TABLE .
342 " WHERE google_place_id IN (" . implode(', ', array_fill(0, count($google_place_ids), '%d')) . ")" .
343 " ORDER BY time DESC LIMIT 10",
344 $google_place_ids
345 )
346 );
347
348 foreach ($reviews as $rev) {
349 $review = json_decode(json_encode(
350 array(
351 'id' => $rev->id,
352 'hide' => $rev->hide,
353 'rating' => $rev->rating,
354 'text' => wp_encode_emoji($rev->text),
355 'author_url' => $rev->author_url,
356 'author_name' => $rev->author_name,
357 'time' => $rev->time,
358 )
359 ));
360 array_push($google_reviews, $review);
361 }
362
363 // -------------- Get Google stats --------------
364 $stats = $wpdb->get_results(
365 $wpdb->prepare(
366 "SELECT * FROM " . $wpdb->prefix . Database::STATS_TABLE .
367 " WHERE google_place_id IN (" . implode(', ', array_fill(0, count($google_place_ids), '%d')) . ")" .
368 " ORDER BY id DESC LIMIT 10000",
369 $google_place_ids
370 )
371 );
372
373 // -------------- Get min/max stats values --------------
374 $stats_minmax = $wpdb->get_results(
375 $wpdb->prepare(
376 "SELECT t1.* FROM " . $wpdb->prefix . Database::STATS_TABLE . " t1" .
377 " JOIN (" .
378 "SELECT min(time) AS min_value, max(time) AS max_value, google_place_id FROM " . $wpdb->prefix . Database::STATS_TABLE .
379 " WHERE google_place_id IN (" . implode(', ', array_fill(0, count($google_place_ids), '%d')) . ")" .
380 " GROUP BY google_place_id" .
381 ") AS t2 ON t1.google_place_id = t2.google_place_id AND (t1.time = t2.min_value OR t1.time = t2.max_value)",
382 $google_place_ids
383 )
384 );
385
386 return
387 array(
388 'rating' => $rating,
389 'review_count' => $review_count,
390 'places' => $google_places,
391 'reviews' => $google_reviews,
392 'stats' => $stats,
393 'stats_minmax' => $stats_minmax
394 );
395 }
396
397 public function merge_biz($businesses, $id = '', $name = '', $url = '', $photo = '', $provider = '') {
398 $count = 0;
399 $rating = 0;
400 $review_count = array();
401 $review_count_manual = array();
402 $business_platform = array();
403 $biz_merge = null;
404 foreach ($businesses as $business) {
405 if ($business->rating < 1) {
406 continue;
407 }
408
409 $count++;
410 $rating += $business->rating;
411
412 if (isset($business->review_count_manual) && $business->review_count_manual > 0) {
413 $review_count_manual[$business->id] = $business->review_count_manual;
414 } else {
415 $review_count[$business->id] = $business->review_count;
416 }
417
418 array_push($business_platform, $business->provider);
419
420 if ($biz_merge == null) {
421 $biz_merge = json_decode(json_encode(
422 array(
423 'id' => strlen($id) > 0 ? $id : $business->id,
424 'name' => strlen($name) > 0 ? $name : $business->name,
425 'url' => strlen($url) > 0 ? $url : $business->url,
426 'photo' => strlen($photo) > 0 ? $photo : $business->photo,
427 'provider' => strlen($provider) > 0 ? $provider : $business->provider,
428 'review_count' => 0,
429 )
430 ));
431 }
432 $rating_tmp = round($rating / $count, 1);
433 $rating_tmp = number_format((float)$rating_tmp, 1, '.', '');
434 $biz_merge->rating = $rating_tmp;
435 }
436 $review_count = array_merge($review_count, $review_count_manual);
437 foreach ($review_count as $id => $count) {
438 $biz_merge->review_count += $count;
439 }
440 $biz_merge->platform = array_unique($business_platform);
441 return $biz_merge;
442 }
443
444 private function get_short_name($author_name){
445 $names = explode(" ", $author_name);
446 if (count($names) > 1) {
447 $last_index = count($names) - 1;
448 $last_name = $names[$last_index];
449 if ($this->_strlen($last_name) > 1) {
450 $last_char = $this->_substr($last_name, 0, 1);
451 $last_name = $this->_strtoupper($last_char) . ".";
452 $names[$last_index] = $last_name;
453 return implode(" ", $names);
454 }
455 }
456 return $author_name;
457 }
458
459 private function _strlen($str) {
460 return function_exists('mb_strlen') ? mb_strlen($str, 'UTF-8') : strlen($str);
461 }
462
463 private function _substr($str, $start, $length = NULL) {
464 return function_exists('mb_substr') ? mb_substr($str, $start, $length, 'UTF-8') : substr($str, $start, $length);
465 }
466
467 private function _strtoupper($str) {
468 return function_exists('mb_strtoupper') ? mb_strtoupper($str, 'UTF-8') : strtoupper($str);
469 }
470
471 }