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

482 lines 19.3 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.25
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.25');
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
76 // add shortcode support
77 self::add_shortcode();
78 } // init
79
80
81 // some things have to be loaded earlier
82 static function plugins_loaded() {
83 load_plugin_textdomain('google-maps-widget', false, basename(dirname(__FILE__)) . '/lang');
84 add_filter('cron_schedules', array('GMW_tracking', 'register_cron_intervals'));
85 } // plugins_loaded
86
87
88 // initialize widgets
89 static function widgets_init() {
90 register_widget('GoogleMapsWidget');
91 } // widgets_init
92
93
94 // add widgets link to plugins page
95 static function plugin_action_links($links) {
96 $settings_link = '<a href="' . admin_url('widgets.php') . '" title="' . __('Configure Google Maps Widget', 'google-maps-widget') . '">' . __('Widgets', 'google-maps-widget') . '</a>';
97 array_unshift($links, $settings_link);
98
99 return $links;
100 } // plugin_action_links
101
102
103 // add links to plugin's description in plugins table
104 static function plugin_meta_links($links, $file) {
105 $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>';
106 $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>';
107 $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>';
108 $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>';
109
110 if ($file == plugin_basename(__FILE__)) {
111 $links[] = $documentation_link;
112 $links[] = $support_link;
113 $links[] = $review_link;
114 $links[] = $donate_link;
115 }
116
117 return $links;
118 } // plugin_meta_links
119
120
121 // check if user has the minimal WP version required by the plugin
122 static function check_wp_version($min_version) {
123 if (!version_compare(get_bloginfo('version'), $min_version, '>=')) {
124 add_action('admin_notices', array(__CLASS__, 'min_version_error'));
125 }
126 } // check_wp_version
127
128
129 // display error message if WP version is too low
130 static function min_version_error() {
131 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>';
132 } // min_version_error
133
134
135 // print dialogs markup in footer
136 static function dialogs_markup() {
137 $out = '';
138 $widgets = GoogleMapsWidget::$widgets;
139
140 if (!$widgets) {
141 wp_dequeue_script('gmw');
142 wp_dequeue_script('gmw-fancybox');
143 return;
144 }
145
146 foreach ($widgets as $widget) {
147 if ($widget['bubble']) {
148 $iwloc = 'addr';
149 } else {
150 $iwloc = 'near';
151 }
152 if ($widget['ll']) {
153 $ll = '&amp;ll=' . $widget['ll'];
154 } else {
155 $ll = '';
156 }
157
158 $lang = substr(@$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
159 if (!$lang) {
160 $lang = 'en';
161 }
162
163 $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;
164
165 $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']) . '">';
166 if ($widget['header']) {
167 $out .= '<div class="gmw-header">' . wpautop(do_shortcode($widget['header'])) . '</div>';
168 }
169 $out .= '<div class="gmw-map"></div>';
170 if ($widget['footer']) {
171 $out .= '<div class="gmw-footer">' . wpautop(do_shortcode($widget['footer'])) . '</div>';
172 }
173 $out .= "</div>\n";
174 } // foreach $widgets
175
176 echo $out;
177 } // dialogs_markup
178
179
180 // check availability and register shortcode
181 static function add_shortcode() {
182 global $shortcode_tags;
183
184 if (isset($shortcode_tags['gmw'])) {
185 add_action('admin_notices', array(__CLASS__, 'notice_sc_conflict_error'));
186 } else {
187 add_shortcode('gmw', array(__CLASS__, 'do_shortcode'));
188 }
189 } // add_shortcode
190
191
192 // display notice if shortcode name is already taken
193 static function notice_sc_conflict_error() {
194 if (!self::is_activated()) {
195 return;
196 }
197
198 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>';
199 } // notice_sc_conflict_error
200
201
202 // enqueue frontend scripts if necessary
203 static function enqueue_scripts() {
204 if (is_active_widget(false, false, 'googlemapswidget', true)) {
205 wp_enqueue_style('gmw', plugins_url('/css/gmw.css', __FILE__), array(), GMW_VER);
206 wp_enqueue_script('gmw-colorbox', plugins_url('/js/jquery.colorbox.min.js', __FILE__), array('jquery'), GMW_VER, true);
207 wp_enqueue_script('gmw', plugins_url('/js/gmw.js', __FILE__), array('jquery'), GMW_VER, true);
208 }
209 } // enqueue_scripts
210
211
212 // enqueue CSS and JS scripts on widgets page
213 static function admin_enqueue_scripts() {
214 global $wp_customize;
215
216 if (self::is_plugin_admin_page() || isset($wp_customize)) {
217 wp_enqueue_script('jquery-ui-tabs');
218 wp_enqueue_script('jquery-ui-dialog');
219 wp_enqueue_script('gmw-cookie', plugins_url('js/jquery.cookie.js', __FILE__), array('jquery'), GMW_VER, true);
220 wp_enqueue_script('gmw-admin', plugins_url('js/gmw-admin.js', __FILE__), array('jquery'), GMW_VER, true);
221
222 wp_enqueue_style('wp-jquery-ui-dialog');
223 wp_enqueue_style('gmw-admin', plugins_url('css/gmw-admin.css', __FILE__), array(), GMW_VER);
224
225 $js_localize = array('subscribe_ok' => __('Check your inbox. Email with activation code is on its way.', 'google-maps-widget'),
226 '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'),
227 'subscribe_error' => __('Something is not right on our end. Sorry :( Try again later.', 'google-maps-widget'),
228 'activate_ok' => __('Superb! Extra features are active ;)', 'google-maps-widget'),
229 'dialog_title' => __('GOOGLE MAPS WIDGET - Activate Extra Features', 'google-maps-widget'));
230 wp_localize_script('gmw-admin', 'gmw', $js_localize);
231 } // if
232 } // admin_enqueue_scripts
233
234
235 // check if plugin's admin page is shown
236 static function is_plugin_admin_page() {
237 $current_screen = get_current_screen();
238
239 if ($current_screen->id == 'widgets') {
240 return true;
241 } else {
242 return false;
243 }
244 } // is_plugin_admin_page
245
246
247 // check if activate-by-subscribing features have been activated
248 static function is_activated() {
249 $options = get_option(GMW_OPTIONS);
250
251 if (isset($options['activated']) && $options['activated'] === true) {
252 return true;
253 } else {
254 return false;
255 }
256 } // is_activated
257
258
259 // echo markup for promo dialog; only on widgets page
260 static function admin_dialogs_markup() {
261 if (!self::is_plugin_admin_page()) {
262 return false;
263 }
264
265 $current_user = wp_get_current_user();
266 if (empty($current_user->user_firstname)) {
267 $name = $current_user->display_name;
268 } else {
269 $name = $current_user->user_firstname;
270 }
271
272 $out = '<div id="gmw_promo_dialog">';
273 $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>';
274 $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>';
275 $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>';
276 $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>';
277 $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>';
278 $out .= '</div>'; // dialog subscribe
279 $out .= '<div id="gmw_dialog_activate"><div class="content"><h3 class="center">' . __('Enter your code and activate extra features', 'google-maps-widget') . '</h3>';
280 $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>';
281 $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>';
282 $out .= '</div>'; // activate screen
283 $out .= '</div>'; // dialog
284
285 echo $out;
286 } // admin_dialogs_markup
287
288
289 // send user's email to MailChimp via our server
290 static function email_subscribe() {
291 $name = trim($_POST['name']);
292 $email = trim($_POST['email']);
293 if (defined('WPLANG')) {
294 $lang = strtolower(substr(WPLANG, 0, 2));
295 } else {
296 $lang = 'en';
297 }
298
299 $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())));
300
301 // something's wrong with our server
302 if ($res['response']['code'] != 200 || is_wp_error($res)) {
303 echo 'err';
304 die();
305 }
306
307 if ($res['body'] == 'ok') {
308 echo "ok";
309 die();
310 } elseif ($res['body'] == 'duplicate') {
311 echo "duplicate";
312 die();
313 } else {
314 // todo var_dump($res['body']);
315 echo 'err';
316 die();
317 }
318 } // email_subscribe
319
320
321 // check activation code and save if valid
322 static function activate_via_code() {
323 $code = trim($_GET['code']);
324
325 if (self::validate_activation_code($code)) {
326 $options = get_option(GMW_OPTIONS);
327 $options['activation_code'] = $code;
328 $options['activated'] = true;
329 update_option(GMW_OPTIONS, $options);
330
331 die("1");
332 } else {
333 die("0");
334 }
335 } // email_activate
336
337
338 // check if activation code for additional features is valid
339 static function validate_activation_code($code) {
340 if (strlen($code) == 6 && ($code[0] + $code[5]) == 9) {
341 return true;
342 } else {
343 return false;
344 }
345 } // validate_activation_code
346
347
348 // helper function for creating dropdowns
349 static function create_select_options($options, $selected = null, $output = true) {
350 $out = "\n";
351
352 foreach ($options as $tmp) {
353 if ($selected == $tmp['val']) {
354 $out .= "<option selected=\"selected\" value=\"{$tmp['val']}\">{$tmp['label']}&nbsp;</option>\n";
355 } else {
356 $out .= "<option value=\"{$tmp['val']}\">{$tmp['label']}&nbsp;</option>\n";
357 }
358 } // foreach
359
360 if ($output) {
361 echo $out;
362 } else {
363 return $out;
364 }
365 } // create_select_options
366
367
368 // fetch coordinates based on the address
369 static function get_coordinates($address, $force_refresh = false) {
370 $address_hash = md5('gmw' . $address);
371
372 if ($force_refresh || ($coordinates = get_transient($address_hash)) === false) {
373 $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address) . '&sensor=false';
374 $result = wp_remote_get($url);
375
376 if (!is_wp_error($result) && $result['response']['code'] == 200) {
377 $data = new SimpleXMLElement($result['body']);
378
379 if ($data->status == 'OK') {
380 $cache_value['lat'] = (string) $data->result->geometry->location->lat;
381 $cache_value['lng'] = (string) $data->result->geometry->location->lng;
382 $cache_value['address'] = (string) $data->result->formatted_address;
383
384 // cache coordinates for 3 months
385 set_transient($address_hash, $cache_value, 3600*24*30*3);
386 $data = $cache_value;
387 } elseif (!$data->status) {
388 return false;
389 } else {
390 return false;
391 }
392 } else {
393 return false;
394 }
395 } else {
396 // data is cached, get it
397 $data = get_transient($address_hash);
398 }
399
400 return $data;
401 } // get_coordinates
402
403
404 // shortcode support for any GMW instance
405 static function do_shortcode($atts, $content = null) {
406 if (!self::is_activated()) {
407 return;
408 }
409
410 global $wp_widget_factory;
411 $atts = shortcode_atts(array('id' => 0), $atts);
412 $id = (int) $atts['id'];
413 $widgets = get_option('widget_googlemapswidget');
414
415 if (!$id || !isset($widgets[$id]) || empty($widgets[$id])) {
416 echo '<span class="gmw-error">Google Maps Widget shortcode error - please double-check the widget ID.</span>';
417 } else {
418 $widget_args = $widgets[$id];
419 $widget_instance['widget_id'] = 'googlemapswidget-' . $id;
420 $widget_instance['widget_name'] = 'Google Maps Widget';
421
422 echo '<span class="gmw-shortcode-widget">';
423 the_widget('GoogleMapsWidget', $widget_args, $widget_instance);
424 echo '</span>';
425 }
426 } // do_shortcode
427
428
429 // activate doesn't get fired on upgrades so we have to compensate
430 public static function upgrade() {
431 $options = get_option(GMW_OPTIONS);
432
433 if (!isset($options['first_version']) || !isset($options['first_install'])) {
434 $options['first_version'] = GMW_VER;
435 $options['first_install'] = current_time('timestamp');
436 update_option(GMW_OPTIONS, $options);
437 }
438 } // upgrade
439
440
441 // write down a few things on plugin activation
442 // NO DATA is sent anywhere unless user explicitly agrees to it!
443 static function activate() {
444 $options = get_option(GMW_OPTIONS);
445
446 if (!isset($options['first_version']) || !isset($options['first_install'])) {
447 $options['first_version'] = GMW_VER;
448 $options['first_install'] = current_time('timestamp');
449 $options['last_tracking'] = false;
450 update_option(GMW_OPTIONS, $options);
451 }
452 } // activate
453
454
455 // clean up on deactivation
456 static function deactivate() {
457 $options = get_option(GMW_OPTIONS);
458
459 if (isset($options['allow_tracking']) && $options['allow_tracking'] === true) {
460 GMW_tracking::clear_cron();
461 }
462 } // deactivate
463
464
465 // clean up on uninstall / delete
466 static function uninstall() {
467 if (!defined('WP_UNINSTALL_PLUGIN')) {
468 return;
469 }
470
471 delete_option(GMW_OPTIONS);
472 } // uninstall
473 } // class GMW
474
475
476 // hook everything up
477 register_activation_hook(__FILE__, array('GMW', 'activate'));
478 register_deactivation_hook(__FILE__, array('GMW', 'deactivate'));
479 register_uninstall_hook(__FILE__, array('GMW', 'uninstall'));
480 add_action('init', array('GMW', 'init'));
481 add_action('plugins_loaded', array('GMW', 'plugins_loaded'));
482 add_action('widgets_init', array('GMW', 'widgets_init'));