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