geo-location.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. require_once dirname( __FILE__ ) . '/geo-location/class.jetpack-geo-location.php';
  3. /**
  4. * Geo-location shortcode for display of location data associated with a post.
  5. *
  6. * Usage with current global $post:
  7. * [geo-location]
  8. *
  9. * Usage with specific post ID:
  10. * [geo-location post=5]
  11. */
  12. add_shortcode( 'geo-location', 'jetpack_geo_shortcode' );
  13. function jetpack_geo_shortcode( $attributes ) {
  14. $attributes = shortcode_atts( array( 'post' => null, 'id' => null ), $attributes );
  15. return jetpack_geo_get_location( $attributes['post'] ? $attributes['post'] : $attributes['id'] );
  16. }
  17. /**
  18. * Get the geo-location data associated with the supplied post ID, if it's available
  19. * and marked as being available for public display. The returned array will contain
  20. * "latitude", "longitude" and "label" keys.
  21. *
  22. * If you do not supply a value for $post_id, the global $post will be used, if
  23. * available.
  24. *
  25. * @param integer|null $post_id
  26. *
  27. * @return array|null
  28. */
  29. function jetpack_geo_get_data( $post_id = null) {
  30. $geo = Jetpack_Geo_Location::init();
  31. if ( ! $post_id ) {
  32. $post_id = $geo->get_post_id();
  33. }
  34. $meta_values = $geo->get_meta_values( $post_id );
  35. if ( ! $meta_values['is_public'] || ! $meta_values['is_populated'] ) {
  36. return null;
  37. }
  38. return array(
  39. 'latitude' => $meta_values['latitude'],
  40. 'longitude' => $meta_values['longitude'],
  41. 'label' => $meta_values['label']
  42. );
  43. }
  44. /**
  45. * Display the label HTML for the geo-location information associated with the supplied
  46. * post ID.
  47. *
  48. * If you do not supply a value for $post_id, the global $post will be used, if
  49. * available.
  50. *
  51. * @param integer|null $post_id
  52. *
  53. * @return void
  54. */
  55. function jetpack_geo_display_location( $post_id = null ) {
  56. echo jetpack_geo_get_location( $post_id );
  57. }
  58. /**
  59. * Return the label HTML for the geo-location information associated with the supplied
  60. * post ID.
  61. *
  62. * If you do not supply a value for $post_id, the global $post will be used, if
  63. * available.
  64. *
  65. * @param integer|null $post_id
  66. *
  67. * @return string
  68. */
  69. function jetpack_geo_get_location( $post_id = null ) {
  70. return Jetpack_Geo_Location::init()->get_location_label( $post_id );
  71. }