| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module: geo-location |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Adds support for geo-location features. |
| 10 |
*/ |
| 11 |
|
| 12 |
require_once __DIR__ . '/geo-location/class.jetpack-geo-location.php'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Geo-location shortcode callback for display of location data associated with a post. |
| 16 |
* |
| 17 |
* Usage with current global $post: |
| 18 |
* [geo-location] |
| 19 |
* |
| 20 |
* Usage with specific post ID: |
| 21 |
* [geo-location post=5] |
| 22 |
* |
| 23 |
* @param array $attributes Shortcode attributes. |
| 24 |
*/ |
| 25 |
function jetpack_geo_shortcode( $attributes ) { |
| 26 |
$attributes = shortcode_atts( |
| 27 |
array( |
| 28 |
'post' => null, |
| 29 |
'id' => null, |
| 30 |
), |
| 31 |
$attributes |
| 32 |
); |
| 33 |
return jetpack_geo_get_location( $attributes['post'] ? $attributes['post'] : $attributes['id'] ); |
| 34 |
} |
| 35 |
add_shortcode( 'geo-location', 'jetpack_geo_shortcode' ); |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the geo-location data associated with the supplied post ID, if it's available |
| 39 |
* and marked as being available for public display. The returned array will contain |
| 40 |
* "latitude", "longitude" and "label" keys. |
| 41 |
* |
| 42 |
* If you do not supply a value for $post_id, the global $post will be used, if |
| 43 |
* available. |
| 44 |
* |
| 45 |
* @param integer|null $post_id Post ID. |
| 46 |
* |
| 47 |
* @return array|null |
| 48 |
*/ |
| 49 |
function jetpack_geo_get_data( $post_id = null ) { |
| 50 |
$geo = Jetpack_Geo_Location::init(); |
| 51 |
|
| 52 |
if ( ! $post_id ) { |
| 53 |
$post_id = $geo->get_post_id(); |
| 54 |
} |
| 55 |
|
| 56 |
$meta_values = $geo->get_meta_values( $post_id ); |
| 57 |
|
| 58 |
if ( ! $meta_values['is_public'] || ! $meta_values['is_populated'] ) { |
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
return array( |
| 63 |
'latitude' => $meta_values['latitude'], |
| 64 |
'longitude' => $meta_values['longitude'], |
| 65 |
'label' => $meta_values['label'], |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Display the label HTML for the geo-location information associated with the supplied |
| 71 |
* post ID. |
| 72 |
* |
| 73 |
* If you do not supply a value for $post_id, the global $post will be used, if |
| 74 |
* available. |
| 75 |
* |
| 76 |
* @param integer|null $post_id Post ID. |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
function jetpack_geo_display_location( $post_id = null ) { |
| 81 |
echo jetpack_geo_get_location( $post_id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in `Jetpack_Geo_Location::get_location_label`. |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Return the label HTML for the geo-location information associated with the supplied |
| 86 |
* post ID. |
| 87 |
* |
| 88 |
* If you do not supply a value for $post_id, the global $post will be used, if |
| 89 |
* available. |
| 90 |
* |
| 91 |
* @param integer|null $post_id Post ID. |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
function jetpack_geo_get_location( $post_id = null ) { |
| 96 |
return Jetpack_Geo_Location::init()->get_location_label( $post_id ); |
| 97 |
} |
| 98 |
|