PluginProbe ʕ •ᴥ•ʔ
Rich Showcase for Google Reviews / 3.1
Rich Showcase for Google Reviews v3.1
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-connect-google.php
widget-google-reviews / includes / core Last commit date
class-connect-google.php 2 years ago class-core.php 2 years ago class-database.php 4 years ago
class-connect-google.php
371 lines
1 <?php
2
3 namespace WP_Rplg_Google_Reviews\Includes\Core;
4
5 class Connect_Google {
6
7 public function __construct() {
8 add_action('wp_ajax_grw_hide_review', array($this, 'hide_review'));
9 add_action('wp_ajax_grw_connect_google', array($this, 'connect_google'));
10 }
11
12 public function hide_review() {
13 global $wpdb;
14
15 if (current_user_can('manage_options')) {
16 if (isset($_POST['grw_wpnonce']) === false) {
17 $error = __('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.', 'widget-google-reviews');
18 $response = compact('error');
19 } else {
20 check_admin_referer('grw_wpnonce', 'grw_wpnonce');
21
22 $review = $wpdb->get_row(
23 $wpdb->prepare(
24 "SELECT * FROM " . $wpdb->prefix . Database::REVIEW_TABLE .
25 " WHERE id = %d", $_POST['id']
26 )
27 );
28
29 $hide = $review->hide == '' ? 'y' : '';
30 $wpdb->update($wpdb->prefix . Database::REVIEW_TABLE, array('hide' => $hide), array('id' => $_POST['id']));
31
32 // Cache clear
33 if ($_POST['feed_id']) {
34 delete_transient('grw_feed_' . GRW_VERSION . '_' . $_POST['feed_id'] . '_reviews', false);
35 } else {
36 $feed_ids = get_option('grw_feed_ids');
37 if (!empty($feed_ids)) {
38 $ids = explode(",", $feed_ids);
39 foreach ($ids as $id) {
40 delete_transient('grw_feed_' . GRW_VERSION . '_' . $id . '_reviews', false);
41 }
42 }
43 }
44
45 $response = array('hide' => $hide);
46 }
47 header('Content-type: text/javascript');
48 echo json_encode($response);
49 die();
50 }
51 }
52
53 public function connect_google() {
54 if (current_user_can('manage_options')) {
55 if (isset($_POST['grw_wpnonce']) === false) {
56 $error = __('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.', 'widget-google-reviews');
57 $response = compact('error');
58 } else {
59 check_admin_referer('grw_wpnonce', 'grw_wpnonce');
60
61 if (isset($_POST['key'])) {
62 $key = sanitize_text_field(wp_unslash($_POST['key']));
63 if (strlen($key) > 0) {
64 update_option('grw_google_api_key', $key);
65 }
66 }
67 $google_api_key = get_option('grw_google_api_key');
68
69 $id = sanitize_text_field(wp_unslash($_POST['id']));
70 $lang = sanitize_text_field(wp_unslash($_POST['lang']));
71 $local_img = sanitize_text_field(wp_unslash($_POST['local_img']));
72
73 if ($google_api_key && strlen($google_api_key) > 0) {
74 $url = $this->api_url($id, $google_api_key, $lang);
75 } else {
76 $url = 'https://app.richplugins.com/gpaw/get/json' .
77 '?siteurl=' . get_option('siteurl') .
78 '&authcode=' . get_option('grw_auth_code') .
79 '&pid=' . $id;
80 if ($lang && strlen($lang) > 0) {
81 $url = $url . '&lang=' . $lang;
82 }
83 }
84
85 $res = wp_remote_get($url);
86 $body = wp_remote_retrieve_body($res);
87 $body_json = json_decode($body);
88
89 if ($body_json && isset($body_json->result)) {
90
91 if ($google_api_key && strlen($google_api_key) > 0) {
92 $photo = $this->business_avatar($body_json->result, $google_api_key);
93 $body_json->result->business_photo = $photo;
94 }
95
96 $this->save_reviews($body_json->result, $local_img);
97
98 $result = array(
99 'id' => $body_json->result->place_id,
100 'name' => $body_json->result->name,
101 'photo' => strlen($body_json->result->business_photo) ? $body_json->result->business_photo : GRW_GOOGLE_BIZ,
102 'reviews' => $body_json->result->reviews
103 );
104 $status = 'success';
105
106 if ($_POST['feed_id']) {
107 delete_transient('grw_feed_' . GRW_VERSION . '_' . $_POST['feed_id'] . '_reviews', false);
108 }
109 } else {
110 $result = $body_json;
111 $status = 'failed';
112 }
113 $response = compact('status', 'result');
114 }
115 header('Content-type: text/javascript');
116 echo json_encode($response);
117 die();
118 }
119 }
120
121 function grw_refresh_reviews($args) {
122
123 $place_id = $args[0];
124 $reviews_lang = $args[1];
125 $local_img = isset($args[2]) ? $args[2] : 'false';
126
127 $url = '';
128 $google_api_key = get_option('grw_google_api_key');
129 $api_key_filled = $google_api_key && strlen($google_api_key) > 0;
130
131 if ($api_key_filled) {
132
133 $url = $this->api_url($place_id, $google_api_key, $reviews_lang, 'newest');
134
135 } else {
136
137 $url = 'https://app.richplugins.com/gpaw/update/json' .
138 '?siteurl=' . get_option('siteurl') .
139 '&authcode=' . get_option('grw_auth_code') .
140 '&pid=' . $place_id .
141 '&time=' . time();
142 if ($reviews_lang && strlen($reviews_lang) > 0) {
143 $url = $url . '&lang=' . $reviews_lang;
144 }
145 }
146
147 if (strlen($url) > 0) {
148 $res = wp_remote_get($url);
149 $body = wp_remote_retrieve_body($res);
150 $body_json = json_decode($body);
151
152 if ($body_json && isset($body_json->result)) {
153
154 if ($api_key_filled) {
155 $photo = $this->business_avatar($body_json->result, $google_api_key);
156 $body_json->result->business_photo = $photo;
157 }
158
159 $this->save_reviews($body_json->result, $local_img);
160 }
161 }
162 }
163
164 function save_reviews($place, $local_img) {
165 global $wpdb;
166
167 $google_place_id = $wpdb->get_var(
168 $wpdb->prepare(
169 "SELECT id FROM " . $wpdb->prefix . Database::BUSINESS_TABLE .
170 " WHERE place_id = %s", $place->place_id
171 )
172 );
173
174 // Insert or update Google place
175 if ($google_place_id) {
176
177 // Update Google place
178 $update_params = array(
179 'name' => $place->name,
180 'rating' => $place->rating,
181 'updated' => round(microtime(true) * 1000),
182 );
183
184 $review_count = isset($place->user_ratings_total) ? $place->user_ratings_total : 0;
185
186 if ($review_count > 0) {
187 $update_params['review_count'] = $review_count;
188 }
189 if (isset($place->business_photo) && strlen($place->business_photo) > 0) {
190 $update_params['photo'] = $place->business_photo;
191 }
192 $wpdb->update($wpdb->prefix . Database::BUSINESS_TABLE, $update_params, array('ID' => $google_place_id));
193
194 // Insert Google place rating stats
195 $stats = $wpdb->get_results(
196 $wpdb->prepare(
197 "SELECT rating, review_count FROM " . $wpdb->prefix . Database::STATS_TABLE .
198 " WHERE google_place_id = %d ORDER BY id DESC LIMIT 1", $google_place_id
199 )
200 );
201 if (count($stats) > 0) {
202 if ($stats[0]->rating != $place->rating || ($review_count > 0 && $stats[0]->review_count != $review_count)) {
203 $wpdb->insert($wpdb->prefix . Database::STATS_TABLE, array(
204 'google_place_id' => $google_place_id,
205 'time' => time(),
206 'rating' => $place->rating,
207 'review_count' => $review_count
208 ));
209 }
210 } else {
211 $wpdb->insert($wpdb->prefix . Database::STATS_TABLE, array(
212 'google_place_id' => $google_place_id,
213 'time' => time(),
214 'rating' => $place->rating,
215 'review_count' => $review_count
216 ));
217 }
218
219 } else {
220
221 // Insert Google place
222 $place_rating = isset($place->rating) ? $place->rating : null;
223 $review_count = isset($place->user_ratings_total) ?
224 $place->user_ratings_total : (isset($place->reviews) ? count($place->reviews) : null);
225
226 $wpdb->insert($wpdb->prefix . Database::BUSINESS_TABLE, array(
227 'place_id' => $place->place_id,
228 'name' => $place->name,
229 'photo' => $place->business_photo,
230 'icon' => $place->icon,
231 'address' => $place->formatted_address,
232 'rating' => $place_rating,
233 'url' => isset($place->url) ? $place->url : null,
234 'website' => isset($place->website) ? $place->website : null,
235 'review_count' => $review_count,
236 'updated' => round(microtime(true) * 1000)
237 ));
238 $google_place_id = $wpdb->insert_id;
239
240 if ($place_rating > 0) {
241 $wpdb->insert($wpdb->prefix . Database::STATS_TABLE, array(
242 'google_place_id' => $google_place_id,
243 'time' => time(),
244 'rating' => $place_rating,
245 'review_count' => $review_count
246 ));
247 }
248 }
249
250 // Insert or update Google reviews
251 if ($place->reviews) {
252
253 $reviews = $place->reviews;
254
255 foreach ($reviews as $review) {
256 $google_review_id = 0;
257 if (isset($review->author_url) && strlen($review->author_url) > 0) {
258 $where = " WHERE author_url = %s";
259 $where_params = array($review->author_url);
260 } elseif (isset($review->author_name) && strlen($review->author_name) > 0) {
261 $where = " WHERE author_name = %s";
262 $where_params = array($review->author_name);
263 } else {
264 $where = " WHERE time = %s";
265 $where_params = array($review->time);
266 }
267
268 $review_lang = null;
269 if (isset($review->language)) {
270 $review_lang = ($review->language == 'en-US' ? 'en' : $review->language);
271 if (strlen($review_lang) > 0) {
272 $where = $where . " AND language = %s";
273 array_push($where_params, $review_lang);
274 }
275 }
276
277 if ($google_place_id) {
278 $where = $where . " AND google_place_id = %d";
279 array_push($where_params, $google_place_id);
280 }
281
282 $google_review_id = $wpdb->get_var(
283 $wpdb->prepare(
284 "SELECT id FROM " . $wpdb->prefix . Database::REVIEW_TABLE . $where, $where_params
285 )
286 );
287
288 $author_img = null;
289 if (isset($review->profile_photo_url)) {
290 if ($local_img === true || $local_img == 'true') {
291 $img_name = $place->place_id . '_' . md5($review->profile_photo_url);
292 $author_img = $this->upload_image($review->profile_photo_url, $img_name);
293 } else {
294 $author_img = $review->profile_photo_url;
295 }
296 }
297
298 if ($google_review_id) {
299 $update_params = array(
300 'rating' => $review->rating,
301 'text' => $review->text
302 );
303 if ($author_img) {
304 $update_params['profile_photo_url'] = $author_img;
305 }
306 $wpdb->update($wpdb->prefix . Database::REVIEW_TABLE, $update_params, array('id' => $google_review_id));
307 } else {
308 $wpdb->insert($wpdb->prefix . Database::REVIEW_TABLE, array(
309 'google_place_id' => $google_place_id,
310 'rating' => $review->rating,
311 'text' => $review->text,
312 'time' => $review->time,
313 'language' => $review_lang,
314 'author_name' => $review->author_name,
315 'author_url' => isset($review->author_url) ? $review->author_url : null,
316 'profile_photo_url' => $author_img
317 ));
318 }
319 }
320 }
321 }
322
323 function api_url($placeid, $google_api_key, $reviews_lang = '', $reviews_sort = '') {
324 $url = GRW_GOOGLE_PLACE_API . 'details/json?placeid=' . $placeid . '&key=' . $google_api_key;
325 if (strlen($reviews_lang) > 0) {
326 $url = $url . '&language=' . $reviews_lang;
327 }
328 if (strlen($reviews_sort) > 0) {
329 $url = $url . '&reviews_sort=' . $reviews_sort;
330 }
331 return $url;
332 }
333
334 function business_avatar($response_result_json, $google_api_key) {
335 if (isset($response_result_json->photos)) {
336 $url = add_query_arg(
337 array(
338 'photoreference' => $response_result_json->photos[0]->photo_reference,
339 'key' => $google_api_key,
340 'maxwidth' => '300',
341 'maxheight' => '300',
342 ),
343 'https://maps.googleapis.com/maps/api/place/photo'
344 );
345 return $this->upload_image($url, $response_result_json->place_id);
346 }
347 return null;
348 }
349
350 function upload_image($url, $name) {
351 $res = wp_remote_get($url, array('timeout' => 8));
352
353 if(is_wp_error($res)) {
354 // LOG
355 return null;
356 }
357
358 $bits = wp_remote_retrieve_body($res);
359 $filename = $name . '.jpg';
360
361 $upload_dir = wp_upload_dir();
362 $full_filepath = $upload_dir['path'] . '/' . $filename;
363 if (file_exists($full_filepath)) {
364 wp_delete_file($full_filepath);
365 }
366
367 $upload = wp_upload_bits($filename, null, $bits);
368 return $upload['url'];
369 }
370
371 }