' . __('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') . ''; $activate_link = '' . __('Activate premium features for FREE', 'google-maps-widget') . ''; if ($file == plugin_basename(__FILE__)) { $links[] = $documentation_link; $links[] = $support_link; $links[] = $review_link; $links[] = $donate_link; if (!self::is_activated()) { $links[] = $activate_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__, 'notice_min_version_error')); } } // check_wp_version // display error message if WP version is too low static function notice_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.', 'google-maps-widget'), get_bloginfo('version'), admin_url('update-core.php')) . '

'; } // notice_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 // handle dismiss button for all notices // todo - convert all notices static function dismiss_notice() { $options = get_option(GMW_OPTIONS, array()); if (isset($_GET['notice']) && $_GET['notice'] == 'upgrade') { $options['dismiss_notice_upgrade'] = true; update_option(GMW_OPTIONS, $options); } if (isset($_GET['notice']) && $_GET['notice'] == 'rate') { $options['dismiss_notice_rate'] = true; update_option(GMW_OPTIONS, $options); } if ($_GET['redirect']) { wp_redirect($_GET['redirect']); } else { wp_redirect(admin_url()); } exit; } // dismiss_notice // maybe show some notices static function add_notices() { $options = get_option(GMW_OPTIONS, array()); $notice = false; if ((!isset($options['dismiss_notice_upgrade']) || $options['dismiss_notice_upgrade'] == false) && !self::is_activated() && (GMW_tracking::count_active_widgets() > 0 || (current_time('timestamp') - $options['first_install']) > (DAY_IN_SECONDS * 3))) { add_action('admin_notices', array(__CLASS__, 'notice_activate_extra_features')); $notice = true; } // show upgrade notice if (!$notice && (!isset($options['dismiss_notice_rate']) || $options['dismiss_notice_rate'] == false) && GMW_tracking::count_active_widgets() > 0 && (current_time('timestamp') - $options['first_install']) > (DAY_IN_SECONDS * 20)) { add_action('admin_notices', array(__CLASS__, 'notice_rate_plugin')); } // show rate notice } // add_notices // display message to get extra features for GMW static function notice_activate_extra_features() { $activate_url = admin_url('widgets.php?gmw_open_promo_dialog'); $dismiss_url = add_query_arg(array('action' => 'gmw_dismiss_notice', 'notice' => 'upgrade', 'redirect' => urlencode($_SERVER['REQUEST_URI'])), admin_url('admin.php')); // todo detect WP version and add support for "is-dismissible" // todo remove style from HTML echo '

' . __('Google Maps Widget has extra premium features you can get for FREE. This is a limited time offer so act now!', 'google-maps-widget'); echo '
' . __('Activate premium features for FREE', 'google-maps-widget') . ''; echo '  ' . __('Dismiss notice', 'google-maps-widget') . ''; echo '

'; } // notice_activate_extra_features // display message to get extra features for GMW static function notice_rate_plugin() { $rate_url = 'https://wordpress.org/support/view/plugin-reviews/google-maps-widget?rate=5#postform'; $dismiss_url = add_query_arg(array('action' => 'gmw_dismiss_notice', 'notice' => 'rate', 'redirect' => urlencode($_SERVER['REQUEST_URI'])), admin_url('admin.php')); // todo detect WP version and add support for "is-dismissible" // todo remove style from HTML echo '

' . __('Hi! We saw you\'ve been using Google Maps Widget for some time and wanted to ask for your help to make the plugin even better.
We don\'t need money :), just a minute of your time to rate the plugin. Thank you!', 'google-maps-widget'); echo '
' . __('Help us out & rate the plugin', 'google-maps-widget') . ''; echo '  ' . __('Dismiss notice', 'google-maps-widget') . ''; echo '

'; } // notice_rate_plugin // 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::$version); wp_enqueue_script('gmw-colorbox', plugins_url('/js/jquery.colorbox.min.js', __FILE__), array('jquery'), GMW::$version, true); wp_enqueue_script('gmw', plugins_url('/js/gmw.js', __FILE__), array('jquery'), GMW::$version, 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::$version, true); wp_enqueue_script('gmw-admin', plugins_url('js/gmw-admin.js', __FILE__), array('jquery'), GMW::$version, true); wp_enqueue_style('wp-jquery-ui-dialog'); wp_enqueue_style('gmw-admin', plugins_url('css/gmw-admin.css', __FILE__), array(), GMW::$version); $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'), 'undocumented_error' => __('An undocumented error has occured. Please refresh the page and try again.', 'google-maps-widget'), 'id_base' => 'googlemapswidget'); 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 .= '

' . __('Fill out the form and
get extra features & options for FREE instantly!', 'google-maps-widget') . '

'; $out .= '

'; $out .= '

'; $out .= '

Activate extra features
I already have an activation code

'; $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)) { wp_send_json_error('unknown'); } if ($res['body'] == 'ok') { wp_send_json_success(); } elseif ($res['body'] == 'duplicate') { wp_send_json_error('duplicate'); } else { wp_send_json_error('unknown'); } } // email_subscribe // check activation code and save if valid static function activate_via_code() { $code = trim($_POST['code']); if (self::validate_activation_code($code)) { $options = get_option(GMW_OPTIONS); $options['activation_code'] = $code; $options['activated'] = true; update_option(GMW_OPTIONS, $options); wp_send_json_success(); } else { wp_send_json_error(); } } // 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::$version; $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::$version; $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'));