-
Notifications
You must be signed in to change notification settings - Fork 15
/
rest-api.php
86 lines (78 loc) · 1.98 KB
/
rest-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/** @noinspection AutoloadingIssuesInspection */
/**
* Integration with the WordPress REST API.
*
* @since 1.11.0
*/
class GeoMashupRestAPI {
/**
* Register the Geo Mashup REST API elements.
*
* Call from the rest_api_init hook.
*
* @since 1.11.0
*/
public static function init() {
register_rest_field(
array( 'post', 'comment' ),
'geo',
array(
'get_callback' => array( __CLASS__, 'get_geo' ),
'schema' => self::geo_schema(),
)
);
}
/**
* Add the geo field to a WordPress object.
*
* The get_callback for register_rest_field().
*
* @since 1.11.0
*
* @param array $object The WordPress object data as an associative array.
*
* @return array|null The geo field data for the given object.
*/
public static function get_geo( array $object ) {
$object_type = func_get_arg( 3 );
$location = GeoMashupDB::get_object_location( $object_type, $object['id'] );
if ( empty( $location ) ) {
return null;
}
return array(
'latitude' => (float) $location->lat,
'longitude' => (float) $location->lng,
'description' => $location->address,
);
}
/**
* @since 1.11.0
* @return array The geo field schema.
*/
public static function geo_schema() {
return array(
'readonly' => true,
'description' => __( 'Geo Mashup coordinates associated with the object.', 'GeoMashup' ),
'type' => 'object',
'properties' => array(
'latitude' => array(
'description' => __( 'The decimal latitude in the WGS 84 datum.', 'GeoMashup' ),
'type' => 'number',
'minimum' => - 90,
'maximum' => 90,
),
'longitude' => array(
'description' => __( 'The decimal longitude in the WGS 84 datum.', 'GeoMashup' ),
'type' => 'number',
'minimum' => - 180,
'maximum' => 180,
),
'description' => array(
'description' => __( 'An address or general description of the location.', 'GeoMashup' ),
'type' => 'string'
)
)
);
}
}