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-google-dao.php
widget-google-reviews / includes / core Last commit date
class-connect-helper.php 1 year ago class-core.php 11 months ago class-database.php 11 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 11 months ago class-google-utils.php 1 year ago
class-google-dao.php
295 lines
1 <?php
2
3 namespace WP_Rplg_Google_Reviews\Includes\Core;
4
5 class Google_Dao {
6
7 private $helper;
8
9 public function __construct(Connect_Helper $helper) {
10 $this->helper = $helper;
11 }
12
13 public function save($place, $local_img = true) {
14 global $wpdb;
15
16 $log = array(round(microtime(true) * 1000));
17 update_option('grw_last_error', '');
18
19 $db_place_id = $wpdb->get_var(
20 $wpdb->prepare(
21 "SELECT id FROM " . $wpdb->prefix . Database::BUSINESS_TABLE . " WHERE place_id = %s", $place->place_id
22 )
23 );
24
25 // Insert or update Google place
26 if ($db_place_id) {
27 $this->update_place($place, $db_place_id, $log);
28 } else {
29 $db_place_id = $this->insert_place($place, $log);
30 }
31 $this->log_last_error($wpdb);
32
33 // Insert or update Google reviews
34 if (isset($place->reviews)) {
35
36 $reviews = $place->reviews;
37
38 foreach ($reviews as $review) {
39 $db_review_id = 0;
40 if (isset($review->author_url) && strlen($review->author_url) > 0) {
41 $where = " WHERE author_url = %s";
42 $where_params = array($review->author_url);
43 } else {
44 $where = " WHERE time = %s";
45 $where_params = array($review->time);
46 if (isset($review->author_name) && strlen($review->author_name) > 0) {
47 $where = $where . " AND author_name = %s";
48 array_push($where_params, $review->author_name);
49 }
50 }
51
52 $review_lang = null;
53 if (isset($review->language)) {
54 $review_lang = ($review->language == 'en-US' ? 'en' : $review->language);
55 if (strlen($review_lang) > 0) {
56 $where = $where . " AND language = %s";
57 array_push($where_params, $review_lang);
58 }
59 }
60
61 if ($db_place_id) {
62 $where = $where . " AND google_place_id = %d";
63 array_push($where_params, $db_place_id);
64 }
65
66 $sql = "SELECT id FROM " . $wpdb->prefix . Database::REVIEW_TABLE . $where;
67 $db_review_id = $wpdb->get_var($wpdb->prepare($sql, $where_params));
68
69 $author_img = null;
70 if (isset($review->profile_photo_url)) {
71 if ($local_img === true || $local_img == 'true') {
72 $img_name = $place->place_id . '_' . md5($review->profile_photo_url);
73 $author_img = $this->helper->upload_image($review->profile_photo_url, $img_name);
74 } else {
75 $author_img = $review->profile_photo_url;
76 }
77 }
78
79 $images = null;
80 if (isset($review->images) && count($review->images) > 0) {
81 if ($local_img === true || $local_img == 'true') {
82 $saved_imgs = [];
83 foreach ($review->images as $img) {
84 $img_name = $place->place_id . '_img_' . md5($img);
85 $saved_img = $this->helper->upload_image($img, $img_name);
86 array_push($saved_imgs, $saved_img);
87 }
88 $images = implode(';', $saved_imgs);
89 } else {
90 $images = implode(';', $review->images);
91 }
92 }
93
94 $reply = null;
95 $reply_time = null;
96 if (isset($review->reply) && strlen($review->reply) > 0) {
97 $reply = $review->reply;
98 $reply_time = $review->reply_time;
99 }
100
101 if ($db_review_id) {
102 $this->update_review($review, $review_lang, $author_img, $images, $reply, $reply_time, $db_review_id, $log);
103 } else {
104 $this->insert_review($review, $review_lang, $author_img, $images, $reply, $reply_time, $db_place_id, $log);
105 }
106 $this->log_last_error($wpdb);
107 }
108 }
109
110 update_option('grw_save_log', implode('_', $log));
111 }
112
113 public function insert_place($place, &$log = []) {
114 global $wpdb;
115
116 // Insert Google place
117 $pid = $place->place_id;
118 $name = $place->name;
119 $rating = isset($place->rating) ? $place->rating : null;
120 $review_count = isset($place->user_ratings_total) ? $place->user_ratings_total : (isset($place->reviews) ? count($place->reviews) : null);
121
122 $atts = array(
123 'place_id' => $pid,
124 'rating' => $rating,
125 'review_count' => $review_count,
126 'name' => $name,
127 'photo' => isset($place->business_photo) ? $place->business_photo : null,
128 'url' => isset($place->url) ? $place->url : null,
129 'website' => isset($place->website) ? $place->website : null,
130 'icon' => isset($place->icon) ? $place->icon : null,
131 'address' => isset($place->formatted_address) ? $place->formatted_address : null,
132 'updated' => round(microtime(true) * 1000)
133 );
134 if (isset($place->map_url) && strlen($place->map_url) > 0) {
135 $atts['map_url'] = $place->map_url;
136 }
137
138 $wpdb->insert($wpdb->prefix . Database::BUSINESS_TABLE, $atts);
139 $db_place_id = $wpdb->insert_id;
140
141 array_push($log, 'ip[' . $pid . ',' . $name . ',' . $rating . ',' . $review_count . ']');
142
143 if ($rating > 0) {
144 $this->insert_stats($rating, $review_count, $db_place_id);
145 }
146
147 return $db_place_id;
148 }
149
150 public function update_place($place, $db_place_id, &$log = []) {
151 global $wpdb;
152
153 $name = $place->name;
154 $rating = $place->rating;
155
156 // Update Google place name and rating
157 $update_params = array(
158 'name' => $name,
159 'rating' => $rating,
160 'updated' => round(microtime(true) * 1000),
161 );
162
163 // Update total reviews
164 $review_count = isset($place->user_ratings_total) ? $place->user_ratings_total : 0;
165 if ($review_count > 0) {
166 $update_params['review_count'] = $review_count;
167 }
168
169 // Update business photo
170 if (isset($place->business_photo) && strlen($place->business_photo) > 0) {
171 $update_params['photo'] = $place->business_photo;
172 }
173
174 // Update map URL
175 if (isset($place->map_url) && strlen($place->map_url) > 0) {
176 $update_params['map_url'] = $place->map_url;
177 }
178
179 $wpdb->update($wpdb->prefix . Database::BUSINESS_TABLE, $update_params, array('ID' => $db_place_id));
180
181 array_push($log, 'up[' . $db_place_id . ',' . $name . ',' . $rating . ',' . $review_count . ']');
182
183 $this->update_stats($place->rating, $review_count, $db_place_id);
184 }
185
186 public function update_stats($rating, $review_count, $db_place_id) {
187 global $wpdb;
188
189 // Insert Google place rating stats
190 $stats = $wpdb->get_results(
191 $wpdb->prepare(
192 "SELECT rating, review_count FROM " . $wpdb->prefix . Database::STATS_TABLE .
193 " WHERE google_place_id = %d ORDER BY id DESC LIMIT 1", $db_place_id
194 )
195 );
196 if (count($stats) > 0) {
197 if ($stats[0]->rating != $rating || ($review_count > 0 && $stats[0]->review_count != $review_count)) {
198 $this->insert_stats($rating, $review_count, $db_place_id);
199 }
200 } else {
201 $this->insert_stats($rating, $review_count, $db_place_id);
202 }
203 }
204
205 public function insert_stats($rating, $review_count, $db_place_id) {
206 global $wpdb;
207
208 $wpdb->insert($wpdb->prefix . Database::STATS_TABLE, array(
209 'google_place_id' => $db_place_id,
210 'time' => time(),
211 'rating' => $rating,
212 'review_count' => $review_count
213 ));
214 $this->log_last_error($wpdb);
215 }
216
217 public function update_review($review, $review_lang, $author_img, $images, $reply, $reply_time, $db_review_id, &$log = []) {
218 global $wpdb;
219
220 $update_params = array(
221 'rating' => $review->rating,
222 'text' => $review->text
223 );
224 if ($author_img) {
225 $update_params['profile_photo_url'] = $author_img;
226 }
227 if ($images) {
228 $update_params['images'] = $images;
229 }
230 if ($reply) {
231 $update_params['reply'] = $reply;
232 $update_params['reply_time'] = $reply_time;
233 }
234 if (isset($review->url)) {
235 $update_params['url'] = $review->url;
236 }
237 if (isset($review->provider)) {
238 $update_params['provider'] = $review->provider;
239 }
240 $wpdb->update($wpdb->prefix . Database::REVIEW_TABLE, $update_params, array('id' => $db_review_id));
241
242 array_push(
243 $log,
244 'ur[' .
245 $db_review_id . ',' .
246 $review->author_name . ',' .
247 $review->rating . ',' .
248 (isset($review->text) ? strlen($review->text) : '') . ',' .
249 $review_lang .
250 ']'
251 );
252 }
253
254 public function insert_review($review, $review_lang, $author_img, $images, $reply, $reply_time, $db_place_id, &$log = []) {
255 global $wpdb;
256
257 $wpdb->insert($wpdb->prefix . Database::REVIEW_TABLE, array(
258 'google_place_id' => $db_place_id,
259 'rating' => $review->rating,
260 'text' => $review->text,
261 'time' => $review->time,
262 'language' => $review_lang,
263 'author_name' => $review->author_name,
264 'author_url' => isset($review->author_url) ? $review->author_url : null,
265 'profile_photo_url' => $author_img,
266 'url' => isset($review->url) ? $review->url : null,
267 'provider' => isset($review->provider) ? $review->provider : null,
268 'images' => $images,
269 'reply' => $reply,
270 'reply_time' => $reply_time
271 ));
272
273 array_push(
274 $log,
275 'ir[' .
276 $review->author_name . ',' .
277 $review->rating . ',' .
278 (isset($review->text) ? strlen($review->text) : '') . ',' .
279 $review_lang .
280 ']'
281 );
282 }
283
284 private function log_last_error($wpdb) {
285 if (isset($wpdb->last_error) && strlen($wpdb->last_error) > 0) {
286 $last_error = $wpdb->last_error;
287 $opt = get_option('grw_last_error');
288 if (empty($opt)) {
289 $now = floor(microtime(true) * 1000);
290 $opt = $now . ': ';
291 }
292 update_option('grw_last_error', $opt . $last_error . '; ');
293 }
294 }
295 }