Geolocation API and display the location using Google Maps. It also supports the geo-location-javascript library and the MaxMind GeoIP Javascript Service for backwards compatibility.
* Plugin Name: Author geoLocation
* Plugin URI: http://xenthrax.com/wordpress/author-geolocation/
* Version: 1.1a1
*
* Plugin Shortlink: http://xenthrax.com/author-geolocation/
* Other Plugins: http://xenthrax.com/wordpress/
*
* WordPress Plugin: https://wordpress.org/extend/plugins/author-geolocation/
* GitHub Repo: https://github.com/TheTomThorogood/Author-geoLocation
*/
/**
* If you notice any issues or bugs in the plugin please contact me [@link http://xenthrax.com/about]
* If you make any revisions to and/or re-release this plugin please contact me [@link http://xenthrax.com/about]
*/
/**
* Copyright (c) 2010-2012 Tom Thorogood
*
* This file is part of "Author geoLocation" Wordpress Plugin.
*
* "Author geoLocation" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 3.
*
* You may NOT assume that you can use any other version of the GPL.
*
* "Author geoLocation" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "Author geoLocation". If not, see .
*/
/**
* @package Author geoLocation
* @since 1.0
*/
class Author_geoLocation {
/**
* @access private
* @since 1.0
* @var array
*/
private $notices = array();
/**
* @access public
* @since 1.0
* @return string Author geoLocation version
*/
function version() {
static $plugin_data;
if (is_null($plugin_data))
$plugin_data = get_file_data(__FILE__, array('Version' => 'Version'));
return $plugin_data['Version'];
}
/**
* @since 1.0
*/
function Author_geoLocation() {
$args = func_get_args();
call_user_func_array(array(&$this, '__construct'), $args);
}
/**
* @since 1.0
*/
function __construct() {
load_plugin_textdomain('author-geolocation', false, basename(dirname(__FILE__)) . '/lang');
$this->add_option('version', $this->version());
foreach ($this->default_options() as $name => $value)
$this->add_option($name, $value);
foreach (array('admin_head-post-new.php', 'admin_head-post.php') as $filter)
add_action($filter, array(&$this, '_admin_head_post'));
foreach (array('load-post-new.php', 'load-post.php') as $filter)
add_action($filter, array(&$this, '_admin_post_init'));
add_action('admin_head-' . $this->slug(true), array(&$this, '_admin_head_options'));
add_action('load-' . $this->slug(true), array(&$this, '_options_init'));
add_action('admin_notices', array(&$this, '_admin_notices'));
add_action('the_content', array(&$this, '_the_content'));
add_action('admin_menu', array(&$this, '_admin_menu'));
add_action('save_post', array(&$this, '_save_post'));
add_action('wp_head', array(&$this, '_head'));
add_action('init', array(&$this, '_init'));
add_shortcode('address', array(&$this, '_address_shortcode'));
add_shortcode('map', array(&$this, '_map_shortcode'));
}
/**
* @access private
* @since 1.0
* @return void
*/
function _init() {
wp_enqueue_script('google-maps', '//maps.google.com/maps/api/js?sensor=false', array(), 3);
}
/**
* @access private
* @since 1.0
* @return void
*/
function _admin_post_init() {
wp_enqueue_script('jquery');
wp_enqueue_script('google-maps', '//maps.google.com/maps/api/js?sensor=false', array(), 3);
if ($this->get_option('legacy')) {
wp_enqueue_script('google-gears', $this->cwu() . '/js/gears.init.js', array(), null);
wp_enqueue_script('geo-location', $this->cwu() . '/js/geo.js', array(), '0.4.7');
}
}
/**
* @access private
* @since 1.0
* @return void
*/
function _head() {
?>
true,
'position' => 'after',
'type' => 'ROADMAP',
'zoom' => 15
);
}
/**
* @access private
* @since 1.0
* @return string Author geoLocation slug
*/
private function slug($settings = false) {
return $settings ? 'settings_page_' . basename(dirname(__FILE__)) . '/' . substr(basename(__FILE__), 0, -4) : basename(dirname(__FILE__)) . '/' . basename(__FILE__);
}
/**
* @note WordPress < 2.9.0 will always return true
* @access public
* @since 1.0
* @return bool Is latest version of Author geoLocation
*/
function latest_version() {
if (function_exists('get_site_transient')) {
$plugins = get_site_transient('update_plugins');
return (!isset($plugins->response) || !is_array($plugins->response) || !isset($plugins->response[$this->slug()]));
}
return true;
}
/**
* @access private
* @since 1.0
* @return string Curent workign url
*/
private function cwu() {
return WP_PLUGIN_URL . '/' . basename(dirname(__FILE__));
}
/**
* @access private
* @since 1.0
* @return void
*/
function _options_init() {
if (isset($_POST['Author-geoLocation-submit'])) {
if (check_admin_referer(__FILE__ . $this->version())) {
$this->set_option('legacy', isset($_POST['Author-geoLocation-legacy']));
if (isset($_POST['Author-geoLocation-position'])) {
$position = stripslashes(strtolower(trim($_POST['Author-geoLocation-position'])));
if (in_array($position, array('manual', 'before', 'after')))
$this->set_option('position', $position);
}
if (isset($_POST['Author-geoLocation-type'])) {
$type = stripslashes(strtoupper(trim($_POST['Author-geoLocation-type'])));
if (in_array($type, array('ROADMAP', 'SATELLITE', 'HYBRID', 'TERRAIN')))
$this->set_option('type', $type);
}
if (isset($_POST['Author-geoLocation-zoom'])) {
$zoom = stripslashes(trim($_POST['Author-geoLocation-zoom']));
if (is_numeric($zoom) && intval($zoom) >= 0)
$this->set_option('zoom', intval($zoom));
}
$this->add_notice('Options saved successfully.');
}
} else if (isset($_POST['Author-geoLocation-reset'])) {
if (check_admin_referer(__FILE__ . $this->version())) {
foreach ($this->default_options() as $name => $value)
$this->set_option($name, $value);
$this->add_notice('Options reset.');
}
}
}
/**
* @access public
* @since 1.0
* @param int $id
* @return object|null Location
*/
function location($id = 0) {
if (empty($id))
$id = get_the_ID();
$location = get_post_meta($id, '_Author-geoLocation-location', true);
if (empty($location))
return null;
if (is_object($location))
$location = get_object_vars($location);
else if (is_string($location))
$location = @unserialize($location);
if (empty($location))
return null;
$location = (array)$location;
if (isset($location['error']) && $location['error'])
return false;
if (!isset($location['latitude'], $location['longitude'], $location['latlng']) || empty($location['latitude']) || empty($location['longitude']) || empty($location['latlng']))
return null;
unset($location['error'], $location['post_id']);
return (object)array_merge(array(
'address' => '',
'latlng' => '0,0',
'latitude' => '0',
'longitude' => '0',
'position' => $this->get_option('position'),
'post_id' => $id,
'type' => $this->get_option('type'),
'zoom' => $this->get_option('zoom')
), $location);
}
/**
* @access public
* @since 1.0
* @param int $id
* @return object|null Last known location
*/
function lastLocation($id = 0) {
global $wpdb;
if (empty($id))
$id = get_current_user_id();
$last = $wpdb->get_var($wpdb->prepare("SELECT post.ID FROM $wpdb->postmeta meta, $wpdb->posts post WHERE post.post_author = '%d' AND meta.post_ID = post.ID AND meta.meta_key = '_location' ORDER BY post.post_date_gmt DESC LIMIT 0,1", $id));
return !empty($last) ? $this->location($last) : null;
}
/**
* @access private
* @since 1.0
* @param string $msg
* @param string $type Optional.
* @param int $priority Optional.
* @return void
*/
private function add_notice($msg, $type = 'updated', $priority = false) {
$type = strtolower($type);
$priority = ($priority === false) ? (($type == 'error') ? 5 : 10) : (int)$priority;
if (!isset($this->notices[$priority]))
$this->notices[$priority] = array();
$this->notices[$priority][] = (object)array(
'msg' => $msg,
'type' => $type
);
}
/**
* @access private
* @since 1.0
* @return void
*/
function _admin_notices() {
ksort($this->notices);
foreach ($this->notices as $priority => $notices) {
foreach ($notices as $notice)
echo '
\n";
}
}
/**
* @access private
* @since 1.0
* @param object $location
* @return string Adress link HTML
*/
function _address_html($location) {
switch ($location->type) {
case 'HYBRID':
$map_t = 'h';
break;
case 'SATELLITE':
$map_t = 'k';
break;
case 'TERRAIN':
$map_t = 'p';
break;
case 'ROADMAP':
default:
$map_t = 'm';
break;
}
$address = esc_html($location->address);
return <<{$address}
EOD;
}
/**
* @access private
* @since 1.0
* @param object $location
* @param bool $show_address Optional.
* @return string Map HTML
*/
private function map_html($location, $show_address = true) {
static $mapcount = 0;
$mapcount++;
$address = sprintf(__('Posted from %1$s', 'author-geolocation'), $this->_address_html($location));
$address_style = $show_address ? '' : ' style="display:none;"';
$type = strtolower($location->type);
$size = apply_filters('Author_geoLocation_fallback_image_size', '640x250', $location);
return <<version()}: http://xenthrax.com/wordpress/author-geolocation/-->
EOD;
}
/**
* @access private
* @since 1.0
* @param array $atts
* @return string Map HTML
*/
function _map_shortcode($atts) {
$atts = shortcode_atts(array('dont_hide' => false, 'post_id' => 0, 'show_address' => false), $atts);
if ($atts['dont_hide'] || is_singular()) {
$location = $this->location($atts['post_id']);
if ($location)
return $this->map_html($location, $atts['show_address']);
}
return '';
}
/**
* @access private
* @since 1.0
* @param array $atts
* @return string Address
*/
function _address_shortcode($atts) {
$atts = shortcode_atts(array('before' => __('Posted from ', 'author-geolocation'), 'after' => '.', 'linkify' => true, 'post_id' => 0), $atts);
$location = $this->location($atts['post_id']);
return $location ? $atts['before'] . ($atts['linkify'] ? $this->_address_html($location) : htmlentities($location->address)) . $atts['after'] : '';
}
/**
* @access private
* @since 1.0
* @param string $content
* @return Post content
*/
function _the_content($content) {
if (is_singular()) {
$location = $this->location();
if ($location) {
switch ($location->position) {
case 'before':
$content = $this->map_html($location) . $content;
break;
case 'after':
$content .= $this->map_html($location);
break;
}
}
}
return $content;
}
/**
* @access private
* @since 1.0
* @param int $id
* @return int Post id
*/
function _save_post($id) {
if (isset($_POST['Author-geoLocation-nonce'])
&& wp_verify_nonce($_POST['Author-geoLocation-nonce'], __FILE__ . $this->version())
&& (!defined('DOING_AUTOSAVE') || !DOING_AUTOSAVE)
// && current_user_can('edit_' . ($_POST['post_type'] == 'page' ? 'page' : 'post'), $id)) {
&& current_user_can(sprintf('edit_%s', $_POST['post_type']), $id)) {
$address = stripslashes(trim($_POST['Author-geoLocation-address']));
$location = array('error' => true);
if (!empty($address)) {
$latlng = stripslashes(trim($_POST['Author-geoLocation-latlng']));
if (empty($latlng) || !$_POST['Author-geoLocation-js']) {
$result = wp_remote_get('http://maps.google.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false');
$json = json_decode($result['body']);
if (!is_a($json, 'WP_Error') && $json->status == 'OK' && count($json->results) > 0) {
$latlng = $json->results[0]->geometry->location->lat . ',' . $json->results[0]->geometry->location->lng;
$latlng_ = array(
$json->results[0]->geometry->location->lat,
$json->results[0]->geometry->location->lng
);
} else {
$this->add_notice(sprintf(__('Author geoLocation encountered an error while trying to convert %1$s to latlng coordanates.', 'author-geolocation'), htmlentities($address));
return $id;
}
} else
$latlng_ = explode(',', $latlng);
if (!empty($latlng) && count($latlng_) == 2 && !empty($latlng_[0]) && !empty($latlng_[1])) {
$suffix = $_POST['Author-geoLocation-js'] ? '' : '-no-js';
$position = stripslashes(strtolower(trim($_POST['Author-geoLocation-position'])));
$type = stripslashes(strtoupper(trim($_POST['Author-geoLocation-type' . $suffix])));
$zoom = stripslashes(trim($_POST['Author-geoLocation-zoom' . $suffix]));
$location = array(
'address' => $address,
'latlng' => $latlng,
'latitude' => $latlng_[0],
'longitude' => $latlng_[1],
'position' => in_array($position, array('manual', 'before', 'after')) ? $position : $this->get_option('position'),
'type' => in_array($type, array('ROADMAP', 'SATELLITE', 'HYBRID', 'TERRAIN')) ? $type : $this->get_option('type'),
'zoom' => (is_numeric($zoom) && intval($zoom) >= 0) ? intval($zoom) : $this->get_option('zoom')
);
}
}
update_post_meta($id, '_Author-geoLocation-location', serialize($location));
}
return $id;
}
/**
* @access private
* @since 1.0
* @return void
*/
function _admin_meta_box() {
$location = (isset($_GET['post']) && !empty($_GET['post'])) ? $this->location(intval(stripslashes($_GET['post']))) : null;
if (is_null($location))
$location = $this->lastLocation();
$pos = $location ? $location->position : $this->get_option('position');
$type = $location ? $location->type : $this->get_option('type');
$zoom = $location ? $location->zoom : $this->get_option('zoom');
?>
location(intval(stripslashes($_GET['post']))) : null;
if (is_null($location))
$location = $this->lastLocation();
?>
get_option('position');
$type = $this->get_option('type');
?>
location($id), $id);
}
}
/**
* @access public
* @since 1.0
* @param int $id Optional.
* @param string $before Optional.
* @param string $after Optional.
* @param bool $linkify Optional.
* @return void
*/
if (!function_exists('the_location')) {
function the_location($post_id = 0, $before = 'Posted from ', $after = '.', $linkify = true) {
global $Author_geoLocation;
echo apply_filters('the_location', $Author_geoLocation->_address_shortcode(array('before' => $before, 'after' => $after, 'linkify' => $linkify, 'post_id' => $post_id)), $post_id);
}
}
?>