class-acf-field-google-map.php 8.5 KB

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