/modules/widgets/contact-info.php

  1. <?php 
  2.  
  3. if ( ! class_exists( 'Jetpack_Contact_Info_Widget' ) ) { 
  4.  
  5. //register Contact_Info_Widget widget 
  6. function jetpack_contact_info_widget_init() { 
  7. register_widget( 'Jetpack_Contact_Info_Widget' ); 
  8.  
  9. add_action( 'widgets_init', 'jetpack_contact_info_widget_init' ); 
  10.  
  11. /** 
  12. * Makes a custom Widget for displaying Resturant Location, Hours and Contact Info available. 
  13. * 
  14. * @package WordPress 
  15. */ 
  16. class Jetpack_Contact_Info_Widget extends WP_Widget { 
  17.  
  18. /** 
  19. * Constructor 
  20. * 
  21. * @return void 
  22. **/ 
  23. function __construct() { 
  24. $widget_ops = array( 
  25. 'classname' => 'widget_contact_info',  
  26. 'description' => __( 'Display your location, hours, and contact information.', 'jetpack' ) 
  27. ); 
  28. parent::__construct( 
  29. 'widget_contact_info',  
  30. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  31. apply_filters( 'jetpack_widget_name', __( 'Contact Info', 'jetpack' ) ),  
  32. $widget_ops 
  33. ); 
  34. $this->alt_option_name = 'widget_contact_info'; 
  35.  
  36.  
  37. /** 
  38. * Return an associative array of default values 
  39. * 
  40. * These values are used in new widgets. 
  41. * 
  42. * @return array Array of default values for the Widget's options 
  43. */ 
  44. public function defaults() { 
  45. return array( 
  46. 'title' => __( 'Hours & Info', 'jetpack' ),  
  47. 'address' => __( "3999 Mission Boulevard, \nSan Diego CA 92109", 'jetpack' ),  
  48. 'phone' => _x( '1-202-555-1212', 'Example of a phone number', 'jetpack' ),  
  49. 'hours' => __( "Lunch: 11am - 2pm \nDinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am", 'jetpack' ),  
  50. 'showmap' => 1,  
  51. 'lat' => null,  
  52. 'lon' => null 
  53. ); 
  54. /** 
  55. * Outputs the HTML for this widget. 
  56. * 
  57. * @param array An array of standard parameters for widgets in this theme 
  58. * @param array An array of settings for this widget instance 
  59. * @return void Echoes it's output 
  60. **/ 
  61. function widget( $args, $instance ) { 
  62. $instance = wp_parse_args( $instance, $this->defaults() ); 
  63.  
  64. extract( $args, EXTR_SKIP ); 
  65.  
  66. echo $before_widget; 
  67.  
  68. if ( $instance['title'] != '' ) 
  69. echo $before_title . $instance['title'] . $after_title; 
  70.  
  71.  
  72. $map_link = 0; 
  73.  
  74.  
  75. if ( $instance['address'] != '' ) { 
  76.  
  77. $showmap = $instance['showmap']; 
  78.  
  79. if ( $showmap && $this->has_good_map( $instance ) ) { 
  80.  
  81. $lat = $instance['lat']; 
  82. $lon = $instance['lon']; 
  83.  
  84. echo $this->build_map( $lat, $lon ); 
  85.  
  86. $map_link = $this->build_map_link( $instance['address'] ); 
  87.  
  88. echo '<div class="confit-address"><a href="' . esc_url( $map_link ) . '" target="_blank">' . str_replace( "\n", "<br/>", esc_html( $instance['address'] ) ) . "</a></div>"; 
  89.  
  90.  
  91.  
  92.  
  93. if ( $instance['phone'] != '' ) { 
  94.  
  95. if( wp_is_mobile() ) { 
  96. echo '<div class="confit-phone"><a href="'. esc_url( 'tel:'. $instance['phone'] ) . '">' . esc_html( $instance['phone'] ) . "</a></div>"; 
  97. } else { 
  98. echo '<div class="confit-phone">' . esc_html( $instance['phone'] ) . '</div>'; 
  99.  
  100.  
  101.  
  102. if ( $instance['hours'] != '' ) { 
  103. echo '<div class="confit-hours">' . str_replace( "\n", "<br/>", esc_html( $instance['hours'] ) ) . "</div>"; 
  104.  
  105.  
  106. echo $after_widget; 
  107.  
  108.  
  109.  
  110. /** 
  111. * Deals with the settings when they are saved by the admin. Here is 
  112. * where any validation should be dealt with. 
  113. **/ 
  114. function update( $new_instance, $old_instance ) { 
  115. $update_lat_lon = false; 
  116. if ( $this->urlencode_address( $old_instance['address'] ) != $this->urlencode_address( $new_instance['address'] ) ) { 
  117. $update_lat_lon = true; 
  118.  
  119. $instance = array(); 
  120. $instance['title'] = wp_kses( $new_instance['title'], array() ); 
  121. $instance['address'] = wp_kses( $new_instance['address'], array() ); 
  122. $instance['phone'] = wp_kses( $new_instance['phone'], array() ); 
  123. $instance['hours'] = wp_kses( $new_instance['hours'], array() ); 
  124. $instance['lat'] = isset( $old_instance['lat'] ) ? floatval( $old_instance['lat'] ) : 0; 
  125. $instance['lon'] = isset( $old_instance['lon'] ) ? floatval( $old_instance['lon'] ) : 0; 
  126.  
  127. if ( ! $instance['lat'] || ! $instance['lon'] ) { 
  128. $update_lat_lon = true; 
  129.  
  130. if ( $instance['address'] && $update_lat_lon ) { 
  131.  
  132. // Get the lat/lon of the user specified address. 
  133. $address = $this->urlencode_address( $instance['address'] ); 
  134. $path = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . $address; 
  135. $json = wp_remote_retrieve_body( wp_remote_get( $path ) ); 
  136.  
  137. if ( ! $json ) { 
  138. // The read failed :( 
  139. esc_html_e( "There was a problem getting the data to display this address on a map. Please refresh your browser and try again.", 'jetpack' ); 
  140. die(); 
  141.  
  142. $json_obj = json_decode( $json ); 
  143.  
  144. if ( $err = $json_obj->status == "ZERO_RESULTS" ) { 
  145. // The address supplied does not have a matching lat / lon. 
  146. // No map is available. 
  147. $instance['lat'] = "0"; 
  148. $instance['lon'] = "0"; 
  149. } else { 
  150.  
  151. $loc = $json_obj->results[0]->geometry->location; 
  152.  
  153. $lat = floatval( $loc->lat ); 
  154. $lon = floatval( $loc->lng ); 
  155.  
  156. $instance['lat'] = "$lat"; 
  157. $instance['lon'] = "$lon"; 
  158.  
  159. if ( ! isset( $new_instance['showmap'] ) ) { 
  160. $instance['showmap'] = 0; 
  161. } else { 
  162. $instance['showmap'] = intval( $new_instance['showmap'] ); 
  163.  
  164. return $instance; 
  165.  
  166.  
  167. /** 
  168. * Displays the form for this widget on the Widgets page of the WP Admin area. 
  169. **/ 
  170. function form( $instance ) { 
  171. $instance = wp_parse_args( $instance, $this->defaults() ); 
  172. extract( $instance ); 
  173.  
  174. $disabled = !$this->has_good_map( $instance ); 
  175. ?> 
  176. <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> 
  177.  
  178. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p> 
  179.  
  180. <p><label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label> 
  181. <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'address' ) ); ?>"><?php echo esc_textarea( $address ); ?></textarea> 
  182. <?php 
  183. if ( $this->has_good_map( $instance ) ) { 
  184. ?> 
  185. <input class="" id="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'showmap' ) ); ?>" value="1" type="checkbox" <?php checked( $showmap , 1); ?> /> 
  186. <label for="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>"><?php esc_html_e( 'Show map', 'jetpack' ); ?></label></p> 
  187. <?php 
  188. } else { 
  189. ?> 
  190. <span class="error-message"><?php _e( 'Sorry. We can not plot this address. A map will not be displayed. Is the address formatted correctly?', 'jetpack' ); ?></span></p> 
  191. <input id="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'showmap' ) ); ?>" value="<?php echo( intval( $instance['showmap'] ) ); ?>" type="hidden" /> 
  192. <?php 
  193. ?> 
  194.  
  195. <p><label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label> 
  196. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'phone' ) ); ?>" type="text" value="<?php echo esc_attr( $phone ); ?>" /></p> 
  197.  
  198. <p><label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label> 
  199.  
  200. <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hours' ) ); ?>"><?php echo esc_textarea( $hours ); ?></textarea></p> 
  201.  
  202. <?php 
  203.  
  204.  
  205. function build_map_link( $address ) { 
  206. // Google map urls have lots of available params but zoom (z) and query (q) are enough. 
  207. return "http://maps.google.com/maps?z=16&q=" . $this->urlencode_address( $address ); 
  208.  
  209.  
  210. function build_map( $lat, $lon ) { 
  211.  
  212. wp_enqueue_script( "jquery" ); 
  213. wp_enqueue_script( "google-maps", "https://maps.googleapis.com/maps/api/js?sensor=false" ); 
  214. wp_enqueue_script( "contact-info-map-js", plugins_url( 'contact-info/contact-info-map.js', __FILE__ ), array( 'jquery', 'google-maps' ), 20150127 ); 
  215. wp_enqueue_style( "contact-info-map-css", plugins_url( 'contact-info/contact-info-map.css', __FILE__ ), null, 20150127 ); 
  216.  
  217. $lat = esc_attr( $lat ); 
  218. $lon = esc_attr( $lon ); 
  219. $html = <<<EOT 
  220. <div class="contact-map"> 
  221. <input type="hidden" class="contact-info-map-lat" value="$lat" /> 
  222. <input type="hidden" class="contact-info-map-lon" value="$lon" /> 
  223. <div class="contact-info-map-canvas"></div></div> 
  224. EOT; 
  225.  
  226. return $html; 
  227.  
  228.  
  229. function urlencode_address( $address ) { 
  230.  
  231. $address = strtolower( $address ); 
  232. $address = preg_replace( "/\s+/", " ", trim( $address ) ); // Get rid of any unwanted whitespace 
  233. $address = str_ireplace( " ", "+", $address ); // Use + not %20 
  234. urlencode( $address ); 
  235.  
  236. return $address; 
  237.  
  238.  
  239. function has_good_map( $instance ) { 
  240. // The lat and lon of an address that could not be plotted will have values of 0 and 0. 
  241. return ! ( $instance['lat'] == "0" && $instance['lon'] == "0" ); 
  242.  
  243.  
.