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