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