| 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: 1.93 |
| 8 |
Author URI: http://www.webfactoryltd.com/ |
| 9 |
Text Domain: google-maps-widget |
| 10 |
Domain Path: lang |
| 11 |
|
| 12 |
Copyright 2012 - 2014 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', '1.93'); |
| 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 |
self::upgrade(); |
| 53 |
|
| 54 |
// aditional links in plugin description |
| 55 |
add_filter('plugin_action_links_' . basename(dirname(__FILE__)) . '/' . basename(__FILE__), |
| 56 |
array(__CLASS__, 'plugin_action_links')); |
| 57 |
add_filter('plugin_row_meta', array(__CLASS__, 'plugin_meta_links'), 10, 2); |
| 58 |
|
| 59 |
// enqueue admin scripts |
| 60 |
add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_enqueue_scripts')); |
| 61 |
add_action('customize_controls_enqueue_scripts', array(__CLASS__, 'admin_enqueue_scripts')); |
| 62 |
} else { |
| 63 |
// enqueue frontend scripts |
| 64 |
add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_scripts')); |
| 65 |
add_action('wp_footer', array(__CLASS__, 'dialogs_markup')); |
| 66 |
} |
| 67 |
} // init |
| 68 |
|
| 69 |
|
| 70 |
// some things have to be loaded earlier |
| 71 |
static function plugins_loaded() { |
| 72 |
load_plugin_textdomain('google-maps-widget', false, basename(dirname(__FILE__)) . '/lang'); |
| 73 |
add_filter('cron_schedules', array('GMW_tracking', 'register_cron_intervals')); |
| 74 |
} // plugins_loaded |
| 75 |
|
| 76 |
|
| 77 |
// initialize widgets |
| 78 |
static function widgets_init() { |
| 79 |
register_widget('GoogleMapsWidget'); |
| 80 |
} // widgets_init |
| 81 |
|
| 82 |
|
| 83 |
// add widgets link to plugins page |
| 84 |
static function plugin_action_links($links) { |
| 85 |
$settings_link = '<a href="' . admin_url('widgets.php') . '" title="' . __('Configure Google Maps Widget', 'google-maps-widget') . '">' . __('Widgets', 'google-maps-widget') . '</a>'; |
| 86 |
array_unshift($links, $settings_link); |
| 87 |
|
| 88 |
return $links; |
| 89 |
} // plugin_action_links |
| 90 |
|
| 91 |
|
| 92 |
// add links to plugin's description in plugins table |
| 93 |
static function plugin_meta_links($links, $file) { |
| 94 |
$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>'; |
| 95 |
$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>'; |
| 96 |
$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>'; |
| 97 |
$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>'; |
| 98 |
|
| 99 |
if ($file == plugin_basename(__FILE__)) { |
| 100 |
$links[] = $documentation_link; |
| 101 |
$links[] = $support_link; |
| 102 |
$links[] = $review_link; |
| 103 |
$links[] = $donate_link; |
| 104 |
} |
| 105 |
|
| 106 |
return $links; |
| 107 |
} // plugin_meta_links |
| 108 |
|
| 109 |
|
| 110 |
// check if user has the minimal WP version required by the plugin |
| 111 |
static function check_wp_version($min_version) { |
| 112 |
if (!version_compare(get_bloginfo('version'), $min_version, '>=')) { |
| 113 |
add_action('admin_notices', array(__CLASS__, 'min_version_error')); |
| 114 |
} |
| 115 |
} // check_wp_version |
| 116 |
|
| 117 |
|
| 118 |
// display error message if WP version is too low |
| 119 |
static function min_version_error() { |
| 120 |
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>'; |
| 121 |
} // min_version_error |
| 122 |
|
| 123 |
|
| 124 |
// print dialogs markup in footer |
| 125 |
static function dialogs_markup() { |
| 126 |
$out = ''; |
| 127 |
$widgets = GoogleMapsWidget::$widgets; |
| 128 |
|
| 129 |
if (!$widgets) { |
| 130 |
wp_dequeue_script('gmw'); |
| 131 |
wp_dequeue_script('gmw-fancybox'); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
foreach ($widgets as $widget) { |
| 136 |
if ($widget['bubble']) { |
| 137 |
$iwloc = 'addr'; |
| 138 |
} else { |
| 139 |
$iwloc = 'near'; |
| 140 |
} |
| 141 |
if ($widget['ll']) { |
| 142 |
$ll = '&ll=' . $widget['ll']; |
| 143 |
} else { |
| 144 |
$ll = ''; |
| 145 |
} |
| 146 |
|
| 147 |
$lang = substr(@$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
| 148 |
if (!$lang) { |
| 149 |
$lang = 'en'; |
| 150 |
} |
| 151 |
|
| 152 |
$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; |
| 153 |
|
| 154 |
$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']) . '">'; |
| 155 |
if ($widget['header']) { |
| 156 |
$out .= '<div class="gmw-header">' . wpautop(do_shortcode($widget['header'])) . '</div>'; |
| 157 |
} |
| 158 |
$out .= '<div class="gmw-map"></div>'; |
| 159 |
if ($widget['footer']) { |
| 160 |
$out .= '<div class="gmw-footer">' . wpautop(do_shortcode($widget['footer'])) . '</div>'; |
| 161 |
} |
| 162 |
$out .= "</div>\n"; |
| 163 |
} // foreach $widgets |
| 164 |
|
| 165 |
echo $out; |
| 166 |
} // run_scroller |
| 167 |
|
| 168 |
|
| 169 |
// enqueue frontend scripts if necessary |
| 170 |
static function enqueue_scripts() { |
| 171 |
if (is_active_widget(false, false, 'googlemapswidget', true)) { |
| 172 |
wp_enqueue_style('gmw', plugins_url('/css/gmw.css', __FILE__), array(), GMW_VER); |
| 173 |
wp_enqueue_script('gmw-colorbox', plugins_url('/js/jquery.colorbox-min.js', __FILE__), array('jquery'), GMW_VER, true); |
| 174 |
wp_enqueue_script('gmw', plugins_url('/js/gmw.js', __FILE__), array('jquery'), GMW_VER, true); |
| 175 |
} |
| 176 |
} // enqueue_scripts |
| 177 |
|
| 178 |
|
| 179 |
// enqueue CSS and JS scripts on widgets page |
| 180 |
static function admin_enqueue_scripts() { |
| 181 |
global $wp_customize; |
| 182 |
|
| 183 |
if (self::is_plugin_admin_page() || isset($wp_customize)) { |
| 184 |
wp_enqueue_script('jquery-ui-tabs'); |
| 185 |
wp_enqueue_script('gmw-cookie', plugins_url('js/jquery.cookie.js', __FILE__), array('jquery'), GMW_VER, true); |
| 186 |
wp_enqueue_script('gmw-admin', plugins_url('js/gmw-admin.js', __FILE__), array('jquery'), GMW_VER, true); |
| 187 |
wp_enqueue_style('gmw-admin', plugins_url('css/gmw-admin.css', __FILE__), array(), GMW_VER); |
| 188 |
} // if |
| 189 |
} // admin_enqueue_scripts |
| 190 |
|
| 191 |
|
| 192 |
// check if plugin's admin page is shown |
| 193 |
static function is_plugin_admin_page() { |
| 194 |
$current_screen = get_current_screen(); |
| 195 |
|
| 196 |
if ($current_screen->id == 'widgets') { |
| 197 |
return true; |
| 198 |
} else { |
| 199 |
return false; |
| 200 |
} |
| 201 |
} // is_plugin_admin_page |
| 202 |
|
| 203 |
|
| 204 |
// helper function for creating dropdowns |
| 205 |
static function create_select_options($options, $selected = null, $output = true) { |
| 206 |
$out = "\n"; |
| 207 |
|
| 208 |
foreach ($options as $tmp) { |
| 209 |
if ($selected == $tmp['val']) { |
| 210 |
$out .= "<option selected=\"selected\" value=\"{$tmp['val']}\">{$tmp['label']} </option>\n"; |
| 211 |
} else { |
| 212 |
$out .= "<option value=\"{$tmp['val']}\">{$tmp['label']} </option>\n"; |
| 213 |
} |
| 214 |
} // foreach |
| 215 |
|
| 216 |
if ($output) { |
| 217 |
echo $out; |
| 218 |
} else { |
| 219 |
return $out; |
| 220 |
} |
| 221 |
} // create_select_options |
| 222 |
|
| 223 |
|
| 224 |
// fetch coordinates based on the address |
| 225 |
static function get_coordinates($address, $force_refresh = false) { |
| 226 |
$address_hash = md5('gmw' . $address); |
| 227 |
|
| 228 |
if ($force_refresh || ($coordinates = get_transient($address_hash)) === false) { |
| 229 |
$url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address) . '&sensor=false'; |
| 230 |
$result = wp_remote_get($url); |
| 231 |
|
| 232 |
if (!is_wp_error($result) && $result['response']['code'] == 200) { |
| 233 |
$data = new SimpleXMLElement($result['body']); |
| 234 |
|
| 235 |
if ($data->status == 'OK') { |
| 236 |
$cache_value['lat'] = (string) $data->result->geometry->location->lat; |
| 237 |
$cache_value['lng'] = (string) $data->result->geometry->location->lng; |
| 238 |
$cache_value['address'] = (string) $data->result->formatted_address; |
| 239 |
|
| 240 |
// cache coordinates for 3 months |
| 241 |
set_transient($address_hash, $cache_value, 3600*24*30*3); |
| 242 |
$data = $cache_value; |
| 243 |
} elseif (!$data->status) { |
| 244 |
return false; |
| 245 |
} else { |
| 246 |
return false; |
| 247 |
} |
| 248 |
} else { |
| 249 |
return false; |
| 250 |
} |
| 251 |
} else { |
| 252 |
// data is cached, get it |
| 253 |
$data = get_transient($address_hash); |
| 254 |
} |
| 255 |
|
| 256 |
return $data; |
| 257 |
} // get_coordinates |
| 258 |
|
| 259 |
|
| 260 |
// activate doesn't get fired on upgrades so we have to compensate |
| 261 |
public static function upgrade() { |
| 262 |
$options = get_option(GMW_OPTIONS); |
| 263 |
|
| 264 |
if (!isset($options['first_version']) || !isset($options['first_install'])) { |
| 265 |
$options['first_version'] = GMW_VER; |
| 266 |
$options['first_install'] = current_time('timestamp'); |
| 267 |
update_option(GMW_OPTIONS, $options); |
| 268 |
} |
| 269 |
} // upgrade |
| 270 |
|
| 271 |
|
| 272 |
// write down a few things on plugin activation |
| 273 |
// NO DATA is sent anywhere unless user explicitly agrees to it! |
| 274 |
static function activate() { |
| 275 |
$options = get_option(GMW_OPTIONS); |
| 276 |
|
| 277 |
if (!isset($options['first_version']) || !isset($options['first_install'])) { |
| 278 |
$options['first_version'] = GMW_VER; |
| 279 |
$options['first_install'] = current_time('timestamp'); |
| 280 |
$options['last_tracking'] = false; |
| 281 |
update_option(GMW_OPTIONS, $options); |
| 282 |
} |
| 283 |
} // activate |
| 284 |
|
| 285 |
|
| 286 |
// clean up on deactivation |
| 287 |
static function deactivate() { |
| 288 |
$options = get_option(GMW_OPTIONS); |
| 289 |
|
| 290 |
if (isset($options['allow_tracking']) && $options['allow_tracking'] === false) { |
| 291 |
unset($options['allow_tracking']); |
| 292 |
update_option(GMW_OPTIONS, $options); |
| 293 |
} elseif (isset($options['allow_tracking']) && $options['allow_tracking'] === true) { |
| 294 |
GMW_tracking::clear_cron(); |
| 295 |
} |
| 296 |
} // activate |
| 297 |
} // class GMW |
| 298 |
|
| 299 |
|
| 300 |
// hook everything up |
| 301 |
register_activation_hook(__FILE__, array('GMW', 'activate')); |
| 302 |
register_deactivation_hook(__FILE__, array('GMW', 'deactivate')); |
| 303 |
add_action('init', array('GMW', 'init')); |
| 304 |
add_action('plugins_loaded', array('GMW', 'plugins_loaded')); |
| 305 |
add_action('widgets_init', array('GMW', 'widgets_init')); |