PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/contact-info.php

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