class-core.php
434 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 | 'disable_user_link' => false, |
| 16 | 'hide_based_on' => false, |
| 17 | 'hide_writereview' => false, |
| 18 | 'hide_reviews' => false, |
| 19 | 'hide_avatar' => false, |
| 20 | 'hide_backgnd' => false, |
| 21 | 'show_round' => false, |
| 22 | 'show_shadow' => false, |
| 23 | |
| 24 | 'slider_autoplay' => true, |
| 25 | 'slider_hide_border' => false, |
| 26 | 'slider_hide_prevnext' => false, |
| 27 | 'slider_hide_dots' => false, |
| 28 | 'slider_text_height' => '', |
| 29 | 'slider_speed' => 5, |
| 30 | |
| 31 | 'header_merge_social' => false, |
| 32 | 'header_hide_social' => false, |
| 33 | 'header_center' => false, |
| 34 | 'header_hide_photo' => false, |
| 35 | 'header_hide_name' => false, |
| 36 | |
| 37 | 'dark_theme' => false, |
| 38 | 'centered' => false, |
| 39 | 'max_width' => '', |
| 40 | 'max_height' => '', |
| 41 | |
| 42 | 'open_link' => true, |
| 43 | 'nofollow_link' => true, |
| 44 | 'lazy_load_img' => true, |
| 45 | 'google_def_rev_link' => false, |
| 46 | 'reviewer_avatar_size' => 56, |
| 47 | 'reviews_limit' => '', |
| 48 | 'cache' => 12, |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | public function get_reviews($feed, $is_admin = false) { |
| 53 | $connection = json_decode($feed->post_content); |
| 54 | |
| 55 | if ($is_admin) { |
| 56 | return $this->get_data($connection, $is_admin); |
| 57 | } |
| 58 | |
| 59 | $cache_time = isset($connection->options->cache) ? $connection->options->cache : null; |
| 60 | $data_cache_key = 'grw_feed_' . GRW_VERSION . '_' . $feed->ID . '_reviews'; |
| 61 | $connection_cache_key = 'grw_feed_' . GRW_VERSION . '_' . $feed->ID . '_options'; |
| 62 | |
| 63 | $data = get_transient($data_cache_key); |
| 64 | $cached_connection = get_transient($connection_cache_key); |
| 65 | $serialized_connection = serialize($connection); |
| 66 | |
| 67 | if ($data === false || $serialized_connection !== $cached_connection || !$cache_time) { |
| 68 | $expiration = $cache_time; |
| 69 | switch ($expiration) { |
| 70 | case '1': |
| 71 | $expiration = 3600; |
| 72 | break; |
| 73 | case '3': |
| 74 | $expiration = 3600 * 3; |
| 75 | break; |
| 76 | case '6': |
| 77 | $expiration = 3600 * 6; |
| 78 | break; |
| 79 | case '12': |
| 80 | $expiration = 3600 * 12; |
| 81 | break; |
| 82 | case '24': |
| 83 | $expiration = 3600 * 24; |
| 84 | break; |
| 85 | case '48': |
| 86 | $expiration = 3600 * 48; |
| 87 | break; |
| 88 | case '168': |
| 89 | $expiration = 3600 * 168; |
| 90 | break; |
| 91 | default: |
| 92 | $expiration = 3600 * 24; |
| 93 | } |
| 94 | $data = $this->get_data($connection, $is_admin); |
| 95 | set_transient($data_cache_key, $data, $expiration); |
| 96 | set_transient($connection_cache_key, $serialized_connection, $expiration); |
| 97 | } |
| 98 | return $data; |
| 99 | } |
| 100 | |
| 101 | public function get_data($connection, $is_admin = false) { |
| 102 | |
| 103 | if ($connection == null) { |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | foreach ($this->get_default_options() as $field => $value) { |
| 108 | $connection->options->{$field} = isset($connection->options->{$field}) ? esc_attr($connection->options->{$field}) : $value; |
| 109 | } |
| 110 | $options = $connection->options; |
| 111 | |
| 112 | if (isset($connection->connections) && is_array($connection->connections)) { |
| 113 | $google_business = null; |
| 114 | foreach ($connection->connections as $conn) { |
| 115 | switch ($conn->platform) { |
| 116 | case 'google': |
| 117 | if (!$google_business) $google_business = array(); |
| 118 | array_push($google_business, $conn); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | } else { |
| 123 | $google_business = isset($connection->google) ? $connection->google : null; |
| 124 | } |
| 125 | |
| 126 | $google_biz = array(); |
| 127 | $google_reviews = array(); |
| 128 | |
| 129 | if ($google_business != null) { |
| 130 | |
| 131 | foreach ($google_business as $biz) { |
| 132 | |
| 133 | $result = $this->get_google_reviews($biz, $is_admin); |
| 134 | array_push($google_biz, $result['business']); |
| 135 | $google_reviews = array_merge($google_reviews, $result['reviews']); |
| 136 | |
| 137 | if (isset($biz->refresh) && $biz->refresh) { |
| 138 | $args = array($biz->id); |
| 139 | if (isset($biz->lang) && strlen($biz->lang) > 0) { |
| 140 | array_push($args, $biz->lang); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | $social_biz = array(); |
| 147 | if ($options->header_merge_social) { |
| 148 | if (count($google_biz) > 0) { |
| 149 | array_push($social_biz, $this->merge_biz($google_biz)); |
| 150 | } |
| 151 | } else { |
| 152 | $social_biz = array_merge($google_biz); |
| 153 | } |
| 154 | |
| 155 | $businesses = array(); |
| 156 | if (!$options->header_hide_social) { |
| 157 | $businesses = $social_biz; |
| 158 | } |
| 159 | |
| 160 | $reviews = array(); |
| 161 | if (!$options->hide_reviews) { |
| 162 | |
| 163 | $revs = array(); |
| 164 | if (count($google_reviews) > 0) { |
| 165 | array_push($revs, $google_reviews); |
| 166 | } |
| 167 | |
| 168 | // Sorting |
| 169 | while (count($revs) > 0) { |
| 170 | foreach ($revs as $i => $value) { |
| 171 | $review = array_shift($revs[$i]); |
| 172 | array_push($reviews, $review); |
| 173 | if (count($revs[$i]) < 1) { |
| 174 | unset($revs[$i]); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Normalize reviews array indexes after unset filter above |
| 180 | $reviews = array_values($reviews); |
| 181 | |
| 182 | // Trim reviews limit |
| 183 | if ($options->reviews_limit > 0) { |
| 184 | $reviews = array_slice($google_reviews, 0, $options->reviews_limit); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return array('businesses' => $businesses, 'reviews' => $reviews, 'options' => $options); |
| 189 | } |
| 190 | |
| 191 | public function get_google_reviews($google_biz, $is_admin = false) { |
| 192 | global $wpdb; |
| 193 | |
| 194 | $rating = 0; |
| 195 | $review_count = 0; |
| 196 | $reviews = []; |
| 197 | |
| 198 | // Get Google place |
| 199 | $place = $wpdb->get_row( |
| 200 | $wpdb->prepare( |
| 201 | "SELECT * FROM " . $wpdb->prefix . Database::BUSINESS_TABLE . |
| 202 | " WHERE place_id = %s", $google_biz->id |
| 203 | ) |
| 204 | ); |
| 205 | |
| 206 | if ($place) { |
| 207 | |
| 208 | // Get Google reviews |
| 209 | $reviews_where = $is_admin ? '' : ' AND hide = \'\''; |
| 210 | |
| 211 | $reviews_lang = strlen($google_biz->lang) > 0 ? $google_biz->lang : 'en'; |
| 212 | $reviews_where = $reviews_where . ' AND (language = \'' . $reviews_lang . '\' OR language = \'\' OR language IS NULL)'; |
| 213 | |
| 214 | $reviews = $wpdb->get_results( |
| 215 | $wpdb->prepare( |
| 216 | "SELECT * FROM " . $wpdb->prefix . Database::REVIEW_TABLE . |
| 217 | " WHERE google_place_id = %d" . $reviews_where . " ORDER BY time DESC", $place->id |
| 218 | ) |
| 219 | ); |
| 220 | |
| 221 | // Setup Google photo |
| 222 | $place->photo = strlen($google_biz->photo) > 0 ? $google_biz->photo : (strlen($place->photo) > 0 ? $place->photo : GRW_GOOGLE_BIZ); |
| 223 | |
| 224 | // Calculate Google reviews count |
| 225 | if (isset($place->review_count) && $place->review_count > 0) { |
| 226 | $review_count = $place->review_count; |
| 227 | } else { |
| 228 | $review_count = $wpdb->get_var( |
| 229 | $wpdb->prepare( |
| 230 | "SELECT count(*) FROM " . $wpdb->prefix . Database::REVIEW_TABLE . |
| 231 | " WHERE google_place_id = %d", $place->id |
| 232 | ) |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | // Calculate Google rating |
| 237 | $rating = 0; |
| 238 | if ($place->rating > 0) { |
| 239 | $rating = $place->rating; |
| 240 | } else if (count($reviews) > 0) { |
| 241 | foreach ($reviews as $review) { |
| 242 | $rating = $rating + $review->rating; |
| 243 | } |
| 244 | $rating = round($rating / count($reviews), 1); |
| 245 | } |
| 246 | $rating = number_format((float)$rating, 1, '.', ''); |
| 247 | } |
| 248 | |
| 249 | $business = json_decode(json_encode( |
| 250 | array( |
| 251 | 'id' => $google_biz->id, |
| 252 | 'name' => $google_biz->name ? $google_biz->name : $place->name, |
| 253 | 'url' => isset($place->url) ? $place->url : null, |
| 254 | 'photo' => isset($place->photo) ? $place->photo : GRW_GOOGLE_BIZ, |
| 255 | 'address' => isset($place->address) ? $place->address : null, |
| 256 | 'rating' => $rating, |
| 257 | 'review_count' => $review_count, |
| 258 | 'provider' => 'google' |
| 259 | ) |
| 260 | )); |
| 261 | |
| 262 | $google_reviews = array(); |
| 263 | foreach ($reviews as $rev) { |
| 264 | $review = json_decode(json_encode( |
| 265 | array( |
| 266 | 'id' => $rev->id, |
| 267 | 'hide' => $rev->hide, |
| 268 | 'biz_id' => $google_biz->id, |
| 269 | 'biz_url' => $place->url, |
| 270 | 'rating' => $rev->rating, |
| 271 | 'text' => wp_encode_emoji($rev->text), |
| 272 | 'author_avatar' => $rev->profile_photo_url, |
| 273 | 'author_url' => $rev->author_url, |
| 274 | 'author_name' => $rev->author_name, |
| 275 | 'time' => $rev->time, |
| 276 | 'provider' => 'google', |
| 277 | ) |
| 278 | )); |
| 279 | array_push($google_reviews, $review); |
| 280 | } |
| 281 | |
| 282 | return array('business' => $business, 'reviews' => $google_reviews); |
| 283 | } |
| 284 | |
| 285 | public function get_overview($place_id = 0) { |
| 286 | global $wpdb; |
| 287 | |
| 288 | // -------------- Get Google place -------------- |
| 289 | $place_sql = "SELECT id, place_id, name, rating, review_count, updated" . |
| 290 | " FROM " . $wpdb->prefix . Database::BUSINESS_TABLE . |
| 291 | " WHERE rating > 0 AND review_count > 0" . ($place_id > 0 ? ' AND id = %d' : '') . |
| 292 | " ORDER BY id DESC"; |
| 293 | |
| 294 | $places = $place_id > 0 ? |
| 295 | // Query for specific Google place |
| 296 | $wpdb->get_results($wpdb->prepare($place_sql, sanitize_text_field(wp_unslash($place_id)))) : |
| 297 | // Query for summary (all places) |
| 298 | $wpdb->get_results($place_sql); |
| 299 | |
| 300 | $count = count($places); |
| 301 | if ($count < 1) { |
| 302 | return null; |
| 303 | } |
| 304 | |
| 305 | $rating = 0; |
| 306 | $review_count = 0; |
| 307 | $google_places = array(); |
| 308 | $google_place_ids = array(); |
| 309 | |
| 310 | foreach ($places as $place) { |
| 311 | $id = $place->id; |
| 312 | $name = $place->name; |
| 313 | $rating += $place->rating; |
| 314 | $review_count += $place->review_count; |
| 315 | |
| 316 | array_push($google_place_ids, $place->id); |
| 317 | array_push($google_places, json_decode(json_encode(array('id' => $place->id, 'name' => $place->name, 'updated' => $place->updated)))); |
| 318 | } |
| 319 | |
| 320 | if ($count > 1) { |
| 321 | $rating = round($rating / $count, 1); |
| 322 | $rating = number_format((float)$rating, 1, '.', ''); |
| 323 | array_unshift($google_places, json_decode(json_encode(array('id' => 0, 'name' => 'Summary for all places')))); |
| 324 | } |
| 325 | |
| 326 | // -------------- Get Google reviews -------------- |
| 327 | $google_reviews = array(); |
| 328 | |
| 329 | $reviews = $wpdb->get_results( |
| 330 | $wpdb->prepare( |
| 331 | "SELECT * FROM " . $wpdb->prefix . Database::REVIEW_TABLE . |
| 332 | " WHERE google_place_id IN (" . implode(', ', array_fill(0, count($google_place_ids), '%d')) . ")" . |
| 333 | " ORDER BY time DESC LIMIT 10", |
| 334 | $google_place_ids |
| 335 | ) |
| 336 | ); |
| 337 | |
| 338 | foreach ($reviews as $rev) { |
| 339 | $review = json_decode(json_encode( |
| 340 | array( |
| 341 | 'id' => $rev->id, |
| 342 | 'hide' => $rev->hide, |
| 343 | 'rating' => $rev->rating, |
| 344 | 'text' => wp_encode_emoji($rev->text), |
| 345 | 'author_url' => $rev->author_url, |
| 346 | 'author_name' => $rev->author_name, |
| 347 | 'time' => $rev->time, |
| 348 | ) |
| 349 | )); |
| 350 | array_push($google_reviews, $review); |
| 351 | } |
| 352 | |
| 353 | // -------------- Get Google stats -------------- |
| 354 | $stats = $wpdb->get_results( |
| 355 | $wpdb->prepare( |
| 356 | "SELECT * FROM " . $wpdb->prefix . Database::STATS_TABLE . |
| 357 | " WHERE google_place_id IN (" . implode(', ', array_fill(0, count($google_place_ids), '%d')) . ")" . |
| 358 | " ORDER BY id DESC LIMIT 10000", |
| 359 | $google_place_ids |
| 360 | ) |
| 361 | ); |
| 362 | |
| 363 | // -------------- Get min/max stats values -------------- |
| 364 | $stats_minmax = $wpdb->get_results( |
| 365 | $wpdb->prepare( |
| 366 | "SELECT t1.* FROM " . $wpdb->prefix . Database::STATS_TABLE . " t1" . |
| 367 | " JOIN (" . |
| 368 | "SELECT min(time) AS min_value, max(time) AS max_value, google_place_id FROM " . $wpdb->prefix . Database::STATS_TABLE . |
| 369 | " WHERE google_place_id IN (" . implode(', ', array_fill(0, count($google_place_ids), '%d')) . ")" . |
| 370 | " GROUP BY google_place_id" . |
| 371 | ") AS t2 ON t1.google_place_id = t2.google_place_id AND (t1.time = t2.min_value OR t1.time = t2.max_value)", |
| 372 | $google_place_ids |
| 373 | ) |
| 374 | ); |
| 375 | |
| 376 | return |
| 377 | array( |
| 378 | 'rating' => $rating, |
| 379 | 'review_count' => $review_count, |
| 380 | 'places' => $google_places, |
| 381 | 'reviews' => $google_reviews, |
| 382 | 'stats' => $stats, |
| 383 | 'stats_minmax' => $stats_minmax |
| 384 | ); |
| 385 | } |
| 386 | |
| 387 | public function merge_biz($businesses, $id = '', $name = '', $url = '', $photo = '', $provider = '') { |
| 388 | $count = 0; |
| 389 | $rating = 0; |
| 390 | $review_count = array(); |
| 391 | $review_count_manual = array(); |
| 392 | $business_platform = array(); |
| 393 | $biz_merge = null; |
| 394 | foreach ($businesses as $business) { |
| 395 | if ($business->rating < 1) { |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | $count++; |
| 400 | $rating += $business->rating; |
| 401 | |
| 402 | if (isset($business->review_count_manual) && $business->review_count_manual > 0) { |
| 403 | $review_count_manual[$business->id] = $business->review_count_manual; |
| 404 | } else { |
| 405 | $review_count[$business->id] = $business->review_count; |
| 406 | } |
| 407 | |
| 408 | array_push($business_platform, $business->provider); |
| 409 | |
| 410 | if ($biz_merge == null) { |
| 411 | $biz_merge = json_decode(json_encode( |
| 412 | array( |
| 413 | 'id' => strlen($id) > 0 ? $id : $business->id, |
| 414 | 'name' => strlen($name) > 0 ? $name : $business->name, |
| 415 | 'url' => strlen($url) > 0 ? $url : $business->url, |
| 416 | 'photo' => strlen($photo) > 0 ? $photo : $business->photo, |
| 417 | 'provider' => strlen($provider) > 0 ? $provider : $business->provider, |
| 418 | 'review_count' => 0, |
| 419 | ) |
| 420 | )); |
| 421 | } |
| 422 | $rating_tmp = round($rating / $count, 1); |
| 423 | $rating_tmp = number_format((float)$rating_tmp, 1, '.', ''); |
| 424 | $biz_merge->rating = $rating_tmp; |
| 425 | } |
| 426 | $review_count = array_merge($review_count, $review_count_manual); |
| 427 | foreach ($review_count as $id => $count) { |
| 428 | $biz_merge->review_count += $count; |
| 429 | } |
| 430 | $biz_merge->platform = array_unique($business_platform); |
| 431 | return $biz_merge; |
| 432 | } |
| 433 | |
| 434 | } |