Jetpack_Contact_Info_Widget

Makes a custom Widget for displaying Restaurant Location/Map, Hours, and Contact Info available.

Defined (1)

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

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