PluginProbe ʕ •ᴥ•ʔ
Rich Showcase for Google Reviews / 1.7
Rich Showcase for Google Reviews v1.7
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 / grw.php
widget-google-reviews Last commit date
api 9 years ago helper 9 years ago languages 8 years ago static 7 years ago grw-finder.php 7 years ago grw-options.php 7 years ago grw-reviews-helper.php 7 years ago grw-reviews.php 7 years ago grw-setting.php 7 years ago grw-widget.php 7 years ago grw.php 7 years ago readme.txt 7 years ago
grw.php
545 lines
1 <?php
2 /*
3 Plugin Name: Google Reviews Widget
4 Plugin URI: https://richplugins.com/google-reviews-pro-wordpress-plugin
5 Description: Instantly Google Places Reviews on your website to increase user confidence and SEO.
6 Author: RichPlugins <support@richplugins.com>
7 Version: 1.7
8 Author URI: https://richplugins.com
9 */
10
11 require(ABSPATH . 'wp-includes/version.php');
12
13 include_once(dirname(__FILE__) . '/api/urlopen.php');
14 include_once(dirname(__FILE__) . '/helper/debug.php');
15
16 define('GRW_VERSION', '1.7');
17 define('GRW_GOOGLE_PLACE_API', 'https://maps.googleapis.com/maps/api/place/');
18 define('GRW_GOOGLE_AVATAR', 'https://lh3.googleusercontent.com/-8hepWJzFXpE/AAAAAAAAAAI/AAAAAAAAAAA/I80WzYfIxCQ/s64-c/114307615494839964028.jpg');
19 define('GRW_PLUGIN_URL', plugins_url(basename(plugin_dir_path(__FILE__ )), basename(__FILE__)));
20
21 function grw_options() {
22 return array(
23 'grw_version',
24 'grw_active',
25 'grw_google_api_key',
26 'grw_language',
27 );
28 }
29
30 /*-------------------------------- Widget --------------------------------*/
31 function grw_init_widget() {
32 if (!class_exists('Goog_Reviews_Widget' ) ) {
33 require 'grw-widget.php';
34 }
35 }
36 add_action('widgets_init', 'grw_init_widget');
37
38 function grw_register_widget() {
39 return register_widget("Goog_Reviews_Widget");
40 }
41 add_action('widgets_init', 'grw_register_widget');
42
43 /*-------------------------------- Menu --------------------------------*/
44 function grw_setting_menu() {
45 add_submenu_page(
46 'options-general.php',
47 'Google Reviews Widget',
48 'Google Reviews Widget',
49 'moderate_comments',
50 'grw',
51 'grw_setting'
52 );
53 }
54 add_action('admin_menu', 'grw_setting_menu', 10);
55
56 function grw_setting() {
57 include_once(dirname(__FILE__) . '/grw-setting.php');
58 }
59
60 /*-------------------------------- Links --------------------------------*/
61 function grw_plugin_action_links($links, $file) {
62 $plugin_file = basename(__FILE__);
63 if (basename($file) == $plugin_file) {
64 $settings_link = '<a href="' . admin_url('options-general.php?page=grw') . '">'.grw_i('Settings') . '</a>';
65 array_unshift($links, $settings_link);
66 }
67 return $links;
68 }
69 add_filter('plugin_action_links', 'grw_plugin_action_links', 10, 2);
70
71 /*-------------------------------- Row Meta --------------------------------*/
72 function grw_plugin_row_meta($input, $file) {
73 if ($file != plugin_basename( __FILE__ )) {
74 return $input;
75 }
76
77 $links = array(
78 '<a href="' . esc_url('https://richplugins.com/documentation') . '" target="_blank">' . grw_i('View Documentation') . '</a>',
79 '<a href="' . esc_url('https://richplugins.com/google-reviews-pro-wordpress-plugin') . '" target="_blank">' . grw_i('Upgrade to Business') . ' &raquo;</a>',
80 );
81 $input = array_merge($input, $links);
82 return $input;
83 }
84 add_filter('plugin_row_meta', 'grw_plugin_row_meta', 10, 2);
85
86 /*-------------------------------- Database --------------------------------*/
87 function grw_activation($network_wide = false) {
88 add_option('grw_is_multisite', $network_wide);
89 if (grw_does_need_update()) {
90 grw_install();
91 }
92 }
93 register_activation_hook(__FILE__, 'grw_activation');
94
95 function grw_install() {
96
97 $version = (string)get_option('grw_version');
98 if (!$version) {
99 $version = '0';
100 }
101
102 $network_wide = get_option('grw_is_multisite');
103
104 if ($network_wide) {
105 $site_ids = get_sites(array(
106 'fields' => 'ids',
107 'network_id' => get_current_network_id()
108 ));
109 foreach($site_ids as $site_id) {
110 switch_to_blog($site_id);
111 grw_install_single_site($version);
112 restore_current_blog();
113 }
114 } else {
115 grw_install_single_site($version);
116 }
117 }
118
119 function grw_install_single_site($version) {
120 grw_install_db();
121
122 if (version_compare($version, GRW_VERSION, '=')) {
123 return;
124 }
125
126 add_option('grw_active', '1');
127 add_option('grw_google_api_key', '');
128 update_option('grw_version', GRW_VERSION);
129 }
130
131 function grw_install_db() {
132 global $wpdb;
133
134 $charset_collate = $wpdb->get_charset_collate();
135
136 $sql = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix . "grp_google_place (".
137 "id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,".
138 "place_id VARCHAR(80) NOT NULL,".
139 "name VARCHAR(255) NOT NULL,".
140 "photo VARCHAR(255),".
141 "icon VARCHAR(255),".
142 "address VARCHAR(255),".
143 "rating DOUBLE PRECISION,".
144 "url VARCHAR(255),".
145 "website VARCHAR(255),".
146 "updated BIGINT(20),".
147 "PRIMARY KEY (`id`),".
148 "UNIQUE INDEX grp_place_id (`place_id`)".
149 ") " . $charset_collate . ";";
150
151 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
152
153 dbDelta($sql);
154
155 $sql = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix . "grp_google_review (".
156 "id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,".
157 "google_place_id BIGINT(20) UNSIGNED NOT NULL,".
158 "hash VARCHAR(40) NOT NULL,".
159 "rating INTEGER NOT NULL,".
160 "text VARCHAR(10000),".
161 "time INTEGER NOT NULL,".
162 "language VARCHAR(10),".
163 "author_name VARCHAR(255),".
164 "author_url VARCHAR(255),".
165 "profile_photo_url VARCHAR(255),".
166 "PRIMARY KEY (`id`),".
167 "UNIQUE INDEX grp_google_review_hash (`hash`),".
168 "INDEX grp_google_place_id (`google_place_id`)".
169 ") " . $charset_collate . ";";
170
171 dbDelta($sql);
172 }
173
174 function grw_reset($reset_db) {
175 global $wpdb;
176
177 if (function_exists('is_multisite') && is_multisite()) {
178 $current_blog_id = get_current_blog_id();
179 $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
180 foreach ($blog_ids as $blog_id) {
181 switch_to_blog($blog_id);
182 grw_reset_data($reset_db);
183 }
184 switch_to_blog($current_blog_id);
185 } else {
186 grw_reset_data($reset_db);
187 }
188 }
189
190 function grw_reset_data($reset_db) {
191 global $wpdb;
192
193 foreach (grw_options() as $opt) {
194 delete_option($opt);
195 }
196 if ($reset_db) {
197 $wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . "grp_google_place;");
198 $wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . "grp_google_review;");
199 }
200 }
201
202 /*-------------------------------- Request --------------------------------*/
203 function grw_request_handler() {
204 global $wpdb;
205
206 if (!empty($_GET['cf_action'])) {
207
208 switch ($_GET['cf_action']) {
209 case 'grw_google_api_key':
210 if (current_user_can('manage_options')) {
211 if (isset($_POST['grw_wpnonce']) === false) {
212 $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.');
213 $response = compact('error');
214 } else {
215 check_admin_referer('grw_wpnonce', 'grw_wpnonce');
216
217 update_option('grw_google_api_key', trim(sanitize_text_field($_POST['key'])));
218 $status = 'success';
219 $response = compact('status');
220
221 }
222 header('Content-type: text/javascript');
223 echo json_encode($response);
224 die();
225 }
226 break;
227 case 'grw_search':
228 if (current_user_can('manage_options')) {
229 if (isset($_GET['grw_wpnonce']) === false) {
230 $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.');
231 $response = compact('error');
232 } else {
233 check_admin_referer('grw_wpnonce', 'grw_wpnonce');
234
235 $grw_google_api_key = get_option('grw_google_api_key');
236 $url = GRW_GOOGLE_PLACE_API . 'textsearch/json?query=' . $_GET['query'] . '&key=' . $grw_google_api_key;
237
238 $grw_language = get_option('grw_language');
239 if (strlen($grw_language) > 0) {
240 $url = $url . '&language=' . $grw_language;
241 }
242
243 $response = rplg_urlopen($url);
244
245 $response_data = $response['data'];
246 $response_json = rplg_json_decode($response_data);
247 $response_results = $response_json->results;
248
249 foreach ($response_results as $result) {
250 $result->business_photo = grw_business_avatar($result);
251 }
252 }
253 header('Content-type: text/javascript');
254 echo json_encode($response_results);
255 die();
256 }
257 break;
258 case 'grw_reviews':
259 if (current_user_can('manage_options')) {
260 if (isset($_GET['grw_wpnonce']) === false) {
261 $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.');
262 $response = compact('error');
263 } else {
264 check_admin_referer('grw_wpnonce', 'grw_wpnonce');
265
266 $url = grw_api_url($_GET['placeid']);
267
268 $response = rplg_urlopen($url);
269
270 $response_data = $response['data'];
271 $response_json = rplg_json_decode($response_data);
272 $response_result = $response_json->result;
273
274 if (isset($response_result)) {
275 $response_result->business_photo = grw_business_avatar($response_result);
276 }
277 }
278 header('Content-type: text/javascript');
279 echo json_encode($response_json->result);
280 die();
281 }
282 break;
283 case 'grw_save':
284 if (current_user_can('manage_options')) {
285 if (isset($_POST['grw_wpnonce']) === false) {
286 $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.');
287 $response = compact('error');
288 } else {
289 check_admin_referer('grw_wpnonce', 'grw_wpnonce');
290
291 $url = grw_api_url($_POST['placeid'], $_POST['lang']);
292
293 $response = rplg_urlopen($url);
294
295 $response_data = $response['data'];
296 $response_json = rplg_json_decode($response_data);
297
298 if ($response_json && isset($response_json->result)) {
299 $response_json->result->business_photo = grw_business_avatar($response_json->result);
300 grw_save_reviews($response_json->result);
301 $result = $response_json->result;
302 $status = 'success';
303 } else {
304 $result = $response_json;
305 $status = 'failed';
306 }
307 $response = compact('status', 'result');
308 }
309 header('Content-type: text/javascript');
310 echo json_encode($response);
311 die();
312 }
313 break;
314 }
315 }
316 }
317 add_action('init', 'grw_request_handler');
318
319 function grw_save_reviews($place, $min_filter = 0) {
320 global $wpdb;
321
322 $google_place_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "grp_google_place WHERE place_id = %s", $place->place_id));
323 if ($google_place_id) {
324 $wpdb->update($wpdb->prefix . 'grp_google_place', array(
325 'name' => $place->name,
326 'photo' => $place->business_photo,
327 'rating' => $place->rating
328 ), array('ID' => $google_place_id));
329 } else {
330 $wpdb->insert($wpdb->prefix . 'grp_google_place', array(
331 'place_id' => $place->place_id,
332 'name' => $place->name,
333 'photo' => $place->business_photo,
334 'icon' => $place->icon,
335 'address' => $place->formatted_address,
336 'rating' => isset($place->rating) ? $place->rating : null,
337 'url' => isset($place->url) ? $place->url : null,
338 'website' => isset($place->website) ? $place->website : null
339 ));
340 $google_place_id = $wpdb->insert_id;
341 }
342
343 if ($place->reviews) {
344 $reviews = $place->reviews;
345 foreach ($reviews as $review) {
346 if ($min_filter > 0 && $min_filter > $review->rating) {
347 continue;
348 }
349
350 $google_review_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "grp_google_review WHERE time = %s", $review->time));
351 if ($google_review_id) {
352 $update_params = array(
353 'rating' => $review->rating,
354 'text' => $review->text
355 );
356 if (isset($review->profile_photo_url)) {
357 $update_params['profile_photo_url'] = $review->profile_photo_url;
358 }
359 $wpdb->update($wpdb->prefix . 'grp_google_review', $update_params, array('id' => $google_review_id));
360 } else {
361 $wpdb->insert($wpdb->prefix . 'grp_google_review', array(
362 'google_place_id' => $google_place_id,
363 'hash' => $review->time, //TODO: workaround to support old versions
364 'rating' => $review->rating,
365 'text' => $review->text,
366 'time' => $review->time,
367 'language' => $review->language,
368 'author_name' => $review->author_name,
369 'author_url' => isset($review->author_url) ? $review->author_url : null,
370 'profile_photo_url' => isset($review->profile_photo_url) ? $review->profile_photo_url : null
371 ));
372 }
373 }
374 }
375 }
376
377 function grw_lang_init() {
378 $plugin_dir = basename(dirname(__FILE__));
379 load_plugin_textdomain('grw', false, basename( dirname( __FILE__ ) ) . '/languages');
380 }
381 add_action('plugins_loaded', 'grw_lang_init');
382
383 /*-------------------------------- Helpers --------------------------------*/
384 function grw_enabled() {
385 global $id, $post;
386
387 $active = get_option('grw_active');
388 if (empty($active) || $active === '0') { return false; }
389 return true;
390 }
391
392 function grw_api_url($placeid, $reviews_lang = '') {
393 $url = GRW_GOOGLE_PLACE_API . 'details/json?placeid=' . $placeid . '&key=' . get_option('grw_google_api_key');
394
395 $grw_language = strlen($reviews_lang) > 0 ? $reviews_lang : get_option('grw_language');
396 if (strlen($grw_language) > 0) {
397 $url = $url . '&language=' . $grw_language;
398 }
399 return $url;
400 }
401
402 function grw_business_avatar($response_result_json) {
403 if (isset($response_result_json->photos)) {
404 $request_url = add_query_arg(
405 array(
406 'photoreference' => $response_result_json->photos[0]->photo_reference,
407 'key' => get_option('grw_google_api_key'),
408 'maxwidth' => '300',
409 'maxheight' => '300',
410 ),
411 'https://maps.googleapis.com/maps/api/place/photo'
412 );
413 $response = rplg_urlopen($request_url);
414 foreach ($response['headers'] as $header) {
415 if (strpos($header, 'Location: ') !== false) {
416 return str_replace('Location: ', '', $header);
417 }
418 }
419 }
420 return null;
421 }
422
423 function grw_does_need_update() {
424 $version = (string)get_option('grw_version');
425 if (empty($version)) {
426 $version = '0';
427 }
428 if (version_compare($version, '1.0', '<')) {
429 return true;
430 }
431 return false;
432 }
433
434 function grw_i($text, $params=null) {
435 if (!is_array($params)) {
436 $params = func_get_args();
437 $params = array_slice($params, 1);
438 }
439 return vsprintf(__($text, 'grw'), $params);
440 }
441
442 if (!function_exists('esc_html')) {
443 function esc_html( $text ) {
444 $safe_text = wp_check_invalid_utf8( $text );
445 $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
446 return apply_filters( 'esc_html', $safe_text, $text );
447 }
448 }
449
450 if (!function_exists('esc_attr')) {
451 function esc_attr( $text ) {
452 $safe_text = wp_check_invalid_utf8( $text );
453 $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
454 return apply_filters( 'attribute_escape', $safe_text, $text );
455 }
456 }
457
458 /**
459 * JSON ENCODE for PHP < 5.2.0
460 */
461 if (!function_exists('json_encode')) {
462
463 function json_encode($data) {
464 return cfjson_encode($data);
465 }
466
467 function cfjson_encode_string($str) {
468 if(is_bool($str)) {
469 return $str ? 'true' : 'false';
470 }
471
472 return str_replace(
473 array(
474 '\\'
475 , '"'
476 //, '/'
477 , "\n"
478 , "\r"
479 )
480 , array(
481 '\\\\'
482 , '\"'
483 //, '\/'
484 , '\n'
485 , '\r'
486 )
487 , $str
488 );
489 }
490
491 function cfjson_encode($arr) {
492 $json_str = '';
493 if (is_array($arr)) {
494 $pure_array = true;
495 $array_length = count($arr);
496 for ( $i = 0; $i < $array_length ; $i++) {
497 if (!isset($arr[$i])) {
498 $pure_array = false;
499 break;
500 }
501 }
502 if ($pure_array) {
503 $json_str = '[';
504 $temp = array();
505 for ($i=0; $i < $array_length; $i++) {
506 $temp[] = sprintf("%s", cfjson_encode($arr[$i]));
507 }
508 $json_str .= implode(',', $temp);
509 $json_str .="]";
510 }
511 else {
512 $json_str = '{';
513 $temp = array();
514 foreach ($arr as $key => $value) {
515 $temp[] = sprintf("\"%s\":%s", $key, cfjson_encode($value));
516 }
517 $json_str .= implode(',', $temp);
518 $json_str .= '}';
519 }
520 }
521 else if (is_object($arr)) {
522 $json_str = '{';
523 $temp = array();
524 foreach ($arr as $k => $v) {
525 $temp[] = '"'.$k.'":'.cfjson_encode($v);
526 }
527 $json_str .= implode(',', $temp);
528 $json_str .= '}';
529 }
530 else if (is_string($arr)) {
531 $json_str = '"'. cfjson_encode_string($arr) . '"';
532 }
533 else if (is_numeric($arr)) {
534 $json_str = $arr;
535 }
536 else if (is_bool($arr)) {
537 $json_str = $arr ? 'true' : 'false';
538 }
539 else {
540 $json_str = '"'. cfjson_encode_string($arr) . '"';
541 }
542 return $json_str;
543 }
544 }
545 ?>