| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Google Maps Widget |
| 4 |
Plugin URI: http://www.googlemapswidget.com/ |
| 5 |
Description: Display a single-image super-fast loading Google map in a widget. A larger, full featured map is available on click in a lightbox. |
| 6 |
Author: Web factory Ltd |
| 7 |
Version: 2.10 |
| 8 |
Author URI: http://www.webfactoryltd.com/ |
| 9 |
Text Domain: google-maps-widget |
| 10 |
Domain Path: lang |
| 11 |
|
| 12 |
Copyright 2012 - 2015 Web factory Ltd (email : gmw@webfactoryltd.com) |
| 13 |
|
| 14 |
This program is free software; you can redistribute it and/or modify |
| 15 |
it under the terms of the GNU General Public License, version 2, as |
| 16 |
published by the Free Software Foundation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, |
| 19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
GNU General Public License for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License |
| 24 |
along with this program; if not, write to the Free Software |
| 25 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
if (!defined('ABSPATH')) { |
| 30 |
die(); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
define('GMW_VER', '2.10'); |
| 35 |
define('GMW_OPTIONS', 'gmw_options'); |
| 36 |
define('GMW_CRON', 'gmw_cron'); |
| 37 |
|
| 38 |
|
| 39 |
require_once 'gmw-widget.php'; |
| 40 |
require_once 'gmw-tracking.php'; |
| 41 |
|
| 42 |
|
| 43 |
class GMW { |
| 44 |
// hook everything up |
| 45 |
static function init() { |
| 46 |
GMW_tracking::init(); |
| 47 |
|
| 48 |
if (is_admin()) { |
| 49 |
// check if minimal required WP version is used |
| 50 |
self::check_wp_version(3.3); |
| 51 |
|
| 52 |
// check some variables |
| 53 |
self::upgrade(); |
| 54 |
|
| 55 |
// aditional links in plugin description |
| 56 |
add_filter('plugin_action_links_' . basename(dirname(__FILE__)) . '/' . basename(__FILE__), |
| 57 |
array(__CLASS__, 'plugin_action_links')); |
| 58 |
add_filter('plugin_row_meta', array(__CLASS__, 'plugin_meta_links'), 10, 2); |
| 59 |
|
| 60 |
// enqueue admin scripts |
| 61 |
add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_enqueue_scripts')); |
| 62 |
add_action('customize_controls_enqueue_scripts', array(__CLASS__, 'admin_enqueue_scripts')); |
| 63 |
|
| 64 |
// JS dialog markup |
| 65 |
add_action('admin_footer', array(__CLASS__, 'admin_dialogs_markup')); |
| 66 |
|
| 67 |
// register AJAX endpoints |
| 68 |
add_action('wp_ajax_gmw_subscribe', array(__CLASS__, 'email_subscribe')); |
| 69 |
add_action('wp_ajax_gmw_activate', array(__CLASS__, 'activate_via_code')); |
| 70 |
} else { |
| 71 |
// enqueue frontend scripts |
| 72 |
add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_scripts')); |
| 73 |
add_action('wp_footer', array(__CLASS__, 'dialogs_markup')); |
| 74 |
} |
| 75 |
} // init |
| 76 |
|
| 77 |
|
| 78 |
// some things have to be loaded earlier |
| 79 |
static function plugins_loaded() { |
| 80 |
load_plugin_textdomain('google-maps-widget', false, basename(dirname(__FILE__)) . '/lang'); |
| 81 |
add_filter('cron_schedules', array('GMW_tracking', 'register_cron_intervals')); |
| 82 |
} // plugins_loaded |
| 83 |
|
| 84 |
|
| 85 |
// initialize widgets |
| 86 |
static function widgets_init() { |
| 87 |
register_widget('GoogleMapsWidget'); |
| 88 |
} // widgets_init |
| 89 |
|
| 90 |
|
| 91 |
// add widgets link to plugins page |
| 92 |
static function plugin_action_links($links) { |
| 93 |
$settings_link = '<a href="' . admin_url('widgets.php') . '" title="' . __('Configure Google Maps Widget', 'google-maps-widget') . '">' . __('Widgets', 'google-maps-widget') . '</a>'; |
| 94 |
array_unshift($links, $settings_link); |
| 95 |
|
| 96 |
return $links; |
| 97 |
} // plugin_action_links |
| 98 |
|
| 99 |
|
| 100 |
// add links to plugin's description in plugins table |
| 101 |
static function plugin_meta_links($links, $file) { |
| 102 |
$documentation_link = '<a target="_blank" href="http://www.googlemapswidget.com/documentation/" title="' . __('View Google Maps Widget documentation', 'google-maps-widget') . '">'. __('Documentation', 'google-maps-widget') . '</a>'; |
| 103 |
$support_link = '<a target="_blank" href="http://wordpress.org/support/plugin/google-maps-widget" title="' . __('Problems? We are here to help!', 'google-maps-widget') . '">' . __('Support', 'google-maps-widget') . '</a>'; |
| 104 |
$review_link = '<a target="_blank" href="http://wordpress.org/support/view/plugin-reviews/google-maps-widget" title="' . __('If you like it, please review the plugin', 'google-maps-widget') . '">' . __('Review the plugin', 'google-maps-widget') . '</a>'; |
| 105 |
$donate_link = '<a target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=gordan%40webfactoryltd%2ecom&lc=US&item_name=Google%20Maps%20Widget&no_note=0¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest" title="' . __('If you feel we deserve it, buy us coffee', 'google-maps-widget') . '">' . __('Donate', 'google-maps-widget') . '</a>'; |
| 106 |
|
| 107 |
if ($file == plugin_basename(__FILE__)) { |
| 108 |
$links[] = $documentation_link; |
| 109 |
$links[] = $support_link; |
| 110 |
$links[] = $review_link; |
| 111 |
$links[] = $donate_link; |
| 112 |
} |
| 113 |
|
| 114 |
return $links; |
| 115 |
} // plugin_meta_links |
| 116 |
|
| 117 |
|
| 118 |
// check if user has the minimal WP version required by the plugin |
| 119 |
static function check_wp_version($min_version) { |
| 120 |
if (!version_compare(get_bloginfo('version'), $min_version, '>=')) { |
| 121 |
add_action('admin_notices', array(__CLASS__, 'min_version_error')); |
| 122 |
} |
| 123 |
} // check_wp_version |
| 124 |
|
| 125 |
|
| 126 |
// display error message if WP version is too low |
| 127 |
static function min_version_error() { |
| 128 |
echo '<div class="error"><p>' . sprintf('Google Maps Widget <b>requires WordPress version 3.3</b> or higher to function properly. You are using WordPress version %s. Please <a href="%s">update it</a>.', get_bloginfo('version'), admin_url('update-core.php')) . '</p></div>'; |
| 129 |
} // min_version_error |
| 130 |
|
| 131 |
|
| 132 |
// print dialogs markup in footer |
| 133 |
static function dialogs_markup() { |
| 134 |
$out = ''; |
| 135 |
$widgets = GoogleMapsWidget::$widgets; |
| 136 |
|
| 137 |
if (!$widgets) { |
| 138 |
wp_dequeue_script('gmw'); |
| 139 |
wp_dequeue_script('gmw-fancybox'); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
foreach ($widgets as $widget) { |
| 144 |
if ($widget['bubble']) { |
| 145 |
$iwloc = 'addr'; |
| 146 |
} else { |
| 147 |
$iwloc = 'near'; |
| 148 |
} |
| 149 |
if ($widget['ll']) { |
| 150 |
$ll = '&ll=' . $widget['ll']; |
| 151 |
} else { |
| 152 |
$ll = ''; |
| 153 |
} |
| 154 |
|
| 155 |
$lang = substr(@$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
| 156 |
if (!$lang) { |
| 157 |
$lang = 'en'; |
| 158 |
} |
| 159 |
|
| 160 |
$map_url = '//maps.google.com/maps?hl=' . $lang . '&ie=utf8&output=embed&iwloc=' . $iwloc . '&iwd=1&mrt=loc&t=' . $widget['type'] . '&q=' . urlencode(remove_accents($widget['address'])) . '&z=' . urlencode($widget['zoom']) . $ll; |
| 161 |
|
| 162 |
$out .= '<div class="gmw-dialog" style="display: none;" data-map-height="' . $widget['height'] . '" data-map-width="' . $widget['width'] . '" data-map-skin="' . $widget['skin'] . '" data-map-iframe-url="' . $map_url . '" id="gmw-dialog-' . $widget['id'] . '" title="' . esc_attr($widget['title']) . '">'; |
| 163 |
if ($widget['header']) { |
| 164 |
$out .= '<div class="gmw-header">' . wpautop(do_shortcode($widget['header'])) . '</div>'; |
| 165 |
} |
| 166 |
$out .= '<div class="gmw-map"></div>'; |
| 167 |
if ($widget['footer']) { |
| 168 |
$out .= '<div class="gmw-footer">' . wpautop(do_shortcode($widget['footer'])) . '</div>'; |
| 169 |
} |
| 170 |
$out .= "</div>\n"; |
| 171 |
} // foreach $widgets |
| 172 |
|
| 173 |
echo $out; |
| 174 |
} // dialogs_markup |
| 175 |
|
| 176 |
|
| 177 |
// enqueue frontend scripts if necessary |
| 178 |
static function enqueue_scripts() { |
| 179 |
if (is_active_widget(false, false, 'googlemapswidget', true)) { |
| 180 |
wp_enqueue_style('gmw', plugins_url('/css/gmw.css', __FILE__), array(), GMW_VER); |
| 181 |
wp_enqueue_script('gmw-colorbox', plugins_url('/js/jquery.colorbox.min.js', __FILE__), array('jquery'), GMW_VER, true); |
| 182 |
wp_enqueue_script('gmw', plugins_url('/js/gmw.js', __FILE__), array('jquery'), GMW_VER, true); |
| 183 |
} |
| 184 |
} // enqueue_scripts |
| 185 |
|
| 186 |
|
| 187 |
// enqueue CSS and JS scripts on widgets page |
| 188 |
static function admin_enqueue_scripts() { |
| 189 |
global $wp_customize; |
| 190 |
|
| 191 |
if (self::is_plugin_admin_page() || isset($wp_customize)) { |
| 192 |
wp_enqueue_script('jquery-ui-tabs'); |
| 193 |
wp_enqueue_script('jquery-ui-dialog'); |
| 194 |
wp_enqueue_script('gmw-cookie', plugins_url('js/jquery.cookie.js', __FILE__), array('jquery'), GMW_VER, true); |
| 195 |
wp_enqueue_script('gmw-admin', plugins_url('js/gmw-admin.js', __FILE__), array('jquery'), GMW_VER, true); |
| 196 |
|
| 197 |
wp_enqueue_style('wp-jquery-ui-dialog'); |
| 198 |
wp_enqueue_style('gmw-admin', plugins_url('css/gmw-admin.css', __FILE__), array(), GMW_VER); |
| 199 |
} // if |
| 200 |
} // admin_enqueue_scripts |
| 201 |
|
| 202 |
|
| 203 |
// check if plugin's admin page is shown |
| 204 |
static function is_plugin_admin_page() { |
| 205 |
$current_screen = get_current_screen(); |
| 206 |
|
| 207 |
if ($current_screen->id == 'widgets') { |
| 208 |
return true; |
| 209 |
} else { |
| 210 |
return false; |
| 211 |
} |
| 212 |
} // is_plugin_admin_page |
| 213 |
|
| 214 |
|
| 215 |
// check if activate-by-subscribing features have been activated |
| 216 |
static function is_activated() { |
| 217 |
$options = get_option(GMW_OPTIONS); |
| 218 |
|
| 219 |
if (isset($options['activated']) && $options['activated'] === true) { |
| 220 |
return true; |
| 221 |
} else { |
| 222 |
return false; |
| 223 |
} |
| 224 |
} // is_activated |
| 225 |
|
| 226 |
|
| 227 |
// echo markup for promo dialog; only on widgets page |
| 228 |
static function admin_dialogs_markup() { |
| 229 |
if (!self::is_plugin_admin_page()) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
$current_user = wp_get_current_user(); |
| 234 |
if (empty($current_user->user_firstname)) { |
| 235 |
$name = $current_user->display_name; |
| 236 |
} else { |
| 237 |
$name = $current_user->user_firstname; |
| 238 |
} |
| 239 |
|
| 240 |
$out = '<div id="gmw_promo_dialog">'; |
| 241 |
$out .= '<div id="gmw_dialog_subscribe"><div class="content"><h3 class="center">' . __('Subscribe to our newsletter<br>and get extra features & options <b>for FREE</b>!', 'google-maps-widget') . '</h3>'; |
| 242 |
$out .= '<p class="input_row"><input value="' . $name . '" type="text" id="gmw_name" name="gmw_name" placeholder="Your name"><span class="error name" style="display: none;">Please enter your name.</span></p>'; |
| 243 |
$out .= '<p class="input_row"><input value="' . $current_user->user_email . '" type="text" name="gmw_email" id="gmw_email" placeholder="Your email address"><span style="display: none;" class="error email">Please double check your email address.</span></p>'; |
| 244 |
$out .= '<p class="center"><a id="gmw_subscribe" href="#" class="button button-primary big-button">Subscribe & activate extra features</a><br><a href="#" class="" id="gmw_already_subscribed">I\'m already subscribed</a></p></div>'; |
| 245 |
$out .= '<div class="footer"><p><b>Why subscribe?</b></p><ul><li>We\'ll never share your email address</li><li>We won\'t spam you or overwhelm with emails</li><li>Be the first to get notified about new features</li><li>You\'ll get all future upgrades for free as well</li><li>You\'ll get discounts for our premium WP plugins</li></ul></div>'; |
| 246 |
$out .= '</div>'; // dialog subscribe |
| 247 |
$out .= '<div id="gmw_dialog_activate"><div class="content"><h3 class="center">' . __('Enter your code and activate extra features', 'google-maps-widget') . '</h3>'; |
| 248 |
$out .= '<p class="input_row"><input type="text" id="gmw_code" name="gmw_code" placeholder="Your activation code"><span style="display: none;" class="error gmw_code">Please double check the activation code.</span></p><p class="center"><a href="#" class="button button-primary big-button" id="gmw_activate">Activate extra features</a></p></div>'; |
| 249 |
$out .= '<div class="footer"><p><b>FAQ</b></p><ul><li>Already subscribed? Enter your activation code above.</li><li>Didn\'t receive the email? Check your SPAM folder.</li><li>Lost your code or having other problems? <a href="mailto:gmw@webfactoryltd.com?subject=Lost%20activation%20code">Email us</a>.</li><li>Code is valid for an unlimited number of plugin installations.</li></ul></div>'; |
| 250 |
$out .= '</div>'; // activate screen |
| 251 |
$out .= '</div>'; // dialog |
| 252 |
|
| 253 |
echo $out; |
| 254 |
} // admin_dialogs_markup |
| 255 |
|
| 256 |
|
| 257 |
// send user's email to MailChimp via our server |
| 258 |
static function email_subscribe() { |
| 259 |
$name = trim($_POST['name']); |
| 260 |
$email = trim($_POST['email']); |
| 261 |
if (defined('WPLANG')) { |
| 262 |
$lang = strtolower(substr(WPLANG, 0, 2)); |
| 263 |
} else { |
| 264 |
$lang = 'en'; |
| 265 |
} |
| 266 |
|
| 267 |
$res = wp_remote_post('http://www.googlemapswidget.com/subscribe.php', array('body' => array('name' => $name, 'email' => $email, 'lang' => $lang, 'ip' => $_SERVER['REMOTE_ADDR'], 'site' => get_home_url()))); |
| 268 |
|
| 269 |
// something's wrong with our server |
| 270 |
if ($res['response']['code'] != 200 || is_wp_error($res)) { |
| 271 |
echo 'err'; |
| 272 |
die(); |
| 273 |
} |
| 274 |
|
| 275 |
if ($res['body'] == 'ok') { |
| 276 |
echo "ok"; |
| 277 |
die(); |
| 278 |
} elseif ($res['body'] == 'duplicate') { |
| 279 |
echo "duplicate"; |
| 280 |
die(); |
| 281 |
} else { |
| 282 |
// todo var_dump($res['body']); |
| 283 |
echo 'err'; |
| 284 |
die(); |
| 285 |
} |
| 286 |
} // email_subscribe |
| 287 |
|
| 288 |
|
| 289 |
// check activation code and save if valid |
| 290 |
static function activate_via_code() { |
| 291 |
$code = trim($_GET['code']); |
| 292 |
|
| 293 |
if (self::validate_activation_code($code)) { |
| 294 |
$options = get_option(GMW_OPTIONS); |
| 295 |
$options['activation_code'] = $code; |
| 296 |
$options['activated'] = true; |
| 297 |
update_option(GMW_OPTIONS, $options); |
| 298 |
|
| 299 |
die("1"); |
| 300 |
} else { |
| 301 |
die("0"); |
| 302 |
} |
| 303 |
} // email_activate |
| 304 |
|
| 305 |
|
| 306 |
// check if activation code for additional features is valid |
| 307 |
static function validate_activation_code($code) { |
| 308 |
if (strlen($code) == 6 && ($code[0] + $code[5]) == 9) { |
| 309 |
return true; |
| 310 |
} else { |
| 311 |
return false; |
| 312 |
} |
| 313 |
} // validate_activation_code |
| 314 |
|
| 315 |
|
| 316 |
// helper function for creating dropdowns |
| 317 |
static function create_select_options($options, $selected = null, $output = true) { |
| 318 |
$out = "\n"; |
| 319 |
|
| 320 |
foreach ($options as $tmp) { |
| 321 |
if ($selected == $tmp['val']) { |
| 322 |
$out .= "<option selected=\"selected\" value=\"{$tmp['val']}\">{$tmp['label']} </option>\n"; |
| 323 |
} else { |
| 324 |
$out .= "<option value=\"{$tmp['val']}\">{$tmp['label']} </option>\n"; |
| 325 |
} |
| 326 |
} // foreach |
| 327 |
|
| 328 |
if ($output) { |
| 329 |
echo $out; |
| 330 |
} else { |
| 331 |
return $out; |
| 332 |
} |
| 333 |
} // create_select_options |
| 334 |
|
| 335 |
|
| 336 |
// fetch coordinates based on the address |
| 337 |
static function get_coordinates($address, $force_refresh = false) { |
| 338 |
$address_hash = md5('gmw' . $address); |
| 339 |
|
| 340 |
if ($force_refresh || ($coordinates = get_transient($address_hash)) === false) { |
| 341 |
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address) . '&sensor=false'; |
| 342 |
$result = wp_remote_get($url); |
| 343 |
|
| 344 |
if (!is_wp_error($result) && $result['response']['code'] == 200) { |
| 345 |
$data = new SimpleXMLElement($result['body']); |
| 346 |
|
| 347 |
if ($data->status == 'OK') { |
| 348 |
$cache_value['lat'] = (string) $data->result->geometry->location->lat; |
| 349 |
$cache_value['lng'] = (string) $data->result->geometry->location->lng; |
| 350 |
$cache_value['address'] = (string) $data->result->formatted_address; |
| 351 |
|
| 352 |
// cache coordinates for 3 months |
| 353 |
set_transient($address_hash, $cache_value, 3600*24*30*3); |
| 354 |
$data = $cache_value; |
| 355 |
} elseif (!$data->status) { |
| 356 |
return false; |
| 357 |
} else { |
| 358 |
return false; |
| 359 |
} |
| 360 |
} else { |
| 361 |
return false; |
| 362 |
} |
| 363 |
} else { |
| 364 |
// data is cached, get it |
| 365 |
$data = get_transient($address_hash); |
| 366 |
} |
| 367 |
|
| 368 |
return $data; |
| 369 |
} // get_coordinates |
| 370 |
|
| 371 |
|
| 372 |
// activate doesn't get fired on upgrades so we have to compensate |
| 373 |
public static function upgrade() { |
| 374 |
$options = get_option(GMW_OPTIONS); |
| 375 |
|
| 376 |
if (!isset($options['first_version']) || !isset($options['first_install'])) { |
| 377 |
$options['first_version'] = GMW_VER; |
| 378 |
$options['first_install'] = current_time('timestamp'); |
| 379 |
update_option(GMW_OPTIONS, $options); |
| 380 |
} |
| 381 |
} // upgrade |
| 382 |
|
| 383 |
|
| 384 |
// write down a few things on plugin activation |
| 385 |
// NO DATA is sent anywhere unless user explicitly agrees to it! |
| 386 |
static function activate() { |
| 387 |
$options = get_option(GMW_OPTIONS); |
| 388 |
|
| 389 |
if (!isset($options['first_version']) || !isset($options['first_install'])) { |
| 390 |
$options['first_version'] = GMW_VER; |
| 391 |
$options['first_install'] = current_time('timestamp'); |
| 392 |
$options['last_tracking'] = false; |
| 393 |
update_option(GMW_OPTIONS, $options); |
| 394 |
} |
| 395 |
} // activate |
| 396 |
|
| 397 |
|
| 398 |
// clean up on deactivation |
| 399 |
static function deactivate() { |
| 400 |
$options = get_option(GMW_OPTIONS); |
| 401 |
|
| 402 |
if (isset($options['allow_tracking']) && $options['allow_tracking'] === false) { |
| 403 |
unset($options['allow_tracking']); |
| 404 |
update_option(GMW_OPTIONS, $options); |
| 405 |
} elseif (isset($options['allow_tracking']) && $options['allow_tracking'] === true) { |
| 406 |
GMW_tracking::clear_cron(); |
| 407 |
} |
| 408 |
} // activate |
| 409 |
} // class GMW |
| 410 |
|
| 411 |
|
| 412 |
// hook everything up |
| 413 |
register_activation_hook(__FILE__, array('GMW', 'activate')); |
| 414 |
register_deactivation_hook(__FILE__, array('GMW', 'deactivate')); |
| 415 |
add_action('init', array('GMW', 'init')); |
| 416 |
add_action('plugins_loaded', array('GMW', 'plugins_loaded')); |
| 417 |
add_action('widgets_init', array('GMW', 'widgets_init')); |