Advertisement
Guest User

mygeoposition wordpress plugin

a guest
Jan 14th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.67 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: MyGeoPosition.com Geotagging: Geotags / GeoMetatags / GeoFeedtags / GeoMicroformats / Maps
  4.  * Plugin URI: http://www.mygeoposition.com
  5.  * Description: Create geo-posttags, geo-metatags, geo-feedtags, geo-microformats and maps for posts and pages. Display the geotagged location in form of a map before, after or within the post. An easy-to-use geopicker map with auto-locating functionality helps entering locations.
  6.  * Version: 1.3.2
  7.  * Author: Daniel Filzhut
  8.  * Author URI: http://www.filzhut.de
  9.  */
  10.  
  11. //  PHP notices fixed, autosave bug fixed
  12. // by Mei Gwilym, http://mei.gwilym.net/ 2013-01-14
  13.  
  14.  
  15. $mygpGeotagsGeoMetatags_key = "mygpGeotagsGeoMetatags";
  16.  
  17.  
  18.  
  19. /**
  20.  * Define script location.
  21.  *
  22.  */
  23. if ( !defined('WP_CONTENT_URL') ) {
  24.     define('MYGP_GEOTAGS_GEOMETATGS_PLUGINPATH',get_option('siteurl').'/wp-content/plugins/'.plugin_basename(dirname(__FILE__)).'/');
  25.     define('MYGP_GEOTAGS_GEOMETATGS_PLUGINDIR', ABSPATH.'/wp-content/plugins/'.plugin_basename(dirname(__FILE__)).'/');
  26. } else {
  27.     define('MYGP_GEOTAGS_GEOMETATGS_PLUGINPATH', WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__)).'/');
  28.     define('MYGP_GEOTAGS_GEOMETATGS_PLUGINDIR', WP_CONTENT_DIR.'/plugins/'.plugin_basename(dirname(__FILE__)).'/');
  29. }
  30.  
  31.  
  32.  
  33. /**
  34.  * Load translations
  35.  *
  36.  */
  37. function mygpGeotagsGeoMetatags_loadTexts() {
  38.  
  39.   global $mygpGeotagsGeoMetatags_key, $mygpGeotagsGeoMetatags_metaBoxes;
  40.  
  41.   load_plugin_textdomain( $mygpGeotagsGeoMetatags_key, '', dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
  42.  
  43.   $mygpGeotagsGeoMetatags_metaBoxes = array(
  44.     "placename" => array(
  45.         "name" => "placename",
  46.         "title" => "geo.placename",
  47.         "description" => __('City or other placename. Example: New York', $mygpGeotagsGeoMetatags_key)),
  48.     "region" => array(
  49.         "name" => "region",
  50.         "title" => "geo.region",
  51.         "description" => __('Country and state. Example: US-NY', $mygpGeotagsGeoMetatags_key)),
  52.     "position" => array(
  53.         "name" => "position",
  54.         "title" => "geo.position",
  55.         "description" => __('Latitude and Longitude. Example: 40.7143528;-74.0059731', $mygpGeotagsGeoMetatags_key)),
  56.     "addMap" => array(
  57.         "name" => "addMap",
  58.         "title" => __('Show map', $mygpGeotagsGeoMetatags_key),
  59.         "description" => __('', $mygpGeotagsGeoMetatags_key),
  60.         "options" => array(
  61.                 "default" => __('default', $mygpGeotagsGeoMetatags_key),
  62.                 "true" => __('yes', $mygpGeotagsGeoMetatags_key),
  63.                 "false" => __('no', $mygpGeotagsGeoMetatags_key),
  64.             )
  65.         ),
  66.     "mapLocation" => array(
  67.         "name" => "mapLocation",
  68.         "title" => __('Map location', $mygpGeotagsGeoMetatags_key),
  69.         "description" => __('', $mygpGeotagsGeoMetatags_key),
  70.         "options" => array(
  71.                 "default" => __('default', $mygpGeotagsGeoMetatags_key),
  72.                 "before" => __('before post', $mygpGeotagsGeoMetatags_key),
  73.                 "after" => __('after post', $mygpGeotagsGeoMetatags_key),
  74.                 "shortcode" => __('replace [mygp_map] in post', $mygpGeotagsGeoMetatags_key),
  75.             )
  76.         ),
  77.     "zoomLevel" => array(
  78.         "name" => "zoomLevel",
  79.         "title" => __('Zoom level', $mygpGeotagsGeoMetatags_key),
  80.         "description" => __('', $mygpGeotagsGeoMetatags_key),
  81.         "options" => array(
  82.                 "default" => __('default', $mygpGeotagsGeoMetatags_key),
  83.                 "1" => __('1 - World', $mygpGeotagsGeoMetatags_key),
  84.                 "2" => __('2', $mygpGeotagsGeoMetatags_key),
  85.                 "3" => __('3 - Continent', $mygpGeotagsGeoMetatags_key),
  86.                 "4" => __('4', $mygpGeotagsGeoMetatags_key),
  87.                 "5" => __('5 - Country', $mygpGeotagsGeoMetatags_key),
  88.                 "6" => __('6', $mygpGeotagsGeoMetatags_key),
  89.                 "7" => __('7 - State', $mygpGeotagsGeoMetatags_key),
  90.                 "8" => __('8', $mygpGeotagsGeoMetatags_key),
  91.                 "9" => __('9', $mygpGeotagsGeoMetatags_key),
  92.                 "10" => __('10', $mygpGeotagsGeoMetatags_key),
  93.                 "11" => __('11 - City', $mygpGeotagsGeoMetatags_key),
  94.                 "12" => __('12', $mygpGeotagsGeoMetatags_key),
  95.                 "13" => __('13', $mygpGeotagsGeoMetatags_key),
  96.                 "14" => __('14 - District', $mygpGeotagsGeoMetatags_key),
  97.                 "15" => __('15', $mygpGeotagsGeoMetatags_key),
  98.                 "16" => __('16 - Street', $mygpGeotagsGeoMetatags_key),
  99.                 "17" => __('17', $mygpGeotagsGeoMetatags_key),
  100.                 "18" => __('18 - Street corner', $mygpGeotagsGeoMetatags_key),
  101.                 "19" => __('19', $mygpGeotagsGeoMetatags_key),
  102.                 "20" => __('20', $mygpGeotagsGeoMetatags_key),
  103.                 "21" => __('21 - House', $mygpGeotagsGeoMetatags_key),
  104.             )
  105.         )
  106.   );
  107.  
  108.   include('mygeopositioncom-geotags-geometatags.settings.php');
  109.  
  110. }
  111. add_action( 'init', 'mygpGeotagsGeoMetatags_loadTexts' );
  112.  
  113.  
  114.  
  115. /**
  116.  * Add CSS to html head.
  117.  *
  118.  */
  119. function mygpGeotagsGeoMetatags_addCustomHeaderTags(){ 
  120.   echo '<link rel="stylesheet" type="text/css" href="' . MYGP_GEOTAGS_GEOMETATGS_PLUGINPATH . 'mygp_geotags_geometatags.css" />';
  121. }
  122. add_action('admin_head', 'mygpGeotagsGeoMetatags_addCustomHeaderTags');
  123. add_action('wp_head', 'mygpGeotagsGeoMetatags_addCustomHeaderTags');
  124.  
  125.    
  126.    
  127. /**
  128.  * Add metabox to post editor.
  129.  *
  130.  */
  131. function mygpGeotagsGeoMetatags_createMetaBox() {
  132.    
  133.     global $mygpGeotagsGeoMetatags_key;
  134.  
  135.     if (function_exists('add_meta_box')) {
  136.         add_meta_box('new-meta-boxes', __('MyGeoPosition.com Geotags / GeoMetatags / GeoFeedtags / GeoMicroformats', $mygpGeotagsGeoMetatags_key), 'mygpGeotagsGeoMetatags_displayMetaBox', 'post', 'normal', 'high');
  137.         add_meta_box('new-meta-boxes', __('MyGeoPosition.com Geotags / GeoMetatags / GeoFeedtags / GeoMicroformats', $mygpGeotagsGeoMetatags_key), 'mygpGeotagsGeoMetatags_displayMetaBox', 'page', 'normal', 'high');
  138.     }
  139.    
  140. }
  141. add_action('admin_menu', 'mygpGeotagsGeoMetatags_createMetaBox');
  142.  
  143.    
  144.    
  145. /**
  146.  * Meta box code.
  147.  *
  148.  */
  149. function mygpGeotagsGeoMetatags_displayMetaBox() {
  150.    
  151.     global $post, $mygpGeotagsGeoMetatags_metaBoxes, $mygpGeotagsGeoMetatags_key;
  152.  
  153.     ?>
  154.  
  155.     <div class="form-wrap form-wrap-mygp-geotags-geometatags">
  156.      
  157.     <?php
  158.     wp_nonce_field(plugin_basename(__FILE__), $mygpGeotagsGeoMetatags_key . '_wpnonce', false, true);
  159.     foreach($mygpGeotagsGeoMetatags_metaBoxes as $metaBox) {
  160.        
  161.         $data = get_post_meta($post->ID, $mygpGeotagsGeoMetatags_key, true);
  162.        
  163.         if (isset($metaBox['options']) && is_array( $metaBox[ 'options' ] ) ) {?>
  164.  
  165.             <div class="form-field form-required form-field-mygp-select form-field-mygp-<?php echo $metaBox[ 'name' ]; ?>">
  166.                 <label for="geopicker-<?php echo $metaBox[ 'name' ]; ?>"><?php echo $metaBox[ 'title' ]; ?></label>
  167.                 <select name="<?php echo $metaBox[ 'name' ]; ?>" id="geopicker-<?php echo $metaBox[ 'name' ]; ?>">
  168.                 <?php
  169.                 while ( list($key, $value) = each( $metaBox[ 'options' ] ) ) {
  170.                     if ($key == "default") {
  171.                         $key = "";
  172.                         $value = $value . " (" . $metaBox[ 'options' ][ mygpGeotagsGeoMetatags_getOption($metaBox[ 'name' ]) ] . ")";
  173.                     }
  174.                     echo "<option value='" . $key . "' " . ($data[ $metaBox[ 'name' ] ] == $key  ? ' selected="selected"' : '') . ">" . $value . "</option>";
  175.                 }
  176.                 ?>
  177.                 </select>
  178.                 <p><?php echo $metaBox[ 'description' ]; ?></p>
  179.             </div>
  180.          
  181.         <?php
  182.         } else {?>
  183.  
  184.             <div class="form-field form-required form-field-mygp-input form-field-mygp-<?php echo $metaBox[ 'name' ]; ?>">
  185.                 <label for="geopicker-<?php echo $metaBox[ 'name' ]; ?>"><?php echo $metaBox[ 'title' ]; ?></label>
  186.                 <input type="text" name="<?php echo $metaBox[ 'name' ]; ?>" id="geopicker-<?php echo $metaBox[ 'name' ]; ?>" value="<?php if(isset($data[ $metaBox[ 'name' ] ])) echo htmlspecialchars($data[ $metaBox[ 'name' ] ]); ?>" />
  187.                 <p><?php echo $metaBox[ 'description' ]; ?></p>
  188.             </div>
  189.          
  190.         <?php
  191.         }
  192.     }
  193.     ?>
  194.  
  195.     <div class="form-field form-field-mygp-geotags-geometatags-geopicker form-required">
  196.    
  197.     <script type="text/javascript">
  198.      
  199.     /**
  200.      * JSON2.js
  201.      */
  202.    
  203.     if(!this.JSON){this.JSON={};}
  204.     (function(){function f(n){return n<10?'0'+n:n;}
  205.     if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+'-'+
  206.     f(this.getUTCMonth()+1)+'-'+
  207.     f(this.getUTCDate())+'T'+
  208.     f(this.getUTCHours())+':'+
  209.     f(this.getUTCMinutes())+':'+
  210.     f(this.getUTCSeconds())+'Z':null;};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};}
  211.     var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}
  212.     function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);}
  213.     if(typeof rep==='function'){value=rep.call(holder,key,value);}
  214.     switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}
  215.     gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}
  216.     v=partial.length===0?'[]':gap?'[\n'+gap+
  217.     partial.join(',\n'+gap)+'\n'+
  218.     mind+']':'['+partial.join(',')+']';gap=mind;return v;}
  219.     if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==='string'){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}
  220.     v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+
  221.     mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}
  222.     if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' ';}}else if(typeof space==='string'){indent=space;}
  223.     rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}
  224.     return str('',{'':value});};}
  225.     if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}
  226.     return reviver.call(holder,key,value);}
  227.     text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+
  228.     ('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}
  229.     if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof reviver==='function'?walk({'':j},''):j;}
  230.     throw new SyntaxError('JSON.parse');};}}());
  231.    
  232.    
  233.      /**
  234.       *   This implementation of the MyGeoPosition.com GeoPicker is exclusively developed for the Wordpress Admin Area.
  235.       *   It makes sure, that the GeoPicker is NOT running in the administration area javascript context.
  236.       *   Please DO NOT copy&paste/reuse this implementation, as it might change without prior notice.
  237.       *   For information on a regular implementation, please check  http://api.mygeoposition.com.
  238.       */
  239.        
  240.         var tagsContainerQuery = "textarea#tax-input-post_tag";
  241.         var tagsUpdateButtonQuery = "#post_tag input[type='submit'], #post_tag input[type='button']";
  242.      
  243.       function lookupGeoData() {
  244.      
  245.       if (!window.postMessage) {
  246.         alert('<?php echo __('Sorry, the Wordpress version of the geopicker does only run in IE8+, FF4+, Safari or Chrome.', $mygpGeotagsGeoMetatags_key); ?>');
  247.         return;
  248.       }
  249.        
  250.       // Initialize geopicker
  251.       var geopickerUrl = 'http://api.mygeoposition.com/api/geopicker/id-wordpresspipm/?langCode=<?php echo substr(get_locale(), 0, 2); ?>';
  252.           if (document.getElementById('geopicker-position').value) {
  253.             geopickerUrl += '&startAddress=' + document.getElementById('geopicker-position').value.replace(/;/, ",");
  254.           } else if (document.getElementById('geopicker-placename').value) {
  255.             geopickerUrl += '&startAddress=' + document.getElementById('geopicker-placename').value;
  256.           } else if (document.getElementById('geopicker-region').value) {
  257.             geopickerUrl += '&startAddress=' + document.getElementById('geopicker-region').value;
  258.           } else {
  259.             geopickerUrl += '&autolocate=<?php echo mygpGeotagsGeoMetatags_getOption('autolocate'); ?>';
  260.           }
  261.       var mgpGeoWindow = window.open("", "MGPGeoPickerWindow", "width=488,height=518,location=no,menubar=no,resizable=no,status=no,toolbar=no");
  262.       mgpGeoWindow.focus();
  263.       mgpGeoWindow.document.write("<" + "html><" + "head><title>GeoPicker</title></" + "head><" + "body style=\"padding:0px;margin:0px;\">");
  264.       mgpGeoWindow.document.write("<iframe src=\"" + geopickerUrl + "\" width=488 height=518 border=0 frameborder=0 style=\"padding:0px;margin:0px;\"></iframe>");
  265.       mgpGeoWindow.document.write("<script type=\"text/javascript\">");
  266.       mgpGeoWindow.document.write("function receiveMessage(event) {");
  267.       mgpGeoWindow.document.write(" if(event.origin.match(/mygeoposition\.com$/g)) {");                
  268.       mgpGeoWindow.document.write("  var geodata = window.opener.JSON.parse(event.data);");
  269.       mgpGeoWindow.document.write("  window.opener.document.getElementById('geopicker-region').value = geodata.country.short + '-' + geodata.state.short;");
  270.       mgpGeoWindow.document.write("  window.opener.document.getElementById('geopicker-placename').value = geodata.address;");
  271.       mgpGeoWindow.document.write("  window.opener.document.getElementById('geopicker-position').value = geodata.lat + ';' + geodata.lng;");
  272.  
  273.       <?php
  274.       if (mygpGeotagsGeoMetatags_getOption('addPosttags') != 'false') {
  275.       ?>
  276.       mgpGeoWindow.document.write("  window.opener.clearGeoPostTags();");
  277.       mgpGeoWindow.document.write("  window.opener.addPostTags('geotagged,geo:lat=' + geodata.lat + ',geo:lon=' + geodata.lng);");
  278.       mgpGeoWindow.document.write("  window.opener.jQuery(\"#geopicker-placename\").focus();");
  279.       <?php
  280.       }
  281.       ?>
  282.  
  283.       <?php
  284.       if (mygpGeotagsGeoMetatags_getOption('addCityToPosttags') != 'false') {
  285.       ?>
  286.       mgpGeoWindow.document.write("  window.opener.addPostTags(geodata.city.short);");
  287.       mgpGeoWindow.document.write("  window.opener.jQuery(\"#geopicker-placename\").focus();");
  288.       <?php
  289.       }
  290.       ?>
  291.        
  292.       mgpGeoWindow.document.write("  window.close();");
  293.       mgpGeoWindow.document.write(" }");
  294.       mgpGeoWindow.document.write("}");
  295.       mgpGeoWindow.document.write("if (window.addEventListener) { window.addEventListener('message', receiveMessage, false);}");
  296.       mgpGeoWindow.document.write("else {window.attachEvent('onmessage', receiveMessage);}");
  297.       mgpGeoWindow.document.write("</" + "script>");
  298.       mgpGeoWindow.document.write("</" + "body></" + "html>");
  299.      
  300.       }
  301.      
  302.       function clearGeoData() {
  303.           document.getElementById('geopicker-placename').value = '';
  304.           document.getElementById('geopicker-region').value = '';
  305.           document.getElementById('geopicker-position').value = '';
  306.           clearGeoPostTags();
  307.       }
  308.      
  309.       function clearGeoPostTags() {
  310.           if (jQuery) {
  311.             var items = jQuery(tagsContainerQuery).val();
  312.             if (items == undefined) {
  313.                 return;
  314.             }
  315.             var itemsArray = items.split(",");
  316.             var itemsNew = '';
  317.             for (var i = 0; i < itemsArray.length; i++) {
  318.               if (!itemsArray[i].match(/geo:/) && !itemsArray[i].match(/geotagged/)) {
  319.                 itemsNew += itemsArray[i] + ",";
  320.               }            
  321.             }
  322.             jQuery(tagsContainerQuery).val(itemsNew);
  323.             jQuery(tagsUpdateButtonQuery).trigger('click');
  324.           }
  325.       }
  326.      
  327.       function addPostTags(newTag) {
  328.           if (jQuery) {
  329.             jQuery(tagsContainerQuery).val(jQuery(tagsContainerQuery).val() + "," + newTag);
  330.             jQuery(tagsUpdateButtonQuery).trigger('click');
  331.           }
  332.       }
  333.      
  334.         document.write('<button type="button" class="button" onclick="lookupGeoData();" id="mygpGeoPickerButton"><?php echo __('Open Geopicker tool', $mygpGeotagsGeoMetatags_key); ?></button>');
  335.     </script>
  336.       <button type="button" class="button" onclick="clearGeoData();" id="mygpClearGeoDataButton"><?php echo __('Clear geo data', $mygpGeotagsGeoMetatags_key); ?></button>
  337.       <p id="addGeoPicker"><?php echo __('Add a free GeoPicker to your website or application', $mygpGeotagsGeoMetatags_key); ?>: <a href="http://api.mygeoposition.com" target="_blank">http://api.mygeoposition.com</a></p>
  338.     </div>
  339.      
  340.     <div style="clear:both;"></div>
  341.        
  342.     </div>
  343.  
  344. <?php
  345. }
  346.  
  347.    
  348.    
  349. /**
  350.  * Save entered meta data.
  351.  *
  352.  */
  353. function mygpGeotagsGeoMetatags_saveMetaData($post_id) {
  354.    
  355.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
  356.  
  357.     global $mygpGeotagsGeoMetatags_metaBoxes, $mygpGeotagsGeoMetatags_key;
  358.  
  359.     $data = array();
  360.     foreach($mygpGeotagsGeoMetatags_metaBoxes as $metaBox) {
  361.         if(isset($_POST[$metaBox['name']])) $data[$metaBox['name']] = htmlspecialchars($_POST[$metaBox['name']]);
  362.     }
  363.  
  364.     if (isset($_POST[ $mygpGeotagsGeoMetatags_key . '_wpnonce' ]) && !wp_verify_nonce($_POST[ $mygpGeotagsGeoMetatags_key . '_wpnonce' ], plugin_basename(__FILE__)))
  365.         return $post_id;
  366.  
  367.     if (!current_user_can('edit_post', $post_id))
  368.         return $post_id;
  369.  
  370.     update_post_meta($post_id, $mygpGeotagsGeoMetatags_key, $data);
  371.    
  372. }
  373. add_action('save_post', 'mygpGeotagsGeoMetatags_saveMetaData');
  374.  
  375.  
  376.    
  377. /**
  378.  * Add metatags to post in frontend.
  379.  *
  380.  */
  381. function mygpGeotagsGeoMetatags_addGeoMetatags() {
  382.    
  383.     global $wp_query, $mygpGeotagsGeoMetatags_key;
  384.    
  385.     if (is_single() or is_page()) {
  386.        
  387.         if ($wp_query->post && mygpGeotagsGeoMetatags_getOption('addMetatags') != 'false') {
  388.            
  389.             $data = get_post_meta($wp_query->post->ID, $mygpGeotagsGeoMetatags_key, true);
  390.            
  391.             if (isset($data[ 'region' ]) && $data[ 'region' ] != "") {
  392.                 echo "<meta name=\"geo.region\" content=\"" . $data[ 'region' ] . "\" />\n";
  393.             }
  394.            
  395.             if (isset($data[ 'placename' ]) && $data[ 'placename' ] != "") {
  396.                 echo "<meta name=\"geo.placename\" content=\"" . $data[ 'placename' ] . "\" />\n";
  397.             }
  398.            
  399.             if (isset($data[ 'position' ]) && $data[ 'position' ] != "") {
  400.                 echo "<meta name=\"geo.position\" content=\"" . $data[ 'position' ] . "\" />\n";
  401.                 echo "<meta name=\"ICBM\" content=\"" . str_replace(";", ",", $data[ 'position' ]) . "\" />\n";
  402.             }
  403.            
  404.         }
  405.        
  406.     }
  407.    
  408. }
  409. add_action('wp_head', 'mygpGeotagsGeoMetatags_addGeoMetatags');
  410.  
  411.    
  412. /**
  413.  * Add geo namespaces to news feeds.
  414.  *
  415.  */
  416. function mygpGeotagsGeoMetatags_addGeoNamespaces(){
  417.    
  418.     if (mygpGeotagsGeoMetatags_getOption("addFeedtags") != 'false') {
  419.    
  420.         echo "xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"\n";
  421.         echo "\txmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\"\n";
  422.         echo "\txmlns:geourl=\"http://geourl.org/rss/module/\"\n";
  423.         echo "\txmlns:icbm=\"http://postneo.com/icbm\"\n";
  424.    
  425.     }
  426.    
  427. }
  428. add_action('rss2_ns', 'mygpGeotagsGeoMetatags_addGeoNamespaces');
  429. add_action('atom_ns', 'mygpGeotagsGeoMetatags_addGeoNamespaces');
  430. add_action('rdf_ns', 'mygpGeotagsGeoMetatags_addGeoNamespaces');
  431.  
  432.  
  433.  
  434. /**
  435.  * Add geo tags to news feeds
  436.  *
  437.  */
  438. function mygpGeotagsGeoMetatags_addGeoFeedtags(){
  439.    
  440.     global $wp_query, $mygpGeotagsGeoMetatags_key;
  441.    
  442.     if (mygpGeotagsGeoMetatags_getOption("addFeedtags") != 'false') {
  443.  
  444.         $data = get_post_meta($wp_query->post->ID, $mygpGeotagsGeoMetatags_key, true);
  445.        
  446.         $dataSplitted = "";            
  447.         if ($data[ 'position' ] != "") {
  448.             $dataSplitted = explode(";", $data[ 'position' ]);
  449.         }
  450.            
  451.         $lat = trim($dataSplitted[0]);
  452.         $lon = trim($dataSplitted[1]);
  453.    
  454.         if ($lat != "" && $lon != "") {
  455.             echo "\t<geo:lat>$lat</geo:lat>\n\t\t<geo:long>$lon</geo:long>\n";
  456.             echo "\t\t<georss:where>\n\t\t\t<gml:Point>\n\t\t\t\t<gml:pos>$lat $lon</gml:pos>\n\t\t\t</gml:Point>\n\t\t</georss:where>\n";
  457.             echo "\t\t<georss:point>$lat $lon</georss:point>\n";
  458.             echo "\t\t<geourl:latitude>$lat</geourl:latitude>\n\t\t<geourl:longitude>$lon</geourl:longitude>\n";
  459.             echo "\t\t<icbm:latitude>$lat</icbm:latitude>\n\t\t<icbm:longitude>$lon</icbm:longitude>\n";
  460.         }
  461.    
  462.     }
  463.    
  464. }
  465. add_action('rss2_item', 'mygpGeotagsGeoMetatags_addGeoFeedtags');
  466. add_action('atom_entry', 'mygpGeotagsGeoMetatags_addGeoFeedtags');
  467. add_action('rdf_item', 'mygpGeotagsGeoMetatags_addGeoFeedtags');
  468.  
  469.  
  470.  
  471. /**
  472.  * Create and return map (based on current post)
  473.  *
  474.  */
  475. function mygpGeotagsGeoMetatags_getMap() {
  476.  
  477.     global $wp_query, $mygpGeotagsGeoMetatags_key;
  478.    
  479.     $postId = $wp_query->post->ID;
  480.     $data = get_post_meta($postId, $mygpGeotagsGeoMetatags_key, true);
  481.    
  482.     $dataSplitted = "";            
  483.     if (isset($data[ 'position' ]) && $data[ 'position' ] != "") {
  484.         $dataSplitted = explode(";", $data[ 'position' ]);
  485.     } else {
  486.         return "";     
  487.     }
  488.    
  489.     $mapId = 'mygpMap' . $postId;
  490.     $html = '<div id="' . $mapId . '" style="float:' . mygpGeotagsGeoMetatags_getOption("mapFloat", $postId) . ';width:' . mygpGeotagsGeoMetatags_getOption("mapWidth", $postId) . ';height:' . mygpGeotagsGeoMetatags_getOption("mapHeight", $postId) . ';" class="mygpMap"></div>';
  491.     $html .= '<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.1&sensor=false"></script>
  492. <script type="text/javascript">
  493.    var latlng = new google.maps.LatLng(' . $dataSplitted[0] . ', ' . $dataSplitted[1] . ');
  494.    var myOptions = {
  495.      zoom: ' . mygpGeotagsGeoMetatags_getOption("zoomLevel", $postId) . ',
  496.      center: latlng,
  497.      mapTypeId: google.maps.MapTypeId.' . mygpGeotagsGeoMetatags_getOption("mapType", $postId) . '
  498.    };
  499.    var map = new google.maps.Map(document.getElementById("' . $mapId . '"), myOptions);
  500.     map.disableDoubleClickZoom = false;
  501.     map.scrollwheel = true;
  502.     var marker = new google.maps.Marker({
  503.       position: latlng,
  504.       map: map
  505.     });
  506. </script>';
  507.    
  508.     return $html;
  509.    
  510. }
  511.  
  512.  
  513.  
  514. /**
  515.  * Add map to post
  516.  *
  517.  */
  518. function mygpGeotagsGeoMetatags_addMap($content) {
  519.  
  520.     global $wp_query, $mygpGeotagsGeoMetatags_key;
  521.    
  522.     $postId = $wp_query->post->ID;
  523.     $shortcode = '[mygp_map]';
  524.    
  525.     if (mygpGeotagsGeoMetatags_getOption("addMap", $postId) == 'false') {
  526.         return str_replace($shortcode, "", $content);
  527.     }
  528.  
  529.     $html = mygpGeotagsGeoMetatags_getMap();
  530.     if ($html == '') {
  531.         return str_replace($shortcode, "", $content);
  532.     }
  533.  
  534.     switch(mygpGeotagsGeoMetatags_getOption('mapLocation', $postId)) {
  535.         case 'before':
  536.             $content = str_replace($shortcode, '', $content);
  537.             $content = $html . '' . $content;
  538.             break;
  539.         case 'after':
  540.             $content = str_replace($shortcode, '', $content);
  541.             $content = $content . '' . $html;
  542.             break;
  543.         case 'shortcode':
  544.             $content = str_replace($shortcode, $html, $content);
  545.             break;
  546.     }
  547.  
  548.     return $content;
  549. }
  550. add_filter('the_content', 'mygpGeotagsGeoMetatags_addMap');
  551.  
  552.  
  553.  
  554. /**
  555.  * Add microformats to post
  556.  *
  557.  */
  558. function mygpGeotagsGeoMetatags_addMicroformats($content) {
  559.  
  560.     global $wp_query, $mygpGeotagsGeoMetatags_key;
  561.    
  562.     $postId = $wp_query->post->ID;
  563.     $data = get_post_meta($postId, $mygpGeotagsGeoMetatags_key, true);
  564.    
  565.     $dataSplitted = "";            
  566.     if (isset($data['position']) && $data[ 'position' ] != "") {
  567.         $dataSplitted = explode(";", $data[ 'position' ]);
  568.     } else {
  569.         return $content;       
  570.     }
  571.    
  572.     if (mygpGeotagsGeoMetatags_getOption("addMicroformats") == 'false') {
  573.         return $content;
  574.     }
  575.  
  576.     // http://microformats.org/wiki/geo
  577.     // http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=146861
  578.     // TODO: http://schema.org/GeoCoordinates
  579.     // TODO: http://www.data-vocabulary.org/Geo/
  580.     $content = $content . '
  581. <div id="geo-post-' . $postId . '" class="geo geo-post" style="display:none">
  582.   <span class="latitude" title="' . $dataSplitted[0] . '">
  583.      ' . $dataSplitted[0] . '
  584.      <span class="value-title" title="' . ($dataSplitted[0]) . '"></span>
  585.   </span>
  586.   <span class="longitude" title="' . $dataSplitted[1] . '">
  587.      ' . $dataSplitted[1] . '
  588.      <span class="value-title" title="' . ($dataSplitted[1]) . '"></span>
  589.   </span>
  590. </div>';
  591.        
  592.     return $content;
  593. }
  594. add_filter('the_content', 'mygpGeotagsGeoMetatags_addMicroformats');
  595.  
  596.  
  597.  
  598. /**
  599.  * Return plugin options.
  600.  *
  601.  */
  602. function mygpGeotagsGeoMetatags_getOption($option, $postId = '') {
  603.    
  604.     global $mygpGeotagsGeoMetatags_key;
  605.    
  606.     if ($postId != '') {
  607.    
  608.         $data = get_post_meta($postId, $mygpGeotagsGeoMetatags_key, true);
  609.        
  610.         if (isset($data[$option]) && $data[$option] != '') {
  611.            
  612.             return $data[$option];
  613.            
  614.         }
  615.        
  616.     }
  617.    
  618.     $value = get_option('mygpGeotagsGeoMetatags_' . $option);
  619.    
  620.     if (isset($value['text_string'])) {
  621.        
  622.         if (is_bool($value['text_string']) && $value['text_string']) {
  623.             return "true";         
  624.         }
  625.        
  626.         if (is_bool($value['text_string']) && !$value['text_string']) {
  627.             return "false";        
  628.         }
  629.        
  630.         return $value['text_string'];
  631.        
  632.     } else {
  633.        
  634.         switch($option) {
  635.             case 'autolocate';
  636.               return 'true';
  637.               break;
  638.             case 'addMetatags';
  639.               return 'true';
  640.               break;
  641.             case 'addPosttags';
  642.               return 'true';
  643.               break;
  644.             case 'addCityToPosttags';
  645.               return 'true';
  646.               break;
  647.             case 'addFeedtags';
  648.               return 'true';
  649.               break;
  650.             case 'addMicroformats';
  651.               return 'true';
  652.               break;
  653.             case 'addMap';
  654.               return 'true';
  655.               break;
  656.             case 'mapLocation';
  657.               return 'after';
  658.               break;
  659.             case 'mapWidth';
  660.               return '100%';
  661.               break;
  662.             case 'mapHeight';
  663.               return '200px';
  664.               break;
  665.             case 'mapFloat';
  666.               return 'none';
  667.               break;
  668.             case 'mapType';
  669.               return 'ROADMAP';
  670.               break;
  671.             case 'zoomLevel';
  672.               return '12';
  673.               break;
  674.         }
  675.        
  676.     }
  677.    
  678. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement