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