';
echo '
Enter your address
';
}
/* Prints the edit form for pre-WordPress 2.5 post/page */
function geolocation_old_custom_box() {
echo '' . "\n";
echo '
\n";
}
function geolocation_save_postdata($post_id) {
// Check authorization, permissions, autosave, etc
if (!wp_verify_nonce($_POST['geolocation_nonce'], plugin_basename(__FILE__)))
return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if('page' == $_POST['post_type'] ) {
if(!current_user_can('edit_page', $post_id))
return $post_id;
} else {
if(!current_user_can('edit_post', $post_id))
return $post_id;
}
$latitude = clean_coordinate($_POST['geolocation-latitude']);
$longitude = clean_coordinate($_POST['geolocation-longitude']);
$address = reverse_geocode($latitude, $longitude);
$public = $_POST['geolocation-public'];
$on = $_POST['geolocation-on'];
if((clean_coordinate($latitude) != '') && (clean_coordinate($longitude)) != '') {
update_post_meta($post_id, 'geo_latitude', $latitude);
update_post_meta($post_id, 'geo_longitude', $longitude);
if(esc_html($address) != '')
update_post_meta($post_id, 'geo_address', $address);
if($on) {
update_post_meta($post_id, 'geo_enabled', 1);
if($public)
update_post_meta($post_id, 'geo_public', 1);
else
update_post_meta($post_id, 'geo_public', 0);
}
else {
update_post_meta($post_id, 'geo_enabled', 0);
update_post_meta($post_id, 'geo_public', 1);
}
}
return $post_id;
}
function admin_init() {
add_action('admin_head-post-new.php', 'admin_head');
add_action('admin_head-post.php', 'admin_head');
add_action('admin_menu', 'geolocation_add_custom_box');
add_action('save_post', 'geolocation_save_postdata');
}
function admin_head() {
global $post;
$post_id = $post->ID;
$post_type = $post->post_type;
$zoom = (int) get_option('geolocation_default_zoom');
?>
';
//}
function add_geo_support() {
global $geolocation_options, $posts;
// To do: add support for multiple Map API providers
switch(PROVIDER) {
case 'google':
echo add_google_maps($posts);
break;
case 'yahoo':
echo add_yahoo_maps($posts);
break;
case 'bing':
echo add_bing_maps($posts);
break;
}
echo '';
}
function add_google_maps($posts) {
default_settings();
$zoom = (int) get_option('geolocation_default_zoom');
global $post_count;
$post_count = count($posts);
echo '
';
}
function geo_has_shortcode($content) {
$pos = strpos($content, SHORTCODE);
if($pos === false)
return false;
else
return true;
}
function display_location($content) {
default_settings();
global $post, $shortcode_tags, $post_count;
// Backup current registered shortcodes and clear them all out
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array();
$post_id = $post->ID;
$latitude = clean_coordinate(get_post_meta($post->ID, 'geo_latitude', true));
$longitude = clean_coordinate(get_post_meta($post->ID, 'geo_longitude', true));
$address = get_post_meta($post->ID, 'geo_address', true);
$public = (bool)get_post_meta($post->ID, 'geo_public', true);
$on = true;
if(get_post_meta($post->ID, 'geo_enabled', true) != '')
$on = (bool)get_post_meta($post->ID, 'geo_enabled', true);
if(empty($address))
$address = reverse_geocode($latitude, $longitude);
if((!empty($latitude)) && (!empty($longitude) && ($public == true) && ($on == true))) {
$html = '';
switch(esc_attr(get_option('geolocation_map_position')))
{
case 'before':
$content = str_replace(SHORTCODE, '', $content);
$content = $html.'
'.$content;
break;
case 'after':
$content = str_replace(SHORTCODE, '', $content);
$content = $content.'
'.$html;
break;
case 'shortcode':
$content = str_replace(SHORTCODE, $html, $content);
break;
}
}
else {
$content = str_replace(SHORTCODE, '', $content);
}
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;
return $content;
}
function reverse_geocode($latitude, $longitude) {
$url = "http://maps.google.com/maps/api/geocode/json?latlng=".$latitude.",".$longitude."&sensor=false";
$result = wp_remote_get($url);
$json = json_decode($result['body']);
foreach ($json->results as $result)
{
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types)))
$city = $addressPart->long_name;
else if((in_array('administrative_area_level_1', $addressPart->types)) && (in_array('political', $addressPart->types)))
$state = $addressPart->long_name;
else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types)))
$country = $addressPart->long_name;
}
}
if(($city != '') && ($state != '') && ($country != ''))
$address = $city.', '.$state.', '.$country;
else if(($city != '') && ($state != ''))
$address = $city.', '.$state;
else if(($state != '') && ($country != ''))
$address = $state.', '.$country;
else if($country != '')
$address = $country;
return $address;
}
function clean_coordinate($coordinate) {
$pattern = '/^(\-)?(\d{1,3})\.(\d{1,15})/';
preg_match($pattern, $coordinate, $matches);
return $matches[0];
}
function add_settings() {
if ( is_admin() ){ // admin actions
add_options_page('Geolocation Plugin Settings', 'Geolocation', 'administrator', 'geolocation.php', 'geolocation_settings_page', __FILE__);
add_action( 'admin_init', 'register_settings' );
} else {
// non-admin enqueues, actions, and filters
}
}
function register_settings() {
register_setting( 'geolocation-settings-group', 'geolocation_map_width', 'intval' );
register_setting( 'geolocation-settings-group', 'geolocation_map_height', 'intval' );
register_setting( 'geolocation-settings-group', 'geolocation_default_zoom', 'intval' );
register_setting( 'geolocation-settings-group', 'geolocation_map_position' );
register_setting( 'geolocation-settings-group', 'geolocation_wp_pin');
}
function is_checked($field) {
if (get_option($field))
echo ' checked="checked" ';
}
function is_value($field, $value) {
if (get_option($field) == $value)
echo ' checked="checked" ';
}
function default_settings() {
if(get_option('geolocation_map_width') == '0')
update_option('geolocation_map_width', '450');
if(get_option('geolocation_map_height') == '0')
update_option('geolocation_map_height', '200');
if(get_option('geolocation_default_zoom') == '0')
update_option('geolocation_default_zoom', '16');
if(get_option('geolocation_map_position') == '0')
update_option('geolocation_map_position', 'after');
}
function geolocation_settings_page() {
default_settings();
$zoomImage = get_option('geolocation_default_zoom');
if(get_option('geolocation_wp_pin'))
$zoomImage = 'wp_'.$zoomImage.'.png';
else
$zoomImage = $zoomImage.'.png';
?>
Geolocation Plugin Settings