widget-google-reviews
Last commit date
api
9 years ago
helper
9 years ago
languages
5 years ago
static
5 years ago
grw-finder.php
5 years ago
grw-options.php
6 years ago
grw-reviews-helper.php
5 years ago
grw-reviews.php
5 years ago
grw-setting-fig.php
5 years ago
grw-setting-support.php
5 years ago
grw-setting.php
5 years ago
grw-widget.php
6 years ago
grw.php
5 years ago
readme.txt
5 years ago
grw.php
716 lines
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Widget for Google Reviews |
| 4 | Plugin URI: https://richplugins.com/business-reviews-bundle-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.9 |
| 8 | Author URI: https://richplugins.com |
| 9 | Text Domain: widget-google-reviews |
| 10 | Domain Path: /languages |
| 11 | */ |
| 12 | |
| 13 | require(ABSPATH . 'wp-includes/version.php'); |
| 14 | |
| 15 | include_once(dirname(__FILE__) . '/api/urlopen.php'); |
| 16 | include_once(dirname(__FILE__) . '/helper/debug.php'); |
| 17 | |
| 18 | define('GRW_VERSION', '1.9'); |
| 19 | define('GRW_GOOGLE_PLACE_API', 'https://maps.googleapis.com/maps/api/place/'); |
| 20 | define('GRW_GOOGLE_AVATAR', 'https://lh3.googleusercontent.com/-8hepWJzFXpE/AAAAAAAAAAI/AAAAAAAAAAA/I80WzYfIxCQ/s128-c/114307615494839964028.jpg'); |
| 21 | define('GRW_PLUGIN_URL', plugins_url(basename(plugin_dir_path(__FILE__ )), basename(__FILE__))); |
| 22 | |
| 23 | function grw_options() { |
| 24 | return array( |
| 25 | 'grw_version', |
| 26 | 'grw_active', |
| 27 | 'grw_google_api_key', |
| 28 | 'grw_language', |
| 29 | 'grw_activation_time', |
| 30 | 'grw_rev_notice_hide', |
| 31 | 'rplg_rev_notice_show', |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /*-------------------------------- Widget --------------------------------*/ |
| 36 | function grw_setup_widget() { |
| 37 | if (!class_exists('Goog_Reviews_Widget')) { |
| 38 | require 'grw-widget.php'; |
| 39 | register_widget('Goog_Reviews_Widget'); |
| 40 | } |
| 41 | } |
| 42 | add_action('widgets_init', 'grw_setup_widget'); |
| 43 | |
| 44 | /*-------------------------------- Menu --------------------------------*/ |
| 45 | function grw_setting_menu() { |
| 46 | add_submenu_page( |
| 47 | 'options-general.php', |
| 48 | 'Google Reviews Widget', |
| 49 | 'Google Reviews Widget', |
| 50 | 'moderate_comments', |
| 51 | 'grw', |
| 52 | 'grw_setting' |
| 53 | ); |
| 54 | } |
| 55 | add_action('admin_menu', 'grw_setting_menu', 10); |
| 56 | |
| 57 | function grw_setting() { |
| 58 | include_once(dirname(__FILE__) . '/grw-setting.php'); |
| 59 | } |
| 60 | |
| 61 | /*-------------------------------- Links --------------------------------*/ |
| 62 | function grw_plugin_action_links($links, $file) { |
| 63 | $plugin_file = basename(__FILE__); |
| 64 | if (basename($file) == $plugin_file) { |
| 65 | $settings_link = '<a href="' . admin_url('options-general.php?page=grw') . '">'.grw_i('Settings') . '</a>'; |
| 66 | array_unshift($links, $settings_link); |
| 67 | } |
| 68 | return $links; |
| 69 | } |
| 70 | add_filter('plugin_action_links', 'grw_plugin_action_links', 10, 2); |
| 71 | |
| 72 | /*-------------------------------- Row Meta --------------------------------*/ |
| 73 | function grw_plugin_row_meta($input, $file) { |
| 74 | if ($file != plugin_basename( __FILE__ )) { |
| 75 | return $input; |
| 76 | } |
| 77 | |
| 78 | $links = array( |
| 79 | '<a href="' . esc_url('https://richplugins.com/documentation') . '" target="_blank">' . grw_i('View Documentation') . '</a>', |
| 80 | '<a href="' . esc_url('https://richplugins.com/business-reviews-bundle-wordpress-plugin') . '" target="_blank">' . grw_i('Upgrade to Business') . ' »</a>', |
| 81 | ); |
| 82 | $input = array_merge($input, $links); |
| 83 | return $input; |
| 84 | } |
| 85 | add_filter('plugin_row_meta', 'grw_plugin_row_meta', 10, 2); |
| 86 | |
| 87 | /*-------------------------------- Activator --------------------------------*/ |
| 88 | function grw_check_version() { |
| 89 | if (version_compare(get_option('grw_version'), GRW_VERSION, '<')) { |
| 90 | grw_activate(); |
| 91 | } |
| 92 | } |
| 93 | add_action('init', 'grw_check_version'); |
| 94 | |
| 95 | function grw_activation($network_wide = false) { |
| 96 | $now = time(); |
| 97 | update_option('grw_activation_time', $now); |
| 98 | |
| 99 | add_option('grw_is_multisite', $network_wide); |
| 100 | grw_activate(); |
| 101 | } |
| 102 | register_activation_hook(__FILE__, 'grw_activation'); |
| 103 | |
| 104 | function grw_activate() { |
| 105 | $network_wide = get_option('grw_is_multisite'); |
| 106 | if ($network_wide) { |
| 107 | grw_activate_multisite(); |
| 108 | } else { |
| 109 | grw_activate_single_site(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | function grw_activate_multisite() { |
| 114 | global $wpdb; |
| 115 | |
| 116 | $site_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 117 | |
| 118 | foreach($site_ids as $site_id) { |
| 119 | switch_to_blog($site_id); |
| 120 | grw_activate_single_site(); |
| 121 | restore_current_blog(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | function grw_activate_single_site() { |
| 126 | $current_version = GRW_VERSION; |
| 127 | $last_active_version = get_option('grw_version'); |
| 128 | |
| 129 | if (empty($last_active_version)) { |
| 130 | grw_first_install(); |
| 131 | update_option('grw_version', $current_version); |
| 132 | } elseif ($last_active_version !== $current_version) { |
| 133 | grw_exist_install($current_version, $last_active_version); |
| 134 | update_option('grw_version', $current_version); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | function grw_first_install() { |
| 139 | grw_install_db(); |
| 140 | add_option('grw_active', '1'); |
| 141 | add_option('grw_google_api_key', ''); |
| 142 | } |
| 143 | |
| 144 | function grw_exist_install($current_version, $last_active_version) { |
| 145 | global $wpdb; |
| 146 | switch($last_active_version) { |
| 147 | case version_compare($last_active_version, '1.8.2', '<'): |
| 148 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . "grp_google_place ADD review_count INTEGER"); |
| 149 | $place_ids = $wpdb->get_col("SELECT place_id FROM " . $wpdb->prefix . "grp_google_place WHERE rating > 0 LIMIT 5"); |
| 150 | foreach($place_ids as $place_id) { |
| 151 | grw_refresh_reviews(array($place_id)); |
| 152 | } |
| 153 | break; |
| 154 | case version_compare($last_active_version, '1.8.7', '<'): |
| 155 | $row = $wpdb->get_results("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" . $wpdb->prefix . "grp_google_review' AND column_name = 'hide'"); |
| 156 | if(empty($row)){ |
| 157 | $wpdb->query("ALTER TABLE " . $wpdb->prefix . "grp_google_review ADD hide VARCHAR(1) DEFAULT '' NOT NULL"); |
| 158 | } |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | function grw_install_db() { |
| 164 | global $wpdb; |
| 165 | |
| 166 | $charset_collate = $wpdb->get_charset_collate(); |
| 167 | |
| 168 | $sql = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix . "grp_google_place (". |
| 169 | "id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,". |
| 170 | "place_id VARCHAR(80) NOT NULL,". |
| 171 | "name VARCHAR(255) NOT NULL,". |
| 172 | "photo VARCHAR(255),". |
| 173 | "icon VARCHAR(255),". |
| 174 | "address VARCHAR(255),". |
| 175 | "rating DOUBLE PRECISION,". |
| 176 | "url VARCHAR(255),". |
| 177 | "website VARCHAR(255),". |
| 178 | "review_count INTEGER,". |
| 179 | "updated BIGINT(20),". |
| 180 | "PRIMARY KEY (`id`),". |
| 181 | "UNIQUE INDEX grp_place_id (`place_id`)". |
| 182 | ") " . $charset_collate . ";"; |
| 183 | |
| 184 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 185 | |
| 186 | dbDelta($sql); |
| 187 | |
| 188 | $sql = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix . "grp_google_review (". |
| 189 | "id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,". |
| 190 | "google_place_id BIGINT(20) UNSIGNED NOT NULL,". |
| 191 | "hash VARCHAR(40) NOT NULL,". |
| 192 | "rating INTEGER NOT NULL,". |
| 193 | "text VARCHAR(10000),". |
| 194 | "time INTEGER NOT NULL,". |
| 195 | "language VARCHAR(10),". |
| 196 | "author_name VARCHAR(255),". |
| 197 | "author_url VARCHAR(255),". |
| 198 | "profile_photo_url VARCHAR(255),". |
| 199 | "hide VARCHAR(1) DEFAULT '' NOT NULL,". |
| 200 | "PRIMARY KEY (`id`),". |
| 201 | "UNIQUE INDEX grp_google_review_hash (`hash`),". |
| 202 | "INDEX grp_google_place_id (`google_place_id`)". |
| 203 | ") " . $charset_collate . ";"; |
| 204 | |
| 205 | dbDelta($sql); |
| 206 | } |
| 207 | |
| 208 | function grw_reset($reset_db) { |
| 209 | global $wpdb; |
| 210 | |
| 211 | if (function_exists('is_multisite') && is_multisite()) { |
| 212 | $current_blog_id = get_current_blog_id(); |
| 213 | $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
| 214 | foreach ($blog_ids as $blog_id) { |
| 215 | switch_to_blog($blog_id); |
| 216 | grw_reset_data($reset_db); |
| 217 | } |
| 218 | switch_to_blog($current_blog_id); |
| 219 | } else { |
| 220 | grw_reset_data($reset_db); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | function grw_reset_data($reset_db) { |
| 225 | global $wpdb; |
| 226 | |
| 227 | foreach (grw_options() as $opt) { |
| 228 | delete_option($opt); |
| 229 | } |
| 230 | if ($reset_db) { |
| 231 | $wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . "grp_google_place;"); |
| 232 | $wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . "grp_google_review;"); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /*-------------------------------- Shortcode --------------------------------*/ |
| 237 | function grw_shortcode($atts) { |
| 238 | global $wpdb; |
| 239 | |
| 240 | if (!grw_enabled()) return ''; |
| 241 | if (!class_exists('Goog_Reviews_Widget')) return ''; |
| 242 | |
| 243 | $shortcode_atts = array(); |
| 244 | foreach (Goog_Reviews_Widget::$widget_fields as $field => $value) { |
| 245 | $shortcode_atts[$field] = isset($atts[$field]) ? strip_tags(stripslashes($atts[$field])) : ''; |
| 246 | } |
| 247 | |
| 248 | foreach ($shortcode_atts as $variable => $value) { |
| 249 | ${$variable} = esc_attr($shortcode_atts[$variable]); |
| 250 | } |
| 251 | |
| 252 | ob_start(); |
| 253 | if (empty($place_id)) { |
| 254 | ?> |
| 255 | <div class="grw-error" style="padding:10px;color:#b94a48;background-color:#f2dede;border-color:#eed3d7;max-width:200px;"> |
| 256 | <?php echo grw_i('<b>Google Reviews Widget</b>: required attribute place_id is not defined'); ?> |
| 257 | </div> |
| 258 | <?php |
| 259 | } else { |
| 260 | include(dirname(__FILE__) . '/grw-reviews.php'); |
| 261 | } |
| 262 | return preg_replace('/[\n\r]/', '', ob_get_clean()); |
| 263 | } |
| 264 | add_shortcode("grw", "grw_shortcode"); |
| 265 | |
| 266 | /*-------------------------------- Request --------------------------------*/ |
| 267 | function grw_request_handler() { |
| 268 | global $wpdb; |
| 269 | |
| 270 | if (!empty($_GET['cf_action'])) { |
| 271 | |
| 272 | switch ($_GET['cf_action']) { |
| 273 | case 'grw_google_api_key': |
| 274 | if (current_user_can('manage_options')) { |
| 275 | if (isset($_POST['grw_wpnonce']) === false) { |
| 276 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 277 | $response = compact('error'); |
| 278 | } else { |
| 279 | check_admin_referer('grw_wpnonce', 'grw_wpnonce'); |
| 280 | |
| 281 | update_option('grw_google_api_key', trim(sanitize_text_field($_POST['key']))); |
| 282 | $status = 'success'; |
| 283 | $response = compact('status'); |
| 284 | |
| 285 | } |
| 286 | header('Content-type: text/javascript'); |
| 287 | echo json_encode($response); |
| 288 | die(); |
| 289 | } |
| 290 | break; |
| 291 | case 'grw_search': |
| 292 | if (current_user_can('manage_options')) { |
| 293 | if (isset($_GET['grw_wpnonce']) === false) { |
| 294 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 295 | $response = compact('error'); |
| 296 | } else { |
| 297 | check_admin_referer('grw_wpnonce', 'grw_wpnonce'); |
| 298 | |
| 299 | $grw_google_api_key = get_option('grw_google_api_key'); |
| 300 | $url = GRW_GOOGLE_PLACE_API . 'textsearch/json?query=' . $_GET['query'] . '&key=' . $grw_google_api_key; |
| 301 | |
| 302 | $grw_language = get_option('grw_language'); |
| 303 | if (strlen($grw_language) > 0) { |
| 304 | $url = $url . '&language=' . $grw_language; |
| 305 | } |
| 306 | |
| 307 | $response = rplg_urlopen($url); |
| 308 | |
| 309 | $response_data = $response['data']; |
| 310 | $response_json = rplg_json_decode($response_data); |
| 311 | $response_results = $response_json->results; |
| 312 | |
| 313 | foreach ($response_results as $result) { |
| 314 | $result->business_photo = grw_business_avatar($result); |
| 315 | } |
| 316 | } |
| 317 | header('Content-type: text/javascript'); |
| 318 | echo json_encode($response_results); |
| 319 | die(); |
| 320 | } |
| 321 | break; |
| 322 | case 'grw_reviews': |
| 323 | if (current_user_can('manage_options')) { |
| 324 | if (isset($_GET['grw_wpnonce']) === false) { |
| 325 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 326 | $response = compact('error'); |
| 327 | } else { |
| 328 | check_admin_referer('grw_wpnonce', 'grw_wpnonce'); |
| 329 | |
| 330 | $url = grw_api_url($_GET['placeid']); |
| 331 | |
| 332 | $response = rplg_urlopen($url); |
| 333 | |
| 334 | $response_data = $response['data']; |
| 335 | $response_json = rplg_json_decode($response_data); |
| 336 | $response_result = $response_json->result; |
| 337 | |
| 338 | if (isset($response_result)) { |
| 339 | $response_result->business_photo = grw_business_avatar($response_result); |
| 340 | } |
| 341 | } |
| 342 | header('Content-type: text/javascript'); |
| 343 | echo json_encode($response_json->result); |
| 344 | die(); |
| 345 | } |
| 346 | break; |
| 347 | case 'grw_save': |
| 348 | if (current_user_can('manage_options')) { |
| 349 | if (isset($_POST['grw_wpnonce']) === false) { |
| 350 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 351 | $response = compact('error'); |
| 352 | } else { |
| 353 | check_admin_referer('grw_wpnonce', 'grw_wpnonce'); |
| 354 | |
| 355 | $url = grw_api_url($_POST['placeid'], $_POST['lang']); |
| 356 | |
| 357 | $response = rplg_urlopen($url); |
| 358 | |
| 359 | $response_data = $response['data']; |
| 360 | $response_json = rplg_json_decode($response_data); |
| 361 | |
| 362 | if ($response_json && isset($response_json->result)) { |
| 363 | $response_json->result->business_photo = grw_business_avatar($response_json->result); |
| 364 | grw_save_reviews($response_json->result); |
| 365 | $result = $response_json->result; |
| 366 | $status = 'success'; |
| 367 | } else { |
| 368 | $result = $response_json; |
| 369 | $status = 'failed'; |
| 370 | } |
| 371 | $response = compact('status', 'result'); |
| 372 | } |
| 373 | header('Content-type: text/javascript'); |
| 374 | echo json_encode($response); |
| 375 | die(); |
| 376 | } |
| 377 | break; |
| 378 | case 'grw_db_reviews': |
| 379 | if (current_user_can('manage_options')) { |
| 380 | if (isset($_GET['grw_wpnonce']) === false) { |
| 381 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 382 | $response = compact('error'); |
| 383 | } else { |
| 384 | check_admin_referer('grw_wpnonce', 'grw_wpnonce'); |
| 385 | |
| 386 | include_once(dirname(__FILE__) . '/grw-reviews-helper.php'); |
| 387 | |
| 388 | $reviews = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "grp_google_review WHERE google_place_id = %d ORDER BY time DESC", $_GET['id'])); |
| 389 | |
| 390 | ob_start(); |
| 391 | grw_place_reviews(null, $reviews, 'zzz', 120, false, true, true, true, false, false, true); |
| 392 | $response = ob_get_clean(); |
| 393 | } |
| 394 | header('Content-type: text/html'); |
| 395 | header('Access-Control-Allow-Origin: *'); |
| 396 | echo $response; |
| 397 | die(); |
| 398 | } |
| 399 | break; |
| 400 | case 'grw_hide_review': |
| 401 | if (current_user_can('manage_options')) { |
| 402 | if (isset($_POST['grw_wpnonce']) === false) { |
| 403 | $error = grw_i('Unable to call request. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 404 | $response = compact('error'); |
| 405 | } else { |
| 406 | check_admin_referer('grw_wpnonce', 'grw_wpnonce'); |
| 407 | |
| 408 | $review = $wpdb->get_row($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "grp_google_review WHERE id = %d", $_POST['id'])); |
| 409 | $hide = $review->hide == '' ? 'y' : ''; |
| 410 | $wpdb->update($wpdb->prefix . 'grp_google_review', array('hide' => $hide), array('id' => $_POST['id'])); |
| 411 | $response = array('hide' => $hide); |
| 412 | } |
| 413 | header('Content-type: text/javascript'); |
| 414 | echo json_encode($response); |
| 415 | die(); |
| 416 | } |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | add_action('init', 'grw_request_handler'); |
| 422 | |
| 423 | function grw_save_reviews($place, $min_filter = 0) { |
| 424 | global $wpdb; |
| 425 | |
| 426 | $google_place_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "grp_google_place WHERE place_id = %s", $place->place_id)); |
| 427 | if ($google_place_id) { |
| 428 | $wpdb->update($wpdb->prefix . 'grp_google_place', array( |
| 429 | 'name' => $place->name, |
| 430 | 'photo' => $place->business_photo, |
| 431 | 'rating' => $place->rating, |
| 432 | 'review_count' => isset($place->user_ratings_total) ? $place->user_ratings_total : null |
| 433 | ), array('ID' => $google_place_id)); |
| 434 | } else { |
| 435 | $wpdb->insert($wpdb->prefix . 'grp_google_place', array( |
| 436 | 'place_id' => $place->place_id, |
| 437 | 'name' => $place->name, |
| 438 | 'photo' => $place->business_photo, |
| 439 | 'icon' => $place->icon, |
| 440 | 'address' => $place->formatted_address, |
| 441 | 'rating' => isset($place->rating) ? $place->rating : null, |
| 442 | 'url' => isset($place->url) ? $place->url : null, |
| 443 | 'website' => isset($place->website) ? $place->website : null, |
| 444 | 'review_count' => isset($place->user_ratings_total) ? $place->user_ratings_total : null |
| 445 | )); |
| 446 | $google_place_id = $wpdb->insert_id; |
| 447 | } |
| 448 | |
| 449 | if ($place->reviews) { |
| 450 | $reviews = $place->reviews; |
| 451 | foreach ($reviews as $review) { |
| 452 | if ($min_filter > 0 && $min_filter > $review->rating) { |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | $google_review_id = 0; |
| 457 | if (isset($review->author_url) && strlen($review->author_url) > 0) { |
| 458 | $google_review_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "grp_google_review WHERE author_url = %s", $review->author_url)); |
| 459 | } else { |
| 460 | $google_review_id = $wpdb->get_var($wpdb->prepare("SELECT id FROM " . $wpdb->prefix . "grp_google_review WHERE time = %s", $review->time)); |
| 461 | } |
| 462 | |
| 463 | if ($google_review_id) { |
| 464 | $update_params = array( |
| 465 | 'rating' => $review->rating, |
| 466 | 'text' => $review->text |
| 467 | ); |
| 468 | if (isset($review->profile_photo_url)) { |
| 469 | $update_params['profile_photo_url'] = $review->profile_photo_url; |
| 470 | } |
| 471 | $wpdb->update($wpdb->prefix . 'grp_google_review', $update_params, array('id' => $google_review_id)); |
| 472 | } else { |
| 473 | $wpdb->insert($wpdb->prefix . 'grp_google_review', array( |
| 474 | 'google_place_id' => $google_place_id, |
| 475 | 'hash' => $review->time, //TODO: workaround to support old versions |
| 476 | 'rating' => $review->rating, |
| 477 | 'text' => $review->text, |
| 478 | 'time' => $review->time, |
| 479 | 'language' => $review->language, |
| 480 | 'author_name' => $review->author_name, |
| 481 | 'author_url' => isset($review->author_url) ? $review->author_url : null, |
| 482 | 'profile_photo_url' => isset($review->profile_photo_url) ? $review->profile_photo_url : null |
| 483 | )); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /*-------------------------------- Refresh Reviews --------------------------------*/ |
| 490 | function grw_refresh_reviews($args) { |
| 491 | $google_api_key = get_option('grw_google_api_key'); |
| 492 | if (!$google_api_key || strlen($google_api_key) < 1) { |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | $place_id = $args[0]; |
| 497 | $reviews_lang = isset($args[1]) ? $args[1] : ''; |
| 498 | |
| 499 | $url = grw_api_url($place_id, $reviews_lang); |
| 500 | |
| 501 | $response = rplg_urlopen($url); |
| 502 | |
| 503 | $response_data = $response['data']; |
| 504 | $response_json = rplg_json_decode($response_data); |
| 505 | |
| 506 | if ($response_json && isset($response_json->result)) { |
| 507 | $response_json->result->business_photo = grw_business_avatar($response_json->result); |
| 508 | grw_save_reviews($response_json->result); |
| 509 | } |
| 510 | |
| 511 | delete_transient('grw_refresh_reviews_' . join('_', $args)); |
| 512 | } |
| 513 | add_action('grw_refresh_reviews', 'grw_refresh_reviews'); |
| 514 | |
| 515 | /*-------------------------------- Init language --------------------------------*/ |
| 516 | function grw_lang_init() { |
| 517 | $plugin_dir = basename(dirname(__FILE__)); |
| 518 | load_plugin_textdomain('grw', false, $plugin_dir . '/languages'); |
| 519 | } |
| 520 | add_action('plugins_loaded', 'grw_lang_init'); |
| 521 | |
| 522 | /*-------------------------------- Leave review --------------------------------*/ |
| 523 | function grw_admin_notice() { |
| 524 | if (!is_admin()) return; |
| 525 | |
| 526 | $activation_time = get_option('grw_activation_time'); |
| 527 | |
| 528 | if ($activation_time == '') { |
| 529 | $activation_time = time() - 86400*28; |
| 530 | update_option('grw_activation_time', $activation_time); |
| 531 | } |
| 532 | |
| 533 | $rev_notice = isset($_GET['grw_rev_notice']) ? $_GET['grw_rev_notice'] : ''; |
| 534 | if ($rev_notice == 'later') { |
| 535 | $activation_time = time() - 86400*23; |
| 536 | update_option('grw_activation_time', $activation_time); |
| 537 | update_option('grw_rev_notice_hide', 'later'); |
| 538 | } else if ($rev_notice == 'never') { |
| 539 | update_option('grw_rev_notice_hide', 'never'); |
| 540 | } |
| 541 | |
| 542 | $rev_notice_hide = get_option('grw_rev_notice_hide'); |
| 543 | $rev_notice_show = get_option('rplg_rev_notice_show'); |
| 544 | |
| 545 | if ($rev_notice_show == '' || $rev_notice_show == 'grw') { |
| 546 | |
| 547 | if ($rev_notice_hide != 'never' && $activation_time < (time() - 86400*30)) { |
| 548 | update_option('rplg_rev_notice_show', 'grw'); |
| 549 | $class = 'notice notice-info is-dismissible'; |
| 550 | $url = remove_query_arg(array('taction', 'tid', 'sortby', 'sortdir', 'opt')); |
| 551 | $url_later = esc_url(add_query_arg('grw_rev_notice', 'later', $url)); |
| 552 | $url_never = esc_url(add_query_arg('grw_rev_notice', 'never', $url)); |
| 553 | |
| 554 | $notice = '<p style="font-weight:normal;color:#156315">Hey, I noticed you have been using my <b>Google Reviews Widget</b> plugin for a while now – that’s awesome!<br>Could you please do me a BIG favor and give it a 5-star rating on WordPress?<br><br>--<br>Thanks!<br>Daniel K.<br></p><ul style="font-weight:bold;"><li><a href="https://wordpress.org/support/plugin/widget-google-reviews/reviews/#new-post" target="_blank">OK, you deserve it</a></li><li><a href="' . $url_later . '">Not now, maybe later</a></li><li><a href="' . $url_never . '">Do not remind me again</a></li></ul><p>By the way, if you have been thinking about upgrading to the <a href="https://richplugins.com/business-reviews-bundle-wordpress-plugin" target="_blank">Business</a> version, here is a 25% off coupon you can use! -> <b>business25off</b></p>'; |
| 555 | |
| 556 | printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), $notice); |
| 557 | } else { |
| 558 | update_option('rplg_rev_notice_show', ''); |
| 559 | } |
| 560 | |
| 561 | } |
| 562 | } |
| 563 | add_action('admin_notices', 'grw_admin_notice'); |
| 564 | |
| 565 | /*-------------------------------- Helpers --------------------------------*/ |
| 566 | function grw_enabled() { |
| 567 | global $id, $post; |
| 568 | |
| 569 | $active = get_option('grw_active'); |
| 570 | if (empty($active) || $active === '0') { return false; } |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | function grw_api_url($placeid, $reviews_lang = '') { |
| 575 | $url = GRW_GOOGLE_PLACE_API . 'details/json?placeid=' . $placeid . '&key=' . get_option('grw_google_api_key'); |
| 576 | |
| 577 | $grw_language = strlen($reviews_lang) > 0 ? $reviews_lang : get_option('grw_language'); |
| 578 | if (strlen($grw_language) > 0) { |
| 579 | $url = $url . '&language=' . $grw_language; |
| 580 | } |
| 581 | return $url; |
| 582 | } |
| 583 | |
| 584 | function grw_business_avatar($response_result_json) { |
| 585 | if (isset($response_result_json->photos)) { |
| 586 | $request_url = add_query_arg( |
| 587 | array( |
| 588 | 'photoreference' => $response_result_json->photos[0]->photo_reference, |
| 589 | 'key' => get_option('grw_google_api_key'), |
| 590 | 'maxwidth' => '300', |
| 591 | 'maxheight' => '300', |
| 592 | ), |
| 593 | 'https://maps.googleapis.com/maps/api/place/photo' |
| 594 | ); |
| 595 | $response = rplg_urlopen($request_url); |
| 596 | foreach ($response['headers'] as $header) { |
| 597 | if (strpos($header, 'Location: ') !== false) { |
| 598 | return str_replace('Location: ', '', $header); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | return null; |
| 603 | } |
| 604 | |
| 605 | function grw_i($text, $params=null) { |
| 606 | if (!is_array($params)) { |
| 607 | $params = func_get_args(); |
| 608 | $params = array_slice($params, 1); |
| 609 | } |
| 610 | return vsprintf(__($text, 'grw'), $params); |
| 611 | } |
| 612 | |
| 613 | if (!function_exists('esc_html')) { |
| 614 | function esc_html( $text ) { |
| 615 | $safe_text = wp_check_invalid_utf8( $text ); |
| 616 | $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES ); |
| 617 | return apply_filters( 'esc_html', $safe_text, $text ); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if (!function_exists('esc_attr')) { |
| 622 | function esc_attr( $text ) { |
| 623 | $safe_text = wp_check_invalid_utf8( $text ); |
| 624 | $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES ); |
| 625 | return apply_filters( 'attribute_escape', $safe_text, $text ); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * JSON ENCODE for PHP < 5.2.0 |
| 631 | */ |
| 632 | if (!function_exists('json_encode')) { |
| 633 | |
| 634 | function json_encode($data) { |
| 635 | return cfjson_encode($data); |
| 636 | } |
| 637 | |
| 638 | function cfjson_encode_string($str) { |
| 639 | if(is_bool($str)) { |
| 640 | return $str ? 'true' : 'false'; |
| 641 | } |
| 642 | |
| 643 | return str_replace( |
| 644 | array( |
| 645 | '\\' |
| 646 | , '"' |
| 647 | //, '/' |
| 648 | , "\n" |
| 649 | , "\r" |
| 650 | ) |
| 651 | , array( |
| 652 | '\\\\' |
| 653 | , '\"' |
| 654 | //, '\/' |
| 655 | , '\n' |
| 656 | , '\r' |
| 657 | ) |
| 658 | , $str |
| 659 | ); |
| 660 | } |
| 661 | |
| 662 | function cfjson_encode($arr) { |
| 663 | $json_str = ''; |
| 664 | if (is_array($arr)) { |
| 665 | $pure_array = true; |
| 666 | $array_length = count($arr); |
| 667 | for ( $i = 0; $i < $array_length ; $i++) { |
| 668 | if (!isset($arr[$i])) { |
| 669 | $pure_array = false; |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | if ($pure_array) { |
| 674 | $json_str = '['; |
| 675 | $temp = array(); |
| 676 | for ($i=0; $i < $array_length; $i++) { |
| 677 | $temp[] = sprintf("%s", cfjson_encode($arr[$i])); |
| 678 | } |
| 679 | $json_str .= implode(',', $temp); |
| 680 | $json_str .="]"; |
| 681 | } |
| 682 | else { |
| 683 | $json_str = '{'; |
| 684 | $temp = array(); |
| 685 | foreach ($arr as $key => $value) { |
| 686 | $temp[] = sprintf("\"%s\":%s", $key, cfjson_encode($value)); |
| 687 | } |
| 688 | $json_str .= implode(',', $temp); |
| 689 | $json_str .= '}'; |
| 690 | } |
| 691 | } |
| 692 | else if (is_object($arr)) { |
| 693 | $json_str = '{'; |
| 694 | $temp = array(); |
| 695 | foreach ($arr as $k => $v) { |
| 696 | $temp[] = '"'.$k.'":'.cfjson_encode($v); |
| 697 | } |
| 698 | $json_str .= implode(',', $temp); |
| 699 | $json_str .= '}'; |
| 700 | } |
| 701 | else if (is_string($arr)) { |
| 702 | $json_str = '"'. cfjson_encode_string($arr) . '"'; |
| 703 | } |
| 704 | else if (is_numeric($arr)) { |
| 705 | $json_str = $arr; |
| 706 | } |
| 707 | else if (is_bool($arr)) { |
| 708 | $json_str = $arr ? 'true' : 'false'; |
| 709 | } |
| 710 | else { |
| 711 | $json_str = '"'. cfjson_encode_string($arr) . '"'; |
| 712 | } |
| 713 | return $json_str; |
| 714 | } |
| 715 | } |
| 716 | ?> |