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
|
|
|
* Whether to lazy load this field. |
15
|
|
|
* |
16
|
|
|
* @var bool |
17
|
|
|
*/ |
18
|
|
|
protected $lazyload = true; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Default latitude. |
22
|
|
|
* |
23
|
|
|
* @var float |
24
|
|
|
*/ |
25
|
|
|
protected $default_lat = 40.346544; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Default longitude. |
29
|
|
|
* |
30
|
|
|
* @var float |
31
|
|
|
*/ |
32
|
|
|
protected $default_lng = -101.645507; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Default zoom. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $default_zoom = 10; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a field from a certain type with the specified label. |
43
|
|
|
* @param string $name Field name |
44
|
|
|
* @param string $label Field label |
45
|
|
|
*/ |
46
|
|
|
protected function __construct( $name, $label ) { |
47
|
|
|
$this->value = new Value_Set( Value_Set::TYPE_MULTIPLE_PROPERTIES, array( 'lat' => '', 'lng' => '', 'zoom' => '', 'address' => '' ) ); |
48
|
|
|
parent::__construct( $name, $label ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Enqueue scripts in the administration |
53
|
|
|
*/ |
54
|
|
|
public static function admin_enqueue_scripts() { |
55
|
|
|
$api_key = apply_filters( 'carbon_map_api_key', false ); |
56
|
|
|
$url = apply_filters( 'carbon_map_url', '//maps.googleapis.com/maps/api/js?' . ( $api_key ? 'key=' . $api_key : '' ), $api_key ); |
57
|
|
|
|
58
|
|
|
wp_enqueue_script( 'carbon-google-maps', $url, array(), null ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Convert lat and lng to a comma-separated list |
63
|
|
|
*/ |
64
|
|
|
protected function lat_lng_to_latlng( $lat, $lng ) { |
65
|
|
|
return ( ! empty( $lat ) && ! empty( $lng ) ) ? $lat . ',' . $lng : ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Load the field value from an input array based on it's name |
70
|
|
|
* |
71
|
|
|
* @param array $input (optional) Array of field names and values. Defaults to $_POST |
72
|
|
|
**/ |
73
|
|
|
public function set_value_from_input( $input = null ) { |
74
|
|
|
if ( is_null( $input ) ) { |
75
|
|
|
$input = $_POST; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ( ! isset( $input[ $this->get_name() ] ) ) { |
79
|
|
|
$this->set_value( null ); |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$value_set = array( |
84
|
|
|
'lat' => '', |
85
|
|
|
'lng' => '', |
86
|
|
|
'zoom' => '', |
87
|
|
|
'address' => '', |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
foreach ( $value_set as $key => $v ) { |
91
|
|
|
if ( isset( $input[ $this->get_name() ][ $key ] ) ) { |
92
|
|
|
$value_set[ $key ] = $input[ $this->get_name() ][ $key ]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$value_set['lat'] = (float) $value_set['lat']; |
97
|
|
|
$value_set['lng'] = (float) $value_set['lng']; |
98
|
|
|
$value_set['zoom'] = (int) $value_set['zoom']; |
99
|
|
|
$value_set[ Value_Set::VALUE_PROPERTY ] = $this->lat_lng_to_latlng( $value_set['lat'], $value_set['lng'] ); |
100
|
|
|
|
101
|
|
|
$this->set_value( $value_set ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
106
|
|
|
* This data will be available in the Underscore template and the Backbone Model. |
107
|
|
|
* |
108
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function to_json( $load ) { |
112
|
|
|
$field_data = parent::to_json( $load ); |
113
|
|
|
|
114
|
|
|
$value_set = $this->get_value(); |
115
|
|
|
if ( $value_set === null ) { |
116
|
|
|
$value_set = array( |
117
|
|
|
'lat' => floatval( $this->default_lat ), |
118
|
|
|
'lng' => floatval( $this->default_lng ), |
119
|
|
|
'zoom' => intval( $this->default_zoom ), |
120
|
|
|
'address' => '', |
121
|
|
|
); |
122
|
|
|
$value_set[ Value_Set::VALUE_PROPERTY ] = $this->lat_lng_to_latlng( $value_set['lat'], $value_set['lng'] ); |
123
|
|
|
} |
124
|
|
|
$field_data = array_merge( $field_data, array( |
125
|
|
|
'value' => $value_set['value'], |
126
|
|
|
'lat' => floatval( $value_set['lat'] ), |
127
|
|
|
'lng' => floatval( $value_set['lng'] ), |
128
|
|
|
'zoom' => intval( $value_set['zoom'] ), |
129
|
|
|
'address' => $value_set['address'], |
130
|
|
|
) ); |
131
|
|
|
|
132
|
|
|
return $field_data; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Underscore template of this field. |
137
|
|
|
*/ |
138
|
|
|
public function template() { |
139
|
|
|
?> |
140
|
|
|
<div class="carbon-map-search"> |
141
|
|
|
<p><?php _e( 'Locate Address on the map', \Carbon_Fields\TEXT_DOMAIN ); ?>: </p> |
142
|
|
|
|
143
|
|
|
<div class="carbon-map-search-row"> |
144
|
|
|
<input type="text" name="{{{ name }}}[address]" value="{{{ address }}}" class="regular-text address carbon-map-search-address" placeholder="Search..." /> |
145
|
|
|
<span class="carbon-map-search-button dashicons-before dashicons-search"> |
146
|
|
|
<?php _e( 'Find', \Carbon_Fields\TEXT_DOMAIN ); ?> |
147
|
|
|
</span> |
148
|
|
|
</div> |
149
|
|
|
|
150
|
|
|
<input type="hidden" name="{{{ name }}}[lat]" value="{{{ lat }}}" /> |
151
|
|
|
<input type="hidden" name="{{{ name }}}[lng]" value="{{{ lng }}}" /> |
152
|
|
|
<input type="hidden" name="{{{ name }}}[zoom]" value="{{{ zoom }}}" /> |
153
|
|
|
</div> |
154
|
|
|
<div class="carbon-map-canvas"> </div> |
155
|
|
|
<?php |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the coords and zoom of this field. |
160
|
|
|
* @param string $lat Latitude |
161
|
|
|
* @param string $lng Longitude |
162
|
|
|
* @param int $zoom Zoom level |
163
|
|
|
*/ |
164
|
|
|
public function set_position( $lat, $lng, $zoom ) { |
165
|
|
|
$this->default_lat = floatval( $lat ); |
166
|
|
|
$this->default_lng = floatval( $lng ); |
167
|
|
|
$this->default_zoom = $zoom; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|