PageRenderTime 92ms CodeModel.GetById 57ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/templates.php

https://github.com/petehobo/WP-Geo
PHP | 446 lines | 231 code | 129 blank | 86 comment | 43 complexity | 1f88b31309e5a673e4796d82d7b44fb0 MD5 | raw file
  1. <?php
  2. /**
  3. * @package WP Geo
  4. * @subpackage Includes > Template
  5. * @author Ben Huson <ben@thewhiteroom.net>
  6. */
  7. /**
  8. * @method WP Geo Latitude
  9. * @description Outputs the post latitude.
  10. * @param $post_id = Post ID (optional)
  11. */
  12. function wpgeo_latitude( $post_id = null ) {
  13. echo get_wpgeo_latitude( $post_id );
  14. }
  15. /**
  16. * @method WP Geo Longitude
  17. * @description Outputs the post longitude.
  18. * @param $post_id = Post ID (optional)
  19. */
  20. function wpgeo_longitude( $post_id = null ) {
  21. echo get_wpgeo_longitude( $post_id );
  22. }
  23. /**
  24. * @method WP Geo Title
  25. * @description Outputs the post title.
  26. * @param $post_id = Post ID (optional)
  27. * @param $default_to_post_title = Default to post title if point title empty (optional)
  28. */
  29. function wpgeo_title( $post_id = null, $default_to_post_title = true ) {
  30. echo get_wpgeo_title( $post_id, $default_to_post_title );
  31. }
  32. /**
  33. * @method Get WP Geo Latitude
  34. * @description Gets the post latitude.
  35. * @param $post_id = Post ID (optional)
  36. * @return (float) Latitude
  37. */
  38. function get_wpgeo_latitude( $post_id = null ) {
  39. global $post;
  40. $id = absint($post_id) > 0 ? absint($post_id) : $post->ID;
  41. if ( absint($id) > 0 ) {
  42. return get_post_meta( absint($id), WPGEO_LATITUDE_META, true );
  43. }
  44. return null;
  45. }
  46. /**
  47. * @method Get WP Geo Longitude
  48. * @description Gets the post longitude.
  49. * @param $post_id = Post ID (optional)
  50. * @return (float) Longitude
  51. */
  52. function get_wpgeo_longitude( $post_id = null ) {
  53. global $post;
  54. $id = absint($post_id) > 0 ? absint($post_id) : $post->ID;
  55. if ( absint($id) > 0 ) {
  56. return get_post_meta( absint($id), WPGEO_LONGITUDE_META, true );
  57. }
  58. return null;
  59. }
  60. /**
  61. * @method Get WP Geo Title
  62. * @description Gets the post title.
  63. * @param $post_id = Post ID (optional)
  64. * @param $default_to_post_title = Default to post title if point title empty (optional)
  65. * @return (string) Title
  66. */
  67. function get_wpgeo_title( $post_id = null, $default_to_post_title = true ) {
  68. global $post;
  69. $id = absint( $post_id ) > 0 ? absint( $post_id ) : $post->ID;
  70. if ( absint( $id ) > 0 ) {
  71. $title = get_post_meta( $id, WPGEO_TITLE_META, true );
  72. if ( empty( $title ) && $default_to_post_title ) {
  73. $p = &get_post( $id );
  74. $title = isset( $p->post_title ) ? $p->post_title : '';
  75. }
  76. $title = apply_filters( 'wpgeo_point_title', $title, $id );
  77. return $title;
  78. }
  79. return null;
  80. }
  81. /**
  82. * @method WP Geo Map Link
  83. * @description Gets a link to an external map.
  84. * @param $args = Array of arguments (optional)
  85. * @return (string) Map URL
  86. */
  87. function wpgeo_map_link( $args = null ) {
  88. global $post;
  89. $defaults = array(
  90. 'post_id' => $post->ID,
  91. 'latitude' => null,
  92. 'longitude' => null,
  93. 'zoom' => 5,
  94. 'echo' => 1
  95. );
  96. // Validate Args
  97. $r = wp_parse_args( $args, $defaults );
  98. $r['post_id'] = absint( $r['post_id'] );
  99. $r['latitude'] = (float) $r['latitude'];
  100. $r['longitude'] = (float) $r['longitude'];
  101. $r['zoom'] = absint( $r['zoom'] );
  102. $r['echo'] = absint( $r['echo'] );
  103. // If a post is specified override lat/lng...
  104. if ( !$r['latitude'] && !$r['longitude'] ) {
  105. $r['latitude'] = get_wpgeo_latitude( $r['post_id'] );
  106. $r['longitude'] = get_wpgeo_longitude( $r['post_id'] );
  107. }
  108. // If lat/lng...
  109. $url = '';
  110. if ( $r['latitude'] && $r['longitude'] ) {
  111. $q = 'q=' . $r['latitude'] . ',' . $r['longitude'];
  112. $z = $r['zoom'] ? '&z=' . $r['zoom'] : '';
  113. $url = 'http://maps.google.co.uk/maps?' . $q . $z;
  114. $url = apply_filters( 'wpgeo_map_link', $url, $r );
  115. }
  116. // Output
  117. if ( $r['echo'] == 0 ) {
  118. return $url;
  119. } else {
  120. echo $url;
  121. }
  122. }
  123. /**
  124. * @method WP Geo Post Map
  125. * @description Outputs the HTML for a post map.
  126. * @param $post_id = Post ID (optional)
  127. */
  128. function wpgeo_post_map( $post_id = null ) {
  129. echo get_wpgeo_post_map( $post_id );
  130. }
  131. /**
  132. * @method Get WP Geo Post Map
  133. * @description Gets the HTML for a post map.
  134. * @param $post_id = Post ID (optional)
  135. * @return (string) HTML
  136. */
  137. function get_wpgeo_post_map( $post_id = null ) {
  138. global $post, $wpgeo;
  139. $id = absint($post_id) > 0 ? absint($post_id) : $post->ID;
  140. $wp_geo_options = get_option( 'wp_geo_options' );
  141. $show_post_map = apply_filters( 'wpgeo_show_post_map', $wp_geo_options['show_post_map'], $id );
  142. $latitude = get_post_meta( $id, WPGEO_LATITUDE_META, true );
  143. $longitude = get_post_meta( $id, WPGEO_LONGITUDE_META, true );
  144. if ( !is_numeric( $latitude ) || !is_numeric( $longitude ) )
  145. return '';
  146. if ( $id > 0 && !is_feed() ) {
  147. if ( $wpgeo->show_maps() && $show_post_map != 'TOP' && $show_post_map != 'BOTTOM' && $wpgeo->checkGoogleAPIKey() ) {
  148. $map_width = $wp_geo_options['default_map_width'];
  149. $map_height = $wp_geo_options['default_map_height'];
  150. if ( is_numeric( $map_width ) ) {
  151. $map_width = $map_width . 'px';
  152. }
  153. if ( is_numeric( $map_height ) ) {
  154. $map_height = $map_height . 'px';
  155. }
  156. return '<div class="wp_geo_map" id="wp_geo_map_' . $id . '" style="width:' . $map_width . '; height:' . $map_height . ';"></div>';
  157. }
  158. }
  159. return '';
  160. }
  161. /**
  162. * @method Get WP Geo Map
  163. */
  164. function get_wpgeo_map( $query, $options = null ) {
  165. global $wpgeo_map_id;
  166. $wpgeo_map_id++;
  167. $id = 'wpgeo_map_id_' . $wpgeo_map_id;
  168. $wp_geo_options = get_option('wp_geo_options');
  169. $defaults = array(
  170. 'width' => $wp_geo_options['default_map_width'],
  171. 'height' => $wp_geo_options['default_map_height'],
  172. 'type' => $wp_geo_options['google_map_type'],
  173. 'polylines' => $wp_geo_options['show_polylines'],
  174. 'polyline_colour' => $wp_geo_options['polyline_colour'],
  175. 'align' => 'none',
  176. 'numberposts' => -1,
  177. 'post_type' => null,
  178. 'post_status' => 'publish',
  179. 'orderby' => 'post_date',
  180. 'order' => 'DESC',
  181. 'markers' => 'large'
  182. );
  183. // Validate Args
  184. $r = wp_parse_args( $query, $defaults );
  185. if ( is_numeric( $r['width'] ) ) {
  186. $r['width'] .= 'px';
  187. }
  188. if ( is_numeric( $r['height'] ) ) {
  189. $r['height'] .= 'px';
  190. }
  191. $posts = get_posts( $query );
  192. $output = '
  193. <div id="' . $id . '" class="wpgeo_map" style="width:' . $r['width'] . '; height:' . $r['height'] . ';float:' . $r['align'] . '"></div>
  194. <script type="text/javascript">
  195. <!--
  196. jQuery(window).load( function() {
  197. if ( GBrowserIsCompatible() ) {
  198. var bounds = new GLatLngBounds();
  199. map = new GMap2(document.getElementById("' . $id . '"));
  200. ' . WPGeo_API_GMap2::render_map_control( 'map', 'GLargeMapControl3D' ) . '
  201. map.setMapType(' . $r['type'] . ');
  202. ';
  203. if ( $posts ) :
  204. $polyline = new WPGeo_Polyline( array(
  205. 'color' => $r['polyline_colour']
  206. ) );
  207. foreach ( $posts as $post ) {
  208. $latitude = get_post_meta($post->ID, WPGEO_LATITUDE_META, true);
  209. $longitude = get_post_meta($post->ID, WPGEO_LONGITUDE_META, true);
  210. if ( is_numeric($latitude) && is_numeric($longitude) ) {
  211. $marker = get_post_meta($post->ID, WPGEO_MARKER_META, true);
  212. if ( empty( $marker ) )
  213. $marker = $r['markers'];
  214. $icon = 'wpgeo_icon_' . apply_filters( 'wpgeo_marker_icon', $marker, $post, 'wpgeo_map' );
  215. $polyline->add_coord( $latitude, $longitude );
  216. $output .= '
  217. var center = new GLatLng(' . $latitude . ',' . $longitude . ');
  218. var marker = new wpgeo_createMarker2(map, center, ' . $icon . ', \'' . esc_js( $post->post_title ) . '\', \'' . get_permalink($post->ID) . '\');
  219. bounds.extend(center);
  220. ';
  221. }
  222. }
  223. if ( $r['polylines'] == 'Y' ) {
  224. $output .= WPGeo_API_GMap2::render_map_overlay( 'map', WPGeo_API_GMap2::render_polyline( $polyline ) );
  225. }
  226. $output .= '
  227. zoom = map.getBoundsZoomLevel(bounds);
  228. map.setCenter(bounds.getCenter(), zoom);
  229. ';
  230. else :
  231. $output .= '
  232. map.setCenter(new GLatLng(' . $wp_geo_options['default_map_latitude'] . ', ' . $wp_geo_options['default_map_longitude'] . '), ' . $wp_geo_options['default_map_zoom'] . ');';
  233. endif;
  234. $output .= '
  235. }
  236. } );
  237. -->
  238. </script>
  239. ';
  240. return $output;
  241. }
  242. /**
  243. * @method WP Geo Map
  244. */
  245. function wpgeo_map( $query, $options = null ) {
  246. echo get_wpgeo_map( $query, $options );
  247. }
  248. /**
  249. * @method WP Geo Post Static Map
  250. * @description Outputs the HTML for a static post map.
  251. * @param $post_id = Post ID (optional)
  252. * @param $query = Optional parameters
  253. */
  254. function wpgeo_post_static_map( $post_id = null, $query = null ) {
  255. echo get_wpgeo_post_static_map( $post_id, $query );
  256. }
  257. /**
  258. * @method Get WP Geo Post Static Map
  259. * @description Gets the HTML for a static post map.
  260. * @param $post_id = Post ID (optional)
  261. * @param $query = Optional parameters
  262. * @return (string) HTML
  263. */
  264. function get_wpgeo_post_static_map( $post_id = null, $query = null ) {
  265. global $post, $wpgeo;
  266. $id = absint($post_id) > 0 ? absint($post_id) : $post->ID;
  267. $latitude = get_post_meta( $id, WPGEO_LATITUDE_META, true );
  268. $longitude = get_post_meta( $id, WPGEO_LONGITUDE_META, true );
  269. if ( !is_numeric( $latitude ) || !is_numeric( $longitude ) )
  270. return '';
  271. if ( !$id || is_feed() ) {
  272. return '';
  273. }
  274. if ( !$wpgeo->show_maps() || !$wpgeo->checkGoogleAPIKey() ) {
  275. return '';
  276. }
  277. // fetch wp geo options & post settings
  278. $wp_geo_options = get_option( 'wp_geo_options' );
  279. $settings = get_post_meta( $id, WPGEO_MAP_SETTINGS_META, true );
  280. // options
  281. $defaults = array(
  282. 'width' => $wp_geo_options['default_map_width'],
  283. 'height' => $wp_geo_options['default_map_height'],
  284. 'maptype' => $wp_geo_options['google_map_type'],
  285. 'zoom' => $wp_geo_options['default_map_zoom'],
  286. );
  287. $options = wp_parse_args( $query, $defaults );
  288. // translate WP-geo maptypes to static map type url param
  289. $types = array(
  290. 'G_NORMAL_MAP' => 'roadmap',
  291. 'G_SATELLITE_MAP' => 'satellite',
  292. 'G_PHYSICAL_MAP' => 'terrain',
  293. 'G_HYBRID_MAP' => 'hybrid'
  294. );
  295. // default: center on location marker
  296. $centerLatitude = $latitude;
  297. $centerLongitude = $longitude;
  298. // custom map settings?
  299. if ( isset( $settings['zoom'] ) && is_numeric( $settings['zoom'] ) ) {
  300. $options['zoom'] = $settings['zoom'];
  301. }
  302. if ( !empty($settings['type']) ) {
  303. $options['maptype'] = $settings['type'];
  304. }
  305. if ( !empty($settings['centre']) ) {
  306. list($centerLatitude, $centerLongitude) = explode( ',', $settings['centre'] );
  307. }
  308. $url = 'http://maps.googleapis.com/maps/api/staticmap?';
  309. $url .= 'center=' . $centerLatitude . ',' . $centerLongitude;
  310. $url .= '&zoom=' . $options['zoom'];
  311. $url .= '&size=' . $options['width'] . 'x' . $options['height'];
  312. $url .= '&maptype=' . $types[$options['maptype']];
  313. $url .= '&markers=color:red%7C' . $latitude . ',' . $longitude;
  314. $url .= '&sensor=false';
  315. return '<img id="wp_geo_static_map_' . $id . '" src="' . $url . '" class="wp_geo_static_map" />';
  316. }
  317. ?>