/core/fields/google-map.php

  1. <?php 
  2.  
  3. class acf_field_google_map extends acf_field 
  4. /** 
  5. * __construct 
  6. * 
  7. * Set name / label needed for actions / filters 
  8. * 
  9. * @since 3.6 
  10. * @date 23/01/13 
  11. */ 
  12.  
  13. function __construct() 
  14. // vars 
  15. $this->name = 'google_map'; 
  16. $this->label = __("Google Map", 'acf'); 
  17. $this->category = __("jQuery", 'acf'); 
  18. $this->defaults = array( 
  19. 'height' => '',  
  20. 'center_lat' => '',  
  21. 'center_lng' => '',  
  22. 'zoom' => '' 
  23. ); 
  24. $this->default_values = array( 
  25. 'height' => '400',  
  26. 'center_lat' => '-37.81411',  
  27. 'center_lng' => '144.96328',  
  28. 'zoom' => '14' 
  29. ); 
  30. $this->l10n = array( 
  31. 'locating' => __("Locating", 'acf'),  
  32. 'browser_support' => __("Sorry, this browser does not support geolocation", 'acf'),  
  33. ); 
  34.  
  35.  
  36. // do not delete! 
  37. parent::__construct(); 
  38.  
  39.  
  40. /** 
  41. * create_field() 
  42. * 
  43. * Create the HTML interface for your field 
  44. * 
  45. * @param $field - an array holding all the field's data 
  46. * 
  47. * @type action 
  48. * @since 3.6 
  49. * @date 23/01/13 
  50. */ 
  51.  
  52. function create_field( $field ) 
  53. // require the googlemaps JS ( this script is now lazy loaded via JS ) 
  54. //wp_enqueue_script('acf-googlemaps'); 
  55.  
  56.  
  57. // default value 
  58. if( !is_array($field['value']) ) 
  59. $field['value'] = array(); 
  60.  
  61. $field['value'] = wp_parse_args($field['value'], array( 
  62. 'address' => '',  
  63. 'lat' => '',  
  64. 'lng' => '' 
  65. )); 
  66.  
  67.  
  68. // default options 
  69. foreach( $this->default_values as $k => $v ) 
  70. if( ! $field[ $k ] ) 
  71. $field[ $k ] = $v; 
  72. }  
  73.  
  74.  
  75. // vars 
  76. $o = array( 
  77. 'class' => '',  
  78. ); 
  79.  
  80. if( $field['value']['address'] ) 
  81. $o['class'] = 'active'; 
  82.  
  83.  
  84. $atts = ''; 
  85. $keys = array(  
  86. 'data-id' => 'id',  
  87. 'data-lat' => 'center_lat',  
  88. 'data-lng' => 'center_lng',  
  89. 'data-zoom' => 'zoom' 
  90. ); 
  91.  
  92. foreach( $keys as $k => $v ) 
  93. $atts .= ' ' . $k . '="' . esc_attr( $field[ $v ] ) . '"';  
  94.  
  95. ?> 
  96. <div class="acf-google-map <?php echo $o['class']; ?>" <?php echo $atts; ?>> 
  97.  
  98. <div style="display:none;"> 
  99. <?php foreach( $field['value'] as $k => $v ): ?> 
  100. <input type="hidden" class="input-<?php echo $k; ?>" name="<?php echo esc_attr($field['name']); ?>[<?php echo $k; ?>]" value="<?php echo esc_attr( $v ); ?>" /> 
  101. <?php endforeach; ?> 
  102. </div> 
  103.  
  104. <div class="title"> 
  105.  
  106. <div class="has-value"> 
  107. <a href="#" class="acf-sprite-remove ir" title="<?php _e("Clear location", 'acf'); ?>">Remove</a> 
  108. <h4><?php echo $field['value']['address']; ?></h4> 
  109. </div> 
  110.  
  111. <div class="no-value"> 
  112. <a href="#" class="acf-sprite-locate ir" title="<?php _e("Find current location", 'acf'); ?>">Locate</a> 
  113. <input type="text" placeholder="<?php _e("Search for address...", 'acf'); ?>" class="search" /> 
  114. </div> 
  115.  
  116. </div> 
  117.  
  118. <div class="canvas" style="height: <?php echo $field['height']; ?>px"> 
  119.  
  120. </div> 
  121.  
  122. </div> 
  123. <?php 
  124.  
  125.  
  126.  
  127. /** 
  128. * create_options() 
  129. * 
  130. * Create extra options for your field. This is rendered when editing a field. 
  131. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  132. * 
  133. * @type action 
  134. * @since 3.6 
  135. * @date 23/01/13 
  136. * 
  137. * @param $field - an array holding all the field's data 
  138. */ 
  139.  
  140. function create_options( $field ) 
  141. // vars 
  142. $key = $field['name']; 
  143.  
  144. ?> 
  145. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  146. <td class="label"> 
  147. <label><?php _e("Center", 'acf'); ?></label> 
  148. <p class="description"><?php _e('Center the initial map', 'acf'); ?></p> 
  149. </td> 
  150. <td> 
  151. <ul class="hl clearfix"> 
  152. <li style="width:48%;"> 
  153. <?php  
  154.  
  155. do_action('acf/create_field', array( 
  156. 'type' => 'text',  
  157. 'name' => 'fields['.$key.'][center_lat]',  
  158. 'value' => $field['center_lat'],  
  159. 'prepend' => 'lat',  
  160. 'placeholder' => $this->default_values['center_lat'] 
  161. )); 
  162.  
  163. ?> 
  164. </li> 
  165. <li style="width:48%; margin-left:4%;"> 
  166. <?php  
  167.  
  168. do_action('acf/create_field', array( 
  169. 'type' => 'text',  
  170. 'name' => 'fields['.$key.'][center_lng]',  
  171. 'value' => $field['center_lng'],  
  172. 'prepend' => 'lng',  
  173. 'placeholder' => $this->default_values['center_lng'] 
  174. )); 
  175.  
  176. ?> 
  177. </li> 
  178. </ul> 
  179.  
  180. </td> 
  181. </tr> 
  182. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  183. <td class="label"> 
  184. <label><?php _e("Zoom", 'acf'); ?></label> 
  185. <p class="description"><?php _e('Set the initial zoom level', 'acf'); ?></p> 
  186. </td> 
  187. <td> 
  188. <?php  
  189.  
  190. do_action('acf/create_field', array( 
  191. 'type' => 'number',  
  192. 'name' => 'fields['.$key.'][zoom]',  
  193. 'value' => $field['zoom'],  
  194. 'placeholder' => $this->default_values['zoom'] 
  195. )); 
  196.  
  197. ?> 
  198. </td> 
  199. </tr> 
  200. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  201. <td class="label"> 
  202. <label><?php _e("Height", 'acf'); ?></label> 
  203. <p class="description"><?php _e('Customise the map height', 'acf'); ?></p> 
  204. </td> 
  205. <td> 
  206. <?php  
  207.  
  208. do_action('acf/create_field', array( 
  209. 'type' => 'number',  
  210. 'name' => 'fields['.$key.'][height]',  
  211. 'value' => $field['height'],  
  212. 'append' => 'px',  
  213. 'placeholder' => $this->default_values['height'] 
  214. )); 
  215.  
  216. ?> 
  217. </td> 
  218. </tr> 
  219. <?php 
  220.  
  221.  
  222.  
  223. /** 
  224. * update_value() 
  225. * 
  226. * This filter is appied to the $value before it is updated in the db 
  227. * 
  228. * @type filter 
  229. * @since 3.6 
  230. * @date 23/01/13 
  231. * 
  232. * @param $value - the value which will be saved in the database 
  233. * @param $post_id - the $post_id of which the value will be saved 
  234. * @param $field - the field array holding all the field options 
  235. * 
  236. * @return $value - the modified value 
  237. */ 
  238.  
  239. function update_value( $value, $post_id, $field ) { 
  240.  
  241. if( empty($value) || empty($value['lat']) || empty($value['lng']) ) { 
  242.  
  243. return false; 
  244.  
  245.  
  246.  
  247. // return 
  248. return $value; 
  249.  
  250.  
  251. /** 
  252. * input_admin_footer 
  253. * 
  254. * description 
  255. * 
  256. * @type function 
  257. * @date 6/03/2014 
  258. * @since 5.0.0 
  259. * 
  260. * @param $post_id (int) 
  261. * @return $post_id (int) 
  262. */ 
  263.  
  264. function input_admin_head() { 
  265.  
  266. add_action( 'admin_footer', array( $this, 'input_admin_footer') ); 
  267.  
  268.  
  269. function input_admin_footer() { 
  270.  
  271. // vars 
  272. $api = array( 
  273. 'libraries' => 'places',  
  274. 'key' => '',  
  275. 'client' => '' 
  276. ); 
  277.  
  278.  
  279. // filter 
  280. $api = apply_filters('acf/fields/google_map/api', $api); 
  281.  
  282.  
  283. // remove empty 
  284. if( empty($api['key']) ) unset($api['key']); 
  285. if( empty($api['client']) ) unset($api['client']); 
  286.  
  287.  
  288. ?> 
  289. <script type="text/javascript"> 
  290. acf.fields.google_map.api = <?php echo json_encode($api); ?>; 
  291. </script> 
  292. <?php 
  293.  
  294.  
  295. new acf_field_google_map(); 
  296.  
  297. ?> 
.