| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Geo |
|---|
| 4 | Plugin URI: http://www.asymptomatic.net/wp-hacks |
|---|
| 5 | Description: Attaches geographic coordinate information to posts. <br />Licensed under the <a href="http://www.opensource.org/licenses/gpl-license.php">GNU General Public License</a>, Copyright 2004 Owen Winkler. |
|---|
| 6 | Version: 1.0 |
|---|
| 7 | Author: Owen Winkler |
|---|
| 8 | Author URI: http://www.asymptomatic.net |
|---|
| 9 | Minimum WordPress Version Required: 1.5.1 |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | /* |
|---|
| 13 | Geo - Attaches geographic coordinate information to posts. |
|---|
| 14 | Copyright (c) 2004 Owen Winkler |
|---|
| 15 | |
|---|
| 16 | This program is free software; you can redistribute it |
|---|
| 17 | and/or modify it under the terms of the GNU General Public |
|---|
| 18 | License as published by the Free Software Foundation; |
|---|
| 19 | either version 2 of the License, or (at your option) any |
|---|
| 20 | later version. |
|---|
| 21 | |
|---|
| 22 | This program is distributed in the hope that it will be |
|---|
| 23 | useful, but WITHOUT ANY WARRANTY; without even the implied |
|---|
| 24 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|---|
| 25 | PURPOSE. See the GNU General Public License for more |
|---|
| 26 | details. |
|---|
| 27 | |
|---|
| 28 | You should have received a copy of the GNU General Public |
|---|
| 29 | License along with this program; if not, write to the Free |
|---|
| 30 | Software Foundation, Inc., 59 Temple Place, Suite 330, |
|---|
| 31 | Boston, MA 02111-1307 USA |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | /* |
|---|
| 35 | |
|---|
| 36 | INSTRUCTIONS: |
|---|
| 37 | |
|---|
| 38 | Drop this file into your WordPress plugins directory, and then |
|---|
| 39 | Activate it on the Plugins tab of the WordPress admin console. |
|---|
| 40 | |
|---|
| 41 | See http://dev.wp-plugins.org/wiki/GeoPlugin for more |
|---|
| 42 | detailed installation instructions and usage information. |
|---|
| 43 | |
|---|
| 44 | */ |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | load_plugin_textdomain('Geo'); |
|---|
| 48 | |
|---|
| 49 | class Geo |
|---|
| 50 | /** |
|---|
| 51 | * Provide a namespace for our plugin |
|---|
| 52 | * |
|---|
| 53 | * @version 1.0 |
|---|
| 54 | */ |
|---|
| 55 | { |
|---|
| 56 | function options_page_geo() |
|---|
| 57 | /** |
|---|
| 58 | * Displays the Options admin interface for the plugin. |
|---|
| 59 | * |
|---|
| 60 | * Long description for the Function. |
|---|
| 61 | * |
|---|
| 62 | * @return mixed Description |
|---|
| 63 | */ |
|---|
| 64 | { |
|---|
| 65 | if(isset($_REQUEST['deleteid'])) |
|---|
| 66 | { |
|---|
| 67 | $geolocations = get_settings('geo_locations'); |
|---|
| 68 | unset($geolocations[$_REQUEST['deleteid']]); |
|---|
| 69 | update_option('geo_locations', $geolocations); |
|---|
| 70 | } |
|---|
| 71 | if(isset($_POST['Options'])) |
|---|
| 72 | { |
|---|
| 73 | $use_geo_positions = $_POST['use_geo_positions'] == 1 ? 1 : 0; |
|---|
| 74 | $use_default_geourl = $_POST['use_default_geourl'] == 1 ? 1 : 0; |
|---|
| 75 | update_option('use_geo_positions', $use_geo_positions); |
|---|
| 76 | update_option('use_default_geourl', $use_default_geourl); |
|---|
| 77 | update_option('default_geourl_lat', $_POST['default_geourl_lat']); |
|---|
| 78 | update_option('default_geourl_lon', $_POST['default_geourl_lon']); |
|---|
| 79 | echo '<div class="updated"><p><strong>' . __('Options updated.', 'Geo') . '</strong></p></div>'; |
|---|
| 80 | |
|---|
| 81 | } |
|---|
| 82 | if(isset($_POST['Submit']) || isset($_POST['Add'])) |
|---|
| 83 | { |
|---|
| 84 | |
|---|
| 85 | $geolocations = get_settings('geo_locations'); |
|---|
| 86 | foreach($geolocations as $name => $coords) |
|---|
| 87 | { |
|---|
| 88 | $geolocations[$name] = "{$_POST['lat'][$name]},{$_POST['lon'][$name]}"; |
|---|
| 89 | } |
|---|
| 90 | if(isset($_POST['new_location']) && ($_POST['new_location'] != '')) |
|---|
| 91 | { |
|---|
| 92 | $geolocations[$_POST['new_location']] = "{$_POST['new_lat']},{$_POST['new_lon']}"; |
|---|
| 93 | } |
|---|
| 94 | update_option('geo_locations', $geolocations); |
|---|
| 95 | |
|---|
| 96 | echo '<div class="updated"><p><strong>' . __('Locations updated.', 'Geo') . '</strong></p></div>'; |
|---|
| 97 | } |
|---|
| 98 | else |
|---|
| 99 | { |
|---|
| 100 | Geo::add_options(); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | $use_geo_positions = get_settings('use_geo_positions'); |
|---|
| 104 | $use_default_geourl = get_settings('use_default_geourl'); |
|---|
| 105 | $default_geourl_lat = get_settings('default_geourl_lat'); |
|---|
| 106 | $default_geourl_lon = get_settings('default_geourl_lon'); |
|---|
| 107 | |
|---|
| 108 | $ck_use_geo_positions = $use_geo_positions == 1 ? ' checked="checked"' : ''; |
|---|
| 109 | $ck_use_default_geourl[intval($use_default_geourl)] = ' checked="checked"'; |
|---|
| 110 | |
|---|
| 111 | $geolocations = get_settings('geo_locations'); |
|---|
| 112 | if(!is_array($geolocations)) $geolocations = array(); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | //Page Presentation |
|---|
| 116 | echo ' |
|---|
| 117 | <div class="wrap"> |
|---|
| 118 | <h2>' . __('Geographic Location Manager', 'Geo') . '</h2> |
|---|
| 119 | <form method="post"> |
|---|
| 120 | <table width="100%" cellspacing="2" cellpadding="5" class="editform"> |
|---|
| 121 | <tr valign="top"> |
|---|
| 122 | <th width="33%" scope="row">' . __('Geographic Tracking Features', 'Geo') . ':</th> |
|---|
| 123 | <td><input type="checkbox" name="use_geo_positions" id="use_geo_positions" ' . $ck_use_geo_positions . ' value="1" /> Enable</td> |
|---|
| 124 | </tr> |
|---|
| 125 | <tr valign="top"> |
|---|
| 126 | <th width="33%" scope="row">' . __('When no location is specified', 'Geo') . ':</th> |
|---|
| 127 | <td> |
|---|
| 128 | <label for="use_default_geourl0"><input type="radio" name="use_default_geourl" id="use_default_geourl0" ' . $ck_use_default_geourl[0] . ' value="0" /> ' . __('Do nothing.', 'Geo') . '</label><br /> |
|---|
| 129 | <label for="use_default_geourl1"><input type="radio" name="use_default_geourl" id="use_default_geourl1" ' . $ck_use_default_geourl[1] . ' value="1" /> ' . __('Use these:', 'Geo') . '</label> |
|---|
| 130 | <dl> |
|---|
| 131 | <dt><label for="default_geourl_lat">' . __('Latitude', 'Geo') . ':</label></dt> |
|---|
| 132 | <dd><input type="text" name="default_geourl_lat" value="' . $default_geourl_lat . '" /></dd> |
|---|
| 133 | <dt><label for="default_geourl_lon">' . __('Longitude', 'Geo') . ':</label></dt> |
|---|
| 134 | <dd><input type="text" name="default_geourl_lon" value="' . $default_geourl_lon . '" /></dd> |
|---|
| 135 | </dl> |
|---|
| 136 | </td> |
|---|
| 137 | </tr> |
|---|
| 138 | </table> |
|---|
| 139 | <div class="submit"><input type="submit" name="Options" value="' . __('Update Options', 'Geo') . ' »" /></div> |
|---|
| 140 | </form> |
|---|
| 141 | </div> |
|---|
| 142 | '; |
|---|
| 143 | |
|---|
| 144 | echo ' |
|---|
| 145 | <div class="wrap"> |
|---|
| 146 | <h2>' . __('Preset Locations', 'Geo') . '</h2> |
|---|
| 147 | <form method="post"> |
|---|
| 148 | <table width="100%" cellpadding="3" cellspacing="3"> |
|---|
| 149 | <thead> |
|---|
| 150 | <tr> |
|---|
| 151 | <th scope="col">'.__('Location Name', 'Geo').'</th> |
|---|
| 152 | <th scope="col">'.__('Latitude', 'Geo').'</th> |
|---|
| 153 | <th scope="col">'.__('Longitude', 'Geo').'</th> |
|---|
| 154 | <th scope="col">'.__('Action', 'Geo').'</th> |
|---|
| 155 | </tr> |
|---|
| 156 | </thead> |
|---|
| 157 | <tbody> |
|---|
| 158 | '; |
|---|
| 159 | |
|---|
| 160 | foreach($geolocations as $name => $coords) |
|---|
| 161 | { |
|---|
| 162 | list($lat, $lon) = split(',', $coords); |
|---|
| 163 | $alternate = $alternate == ''? ' class="alternate"' : ''; |
|---|
| 164 | echo "<tr{$alternate}>"; |
|---|
| 165 | echo "<td>{$name}</td>"; |
|---|
| 166 | echo "<td><input type=\"text\" name=\"lat[{$name}]\" value=\"{$lat}\" /></td>"; |
|---|
| 167 | echo "<td><input type=\"text\" name=\"lon[{$name}]\" value=\"{$lon}\" /></td>"; |
|---|
| 168 | echo "<td><a href=\"" . add_query_arg('deleteid', $name) . "\" |
|---|
| 169 | onClick=\"return confirm('" . __("Are you sure you want to delete this location entry?", 'Geo') . "');\" |
|---|
| 170 | >Delete</a> |
|---|
| 171 | · |
|---|
| 172 | <a href=\"" . get_geo_url('MapQuest', $lat, $lon) . "\">Map</a> |
|---|
| 173 | </td> |
|---|
| 174 | "; |
|---|
| 175 | echo "</tr>"; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | echo '<tr valign="bottom"> |
|---|
| 179 | <td><strong>'.__('New Location', 'Geo').':</strong><br/> |
|---|
| 180 | <input type="text" name="new_location" value="" /></td> |
|---|
| 181 | <td><input type="text" name="new_lat" value="" /></td> |
|---|
| 182 | <td><input type="text" name="new_lon" value="" /></td> |
|---|
| 183 | <td> </td> |
|---|
| 184 | </tr> |
|---|
| 185 | </tbody> |
|---|
| 186 | </table>'; |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | ?> |
|---|
| 190 | |
|---|
| 191 | <div class="submit"><input type="submit" name="Submit" value="<?php _e("Add/Update Locations", 'Geo'); ?> »" /></div> |
|---|
| 192 | </form> |
|---|
| 193 | |
|---|
| 194 | </div> |
|---|
| 195 | <?php |
|---|
| 196 | if(is_callable(array('GeoMap', 'options_page'))) { |
|---|
| 197 | GeoMap::options_page(); |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | function prepare_database() |
|---|
| 202 | /** |
|---|
| 203 | * Moves WordPress 1.2 geo info to meta fields. |
|---|
| 204 | * |
|---|
| 205 | * Long description for the Function. |
|---|
| 206 | * |
|---|
| 207 | * @return mixed Description |
|---|
| 208 | */ |
|---|
| 209 | { |
|---|
| 210 | global $wpdb; |
|---|
| 211 | // Are we in admin? |
|---|
| 212 | if(strstr($_SERVER['REQUEST_URI'], 'plugins.php')) |
|---|
| 213 | { |
|---|
| 214 | // Not already installed? |
|---|
| 215 | if($wpdb->get_var("SELECT count(post_id) FROM {$wpdb->postmeta} WHERE meta_key = '_geo_location'") == 0) |
|---|
| 216 | { |
|---|
| 217 | // Put empty geo markers on any one post to mark plugin as installed |
|---|
| 218 | $one_post_id = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} LIMIT 1;"); |
|---|
| 219 | add_post_meta($one_post_id, '_geo_location', ''); |
|---|
| 220 | |
|---|
| 221 | // Is there a WP 1.2-remnant geo field pair? |
|---|
| 222 | $fieldrow = $wpdb->get_row("SELECT * FROM {$wpdb->posts} LIMIT 1", ARRAY_A); |
|---|
| 223 | $fields = array_keys($fieldrow); |
|---|
| 224 | if(in_array('post_lat', $fields)) |
|---|
| 225 | { |
|---|
| 226 | if($latlon_posts = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE not isnull(post_lat) OR not isnull(post_lon);")) |
|---|
| 227 | { |
|---|
| 228 | foreach($latlon_posts as $post) |
|---|
| 229 | { |
|---|
| 230 | add_post_meta($post->ID, '_geo_location', $post->post_lat . ',' . $post->post_lon); |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | Geo::add_options(); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | return true; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | function add_options() |
|---|
| 241 | /** |
|---|
| 242 | * Adds the options required for geo info |
|---|
| 243 | * |
|---|
| 244 | * Long description for the Function. |
|---|
| 245 | * |
|---|
| 246 | */ |
|---|
| 247 | { |
|---|
| 248 | add_option('use_geo_positions', 0); |
|---|
| 249 | add_option('use_default_geourl', 0); |
|---|
| 250 | add_option('default_geourl_lat', 0); |
|---|
| 251 | add_option('default_geourl_lon', 0); |
|---|
| 252 | add_option('geo_locations', array()); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | function edit_form_advanced($not_used) |
|---|
| 256 | /** |
|---|
| 257 | * Displays the Geo interface on the post editing form |
|---|
| 258 | * |
|---|
| 259 | * Long description for the Function. |
|---|
| 260 | * |
|---|
| 261 | * @param $not_used Description |
|---|
| 262 | */ |
|---|
| 263 | { |
|---|
| 264 | global $postdata; |
|---|
| 265 | |
|---|
| 266 | $geolocations = get_settings('geo_locations'); |
|---|
| 267 | |
|---|
| 268 | echo '<fieldset><legend><a href="http://www.asymptomatic.net/_wiki/GeoPlugin">' . __('Location', 'Geo') . '</a></legend>'; |
|---|
| 269 | |
|---|
| 270 | list($lat, $lon) = split(',', get_post_meta($postdata->ID, '_geo_location', true)); |
|---|
| 271 | echo '<label for="geo_lat">' . __('Latitude:', 'Geo') . ' <input size="10" type="text" value="' . $lat .'" name="geo_lat" id="geo_lat" /></label> '; |
|---|
| 272 | echo '<label for="geo_lon">' . __('Longitude:', 'Geo') . ' <input size="10" type="text" value="' . $lon .'" name="geo_lon" id="geo_lon" /></label> '; |
|---|
| 273 | |
|---|
| 274 | echo '<label for="geo_select">' . __('Preset:', 'Geo') . ' <select id="geo_select" onchange="geo_choosegeo(this);"><option value="">--choose one--</option>'; |
|---|
| 275 | foreach($geolocations as $geolocation => $coords) |
|---|
| 276 | { |
|---|
| 277 | echo "<option value=\"$coords\""; |
|---|
| 278 | if($coords == "{$lat},{$lon}") echo ' selected="selected"'; |
|---|
| 279 | echo ">{$geolocation}</option>\n"; |
|---|
| 280 | } |
|---|
| 281 | echo '</select></label>'; |
|---|
| 282 | |
|---|
| 283 | echo '</fieldset>'; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | function admin_head($not_used) |
|---|
| 287 | /** |
|---|
| 288 | * Outputs support javascript to admin console and prepares the databse. |
|---|
| 289 | * |
|---|
| 290 | * Long description for the Function. |
|---|
| 291 | * |
|---|
| 292 | * @param $not_used Description |
|---|
| 293 | */ |
|---|
| 294 | { |
|---|
| 295 | Geo::prepare_database(); |
|---|
| 296 | if(strstr($_SERVER['REQUEST_URI'], 'post.php')) |
|---|
| 297 | { |
|---|
| 298 | echo ' |
|---|
| 299 | <script type="text/javascript"> |
|---|
| 300 | //<![CDATA[ |
|---|
| 301 | function geo_choosegeo(sel) |
|---|
| 302 | { |
|---|
| 303 | var coord = sel.options[sel.selectedIndex].value; |
|---|
| 304 | var ll = coord.split(","); |
|---|
| 305 | var inps = document.getElementsByTagName("input"); |
|---|
| 306 | for(z=0;z<inps.length;z++) |
|---|
| 307 | { |
|---|
| 308 | if(inps[z].getAttribute("name") == "geo_lat") inps[z].setAttribute("value", ll[0]); |
|---|
| 309 | if(inps[z].getAttribute("name") == "geo_lon") inps[z].setAttribute("value", ll[1]); |
|---|
| 310 | } |
|---|
| 311 | sel.selectedIndex = 0; |
|---|
| 312 | } |
|---|
| 313 | //]]> |
|---|
| 314 | </script>'; |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | function update_post($id) |
|---|
| 319 | /** |
|---|
| 320 | * Updates the geo info for a post when the post is updated. |
|---|
| 321 | * |
|---|
| 322 | * Long description for the Function. |
|---|
| 323 | * |
|---|
| 324 | * @param $id Description |
|---|
| 325 | */ |
|---|
| 326 | { |
|---|
| 327 | delete_post_meta($id, '_geo_location'); |
|---|
| 328 | add_post_meta($id, '_geo_location', $_POST['geo_lat'] . ',' . $_POST['geo_lon']); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | function wp_head($not_used) |
|---|
| 332 | /** |
|---|
| 333 | * Outputs the geo info as meta tags in the blog head. |
|---|
| 334 | * |
|---|
| 335 | * Long description for the Function. |
|---|
| 336 | * |
|---|
| 337 | * @param $not_used Description |
|---|
| 338 | */ |
|---|
| 339 | { |
|---|
| 340 | global $wp_query; |
|---|
| 341 | |
|---|
| 342 | if(!get_settings('use_geo_positions')) return; |
|---|
| 343 | |
|---|
| 344 | list($lat, $lon) = split(',', get_post_meta($wp_query->post->ID, '_geo_location', true)); |
|---|
| 345 | if(is_single() && ($lat != '') && ($lon != '')) |
|---|
| 346 | { |
|---|
| 347 | $title = convert_chars(strip_tags(get_bloginfo("name")))." - ".$wp_query->post->post_title; |
|---|
| 348 | } |
|---|
| 349 | else if(get_settings('use_default_geourl')) |
|---|
| 350 | { |
|---|
| 351 | // send the default here |
|---|
| 352 | $title = convert_chars(strip_tags(get_bloginfo("name"))); |
|---|
| 353 | $lat = get_settings('default_geourl_lat'); |
|---|
| 354 | $lon = get_settings('default_geourl_lon'); |
|---|
| 355 | } |
|---|
| 356 | else |
|---|
| 357 | { |
|---|
| 358 | return; |
|---|
| 359 | } |
|---|
| 360 | echo "<meta name=\"ICBM\" content=\"{$lat}, {$lon}\" />\n"; |
|---|
| 361 | echo "<meta name=\"DC.title\" content=\"{$title}\" />\n"; |
|---|
| 362 | echo "<meta name=\"geo.position\" content=\"{$lat};{$lon}\" />\n"; |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | function logErrors($msg) |
|---|
| 366 | /** |
|---|
| 367 | * Simple error logging function for debugging. |
|---|
| 368 | * |
|---|
| 369 | * Long description for the Function. |
|---|
| 370 | * |
|---|
| 371 | * @return bool Always true |
|---|
| 372 | * @param $msg Description |
|---|
| 373 | */ |
|---|
| 374 | { |
|---|
| 375 | $fp = fopen("../asy.log","a+"); |
|---|
| 376 | fwrite($fp, "\n\n".date("Y-m-d H:i:s - ").$msg); |
|---|
| 377 | fclose($fp); |
|---|
| 378 | return true; |
|---|
| 379 | } |
|---|
| 380 | |
|---|
| 381 | function admin_menu($not_used) |
|---|
| 382 | /** |
|---|
| 383 | * Adds the Geo Info menu to the Options tab in the admin menu. |
|---|
| 384 | * |
|---|
| 385 | * Long description for the Function. |
|---|
| 386 | * |
|---|
| 387 | * @param $not_used Description |
|---|
| 388 | */ |
|---|
| 389 | { |
|---|
| 390 | add_options_page(__('Geo Location Manager', 'Geo'), __('Geo Info', 'Geo'), 5, basename(__FILE__), array('Geo', 'options_page_geo')); |
|---|
| 391 | //add_submenu_page('index.php', __('Geo Location Manager', 'Geo'), __('Geo Info', 'Geo'), 5, basename(__FILE__), array('Geo', 'options_page_geo')); |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | } // End class geo |
|---|
| 395 | |
|---|
| 396 | // Note the array as the second parameter for calling static methods on our nice, safe object namespace |
|---|
| 397 | // For instance, array('Geo', 'edit_form_advanced') calls Geo::edit_form_advanced() |
|---|
| 398 | add_action('edit_form_advanced', array('Geo', 'edit_form_advanced')); |
|---|
| 399 | add_action('admin_head', array('Geo', 'admin_head')); |
|---|
| 400 | add_action('wp_head', array('Geo', 'wp_head')); |
|---|
| 401 | add_action('save_post', array('Geo', 'update_post')); |
|---|
| 402 | add_action('edit_post', array('Geo', 'update_post')); |
|---|
| 403 | add_action('publish_post', array('Geo', 'update_post')); |
|---|
| 404 | add_action('admin_menu', array('Geo', 'admin_menu')); |
|---|
| 405 | |
|---|
| 406 | function pingGeoURL($blog_ID) |
|---|
| 407 | /** |
|---|
| 408 | * Pings geourl.org with blog page ID. |
|---|
| 409 | * |
|---|
| 410 | * Long description for the Function. |
|---|
| 411 | * |
|---|
| 412 | * @param $blog_ID Description |
|---|
| 413 | */ |
|---|
| 414 | { |
|---|
| 415 | $ourUrl = get_settings('home') ."/index.php?p=".$blog_ID; |
|---|
| 416 | $host="geourl.org"; |
|---|
| 417 | $path="/ping/?p=".$ourUrl; |
|---|
| 418 | getRemoteFile($host,$path); |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | function get_Lat() |
|---|
| 422 | /** |
|---|
| 423 | * Returns the latitude setting for a post. |
|---|
| 424 | * |
|---|
| 425 | * Long description for the Function. |
|---|
| 426 | * |
|---|
| 427 | * @return string Description |
|---|
| 428 | */ |
|---|
| 429 | { |
|---|
| 430 | global $post; |
|---|
| 431 | |
|---|
| 432 | list($lat, $lon) = split(',', get_post_meta($post->ID, '_geo_location', true)); |
|---|
| 433 | if ($lat != '') { |
|---|
| 434 | return trim($lat); |
|---|
| 435 | } else if(get_settings('use_default_geourl')) { |
|---|
| 436 | return trim(get_settings('default_geourl_lat')); |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | return ''; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | function get_Lon() |
|---|
| 443 | /** |
|---|
| 444 | * Returns the longitude setting for a post. |
|---|
| 445 | * |
|---|
| 446 | * Long description for the Function. |
|---|
| 447 | * |
|---|
| 448 | * @return string Description |
|---|
| 449 | */ |
|---|
| 450 | { |
|---|
| 451 | global $post; |
|---|
| 452 | |
|---|
| 453 | list($lat, $lon) = split(',', get_post_meta($post->ID, '_geo_location', true)); |
|---|
| 454 | if ($lon != '') { |
|---|
| 455 | return trim($lon); |
|---|
| 456 | } else if(get_settings('use_default_geourl')) { |
|---|
| 457 | return trim(get_settings('default_geourl_lon')); |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | return ''; |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | function the_Lat() |
|---|
| 464 | /** |
|---|
| 465 | * Outputs the latitude for a post. |
|---|
| 466 | * |
|---|
| 467 | * Long description for the Function. |
|---|
| 468 | * |
|---|
| 469 | * @return string Description |
|---|
| 470 | */ |
|---|
| 471 | { |
|---|
| 472 | if(get_settings('use_geo_positions')) { |
|---|
| 473 | if(get_Lat() > 0) { |
|---|
| 474 | echo "".get_Lat()."N"; |
|---|
| 475 | } else { |
|---|
| 476 | echo "".get_Lat()."S"; |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | function the_Lon() |
|---|
| 482 | /** |
|---|
| 483 | * Outputs the longitude for a post. |
|---|
| 484 | * |
|---|
| 485 | * Long description for the Function. |
|---|
| 486 | * |
|---|
| 487 | * @return string Description |
|---|
| 488 | */ |
|---|
| 489 | { |
|---|
| 490 | global $id, $postdata; |
|---|
| 491 | if(get_settings('use_geo_positions')) { |
|---|
| 492 | if(get_Lon() < 0) { |
|---|
| 493 | $temp = get_Lon() * -1; |
|---|
| 494 | echo "".$temp."W"; |
|---|
| 495 | } else { |
|---|
| 496 | echo "".get_Lon()."E"; |
|---|
| 497 | } |
|---|
| 498 | } |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | function geo_PopUpScript() |
|---|
| 502 | /** |
|---|
| 503 | * Outputs the popup javascript used by the dropdown popup-er. |
|---|
| 504 | * |
|---|
| 505 | * Long description for the Function. |
|---|
| 506 | * |
|---|
| 507 | * @return string Description |
|---|
| 508 | */ |
|---|
| 509 | { |
|---|
| 510 | echo " |
|---|
| 511 | <script type='text/javascript'> |
|---|
| 512 | function formHandler(form) { |
|---|
| 513 | var URL = form.site.options[form.site.selectedIndex].value; |
|---|
| 514 | if(URL != \".\") { |
|---|
| 515 | popup = window.open(URL,\"MenuPopup\"); |
|---|
| 516 | } |
|---|
| 517 | } |
|---|
| 518 | </script>"; |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | function geo_urls($lat = '', $lon = '') |
|---|
| 522 | /** |
|---|
| 523 | * Returns an array of named URLs for the llat and lon specified. |
|---|
| 524 | * |
|---|
| 525 | * Long description for the Function. |
|---|
| 526 | * |
|---|
| 527 | * @return array Description |
|---|
| 528 | */ |
|---|
| 529 | { |
|---|
| 530 | $lat = $lat == ''? get_Lat() : $lat; |
|---|
| 531 | $lon = $lon == ''? get_Lon() : $lon; |
|---|
| 532 | $ary = array ( |
|---|
| 533 | 'AcmeMap' => array("http://www.acme.com/mapper?lat={$lat}&long={$lon}&scale=11&theme=Image&width=3&height=2&dot=Yes", __('Acme Mapper', 'Geo')), |
|---|
| 534 | 'GeoURL' => array("http://geourl.org/near/?lat={$lat}&lon={$lon}&dist=500", __('GeoURLs near here', 'Geo')), |
|---|
| 535 | 'GeoCache' => array("http://www.geocaching.com/seek/nearest.aspx?origin_lat={$lat}&origin_long={$lon}&dist=5", __('Geocaches near here', 'Geo')), |
|---|
| 536 | 'MapQuest' => array("http://www.mapquest.com/maps/map.adp?latlongtype=decimal&latitude={$lat}&longitude={$lon}", __('Mapquest map of this spot', 'Geo')), |
|---|
| 537 | 'SideBit' => array("http://www.sidebit.com/ProjectGeoURLMap.php?lat={$lat}&lon={$lon}", __('SideBit URL Map of this spot', 'Geo')), |
|---|
| 538 | 'DegreeConfluence' => array("http://confluence.org/confluence.php?lat={$lat}&lon={$lon}", __('Confluence.org near here', 'Geo')), |
|---|
| 539 | 'TopoZone' => array("http://www.topozone.com/map.asp?lat={$lat}&lon={$lon}", __('Topozone near here', 'Geo')), |
|---|
| 540 | 'FindU' => array("http://www.findu.com/cgi-bin/near.cgi?lat={$lat}&lon={$lon}&scale=100000&zoom=50&type=1&icon=0&&scriptfile=http://mapserver.maptech.com/api/espn/index.cfm", __('FindU near here', 'Geo')), |
|---|
| 541 | 'MapTech' => array("http://mapserver.maptech.com/api/espn/index.cfm?lat={$lat}&lon={$lon}", __('Maptech near here', 'Geo')), |
|---|
| 542 | 'GoogleMaps' => array("http://maps.google.com/maps?ll={$lat}%2C{$lon}&spn=0.015839,0.032747", __('Google Maps', 'Geo')), |
|---|
| 543 | ); |
|---|
| 544 | |
|---|
| 545 | return $ary; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | function geo_UrlPopNav() |
|---|
| 549 | /** |
|---|
| 550 | * Creates a dropdown popup-er for viewing geo sites for a post's lat/long. |
|---|
| 551 | * |
|---|
| 552 | * Long description for the Function. |
|---|
| 553 | * |
|---|
| 554 | */ |
|---|
| 555 | { |
|---|
| 556 | $sites = geo_urls(); |
|---|
| 557 | |
|---|
| 558 | echo '<form action=""><div>\n<select name="site" size="1" onchange="formHandler(this.form);" >'."\n"; |
|---|
| 559 | echo '<option value=".">' . sprintf(__("Sites referencing %s x %s", 'Geo'), get_Lat(), get_Lon()) . "</option>\n"; |
|---|
| 560 | foreach($sites as $site) { |
|---|
| 561 | echo "\t".'<option value="'.$site[0].'">'.$site[1]."</option>\n"; |
|---|
| 562 | } |
|---|
| 563 | echo '</select>\n</div></form>'."\n"; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | function longitude_invalid() |
|---|
| 567 | /** |
|---|
| 568 | * Validates the longitude value of the current post. |
|---|
| 569 | * |
|---|
| 570 | * Long description for the Function. |
|---|
| 571 | * |
|---|
| 572 | * @return boolean True if longitude is out of range. |
|---|
| 573 | */ |
|---|
| 574 | { |
|---|
| 575 | if (get_Lon() == null) return true; |
|---|
| 576 | if (get_Lon() > 360) return true; |
|---|
| 577 | if (get_Lon() < -360) return true; |
|---|
| 578 | } |
|---|
| 579 | |
|---|
| 580 | function get_geo_url($index, $lat = '', $lon = '') |
|---|
| 581 | /** |
|---|
| 582 | * Returns a URL for the indexed geo site at the given coordinates. |
|---|
| 583 | * |
|---|
| 584 | * Long description for the Function. |
|---|
| 585 | * |
|---|
| 586 | * @param $index The associative index of the URL array to return. |
|---|
| 587 | * @param $lat Optional latitude for the URL, if not specified then the current is used. |
|---|
| 588 | * @param $lon Optional longitude for the URL, if not specified then the current is used. |
|---|
| 589 | * |
|---|
| 590 | * @return string The URL of the requested site. |
|---|
| 591 | */ |
|---|
| 592 | { |
|---|
| 593 | $lat = $lat == ''? get_Lat() : $lat; |
|---|
| 594 | $lon = $lon == ''? get_Lon() : $lon; |
|---|
| 595 | $urls = geo_urls($lat, $lon); |
|---|
| 596 | return $urls[$index][0]; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | function the_AcmeMap_Url() |
|---|
| 600 | /** |
|---|
| 601 | * Outputs a URL to the AcmeMap site for the current geo coordinates. |
|---|
| 602 | * |
|---|
| 603 | * Long description for the Function. |
|---|
| 604 | * |
|---|
| 605 | */ |
|---|
| 606 | { |
|---|
| 607 | /* |
|---|
| 608 | // Shouldn't this function work if a coordinate is set? |
|---|
| 609 | // Can't we correct an invalid longitude? |
|---|
| 610 | if (!get_settings('use_geo_positions')) return; |
|---|
| 611 | if (longitude_invalid()) return; |
|---|
| 612 | */ |
|---|
| 613 | echo get_geo_url('AcmeMap'); |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | function the_GeoURL_Url() |
|---|
| 617 | /** |
|---|
| 618 | * Outputs a URL to the GeoURL site for the current geo coordinates. |
|---|
| 619 | * |
|---|
| 620 | * Long description for the Function. |
|---|
| 621 | * |
|---|
| 622 | */ |
|---|
| 623 | { |
|---|
| 624 | echo get_geo_url('GeoURL'); |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | function the_GeoCache_Url() |
|---|
| 628 | /** |
|---|
| 629 | * Outputs a URL to the GeoCaching site for the current geo coordinates. |
|---|
| 630 | * |
|---|
| 631 | * Long description for the Function. |
|---|
| 632 | * |
|---|
| 633 | */ |
|---|
| 634 | { |
|---|
| 635 | echo get_geo_url('GeoCache'); |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | function the_MapQuest_Url() |
|---|
| 639 | /** |
|---|
| 640 | * Outputs a URL to the MapQuest site for the current geo coordinates. |
|---|
| 641 | * |
|---|
| 642 | * Long description for the Function. |
|---|
| 643 | * |
|---|
| 644 | */ |
|---|
| 645 | { |
|---|
| 646 | echo get_geo_url('MapQuest'); |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | function the_SideBit_Url() |
|---|
| 650 | /** |
|---|
| 651 | * Outputs a URL to the SideBit site for the current geo coordinates. |
|---|
| 652 | * |
|---|
| 653 | * Long description for the Function. |
|---|
| 654 | * |
|---|
| 655 | */ |
|---|
| 656 | { |
|---|
| 657 | echo get_geo_url('SideBit'); |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | function the_DegreeConfluence_Url() |
|---|
| 661 | /** |
|---|
| 662 | * Outputs a URL to the DegreeConfluence site for the current geo coordinates. |
|---|
| 663 | * |
|---|
| 664 | * Long description for the Function. |
|---|
| 665 | * |
|---|
| 666 | */ |
|---|
| 667 | { |
|---|
| 668 | echo get_geo_url('DegreeConfluence'); |
|---|
| 669 | } |
|---|
| 670 | |
|---|
| 671 | function the_TopoZone_Url() |
|---|
| 672 | /** |
|---|
| 673 | * Outputs a URL to the TopoZone site for the current geo coordinates. |
|---|
| 674 | * |
|---|
| 675 | * Long description for the Function. |
|---|
| 676 | * |
|---|
| 677 | */ |
|---|
| 678 | { |
|---|
| 679 | echo get_geo_url('TopoZone'); |
|---|
| 680 | } |
|---|
| 681 | |
|---|
| 682 | function the_FindU_Url() |
|---|
| 683 | /** |
|---|
| 684 | * Outputs a URL to the FindU site for the current geo coordinates. |
|---|
| 685 | * |
|---|
| 686 | * Long description for the Function. |
|---|
| 687 | * |
|---|
| 688 | */ |
|---|
| 689 | { |
|---|
| 690 | echo get_geo_url('FindU'); |
|---|
| 691 | } |
|---|
| 692 | |
|---|
| 693 | function the_MapTech_Url() |
|---|
| 694 | /** |
|---|
| 695 | * Outputs a URL to the MapTech site for the current geo coordinates. |
|---|
| 696 | * |
|---|
| 697 | * Long description for the Function. |
|---|
| 698 | * |
|---|
| 699 | */ |
|---|
| 700 | { |
|---|
| 701 | echo get_geo_url('MapTech'); |
|---|
| 702 | } |
|---|
| 703 | |
|---|
| 704 | function the_GoogleMap_Url() { |
|---|
| 705 | /** |
|---|
| 706 | * Outputs a URL to Google Maps for the current geo coordinates. |
|---|
| 707 | * |
|---|
| 708 | * Long description for the Function. |
|---|
| 709 | * |
|---|
| 710 | */ |
|---|
| 711 | echo get_geo_url('GoogleMaps'); |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | |
|---|
| 715 | function geo_distance($lat1, $lon1, $lat2, $lon2, $unit = 'm' ) |
|---|
| 716 | /** |
|---|
| 717 | * Returns the distance between two geo coordinates. |
|---|
| 718 | * |
|---|
| 719 | * Long description for the Function. |
|---|
| 720 | * |
|---|
| 721 | * @param $lat1 The latitude of the first coordinate |
|---|
| 722 | * @param $lon1 The longitude of the first coordinate |
|---|
| 723 | * @param $lat2 The latitude of the second coordinate |
|---|
| 724 | * @param $lon2 The longitude of the second coordinate |
|---|
| 725 | * @param $unit 'm' for miles, 'k' for kilometers, 'n' for nautical miles. |
|---|
| 726 | * |
|---|
| 727 | * @return float The distance between the to coordinates. |
|---|
| 728 | */ |
|---|
| 729 | { |
|---|
| 730 | $theta = $lon1 - $lon2; |
|---|
| 731 | $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); |
|---|
| 732 | $dist = acos($dist); |
|---|
| 733 | $dist = rad2deg($dist); |
|---|
| 734 | $miles = $dist * 60 * 1.1515; |
|---|
| 735 | $unit = strtoupper($unit); |
|---|
| 736 | |
|---|
| 737 | if ($unit == "k") { |
|---|
| 738 | return ($miles * 1.609344); |
|---|
| 739 | } else if ($unit == "n") { |
|---|
| 740 | return ($miles * 0.8684); |
|---|
| 741 | } else { |
|---|
| 742 | return $miles; |
|---|
| 743 | } |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | ?> |
|---|