' . __('Widgets', 'google-maps-widget') . ''; array_unshift($links, $settings_link); return $links; } // plugin_action_links // add links to plugin's description in plugins table static function plugin_meta_links($links, $file) { $documentation_link = ''. __('Documentation', 'google-maps-widget') . ''; $support_link = '' . __('Support', 'google-maps-widget') . ''; $review_link = '' . __('Review the plugin', 'google-maps-widget') . ''; $donate_link = '' . __('Donate', 'google-maps-widget') . ''; if ($file == plugin_basename(__FILE__)) { $links[] = $documentation_link; $links[] = $support_link; $links[] = $review_link; $links[] = $donate_link; } return $links; } // plugin_meta_links // check if user has the minimal WP version required by the plugin static function check_wp_version($min_version) { if (!version_compare(get_bloginfo('version'), $min_version, '>=')) { add_action('admin_notices', array(__CLASS__, 'min_version_error')); } } // check_wp_version // display error message if WP version is too low static function min_version_error() { echo '

' . sprintf('Google Maps Widget requires WordPress version 3.3 or higher to function properly. You are using WordPress version %s. Please update it.', get_bloginfo('version'), admin_url('update-core.php')) . '

'; } // min_version_error // print dialogs markup in footer static function dialogs_markup() { $out = ''; $widgets = GoogleMapsWidget::$widgets; if (!$widgets) { wp_dequeue_script('gmw'); wp_dequeue_script('gmw-fancybox'); return; } foreach ($widgets as $widget) { if ($widget['bubble']) { $iwloc = 'addr'; } else { $iwloc = 'near'; } if ($widget['ll']) { $ll = '&ll=' . $widget['ll']; } else { $ll = ''; } $lang = substr(@$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); if (!$lang) { $lang = 'en'; } $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; $out .= '\n"; } // foreach $widgets echo $out; } // dialogs_markup // check availability and register shortcode static function add_shortcode() { global $shortcode_tags; if (isset($shortcode_tags['gmw'])) { add_action('admin_notices', array(__CLASS__, 'notice_sc_conflict_error')); } else { add_shortcode('gmw', array(__CLASS__, 'do_shortcode')); } } // add_shortcode // display notice if shortcode name is already taken static function notice_sc_conflict_error() { if (!self::is_activated()) { return; } echo '

' . __('Google Maps Widget shortcode is not active!', 'google-maps-widget') . '' . __(' Shortcode [gmw] is already in use by another plugin or theme. Please deactivate that theme or plugin.', 'google-maps-widget') . '

'; } // notice_sc_conflict_error // enqueue frontend scripts if necessary static function enqueue_scripts() { if (is_active_widget(false, false, 'googlemapswidget', true)) { wp_enqueue_style('gmw', plugins_url('/css/gmw.css', __FILE__), array(), GMW_VER); wp_enqueue_script('gmw-colorbox', plugins_url('/js/jquery.colorbox.min.js', __FILE__), array('jquery'), GMW_VER, true); wp_enqueue_script('gmw', plugins_url('/js/gmw.js', __FILE__), array('jquery'), GMW_VER, true); } } // enqueue_scripts // enqueue CSS and JS scripts on widgets page static function admin_enqueue_scripts() { global $wp_customize; if (self::is_plugin_admin_page() || isset($wp_customize)) { wp_enqueue_script('jquery-ui-tabs'); wp_enqueue_script('jquery-ui-dialog'); wp_enqueue_script('gmw-cookie', plugins_url('js/jquery.cookie.js', __FILE__), array('jquery'), GMW_VER, true); wp_enqueue_script('gmw-admin', plugins_url('js/gmw-admin.js', __FILE__), array('jquery'), GMW_VER, true); wp_enqueue_style('wp-jquery-ui-dialog'); wp_enqueue_style('gmw-admin', plugins_url('css/gmw-admin.css', __FILE__), array(), GMW_VER); $js_localize = array('subscribe_ok' => __('Check your inbox. Email with activation code is on its way.', 'google-maps-widget'), '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'), 'subscribe_error' => __('Something is not right on our end. Sorry :( Try again later.', 'google-maps-widget'), 'activate_ok' => __('Superb! Extra features are active ;)', 'google-maps-widget'), 'dialog_title' => __('GOOGLE MAPS WIDGET - Activate Extra Features', 'google-maps-widget')); wp_localize_script('gmw-admin', 'gmw', $js_localize); } // if } // admin_enqueue_scripts // check if plugin's admin page is shown static function is_plugin_admin_page() { $current_screen = get_current_screen(); if ($current_screen->id == 'widgets') { return true; } else { return false; } } // is_plugin_admin_page // check if activate-by-subscribing features have been activated static function is_activated() { $options = get_option(GMW_OPTIONS); if (isset($options['activated']) && $options['activated'] === true) { return true; } else { return false; } } // is_activated // echo markup for promo dialog; only on widgets page static function admin_dialogs_markup() { if (!self::is_plugin_admin_page()) { return false; } $current_user = wp_get_current_user(); if (empty($current_user->user_firstname)) { $name = $current_user->display_name; } else { $name = $current_user->user_firstname; } $out = '
'; $out .= '

' . __('Subscribe to our newsletter
and get extra features & options for FREE!', 'google-maps-widget') . '

'; $out .= '

'; $out .= '

'; $out .= '

Subscribe & activate extra features
I\'m already subscribed

'; $out .= ''; $out .= '
'; // dialog subscribe $out .= '

' . __('Enter your code and activate extra features', 'google-maps-widget') . '

'; $out .= '

Activate extra features

'; $out .= ''; $out .= '
'; // activate screen $out .= '
'; // dialog echo $out; } // admin_dialogs_markup // send user's email to MailChimp via our server static function email_subscribe() { $name = trim($_POST['name']); $email = trim($_POST['email']); if (defined('WPLANG')) { $lang = strtolower(substr(WPLANG, 0, 2)); } else { $lang = 'en'; } $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()))); // something's wrong with our server if ($res['response']['code'] != 200 || is_wp_error($res)) { echo 'err'; die(); } if ($res['body'] == 'ok') { echo "ok"; die(); } elseif ($res['body'] == 'duplicate') { echo "duplicate"; die(); } else { // todo var_dump($res['body']); echo 'err'; die(); } } // email_subscribe // check activation code and save if valid static function activate_via_code() { $code = trim($_GET['code']); if (self::validate_activation_code($code)) { $options = get_option(GMW_OPTIONS); $options['activation_code'] = $code; $options['activated'] = true; update_option(GMW_OPTIONS, $options); die("1"); } else { die("0"); } } // email_activate // check if activation code for additional features is valid static function validate_activation_code($code) { if (strlen($code) == 6 && ($code[0] + $code[5]) == 9) { return true; } else { return false; } } // validate_activation_code // helper function for creating dropdowns static function create_select_options($options, $selected = null, $output = true) { $out = "\n"; foreach ($options as $tmp) { if ($selected == $tmp['val']) { $out .= "\n"; } else { $out .= "\n"; } } // foreach if ($output) { echo $out; } else { return $out; } } // create_select_options // fetch coordinates based on the address static function get_coordinates($address, $force_refresh = false) { $address_hash = md5('gmw' . $address); if ($force_refresh || ($coordinates = get_transient($address_hash)) === false) { $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . urlencode($address) . '&sensor=false'; $result = wp_remote_get($url); if (!is_wp_error($result) && $result['response']['code'] == 200) { $data = new SimpleXMLElement($result['body']); if ($data->status == 'OK') { $cache_value['lat'] = (string) $data->result->geometry->location->lat; $cache_value['lng'] = (string) $data->result->geometry->location->lng; $cache_value['address'] = (string) $data->result->formatted_address; // cache coordinates for 3 months set_transient($address_hash, $cache_value, 3600*24*30*3); $data = $cache_value; } elseif (!$data->status) { return false; } else { return false; } } else { return false; } } else { // data is cached, get it $data = get_transient($address_hash); } return $data; } // get_coordinates // shortcode support for any GMW instance static function do_shortcode($atts, $content = null) { if (!self::is_activated()) { return; } global $wp_widget_factory; $atts = shortcode_atts(array('id' => 0), $atts); $id = (int) $atts['id']; $widgets = get_option('widget_googlemapswidget'); if (!$id || !isset($widgets[$id]) || empty($widgets[$id])) { echo 'Google Maps Widget shortcode error - please double-check the widget ID.'; } else { $widget_args = $widgets[$id]; $widget_instance['widget_id'] = 'googlemapswidget-' . $id; $widget_instance['widget_name'] = 'Google Maps Widget'; echo ''; the_widget('GoogleMapsWidget', $widget_args, $widget_instance); echo ''; } } // do_shortcode // activate doesn't get fired on upgrades so we have to compensate public static function upgrade() { $options = get_option(GMW_OPTIONS); if (!isset($options['first_version']) || !isset($options['first_install'])) { $options['first_version'] = GMW_VER; $options['first_install'] = current_time('timestamp'); update_option(GMW_OPTIONS, $options); } } // upgrade // write down a few things on plugin activation // NO DATA is sent anywhere unless user explicitly agrees to it! static function activate() { $options = get_option(GMW_OPTIONS); if (!isset($options['first_version']) || !isset($options['first_install'])) { $options['first_version'] = GMW_VER; $options['first_install'] = current_time('timestamp'); $options['last_tracking'] = false; update_option(GMW_OPTIONS, $options); } } // activate // clean up on deactivation static function deactivate() { $options = get_option(GMW_OPTIONS); if (isset($options['allow_tracking']) && $options['allow_tracking'] === true) { GMW_tracking::clear_cron(); } } // deactivate // clean up on uninstall / delete static function uninstall() { if (!defined('WP_UNINSTALL_PLUGIN')) { return; } delete_option(GMW_OPTIONS); } // uninstall } // class GMW // hook everything up register_activation_hook(__FILE__, array('GMW', 'activate')); register_deactivation_hook(__FILE__, array('GMW', 'deactivate')); register_uninstall_hook(__FILE__, array('GMW', 'uninstall')); add_action('init', array('GMW', 'init')); add_action('plugins_loaded', array('GMW', 'plugins_loaded')); add_action('widgets_init', array('GMW', 'widgets_init'));