widget-google-reviews
Last commit date
api
9 years ago
languages
9 years ago
static
9 years ago
grw-reviews-helper.php
9 years ago
grw-reviews.php
9 years ago
grw-setting.php
9 years ago
grw-widget.php
9 years ago
grw.php
9 years ago
readme.txt
9 years ago
grw.php
480 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.5 |
| 8 | Author URI: https://richplugins.com/google-reviews-pro-wordpress-plugin |
| 9 | */ |
| 10 | |
| 11 | require(ABSPATH . 'wp-includes/version.php'); |
| 12 | |
| 13 | require_once(dirname(__FILE__) . '/api/grw-api.php'); |
| 14 | |
| 15 | define('GRW_VERSION', '1.5'); |
| 16 | define('GRW_GOOGLE_PLACE_API', 'https://maps.googleapis.com/maps/api/place/'); |
| 17 | define('GRW_GOOGLE_AVATAR', 'https://lh3.googleusercontent.com/-8hepWJzFXpE/AAAAAAAAAAI/AAAAAAAAAAA/I80WzYfIxCQ/s64-c/114307615494839964028.jpg'); |
| 18 | define('GRW_PLUGIN_URL', plugins_url(basename(plugin_dir_path(__FILE__ )), basename(__FILE__))); |
| 19 | |
| 20 | function grw_google_api_key() { |
| 21 | $keys = array( |
| 22 | 'AIzaSyAi0nomk8c1CBMRiKWuFxdfyF3QFFXZSUM', |
| 23 | 'AIzaSyA_7b55nlagZcTLT8Fiz_x8Hae-D93H5_g', |
| 24 | 'AIzaSyALYBD3qLHSyI86bRtrh_5vr_4Tr2fLoIM', |
| 25 | 'AIzaSyB84O9Qy34aHpilZT2wrDYtWc6X8j2q-XM', |
| 26 | 'AIzaSyB6S2Rl0iczRwO_8udMuGqaQA1ttCE1NgA', |
| 27 | 'AIzaSyBuUPywXpgXMu-RTsuEIBB5gQgp0N114cI', |
| 28 | 'AIzaSyAETBwpe6xxwNXN6fa6q0ray9g5nAq8Dl0', |
| 29 | 'AIzaSyDLTEEEUBSdmeCbps5rLKKBRCC14KmcXv4', |
| 30 | 'AIzaSyDcxWMaaAhpeOFikmam_s3xwXJF7iSlJsc', |
| 31 | 'AIzaSyC12ZI0DTBuvfTbxw7IAkFdhuuaaRsX6lw', |
| 32 | 'AIzaSyCQ2rPW43kyRmlAh1x9k17dQfNi0BDm5Ds', |
| 33 | 'AIzaSyDhhxmCvkORZxJ7Fpo4pElNuDkfV_r74Jc', |
| 34 | 'AIzaSyB3k4oWDJPFR30LLjj27k5MdupZ9yMfXqE', |
| 35 | 'AIzaSyAw9xnTPxpBnYvFuE73_Son-Yy_-opo9Fs', |
| 36 | 'AIzaSyA42G8C-Q0Kr3uLJHmNcngYHaIK5kB6geg', |
| 37 | 'AIzaSyBuXKuG_ITnAg92vsLdyExiY56TEXUphnE', |
| 38 | 'AIzaSyDrYf4yX5Wf5ugvbljgnMfyd8kRhi9hoik' |
| 39 | ); |
| 40 | $idx = array_rand($keys); |
| 41 | return $keys[$idx]; |
| 42 | } |
| 43 | |
| 44 | function grw_options() { |
| 45 | return array( |
| 46 | 'grw_version', |
| 47 | 'grw_active', |
| 48 | 'grw_google_api_key', |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | $grw_api = new GoogleReviewsWidgetAPI(); |
| 53 | |
| 54 | /*-------------------------------- Widget --------------------------------*/ |
| 55 | function grw_init_widget() { |
| 56 | if (!class_exists('Goog_Reviews_Widget' ) ) { |
| 57 | require 'grw-widget.php'; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | add_action('widgets_init', 'grw_init_widget'); |
| 62 | add_action('widgets_init', create_function('', 'register_widget("Goog_Reviews_Widget");')); |
| 63 | |
| 64 | /*-------------------------------- Menu --------------------------------*/ |
| 65 | function grw_setting_menu() { |
| 66 | add_submenu_page( |
| 67 | 'options-general.php', |
| 68 | 'Google Reviews Widget', |
| 69 | 'Google Reviews Widget', |
| 70 | 'moderate_comments', |
| 71 | 'grw', |
| 72 | 'grw_setting' |
| 73 | ); |
| 74 | } |
| 75 | add_action('admin_menu', 'grw_setting_menu', 10); |
| 76 | |
| 77 | function grw_setting() { |
| 78 | include_once(dirname(__FILE__) . '/grw-setting.php'); |
| 79 | } |
| 80 | |
| 81 | /*-------------------------------- Links --------------------------------*/ |
| 82 | function grw_plugin_action_links($links, $file) { |
| 83 | $plugin_file = basename(__FILE__); |
| 84 | if (basename($file) == $plugin_file) { |
| 85 | $settings_link = '<a href="' . admin_url('options-general.php?page=grw') . '">'.grw_i('Settings') . '</a>'; |
| 86 | array_unshift($links, $settings_link); |
| 87 | } |
| 88 | return $links; |
| 89 | } |
| 90 | add_filter('plugin_action_links', 'grw_plugin_action_links', 10, 2); |
| 91 | |
| 92 | /*-------------------------------- Database --------------------------------*/ |
| 93 | function grw_activation() { |
| 94 | if (grw_does_need_update()) { |
| 95 | grw_install(); |
| 96 | } |
| 97 | } |
| 98 | register_activation_hook(__FILE__, 'grw_activation'); |
| 99 | |
| 100 | function grw_install($allow_db_install=true) { |
| 101 | global $wpdb, $userdata; |
| 102 | |
| 103 | $version = (string)get_option('grw_version'); |
| 104 | if (!$version) { |
| 105 | $version = '0'; |
| 106 | } |
| 107 | |
| 108 | if ($allow_db_install) { |
| 109 | grw_install_db($version); |
| 110 | } |
| 111 | |
| 112 | if (version_compare($version, GRW_VERSION, '=')) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | add_option('grw_active', '1'); |
| 117 | add_option('grw_google_api_key', ''); |
| 118 | update_option('grw_version', GRW_VERSION); |
| 119 | } |
| 120 | |
| 121 | function grw_install_db() { |
| 122 | global $wpdb; |
| 123 | |
| 124 | $wpdb->query("CREATE TABLE " . $wpdb->prefix . "grp_google_place (". |
| 125 | "id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,". |
| 126 | "place_id VARCHAR(80) NOT NULL,". |
| 127 | "name VARCHAR(255) NOT NULL,". |
| 128 | "photo VARCHAR(255),". |
| 129 | "icon VARCHAR(255),". |
| 130 | "address VARCHAR(255),". |
| 131 | "rating DOUBLE PRECISION,". |
| 132 | "url VARCHAR(255),". |
| 133 | "website VARCHAR(255),". |
| 134 | "updated BIGINT(20),". |
| 135 | "PRIMARY KEY (`id`),". |
| 136 | "UNIQUE INDEX grp_place_id (`place_id`)". |
| 137 | ");"); |
| 138 | |
| 139 | $wpdb->query("CREATE TABLE " . $wpdb->prefix . "grp_google_review (". |
| 140 | "id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,". |
| 141 | "google_place_id BIGINT(20) UNSIGNED NOT NULL,". |
| 142 | "hash VARCHAR(40) NOT NULL,". |
| 143 | "rating INTEGER NOT NULL,". |
| 144 | "text VARCHAR(10000),". |
| 145 | "time INTEGER NOT NULL,". |
| 146 | "language VARCHAR(2),". |
| 147 | "author_name VARCHAR(255),". |
| 148 | "author_url VARCHAR(255),". |
| 149 | "profile_photo_url VARCHAR(255),". |
| 150 | "PRIMARY KEY (`id`),". |
| 151 | "UNIQUE INDEX grp_google_review_hash (`hash`),". |
| 152 | "INDEX grp_google_place_id (`google_place_id`)". |
| 153 | ");"); |
| 154 | } |
| 155 | |
| 156 | function grw_reset_db() { |
| 157 | global $wpdb; |
| 158 | |
| 159 | $wpdb->query("DROP TABLE " . $wpdb->prefix . "grp_google_place;"); |
| 160 | $wpdb->query("DROP TABLE " . $wpdb->prefix . "grp_google_review;"); |
| 161 | } |
| 162 | |
| 163 | /*-------------------------------- Request --------------------------------*/ |
| 164 | function grw_request_handler() { |
| 165 | global $grw_api; |
| 166 | global $wpdb; |
| 167 | |
| 168 | if (!empty($_GET['cf_action'])) { |
| 169 | |
| 170 | if (!empty($_GET['key'])) { |
| 171 | $google_api_key = $_GET['key']; |
| 172 | } else { |
| 173 | $google_api_key = get_option('grw_google_api_key'); |
| 174 | } |
| 175 | if (!$google_api_key) { |
| 176 | $google_api_key = grw_google_api_key(); |
| 177 | } |
| 178 | |
| 179 | switch ($_GET['cf_action']) { |
| 180 | case 'grw_google_api_key': |
| 181 | if (current_user_can('manage_options')) { |
| 182 | if (isset($_POST['_textsearch_wpnonce']) === false) { |
| 183 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 184 | $response = compact('error'); |
| 185 | } else { |
| 186 | check_admin_referer('grw-wpnonce_grw_textsearch', '_textsearch_wpnonce'); |
| 187 | update_option('grw_google_api_key', $_POST['key']); |
| 188 | $status = 'success'; |
| 189 | $response = compact('status'); |
| 190 | } |
| 191 | header('Content-type: text/javascript'); |
| 192 | echo cf_json_encode($response); |
| 193 | die(); |
| 194 | } |
| 195 | break; |
| 196 | case 'textsearch': |
| 197 | if (current_user_can('manage_options')) { |
| 198 | if (isset($_GET['_textsearch_wpnonce']) === false) { |
| 199 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 200 | $response = compact('error'); |
| 201 | } else { |
| 202 | check_admin_referer('grw-wpnonce_grw_textsearch', '_textsearch_wpnonce'); |
| 203 | $response = $grw_api->textsearch(array( |
| 204 | 'query' => $_GET['query'], |
| 205 | 'key' => $google_api_key, |
| 206 | )); |
| 207 | } |
| 208 | header('Content-type: text/javascript'); |
| 209 | echo cf_json_encode($response); |
| 210 | die(); |
| 211 | } |
| 212 | break; |
| 213 | case 'details': |
| 214 | if (current_user_can('manage_options')) { |
| 215 | if (isset($_GET['_textsearch_wpnonce']) === false) { |
| 216 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 217 | $response = compact('error'); |
| 218 | } else { |
| 219 | check_admin_referer('grw-wpnonce_grw_textsearch', '_textsearch_wpnonce'); |
| 220 | $response = $grw_api->details(array( |
| 221 | 'placeid' => $_GET['placeid'], |
| 222 | 'key' => $google_api_key, |
| 223 | )); |
| 224 | } |
| 225 | header('Content-type: text/javascript'); |
| 226 | echo cf_json_encode($response); |
| 227 | die(); |
| 228 | } |
| 229 | break; |
| 230 | case 'save': |
| 231 | if (current_user_can('manage_options')) { |
| 232 | if (isset($_POST['_textsearch_wpnonce']) === false) { |
| 233 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 234 | $response = compact('error'); |
| 235 | } else { |
| 236 | check_admin_referer('grw-wpnonce_grw_textsearch', '_textsearch_wpnonce'); |
| 237 | $result = $grw_api->details(array( |
| 238 | 'placeid' => $_POST['placeid'], |
| 239 | 'key' => $google_api_key, |
| 240 | )); |
| 241 | if ($result['place']) { |
| 242 | grw_save_reviews($result['place']); |
| 243 | $status = 'success'; |
| 244 | } else { |
| 245 | $status = 'failed'; |
| 246 | } |
| 247 | $response = compact('status'); |
| 248 | } |
| 249 | header('Content-type: text/javascript'); |
| 250 | echo cf_json_encode($response); |
| 251 | die(); |
| 252 | } |
| 253 | break; |
| 254 | case 'save_json': |
| 255 | if (current_user_can('manage_options')) { |
| 256 | if (isset($_POST['_textsearch_wpnonce']) === false) { |
| 257 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 258 | $response = compact('error'); |
| 259 | } else { |
| 260 | check_admin_referer('grw-wpnonce_grw_textsearch', '_textsearch_wpnonce'); |
| 261 | |
| 262 | $place_json = stripslashes($_POST['place']); |
| 263 | $reviews_json = stripslashes($_POST['reviews']); |
| 264 | $place = grw_json_decode($place_json); |
| 265 | $reviews = grw_json_decode($reviews_json); |
| 266 | $place_array = get_object_vars($place); |
| 267 | $place_array['reviews'] = array(); |
| 268 | foreach ($reviews as $review) { |
| 269 | array_push($place_array['reviews'], get_object_vars($review)); |
| 270 | } |
| 271 | |
| 272 | if ($place_array && strlen($place_array['place_id']) > 0) { |
| 273 | grw_save_reviews($place_array); |
| 274 | $status = 'success'; |
| 275 | } else { |
| 276 | $status = 'failed'; |
| 277 | } |
| 278 | $response = compact('status'); |
| 279 | } |
| 280 | header('Content-type: text/javascript'); |
| 281 | echo cf_json_encode($response); |
| 282 | die(); |
| 283 | } |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | add_action('init', 'grw_request_handler'); |
| 289 | |
| 290 | function grw_save_reviews($place, $min_filter = 0) { |
| 291 | global $wpdb; |
| 292 | |
| 293 | $google_place_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "grp_google_place WHERE place_id = %s", $place['place_id'])); |
| 294 | if ($google_place_id) { |
| 295 | $wpdb->update($wpdb->prefix . 'grp_google_place', array('rating' => $place['rating']), array('ID' => $google_place_id)); |
| 296 | } else { |
| 297 | $wpdb->insert($wpdb->prefix . 'grp_google_place', array( |
| 298 | 'place_id' => $place['place_id'], |
| 299 | 'name' => $place['name'], |
| 300 | 'photo' => $place['photo'], |
| 301 | 'icon' => $place['icon'], |
| 302 | 'address' => $place['formatted_address'], |
| 303 | 'rating' => $place['rating'], |
| 304 | 'url' => $place['url'], |
| 305 | 'website' => $place['website'] |
| 306 | )); |
| 307 | $google_place_id = $wpdb->insert_id; |
| 308 | } |
| 309 | |
| 310 | if ($place['reviews']) { |
| 311 | $reviews = $place['reviews']; |
| 312 | foreach ($reviews as $review) { |
| 313 | if ($min_filter > 0 && $min_filter > $review['rating']) { |
| 314 | continue; |
| 315 | } |
| 316 | if (strlen($review['author_url']) < 1) { |
| 317 | continue; |
| 318 | } |
| 319 | $hash = sha1($place['place_id'] . $review['author_url']); |
| 320 | $google_review_hash = $wpdb->get_var($wpdb->prepare("SELECT hash FROM " . $wpdb->prefix . "grp_google_review WHERE hash = %s", $hash)); |
| 321 | if (!$google_review_hash) { |
| 322 | $wpdb->insert($wpdb->prefix . 'grp_google_review', array( |
| 323 | 'google_place_id' => $google_place_id, |
| 324 | 'hash' => $hash, |
| 325 | 'rating' => $review['rating'], |
| 326 | 'text' => $review['text'], |
| 327 | 'time' => $review['time'], |
| 328 | 'language' => $review['language'], |
| 329 | 'author_name' => $review['author_name'], |
| 330 | 'author_url' => $review['author_url'], |
| 331 | 'profile_photo_url' => $review['profile_photo_url'] |
| 332 | )); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | function grw_lang_init() { |
| 339 | $plugin_dir = basename(dirname(__FILE__)); |
| 340 | load_plugin_textdomain('grw', false, basename( dirname( __FILE__ ) ) . '/languages'); |
| 341 | } |
| 342 | add_action('plugins_loaded', 'grw_lang_init'); |
| 343 | |
| 344 | /*-------------------------------- Helpers --------------------------------*/ |
| 345 | function grw_enabled() { |
| 346 | global $id, $post; |
| 347 | |
| 348 | $active = get_option('grw_active'); |
| 349 | if (empty($active) || $active === '0') { return false; } |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | function grw_does_need_update() { |
| 354 | $version = (string)get_option('grw_version'); |
| 355 | if (empty($version)) { |
| 356 | $version = '0'; |
| 357 | } |
| 358 | if (version_compare($version, '1.0', '<')) { |
| 359 | return true; |
| 360 | } |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | function grw_i($text, $params=null) { |
| 365 | if (!is_array($params)) { |
| 366 | $params = func_get_args(); |
| 367 | $params = array_slice($params, 1); |
| 368 | } |
| 369 | return vsprintf(__($text, 'grw'), $params); |
| 370 | } |
| 371 | |
| 372 | if (!function_exists('esc_html')) { |
| 373 | function esc_html( $text ) { |
| 374 | $safe_text = wp_check_invalid_utf8( $text ); |
| 375 | $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES ); |
| 376 | return apply_filters( 'esc_html', $safe_text, $text ); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if (!function_exists('esc_attr')) { |
| 381 | function esc_attr( $text ) { |
| 382 | $safe_text = wp_check_invalid_utf8( $text ); |
| 383 | $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES ); |
| 384 | return apply_filters( 'attribute_escape', $safe_text, $text ); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * JSON ENCODE for PHP < 5.2.0 |
| 390 | * Checks if json_encode is not available and defines json_encode |
| 391 | * to use php_json_encode in its stead |
| 392 | * Works on iteratable objects as well - stdClass is iteratable, so all WP objects are gonna be iteratable |
| 393 | */ |
| 394 | if(!function_exists('cf_json_encode')) { |
| 395 | function cf_json_encode($data) { |
| 396 | |
| 397 | // json_encode is sending an application/x-javascript header on Joyent servers |
| 398 | // for some unknown reason. |
| 399 | return cfjson_encode($data); |
| 400 | } |
| 401 | |
| 402 | function cfjson_encode_string($str) { |
| 403 | if(is_bool($str)) { |
| 404 | return $str ? 'true' : 'false'; |
| 405 | } |
| 406 | |
| 407 | return str_replace( |
| 408 | array( |
| 409 | '\\' |
| 410 | , '"' |
| 411 | //, '/' |
| 412 | , "\n" |
| 413 | , "\r" |
| 414 | ) |
| 415 | , array( |
| 416 | '\\\\' |
| 417 | , '\"' |
| 418 | //, '\/' |
| 419 | , '\n' |
| 420 | , '\r' |
| 421 | ) |
| 422 | , $str |
| 423 | ); |
| 424 | } |
| 425 | |
| 426 | function cfjson_encode($arr) { |
| 427 | $json_str = ''; |
| 428 | if (is_array($arr)) { |
| 429 | $pure_array = true; |
| 430 | $array_length = count($arr); |
| 431 | for ( $i = 0; $i < $array_length ; $i++) { |
| 432 | if (!isset($arr[$i])) { |
| 433 | $pure_array = false; |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | if ($pure_array) { |
| 438 | $json_str = '['; |
| 439 | $temp = array(); |
| 440 | for ($i=0; $i < $array_length; $i++) { |
| 441 | $temp[] = sprintf("%s", cfjson_encode($arr[$i])); |
| 442 | } |
| 443 | $json_str .= implode(',', $temp); |
| 444 | $json_str .="]"; |
| 445 | } |
| 446 | else { |
| 447 | $json_str = '{'; |
| 448 | $temp = array(); |
| 449 | foreach ($arr as $key => $value) { |
| 450 | $temp[] = sprintf("\"%s\":%s", $key, cfjson_encode($value)); |
| 451 | } |
| 452 | $json_str .= implode(',', $temp); |
| 453 | $json_str .= '}'; |
| 454 | } |
| 455 | } |
| 456 | else if (is_object($arr)) { |
| 457 | $json_str = '{'; |
| 458 | $temp = array(); |
| 459 | foreach ($arr as $k => $v) { |
| 460 | $temp[] = '"'.$k.'":'.cfjson_encode($v); |
| 461 | } |
| 462 | $json_str .= implode(',', $temp); |
| 463 | $json_str .= '}'; |
| 464 | } |
| 465 | else if (is_string($arr)) { |
| 466 | $json_str = '"'. cfjson_encode_string($arr) . '"'; |
| 467 | } |
| 468 | else if (is_numeric($arr)) { |
| 469 | $json_str = $arr; |
| 470 | } |
| 471 | else if (is_bool($arr)) { |
| 472 | $json_str = $arr ? 'true' : 'false'; |
| 473 | } |
| 474 | else { |
| 475 | $json_str = '"'. cfjson_encode_string($arr) . '"'; |
| 476 | } |
| 477 | return $json_str; |
| 478 | } |
| 479 | } |
| 480 | ?> |