PluginProbe
Maps Widget for Google Maps / 2.35
Maps Widget for Google Maps v2.35
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.35, at google-maps-widget.php

542 lines 21.9 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. Includes shortcode support and numerous options.
6 Author: Web factory Ltd
7 Version: 2.35
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_OPTIONS', 'gmw_options');
35 define('GMW_CRON', 'gmw_cron');
36
37
38 require_once 'gmw-widget.php';
39 require_once 'gmw-tracking.php';
40
41
42 class GMW {
43 static $version = 2.35;
44
45 // hook everything up
46 static function init() {
47 GMW_tracking::init();
48
49 if (is_admin()) {
50 // check if minimal required WP version is used
51 self::check_wp_version(3.3);
52
53 // check some variables
54 self::upgrade();
55
56 // aditional links in plugin description
57 add_filter('plugin_action_links_' . basename(dirname(__FILE__)) . '/' . basename(__FILE__),
58 array(__CLASS__, 'plugin_action_links'));
59 add_filter('plugin_row_meta', array(__CLASS__, 'plugin_meta_links'), 10, 2);
60
61 // enqueue admin scripts
62 add_action('admin_enqueue_scripts', array(__CLASS__, 'admin_enqueue_scripts'));
63 add_action('customize_controls_enqueue_scripts', array(__CLASS__, 'admin_enqueue_scripts'));
64
65 // JS dialog markup
66 add_action('admin_footer', array(__CLASS__, 'admin_dialogs_markup'));
67
68 // register AJAX endpoints
69 add_action('wp_ajax_gmw_subscribe', array(__CLASS__, 'email_subscribe'));
70 add_action('wp_ajax_gmw_activate', array(__CLASS__, 'activate_via_code'));
71
72 // handle dismiss button for all notices
73 add_action('admin_action_gmw_dismiss_notice', array(__CLASS__, 'dismiss_notice'));
74
75 // ask users to upgrade
76 self::upgrade_incentive();
77 } else {
78 // enqueue frontend scripts
79 add_action('wp_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'));
80 add_action('wp_footer', array(__CLASS__, 'dialogs_markup'));
81 }
82
83 // add shortcode support
84 self::add_shortcode();
85 } // init
86
87
88 // some things have to be loaded earlier
89 static function plugins_loaded() {
90 load_plugin_textdomain('google-maps-widget', false, basename(dirname(__FILE__)) . '/lang');
91 add_filter('cron_schedules', array('GMW_tracking', 'register_cron_intervals'));
92 } // plugins_loaded
93
94
95 // initialize widgets
96 static function widgets_init() {
97 register_widget('GoogleMapsWidget');
98 } // widgets_init
99
100
101 // add widgets link to plugins page
102 static function plugin_action_links($links) {
103 $settings_link = '<a href="' . admin_url('widgets.php') . '" title="' . __('Configure Google Maps Widget', 'google-maps-widget') . '">' . __('Widgets', 'google-maps-widget') . '</a>';
104 array_unshift($links, $settings_link);
105
106 return $links;
107 } // plugin_action_links
108
109
110 // add links to plugin's description in plugins table
111 static function plugin_meta_links($links, $file) {
112 $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>';
113 $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>';
114 $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>';
115 $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>';
116
117 if ($file == plugin_basename(__FILE__)) {
118 $links[] = $documentation_link;
119 $links[] = $support_link;
120 $links[] = $review_link;
121 $links[] = $donate_link;
122 }
123
124 return $links;
125 } // plugin_meta_links
126
127
128 // check if user has the minimal WP version required by the plugin
129 static function check_wp_version($min_version) {
130 if (!version_compare(get_bloginfo('version'), $min_version, '>=')) {
131 add_action('admin_notices', array(__CLASS__, 'notice_min_version_error'));
132 }
133 } // check_wp_version
134
135
136 // display error message if WP version is too low
137 static function notice_min_version_error() {
138 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>.', 'google-maps-widget'), get_bloginfo('version'), admin_url('update-core.php')) . '</p></div>';
139 } // notice_min_version_error
140
141
142 // print dialogs markup in footer
143 static function dialogs_markup() {
144 $out = '';
145 $widgets = GoogleMapsWidget::$widgets;
146
147 if (!$widgets) {
148 wp_dequeue_script('gmw');
149 wp_dequeue_script('gmw-fancybox');
150 return;
151 }
152
153 foreach ($widgets as $widget) {
154 if ($widget['bubble']) {
155 $iwloc = 'addr';
156 } else {
157 $iwloc = 'near';
158 }
159 if ($widget['ll']) {
160 $ll = '&amp;ll=' . $widget['ll'];
161 } else {
162 $ll = '';
163 }
164
165 $lang = substr(@$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
166 if (!$lang) {
167 $lang = 'en';
168 }
169
170 $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;
171
172 $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']) . '">';
173 if ($widget['header']) {
174 $out .= '<div class="gmw-header">' . wpautop(do_shortcode($widget['header'])) . '</div>';
175 }
176 $out .= '<div class="gmw-map"></div>';
177 if ($widget['footer']) {
178 $out .= '<div class="gmw-footer">' . wpautop(do_shortcode($widget['footer'])) . '</div>';
179 }
180 $out .= "</div>\n";
181 } // foreach $widgets
182
183 echo $out;
184 } // dialogs_markup
185
186
187 // check availability and register shortcode
188 static function add_shortcode() {
189 global $shortcode_tags;
190
191 if (isset($shortcode_tags['gmw'])) {
192 add_action('admin_notices', array(__CLASS__, 'notice_sc_conflict_error'));
193 } else {
194 add_shortcode('gmw', array(__CLASS__, 'do_shortcode'));
195 }
196 } // add_shortcode
197
198
199 // display notice if shortcode name is already taken
200 static function notice_sc_conflict_error() {
201 if (!self::is_activated()) {
202 return;
203 }
204
205 echo '<div class="error"><p><strong>' . __('Google Maps Widget shortcode is not active!', 'google-maps-widget') . '</strong>' . __(' Shortcode <i>[gmw]</i> is already in use by another plugin or theme. Please deactivate that theme or plugin.', 'google-maps-widget') . '</p></div>';
206 } // notice_sc_conflict_error
207
208
209 // handle dismiss button for all notices
210 // todo - convert all notices
211 static function dismiss_notice() {
212 $options = get_option(GMW_OPTIONS, array());
213
214 if (isset($_GET['notice']) && $_GET['notice'] == 'upgrade') {
215 $options['dismiss_notice_upgrade'] = true;
216 update_option(GMW_OPTIONS, $options);
217 }
218
219 if ($_GET['redirect']) {
220 wp_redirect($_GET['redirect']);
221 } else {
222 wp_redirect(admin_url());
223 }
224 exit;
225 } // dismiss_notice
226
227
228 // maybe show upgrade notice
229 static function upgrade_incentive() {
230 $options = get_option(GMW_OPTIONS, array());
231
232 // if notice has already been dismissed - skip
233 if (isset($options['dismiss_notice_upgrade']) && $options['dismiss_notice_upgrade'] == true) {
234 return;
235 }
236
237 // if extra features are already active - skip
238 if (self::is_activated()) {
239 return;
240 }
241
242 // if there are no widgets and the plugin is used less than 3 days - skip
243 if (GMW_tracking::count_active_widgets() == 0 &&
244 (current_time('timestamp') - $options['first_install']) < (DAY_IN_SECONDS * 3)) {
245 return;
246 }
247
248 add_action('admin_notices', array(__CLASS__, 'notice_activate_extra_features'));
249 } // upgrade_incentive
250
251
252 // display message to get extra features for GMW
253 static function notice_activate_extra_features() {
254 $activate_url = admin_url('widgets.php?gmw_open_promo_dialog');
255 $dismiss_url = add_query_arg(array('action' => 'gmw_dismiss_notice', 'notice' => 'upgrade', 'redirect' => $_SERVER['REQUEST_URI']), admin_url('admin.php'));
256
257 // todo detect WP version and add support for "notice is-dismissible"
258 // todo remove style from HTML
259 echo '<div id="gmw_activate_notice" class="updated"><p>' . __('<b>Google Maps Widget</b> has extra premium features you can get for <b style="color: #d54e21;">FREE</b>. This is a limited time offer so act now!', 'google-maps-widget');
260
261 echo '<br /><a href="' . esc_url($activate_url) . '" style="vertical-align: baseline; margin-top: 15px;" class="button-primary">' . __('Activate premium features for <b>FREE</b>', 'google-maps-widget') . '</a>';
262 echo '&nbsp;&nbsp;<a href="' . esc_url($dismiss_url) . '" class="">' . __('Dismiss notice', 'google-maps-widget') . '</a>';
263 echo '</p></div>';
264 } // notice_activate_extra_features
265
266
267 // enqueue frontend scripts if necessary
268 static function enqueue_scripts() {
269 if (is_active_widget(false, false, 'googlemapswidget', true)) {
270 wp_enqueue_style('gmw', plugins_url('/css/gmw.css', __FILE__), array(), GMW::$version);
271 wp_enqueue_script('gmw-colorbox', plugins_url('/js/jquery.colorbox.min.js', __FILE__), array('jquery'), GMW::$version, true);
272 wp_enqueue_script('gmw', plugins_url('/js/gmw.js', __FILE__), array('jquery'), GMW::$version, true);
273 }
274 } // enqueue_scripts
275
276
277 // enqueue CSS and JS scripts on widgets page
278 static function admin_enqueue_scripts() {
279 global $wp_customize;
280
281 if (self::is_plugin_admin_page() || isset($wp_customize)) {
282 wp_enqueue_script('jquery-ui-tabs');
283 wp_enqueue_script('jquery-ui-dialog');
284 wp_enqueue_script('gmw-cookie', plugins_url('js/jquery.cookie.js', __FILE__), array('jquery'), GMW::$version, true);
285 wp_enqueue_script('gmw-admin', plugins_url('js/gmw-admin.js', __FILE__), array('jquery'), GMW::$version, true);
286
287 wp_enqueue_style('wp-jquery-ui-dialog');
288 wp_enqueue_style('gmw-admin', plugins_url('css/gmw-admin.css', __FILE__), array(), GMW::$version);
289
290 $js_localize = array('subscribe_ok' => __('Check your inbox. Email with activation code is on its way.', 'google-maps-widget'),
291 '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'),
292 'subscribe_error' => __('Something is not right on our end. Sorry :( Try again later.', 'google-maps-widget'),
293 'activate_ok' => __('Superb! Extra features are active ;)', 'google-maps-widget'),
294 'dialog_title' => __('GOOGLE MAPS WIDGET - Activate Extra Features', 'google-maps-widget'));
295 wp_localize_script('gmw-admin', 'gmw', $js_localize);
296 } // if
297 } // admin_enqueue_scripts
298
299
300 // check if plugin's admin page is shown
301 static function is_plugin_admin_page() {
302 $current_screen = get_current_screen();
303
304 if ($current_screen->id == 'widgets') {
305 return true;
306 } else {
307 return false;
308 }
309 } // is_plugin_admin_page
310
311
312 // check if activate-by-subscribing features have been activated
313 static function is_activated() {
314 $options = get_option(GMW_OPTIONS);
315
316 if (isset($options['activated']) && $options['activated'] === true) {
317 return true;
318 } else {
319 return false;
320 }
321 } // is_activated
322
323
324 // echo markup for promo dialog; only on widgets page
325 static function admin_dialogs_markup() {
326 if (!self::is_plugin_admin_page()) {
327 return false;
328 }
329
330 $current_user = wp_get_current_user();
331 if (empty($current_user->user_firstname)) {
332 $name = $current_user->display_name;
333 } else {
334 $name = $current_user->user_firstname;
335 }
336
337 $out = '<div id="gmw_promo_dialog">';
338 $out .= '<div id="gmw_dialog_subscribe"><div class="content"><h3 class="center">' . __('Fill out the form and<br>get extra features &amp; options <b>for FREE</b> instantly!', 'google-maps-widget') . '</h3>';
339 $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>';
340 $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>';
341 $out .= '<p class="center"><a id="gmw_subscribe" href="#" class="button button-primary big-button">Activate extra features</a><br><a href="#" class="" id="gmw_already_subscribed">I already have an activation code</a></p></div>';
342 $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>';
343 $out .= '</div>'; // dialog subscribe
344 $out .= '<div id="gmw_dialog_activate"><div class="content"><h3 class="center">' . __('Enter your code and activate extra features', 'google-maps-widget') . '</h3>';
345 $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>';
346 $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>';
347 $out .= '</div>'; // activate screen
348 $out .= '</div>'; // dialog
349
350 echo $out;
351 } // admin_dialogs_markup
352
353
354 // send user's email to MailChimp via our server
355 static function email_subscribe() {
356 $name = trim($_POST['name']);
357 $email = trim($_POST['email']);
358 if (defined('WPLANG')) {
359 $lang = strtolower(substr(WPLANG, 0, 2));
360 } else {
361 $lang = 'en';
362 }
363
364 $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())));
365
366 // something's wrong with our server
367 if ($res['response']['code'] != 200 || is_wp_error($res)) {
368 wp_send_json_error('unknown');
369 }
370
371 if ($res['body'] == 'ok') {
372 wp_send_json_success();
373 } elseif ($res['body'] == 'duplicate') {
374 wp_send_json_error('duplicate');
375 } else {
376 wp_send_json_error('unknown');
377 }
378 } // email_subscribe
379
380
381 // check activation code and save if valid
382 static function activate_via_code() {
383 $code = trim($_POST['code']);
384
385 if (self::validate_activation_code($code)) {
386 $options = get_option(GMW_OPTIONS);
387 $options['activation_code'] = $code;
388 $options['activated'] = true;
389 update_option(GMW_OPTIONS, $options);
390
391 wp_send_json_success();
392 } else {
393 wp_send_json_error();
394 }
395 } // email_activate
396
397
398 // check if activation code for additional features is valid
399 static function validate_activation_code($code) {
400 if (strlen($code) == 6 && ($code[0] + $code[5]) == 9) {
401 return true;
402 } else {
403 return false;
404 }
405 } // validate_activation_code
406
407
408 // helper function for creating dropdowns
409 static function create_select_options($options, $selected = null, $output = true) {
410 $out = "\n";
411
412 foreach ($options as $tmp) {
413 if ($selected == $tmp['val']) {
414 $out .= "<option selected=\"selected\" value=\"{$tmp['val']}\">{$tmp['label']}&nbsp;</option>\n";
415 } else {
416 $out .= "<option value=\"{$tmp['val']}\">{$tmp['label']}&nbsp;</option>\n";
417 }
418 } // foreach
419
420 if ($output) {
421 echo $out;
422 } else {
423 return $out;
424 }
425 } // create_select_options
426
427
428 // fetch coordinates based on the address
429 static function get_coordinates($address, $force_refresh = false) {
430 $address_hash = md5('gmw' . $address);
431
432 if ($force_refresh || ($coordinates = get_transient($address_hash)) === false) {
433 $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address) . '&sensor=false';
434 $result = wp_remote_get($url);
435
436 if (!is_wp_error($result) && $result['response']['code'] == 200) {
437 $data = new SimpleXMLElement($result['body']);
438
439 if ($data->status == 'OK') {
440 $cache_value['lat'] = (string) $data->result->geometry->location->lat;
441 $cache_value['lng'] = (string) $data->result->geometry->location->lng;
442 $cache_value['address'] = (string) $data->result->formatted_address;
443
444 // cache coordinates for 3 months
445 set_transient($address_hash, $cache_value, 3600*24*30*3);
446 $data = $cache_value;
447 } elseif (!$data->status) {
448 return false;
449 } else {
450 return false;
451 }
452 } else {
453 return false;
454 }
455 } else {
456 // data is cached, get it
457 $data = get_transient($address_hash);
458 }
459
460 return $data;
461 } // get_coordinates
462
463
464 // shortcode support for any GMW instance
465 static function do_shortcode($atts, $content = null) {
466 if (!self::is_activated()) {
467 return;
468 }
469
470 global $wp_widget_factory;
471 $atts = shortcode_atts(array('id' => 0), $atts);
472 $id = (int) $atts['id'];
473 $widgets = get_option('widget_googlemapswidget');
474
475 if (!$id || !isset($widgets[$id]) || empty($widgets[$id])) {
476 echo '<span class="gmw-error">Google Maps Widget shortcode error - please double-check the widget ID.</span>';
477 } else {
478 $widget_args = $widgets[$id];
479 $widget_instance['widget_id'] = 'googlemapswidget-' . $id;
480 $widget_instance['widget_name'] = 'Google Maps Widget';
481
482 echo '<span class="gmw-shortcode-widget">';
483 the_widget('GoogleMapsWidget', $widget_args, $widget_instance);
484 echo '</span>';
485 }
486 } // do_shortcode
487
488
489 // activate doesn't get fired on upgrades so we have to compensate
490 public static function upgrade() {
491 $options = get_option(GMW_OPTIONS);
492
493 if (!isset($options['first_version']) || !isset($options['first_install'])) {
494 $options['first_version'] = GMW::$version;
495 $options['first_install'] = current_time('timestamp');
496 update_option(GMW_OPTIONS, $options);
497 }
498 } // upgrade
499
500
501 // write down a few things on plugin activation
502 // NO DATA is sent anywhere unless user explicitly agrees to it!
503 static function activate() {
504 $options = get_option(GMW_OPTIONS);
505
506 if (!isset($options['first_version']) || !isset($options['first_install'])) {
507 $options['first_version'] = GMW::$version;
508 $options['first_install'] = current_time('timestamp');
509 $options['last_tracking'] = false;
510 update_option(GMW_OPTIONS, $options);
511 }
512 } // activate
513
514
515 // clean up on deactivation
516 static function deactivate() {
517 $options = get_option(GMW_OPTIONS);
518
519 if (isset($options['allow_tracking']) && $options['allow_tracking'] === true) {
520 GMW_tracking::clear_cron();
521 }
522 } // deactivate
523
524
525 // clean up on uninstall / delete
526 static function uninstall() {
527 if (!defined('WP_UNINSTALL_PLUGIN')) {
528 return;
529 }
530
531 delete_option(GMW_OPTIONS);
532 } // uninstall
533 } // class GMW
534
535
536 // hook everything up
537 register_activation_hook(__FILE__, array('GMW', 'activate'));
538 register_deactivation_hook(__FILE__, array('GMW', 'deactivate'));
539 register_uninstall_hook(__FILE__, array('GMW', 'uninstall'));
540 add_action('init', array('GMW', 'init'));
541 add_action('plugins_loaded', array('GMW', 'plugins_loaded'));
542 add_action('widgets_init', array('GMW', 'widgets_init'));