Completed
Push — try/gutenberg-separate-jetpack... ( e8dd3e...f0efb9 )
by Bernhard
39:26 queued 23:22
created

geo-location.php ➔ jetpack_geo_get_location()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
require_once dirname( __FILE__ ) . '/geo-location/class.jetpack-geo-location.php';
4
5
/**
6
 * Geo-location shortcode for display of location data associated with a post.
7
 *
8
 * Usage with current global $post:
9
 * [geo-location]
10
 *
11
 * Usage with specific post ID:
12
 * [geo-location post=5]
13
 */
14
add_shortcode( 'geo-location', 'jetpack_geo_shortcode' );
15
16
function jetpack_geo_shortcode( $attributes ) {
17
	$attributes = shortcode_atts( array( 'post' => null, 'id' => null ), $attributes );
18
	return jetpack_geo_get_location( $attributes['post'] ? $attributes['post'] : $attributes['id'] );
19
}
20
21
/**
22
 * Get the geo-location data associated with the supplied post ID, if it's available
23
 * and marked as being available for public display.  The returned array will contain
24
 * "latitude", "longitude" and "label" keys.
25
 *
26
 * If you do not supply a value for $post_id, the global $post will be used, if
27
 * available.
28
 *
29
 * @param integer|null $post_id
30
 *
31
 * @return array|null
32
 */
33
function jetpack_geo_get_data( $post_id = null) {
34
	$geo = Jetpack_Geo_Location::init();
35
36
	if ( ! $post_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $post_id of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
37
		$post_id = $geo->get_post_id();
38
	}
39
40
	$meta_values = $geo->get_meta_values( $post_id );
41
42
	if ( ! $meta_values['is_public'] || ! $meta_values['is_populated'] ) {
43
		return null;
44
	}
45
46
	return array(
47
		'latitude'  => $meta_values['latitude'],
48
		'longitude' => $meta_values['longitude'],
49
		'label'     => $meta_values['label']
50
	);
51
}
52
53
/**
54
 * Display the label HTML for the geo-location information associated with the supplied
55
 * post ID.
56
 *
57
 * If you do not supply a value for $post_id, the global $post will be used, if
58
 * available.
59
 *
60
 * @param integer|null $post_id
61
 *
62
 * @return void
63
 */
64
function jetpack_geo_display_location( $post_id = null ) {
65
	echo jetpack_geo_get_location( $post_id );
66
}
67
68
/**
69
 * Return the label HTML for the geo-location information associated with the supplied
70
 * post ID.
71
 *
72
 * If you do not supply a value for $post_id, the global $post will be used, if
73
 * available.
74
 *
75
 * @param integer|null $post_id
76
 *
77
 * @return string
78
 */
79
function jetpack_geo_get_location( $post_id = null ) {
80
	return Jetpack_Geo_Location::init()->get_location_label( $post_id );
81
}
82