PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-google-map.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago index.php 1 year ago
class-acf-field-google-map.php
375 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_google_map' ) ) :
4 #[AllowDynamicProperties]
5 class acf_field_google_map extends acf_field {
6
7
8 /**
9 * This function will setup the field type data
10 *
11 * @type function
12 * @date 5/03/2014
13 * @since 5.0.0
14 *
15 * @param n/a
16 * @return n/a
17 */
18 function initialize() {
19
20 // vars
21 $this->name = 'google_map';
22 $this->label = __( 'Google Map', 'acf' );
23 $this->category = 'advanced';
24 $this->description = __( 'An interactive UI for selecting a location using Google Maps. Requires a Google Maps API key and additional configuration to display correctly.', 'acf' );
25 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-google-map.png';
26 $this->doc_url = 'https://www.advancedcustomfields.com/resources/google-map/';
27 $this->defaults = array(
28 'height' => '',
29 'center_lat' => '',
30 'center_lng' => '',
31 'zoom' => '',
32 );
33 $this->default_values = array(
34 'height' => '400',
35 'center_lat' => '-37.81411',
36 'center_lng' => '144.96328',
37 'zoom' => '14',
38 );
39 }
40
41
42 /**
43 * description
44 *
45 * @type function
46 * @date 16/12/2015
47 * @since 5.3.2
48 *
49 * @param $post_id (int)
50 * @return $post_id (int)
51 */
52 function input_admin_enqueue_scripts() {
53
54 // localize
55 acf_localize_text(
56 array(
57 'Sorry, this browser does not support geolocation' => __( 'Sorry, this browser does not support geolocation', 'acf' ),
58 )
59 );
60
61 // bail early if no enqueue
62 if ( ! acf_get_setting( 'enqueue_google_maps' ) ) {
63 return;
64 }
65
66 // vars
67 $api = array(
68 'key' => acf_get_setting( 'google_api_key' ),
69 'client' => acf_get_setting( 'google_api_client' ),
70 'libraries' => 'places',
71 'ver' => 3,
72 'callback' => 'Function.prototype',
73 'language' => acf_get_locale(),
74 );
75
76 // filter
77 $api = apply_filters( 'acf/fields/google_map/api', $api );
78
79 // remove empty
80 if ( empty( $api['key'] ) ) {
81 unset( $api['key'] );
82 }
83 if ( empty( $api['client'] ) ) {
84 unset( $api['client'] );
85 }
86
87 // construct url
88 $url = add_query_arg( $api, 'https://maps.googleapis.com/maps/api/js' );
89
90 // localize
91 acf_localize_data(
92 array(
93 'google_map_api' => $url,
94 )
95 );
96 }
97
98
99 /**
100 * Create the HTML interface for your field
101 *
102 * @param $field - an array holding all the field's data
103 *
104 * @type action
105 * @since 3.6
106 * @date 23/01/13
107 */
108 function render_field( $field ) {
109
110 // Apply defaults.
111 foreach ( $this->default_values as $k => $v ) {
112 if ( ! $field[ $k ] ) {
113 $field[ $k ] = $v;
114 }
115 }
116
117 // Attrs.
118 $attrs = array(
119 'id' => $field['id'],
120 'class' => "acf-google-map {$field['class']}",
121 'data-lat' => $field['center_lat'],
122 'data-lng' => $field['center_lng'],
123 'data-zoom' => $field['zoom'],
124 );
125
126 $search = '';
127 if ( $field['value'] ) {
128 $attrs['class'] .= ' -value';
129 $search = $field['value']['address'];
130 } else {
131 $field['value'] = '';
132 }
133
134 ?>
135 <div <?php echo acf_esc_attrs( $attrs ); ?>>
136
137 <?php
138 acf_hidden_input(
139 array(
140 'name' => $field['name'],
141 'value' => $field['value'],
142 )
143 );
144 ?>
145
146 <div class="title">
147
148 <div class="acf-actions -hover">
149 <a href="#" data-name="search" class="acf-icon -search grey" title="<?php esc_attr_e( 'Search', 'acf' ); ?>"></a>
150 <a href="#" data-name="clear" class="acf-icon -cancel grey" title="<?php esc_attr_e( 'Clear location', 'acf' ); ?>"></a>
151 <a href="#" data-name="locate" class="acf-icon -location grey" title="<?php esc_attr_e( 'Find current location', 'acf' ); ?>"></a>
152 </div>
153
154 <input class="search" type="text" placeholder="<?php esc_attr_e( 'Search for address...', 'acf' ); ?>" value="<?php echo esc_attr( $search ); ?>" />
155 <i class="acf-loading"></i>
156
157 </div>
158
159 <div class="canvas" style="<?php echo esc_attr( 'height: ' . $field['height'] . 'px' ); ?>"></div>
160
161 </div>
162 <?php
163 }
164
165
166 /**
167 * Create extra options for your field. This is rendered when editing a field.
168 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
169 *
170 * @type action
171 * @since 3.6
172 * @date 23/01/13
173 *
174 * @param $field - an array holding all the field's data
175 */
176 function render_field_settings( $field ) {
177
178 // center_lat
179 acf_render_field_setting(
180 $field,
181 array(
182 'label' => __( 'Center', 'acf' ),
183 'hint' => __( 'Center the initial map', 'acf' ),
184 'type' => 'text',
185 'name' => 'center_lat',
186 'prepend' => 'lat',
187 'placeholder' => $this->default_values['center_lat'],
188 )
189 );
190
191 // center_lng
192 acf_render_field_setting(
193 $field,
194 array(
195 'label' => __( 'Center', 'acf' ),
196 'hint' => __( 'Center the initial map', 'acf' ),
197 'type' => 'text',
198 'name' => 'center_lng',
199 'prepend' => 'lng',
200 'placeholder' => $this->default_values['center_lng'],
201 '_append' => 'center_lat',
202 )
203 );
204
205 // zoom
206 acf_render_field_setting(
207 $field,
208 array(
209 'label' => __( 'Zoom', 'acf' ),
210 'instructions' => __( 'Set the initial zoom level', 'acf' ),
211 'type' => 'text',
212 'name' => 'zoom',
213 'placeholder' => $this->default_values['zoom'],
214 )
215 );
216
217 // allow_null
218 acf_render_field_setting(
219 $field,
220 array(
221 'label' => __( 'Height', 'acf' ),
222 'instructions' => __( 'Customize the map height', 'acf' ),
223 'type' => 'text',
224 'name' => 'height',
225 'append' => 'px',
226 'placeholder' => $this->default_values['height'],
227 )
228 );
229 }
230
231 /**
232 * load_value
233 *
234 * Filters the value loaded from the database.
235 *
236 * @date 16/10/19
237 * @since 5.8.1
238 *
239 * @param mixed $value The value loaded from the database.
240 * @param mixed $post_id The post ID where the value is saved.
241 * @param array $field The field settings array.
242 * @return (array|false)
243 */
244 function load_value( $value, $post_id, $field ) {
245
246 // Ensure value is an array.
247 if ( $value ) {
248 return wp_parse_args(
249 $value,
250 array(
251 'address' => '',
252 'lat' => 0,
253 'lng' => 0,
254 )
255 );
256 }
257
258 // Return default.
259 return false;
260 }
261
262
263 /**
264 * This filter is appied to the $value before it is updated in the db
265 *
266 * @type filter
267 * @since 3.6
268 * @date 23/01/13
269 *
270 * @param $value - the value which will be saved in the database
271 * @param $post_id - the post_id of which the value will be saved
272 * @param $field - the field array holding all the field options
273 *
274 * @return $value - the modified value
275 */
276 function update_value( $value, $post_id, $field ) {
277
278 // decode JSON string.
279 if ( is_string( $value ) ) {
280 $value = json_decode( wp_unslash( $value ), true );
281 }
282
283 // Ensure value is an array.
284 if ( $value ) {
285 return (array) $value;
286 }
287
288 // Return default.
289 return false;
290 }
291
292 /**
293 * Return the schema array for the REST API.
294 *
295 * @param array $field
296 * @return array
297 */
298 public function get_rest_schema( array $field ) {
299 return array(
300 'type' => array( 'object', 'null' ),
301 'required' => ! empty( $field['required'] ),
302 'properties' => array(
303 'address' => array(
304 'type' => 'string',
305 ),
306 'lat' => array(
307 'type' => array( 'string', 'float' ),
308 ),
309 'lng' => array(
310 'type' => array( 'string', 'float' ),
311 ),
312 'zoom' => array(
313 'type' => array( 'string', 'int' ),
314 ),
315 'place_id' => array(
316 'type' => 'string',
317 ),
318 'name' => array(
319 'type' => 'string',
320 ),
321 'street_number' => array(
322 'type' => array( 'string', 'int' ),
323 ),
324 'street_name' => array(
325 'type' => 'string',
326 ),
327 'street_name_short' => array(
328 'type' => 'string',
329 ),
330 'city' => array(
331 'type' => 'string',
332 ),
333 'state' => array(
334 'type' => 'string',
335 ),
336 'state_short' => array(
337 'type' => 'string',
338 ),
339 'post_code' => array(
340 'type' => array( 'string', 'int' ),
341 ),
342 'country' => array(
343 'type' => 'string',
344 ),
345 'country_short' => array(
346 'type' => 'string',
347 ),
348 ),
349 );
350 }
351
352 /**
353 * Apply basic formatting to prepare the value for default REST output.
354 *
355 * @param mixed $value
356 * @param string|integer $post_id
357 * @param array $field
358 * @return mixed
359 */
360 public function format_value_for_rest( $value, $post_id, array $field ) {
361 if ( ! $value ) {
362 return null;
363 }
364
365 return acf_format_numerics( $value );
366 }
367 }
368
369
370 // initialize
371 acf_register_field_type( 'acf_field_google_map' );
372 endif; // class_exists check
373
374 ?>
375