PluginProbe
Maps Widget for Google Maps / 2.15
Maps Widget for Google Maps v2.15
1.86 1.90 1.92 1.93 1.95 2.0 2.01 2.05 2.06 2.10 2.15 2.20 2.25 2.30 2.35 2.40 2.45 2.50 2.51 2.60 2.65 2.66 2.70 2.75 2.80 All 129 releases
google-maps-widget / google-maps-widget.php

google-maps-widget.php in Maps Widget for Google Maps 2.15, at google-maps-widget.php

432 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.15
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.15');
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&currency_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 = '&amp;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 . '&amp;ie=utf8&amp;output=embed&amp;iwloc=' . $iwloc . '&amp;iwd=1&amp;mrt=loc&amp;t=' . $widget['type'] . '&amp;q=' . urlencode(remove_accents($widget['address'])) . '&amp;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
200 $js_localize = array('subscribe_ok' => __('Check your inbox. Email with activation code is on its way.', 'google-maps-widget'),
201 'subscribe_duplicate' => __('You are already subscribed to our list. One activation code is valid for all sites so just use the code you already have.', 'google-maps-widget'),
202 'subscribe_error' => __('Something is not right on our end. Sorry :( Try again later.', 'google-maps-widget'),
203 'activate_ok' => __('Superb! Extra features are active ;)', 'google-maps-widget'),
204 'dialog_title' => __('GOOGLE MAPS WIDGET - Activate Extra Features', 'google-maps-widget'));
205 wp_localize_script('gmw-admin', 'gmw', $js_localize);
206 } // if
207 } // admin_enqueue_scripts
208
209
210 // check if plugin's admin page is shown
211 static function is_plugin_admin_page() {
212 $current_screen = get_current_screen();
213
214 if ($current_screen->id == 'widgets') {
215 return true;
216 } else {
217 return false;
218 }
219 } // is_plugin_admin_page
220
221
222 // check if activate-by-subscribing features have been activated
223 static function is_activated() {
224 $options = get_option(GMW_OPTIONS);
225
226 if (isset($options['activated']) && $options['activated'] === true) {
227 return true;
228 } else {
229 return false;
230 }
231 } // is_activated
232
233
234 // echo markup for promo dialog; only on widgets page
235 static function admin_dialogs_markup() {
236 if (!self::is_plugin_admin_page()) {
237 return false;
238 }
239
240 $current_user = wp_get_current_user();
241 if (empty($current_user->user_firstname)) {
242 $name = $current_user->display_name;
243 } else {
244 $name = $current_user->user_firstname;
245 }
246
247 $out = '<div id="gmw_promo_dialog">';
248 $out .= '<div id="gmw_dialog_subscribe"><div class="content"><h3 class="center">' . __('Subscribe to our newsletter<br>and get extra features &amp; options <b>for FREE</b>!', 'google-maps-widget') . '</h3>';
249 $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>';
250 $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>';
251 $out .= '<p class="center"><a id="gmw_subscribe" href="#" class="button button-primary big-button">Subscribe &amp; activate extra features</a><br><a href="#" class="" id="gmw_already_subscribed">I\'m already subscribed</a></p></div>';
252 $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>';
253 $out .= '</div>'; // dialog subscribe
254 $out .= '<div id="gmw_dialog_activate"><div class="content"><h3 class="center">' . __('Enter your code and activate extra features', 'google-maps-widget') . '</h3>';
255 $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>';
256 $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>';
257 $out .= '</div>'; // activate screen
258 $out .= '</div>'; // dialog
259
260 echo $out;
261 } // admin_dialogs_markup
262
263
264 // send user's email to MailChimp via our server
265 static function email_subscribe() {
266 $name = trim($_POST['name']);
267 $email = trim($_POST['email']);
268 if (defined('WPLANG')) {
269 $lang = strtolower(substr(WPLANG, 0, 2));
270 } else {
271 $lang = 'en';
272 }
273
274 $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())));
275
276 // something's wrong with our server
277 if ($res['response']['code'] != 200 || is_wp_error($res)) {
278 echo 'err';
279 die();
280 }
281
282 if ($res['body'] == 'ok') {
283 echo "ok";
284 die();
285 } elseif ($res['body'] == 'duplicate') {
286 echo "duplicate";
287 die();
288 } else {
289 // todo var_dump($res['body']);
290 echo 'err';
291 die();
292 }
293 } // email_subscribe
294
295
296 // check activation code and save if valid
297 static function activate_via_code() {
298 $code = trim($_GET['code']);
299
300 if (self::validate_activation_code($code)) {
301 $options = get_option(GMW_OPTIONS);
302 $options['activation_code'] = $code;
303 $options['activated'] = true;
304 update_option(GMW_OPTIONS, $options);
305
306 die("1");
307 } else {
308 die("0");
309 }
310 } // email_activate
311
312
313 // check if activation code for additional features is valid
314 static function validate_activation_code($code) {
315 if (strlen($code) == 6 && ($code[0] + $code[5]) == 9) {
316 return true;
317 } else {
318 return false;
319 }
320 } // validate_activation_code
321
322
323 // helper function for creating dropdowns
324 static function create_select_options($options, $selected = null, $output = true) {
325 $out = "\n";
326
327 foreach ($options as $tmp) {
328 if ($selected == $tmp['val']) {
329 $out .= "<option selected=\"selected\" value=\"{$tmp['val']}\">{$tmp['label']}&nbsp;</option>\n";
330 } else {
331 $out .= "<option value=\"{$tmp['val']}\">{$tmp['label']}&nbsp;</option>\n";
332 }
333 } // foreach
334
335 if ($output) {
336 echo $out;
337 } else {
338 return $out;
339 }
340 } // create_select_options
341
342
343 // fetch coordinates based on the address
344 static function get_coordinates($address, $force_refresh = false) {
345 $address_hash = md5('gmw' . $address);
346
347 if ($force_refresh || ($coordinates = get_transient($address_hash)) === false) {
348 $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address) . '&sensor=false';
349 $result = wp_remote_get($url);
350
351 if (!is_wp_error($result) && $result['response']['code'] == 200) {
352 $data = new SimpleXMLElement($result['body']);
353
354 if ($data->status == 'OK') {
355 $cache_value['lat'] = (string) $data->result->geometry->location->lat;
356 $cache_value['lng'] = (string) $data->result->geometry->location->lng;
357 $cache_value['address'] = (string) $data->result->formatted_address;
358
359 // cache coordinates for 3 months
360 set_transient($address_hash, $cache_value, 3600*24*30*3);
361 $data = $cache_value;
362 } elseif (!$data->status) {
363 return false;
364 } else {
365 return false;
366 }
367 } else {
368 return false;
369 }
370 } else {
371 // data is cached, get it
372 $data = get_transient($address_hash);
373 }
374
375 return $data;
376 } // get_coordinates
377
378
379 // activate doesn't get fired on upgrades so we have to compensate
380 public static function upgrade() {
381 $options = get_option(GMW_OPTIONS);
382
383 if (!isset($options['first_version']) || !isset($options['first_install'])) {
384 $options['first_version'] = GMW_VER;
385 $options['first_install'] = current_time('timestamp');
386 update_option(GMW_OPTIONS, $options);
387 }
388 } // upgrade
389
390
391 // write down a few things on plugin activation
392 // NO DATA is sent anywhere unless user explicitly agrees to it!
393 static function activate() {
394 $options = get_option(GMW_OPTIONS);
395
396 if (!isset($options['first_version']) || !isset($options['first_install'])) {
397 $options['first_version'] = GMW_VER;
398 $options['first_install'] = current_time('timestamp');
399 $options['last_tracking'] = false;
400 update_option(GMW_OPTIONS, $options);
401 }
402 } // activate
403
404
405 // clean up on deactivation
406 static function deactivate() {
407 $options = get_option(GMW_OPTIONS);
408
409 if (isset($options['allow_tracking']) && $options['allow_tracking'] === true) {
410 GMW_tracking::clear_cron();
411 }
412 } // deactivate
413
414
415 // clean up on uninstall / delete
416 static function uninstall() {
417 if (!defined('WP_UNINSTALL_PLUGIN')) {
418 return;
419 }
420
421 delete_option(GMW_OPTIONS);
422 } // uninstall
423 } // class GMW
424
425
426 // hook everything up
427 register_activation_hook(__FILE__, array('GMW', 'activate'));
428 register_deactivation_hook(__FILE__, array('GMW', 'deactivate'));
429 register_uninstall_hook(__FILE__, array('GMW', 'uninstall'));
430 add_action('init', array('GMW', 'init'));
431 add_action('plugins_loaded', array('GMW', 'plugins_loaded'));
432 add_action('widgets_init', array('GMW', 'widgets_init'));