acf_field_google_map

The Advanced Custom Fields acf field google map class.

Defined (1)

The class is defined in the following location(s).

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