|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Value_Set\Value_Set; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Google Maps with Address field class. |
|
9
|
|
|
* Allows to manually select a pin, or to position a pin based on a specified address. |
|
10
|
|
|
* Coords (lat, lng), address and zoom are saved in the database. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Map_Field extends Field { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritDoc} |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $default_value = array( |
|
18
|
|
|
Value_Set::VALUE_PROPERTY => '40.346544,-101.645507', |
|
19
|
|
|
'lat' => 40.346544, |
|
20
|
|
|
'lng' => -101.645507, |
|
21
|
|
|
'zoom' => 10, |
|
22
|
|
|
'address' => '', |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritDoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $lazyload = true; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a field from a certain type with the specified label. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $type Field type |
|
34
|
|
|
* @param string $name Field name |
|
35
|
|
|
* @param string $label Field label |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct( $type, $name, $label ) { |
|
38
|
|
|
$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_PROPERTIES, array( 'lat' => '', 'lng' => '', 'zoom' => '', 'address' => '' ) ) ); |
|
39
|
|
|
parent::__construct( $type, $name, $label ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Enqueue scripts and styles in admin |
|
44
|
|
|
* Called once per field type |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function admin_enqueue_scripts() { |
|
47
|
|
|
$api_key = apply_filters( 'carbon_fields_map_field_api_key', false ); |
|
48
|
|
|
$url = apply_filters( 'carbon_fields_map_field_api_url', '//maps.googleapis.com/maps/api/js?' . ( $api_key ? 'key=' . $api_key : '' ), $api_key ); |
|
49
|
|
|
|
|
50
|
|
|
wp_enqueue_script( 'carbon-google-maps', $url, array(), null ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Convert lat and lng to a comma-separated list |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function lat_lng_to_latlng( $lat, $lng ) { |
|
57
|
|
|
return ( ! empty( $lat ) && ! empty( $lng ) ) ? $lat . ',' . $lng : ''; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Load the field value from an input array based on its name |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $input Array of field names and values. |
|
64
|
|
|
* @return self $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function set_value_from_input( $input ) { |
|
67
|
|
|
if ( ! isset( $input[ $this->get_name() ] ) ) { |
|
68
|
|
|
$this->set_value( null ); |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$value_set = array( |
|
73
|
|
|
'lat' => '', |
|
74
|
|
|
'lng' => '', |
|
75
|
|
|
'zoom' => '', |
|
76
|
|
|
'address' => '', |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
foreach ( $value_set as $key => $v ) { |
|
80
|
|
|
if ( isset( $input[ $this->get_name() ][ $key ] ) ) { |
|
81
|
|
|
$value_set[ $key ] = $input[ $this->get_name() ][ $key ]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$value_set['lat'] = (float) $value_set['lat']; |
|
86
|
|
|
$value_set['lng'] = (float) $value_set['lng']; |
|
87
|
|
|
$value_set['zoom'] = (int) $value_set['zoom']; |
|
88
|
|
|
$value_set[ Value_Set::VALUE_PROPERTY ] = $this->lat_lng_to_latlng( $value_set['lat'], $value_set['lng'] ); |
|
89
|
|
|
|
|
90
|
|
|
$this->set_value( $value_set ); |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
|
96
|
|
|
* |
|
97
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function to_json( $load ) { |
|
101
|
|
|
$field_data = parent::to_json( $load ); |
|
102
|
|
|
|
|
103
|
|
|
$value_set = $this->get_value(); |
|
104
|
|
|
$field_data = array_merge( $field_data, array( |
|
105
|
|
|
'value' => array( |
|
106
|
|
|
'lat' => floatval( $value_set['lat'] ), |
|
107
|
|
|
'lng' => floatval( $value_set['lng'] ), |
|
108
|
|
|
'zoom' => intval( $value_set['zoom'] ), |
|
109
|
|
|
'address' => $value_set['address'], |
|
110
|
|
|
'value' => $value_set[ Value_Set::VALUE_PROPERTY ], |
|
111
|
|
|
), |
|
112
|
|
|
) ); |
|
113
|
|
|
|
|
114
|
|
|
return $field_data; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set the coords and zoom of this field. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $lat Latitude |
|
121
|
|
|
* @param string $lng Longitude |
|
122
|
|
|
* @param int $zoom Zoom level |
|
123
|
|
|
* @return self $this |
|
124
|
|
|
*/ |
|
125
|
|
|
public function set_position( $lat, $lng, $zoom ) { |
|
126
|
|
|
return $this->set_default_value( array_merge( |
|
127
|
|
|
$this->get_default_value(), |
|
128
|
|
|
array( |
|
129
|
|
|
Value_Set::VALUE_PROPERTY => $this->lat_lng_to_latlng( $lat, $lng ), |
|
130
|
|
|
'lat' => $lat, |
|
131
|
|
|
'lng' => $lng, |
|
132
|
|
|
'zoom' => $zoom, |
|
133
|
|
|
) |
|
134
|
|
|
) ); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|