' . __('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;
} // run_scroller
// 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('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('gmw-admin', plugins_url('css/gmw-admin.css', __FILE__), array(), GMW_VER);
} // 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
// 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 .= "{$tmp['label']} \n";
} else {
$out .= "{$tmp['label']} \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
// 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'] === false) {
unset($options['allow_tracking']);
update_option(GMW_OPTIONS, $options);
} elseif (isset($options['allow_tracking']) && $options['allow_tracking'] === true) {
GMW_tracking::clear_cron();
}
} // activate
} // class GMW
// hook everything up
register_activation_hook(__FILE__, array('GMW', 'activate'));
register_deactivation_hook(__FILE__, array('GMW', 'deactivate'));
add_action('init', array('GMW', 'init'));
add_action('plugins_loaded', array('GMW', 'plugins_loaded'));
add_action('widgets_init', array('GMW', 'widgets_init'));