|
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
|
|
|
function __construct() { |
|
23
|
|
|
$widget_ops = array( |
|
24
|
|
|
'classname' => 'widget_contact_info', |
|
25
|
|
|
'description' => __( 'Display your location, hours, and contact information.', 'jetpack' ), |
|
26
|
|
|
'customize_selective_refresh' => true, |
|
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
|
|
|
if ( is_customize_preview() ) { |
|
37
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Enqueue scripts and styles. |
|
43
|
|
|
*/ |
|
44
|
|
|
public function enqueue_scripts() { |
|
45
|
|
|
wp_enqueue_style( 'contact-info-map-css', plugins_url( 'contact-info/contact-info-map.css', __FILE__ ), null, 20160623 ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Return an associative array of default values |
|
51
|
|
|
* |
|
52
|
|
|
* These values are used in new widgets. |
|
53
|
|
|
* |
|
54
|
|
|
* @return array Array of default values for the Widget's options |
|
55
|
|
|
*/ |
|
56
|
|
|
public function defaults() { |
|
57
|
|
|
return array( |
|
58
|
|
|
'title' => __( 'Hours & Info', 'jetpack' ), |
|
59
|
|
|
'address' => __( "3999 Mission Boulevard,\nSan Diego CA 92109", 'jetpack' ), |
|
60
|
|
|
'phone' => _x( '1-202-555-1212', 'Example of a phone number', 'jetpack' ), |
|
61
|
|
|
'hours' => __( "Lunch: 11am - 2pm \nDinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am", 'jetpack' ), |
|
62
|
|
|
'showmap' => 1, |
|
63
|
|
|
'apikey' => null, |
|
64
|
|
|
'lat' => null, |
|
65
|
|
|
'lon' => null |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Outputs the HTML for this widget. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $args An array of standard parameters for widgets in this theme |
|
73
|
|
|
* @param array $instance An array of settings for this widget instance |
|
74
|
|
|
* |
|
75
|
|
|
* @return void Echoes it's output |
|
76
|
|
|
**/ |
|
77
|
|
|
function widget( $args, $instance ) { |
|
78
|
|
|
$instance = wp_parse_args( $instance, $this->defaults() ); |
|
79
|
|
|
|
|
80
|
|
|
echo $args['before_widget']; |
|
81
|
|
|
|
|
82
|
|
|
if ( '' != $instance['title'] ) { |
|
83
|
|
|
echo $args['before_title'] . $instance['title'] . $args['after_title']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Fires at the beginning of the Contact Info widget, after the title. |
|
88
|
|
|
* |
|
89
|
|
|
* @module widgets |
|
90
|
|
|
* |
|
91
|
|
|
* @since 3.9.2 |
|
92
|
|
|
*/ |
|
93
|
|
|
do_action( 'jetpack_contact_info_widget_start' ); |
|
94
|
|
|
|
|
95
|
|
|
if ( '' != $instance['address'] ) { |
|
96
|
|
|
|
|
97
|
|
|
$showmap = $instance['showmap']; |
|
98
|
|
|
|
|
99
|
|
|
/** This action is documented in modules/widgets/contact-info.php */ |
|
100
|
|
|
if ( $showmap && $this->has_good_map( $instance ) ) { |
|
101
|
|
|
/** |
|
102
|
|
|
* Set a Google Maps API Key. |
|
103
|
|
|
* |
|
104
|
|
|
* @since 4.1.0 |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $api_key Google Maps API Key |
|
107
|
|
|
*/ |
|
108
|
|
|
$api_key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] ); |
|
109
|
|
|
echo $this->build_map( $instance['address'], $api_key ); |
|
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
|
|
|
if ( '' != $instance['phone'] ) { |
|
118
|
|
|
if ( wp_is_mobile() ) { |
|
119
|
|
|
echo '<div class="confit-phone"><a href="' . esc_url( 'tel:' . $instance['phone'] ) . '">' . esc_html( $instance['phone'] ) . "</a></div>"; |
|
120
|
|
|
} |
|
121
|
|
|
else { |
|
122
|
|
|
echo '<div class="confit-phone">' . esc_html( $instance['phone'] ) . '</div>'; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ( '' != $instance['hours'] ) { |
|
127
|
|
|
echo '<div class="confit-hours">' . str_replace( "\n", "<br/>", esc_html( $instance['hours'] ) ) . "</div>"; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Fires at the end of Contact Info widget. |
|
132
|
|
|
* |
|
133
|
|
|
* @module widgets |
|
134
|
|
|
* |
|
135
|
|
|
* @since 3.9.2 |
|
136
|
|
|
*/ |
|
137
|
|
|
do_action( 'jetpack_contact_info_widget_end' ); |
|
138
|
|
|
|
|
139
|
|
|
echo $args['after_widget']; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Deals with the settings when they are saved by the admin. Here is |
|
145
|
|
|
* where any validation should be dealt with. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $new_instance New configuration values |
|
148
|
|
|
* @param array $old_instance Old configuration values |
|
149
|
|
|
* |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
function update( $new_instance, $old_instance ) { |
|
153
|
|
|
$update_lat_lon = false; |
|
154
|
|
|
if ( |
|
155
|
|
|
! isset( $old_instance['address'] ) || |
|
156
|
|
|
$this->urlencode_address( $old_instance['address'] ) != $this->urlencode_address( $new_instance['address'] ) |
|
157
|
|
|
) { |
|
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['apikey'] = wp_kses( isset( $new_instance['apikey'] ) ? $new_instance['apikey'] : $old_instance['apikey'], array() ); |
|
167
|
|
|
$instance['lat'] = isset( $old_instance['lat'] ) ? floatval( $old_instance['lat'] ) : 0; |
|
168
|
|
|
$instance['lon'] = isset( $old_instance['lon'] ) ? floatval( $old_instance['lon'] ) : 0; |
|
169
|
|
|
|
|
170
|
|
|
if ( ! $instance['lat'] || ! $instance['lon'] ) { |
|
171
|
|
|
$update_lat_lon = true; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ( $instance['address'] && $update_lat_lon ) { |
|
175
|
|
|
|
|
176
|
|
|
// Get the lat/lon of the user specified address. |
|
177
|
|
|
$address = $this->urlencode_address( $instance['address'] ); |
|
178
|
|
|
$path = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . $address; |
|
179
|
|
|
/** This action is documented in modules/widgets/contact-info.php */ |
|
180
|
|
|
$key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] ); |
|
181
|
|
|
|
|
182
|
|
|
if ( ! empty( $key ) ) { |
|
183
|
|
|
$path = add_query_arg( 'key', $key, $path ); |
|
184
|
|
|
} |
|
185
|
|
|
$json = wp_remote_retrieve_body( wp_remote_get( esc_url( $path, null, null ) ) ); |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
if ( ! $json ) { |
|
188
|
|
|
// The read failed :( |
|
189
|
|
|
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' ); |
|
190
|
|
|
die(); |
|
|
|
|
|
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$json_obj = json_decode( $json ); |
|
194
|
|
|
|
|
195
|
|
|
if ( "ZERO_RESULTS" == $json_obj->status ) { |
|
196
|
|
|
// The address supplied does not have a matching lat / lon. |
|
197
|
|
|
// No map is available. |
|
198
|
|
|
$instance['lat'] = "0"; |
|
199
|
|
|
$instance['lon'] = "0"; |
|
200
|
|
|
} |
|
201
|
|
|
else { |
|
202
|
|
|
|
|
203
|
|
|
$loc = $json_obj->results[0]->geometry->location; |
|
204
|
|
|
|
|
205
|
|
|
$lat = floatval( $loc->lat ); |
|
206
|
|
|
$lon = floatval( $loc->lng ); |
|
207
|
|
|
|
|
208
|
|
|
$instance['lat'] = "$lat"; |
|
209
|
|
|
$instance['lon'] = "$lon"; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
if ( ! isset( $new_instance['showmap'] ) ) { |
|
214
|
|
|
$instance['showmap'] = 0; |
|
215
|
|
|
} |
|
216
|
|
|
else { |
|
217
|
|
|
$instance['showmap'] = intval( $new_instance['showmap'] ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return $instance; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Displays the form for this widget on the Widgets page of the WP Admin area. |
|
226
|
|
|
* |
|
227
|
|
|
* @param array $instance Instance configuration. |
|
228
|
|
|
* |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
|
|
function form( $instance ) { |
|
232
|
|
|
$instance = wp_parse_args( $instance, $this->defaults() ); |
|
233
|
|
|
wp_enqueue_script( 'contact-info-admin', plugins_url( 'contact-info/contact-info-admin.js', __FILE__ ), array( 'jquery' ), 20160727 ); |
|
234
|
|
|
|
|
235
|
|
|
?> |
|
236
|
|
|
<p> |
|
237
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
|
238
|
|
|
<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( $instance['title'] ); ?>" /> |
|
239
|
|
|
</p> |
|
240
|
|
|
|
|
241
|
|
|
<p> |
|
242
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label> |
|
243
|
|
|
<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( $instance['address'] ); ?></textarea> |
|
244
|
|
|
<?php |
|
245
|
|
|
if ( $this->has_good_map( $instance ) ) { |
|
246
|
|
|
?> |
|
247
|
|
|
<input class="jp-contact-info-showmap" 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( $instance['showmap'], 1 ); ?> /> |
|
248
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>"><?php esc_html_e( 'Show map', 'jetpack' ); ?></label> |
|
249
|
|
|
<?php |
|
250
|
|
|
} |
|
251
|
|
|
else { |
|
252
|
|
|
?> |
|
253
|
|
|
<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> |
|
254
|
|
|
<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" /> |
|
255
|
|
|
<?php |
|
256
|
|
|
} |
|
257
|
|
|
?> |
|
258
|
|
|
</p> |
|
259
|
|
|
|
|
260
|
|
|
<p class="jp-contact-info-apikey" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>"> |
|
261
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>"> |
|
262
|
|
|
<?php _e( 'Google Maps API Key', 'jetpack' ); ?> |
|
263
|
|
|
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'apikey' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['apikey'] ); ?>" /> |
|
264
|
|
|
<br /> |
|
265
|
|
|
<small><?php printf( wp_kses( __( 'Google now requires an API key to use their maps on your site. <a href="%s">See our documentation</a> for instructions on acquiring a key.' ), array( 'a' => array( 'href' => true ) ) ), 'https://jetpack.com/support/extra-sidebar-widgets/contact-info-widget/' ); ?></small> |
|
266
|
|
|
</label> |
|
267
|
|
|
</p> |
|
268
|
|
|
|
|
269
|
|
|
<p> |
|
270
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label> |
|
271
|
|
|
<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( $instance['phone'] ); ?>" /> |
|
272
|
|
|
</p> |
|
273
|
|
|
|
|
274
|
|
|
<p> |
|
275
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label> |
|
276
|
|
|
<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( $instance['hours'] ); ?></textarea> |
|
277
|
|
|
</p> |
|
278
|
|
|
|
|
279
|
|
|
<?php |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Generate a Google Maps link for the supplied address. |
|
285
|
|
|
* |
|
286
|
|
|
* @param string $address Address to link to. |
|
287
|
|
|
* |
|
288
|
|
|
* @return string |
|
289
|
|
|
*/ |
|
290
|
|
|
function build_map_link( $address ) { |
|
291
|
|
|
// Google map urls have lots of available params but zoom (z) and query (q) are enough. |
|
292
|
|
|
return "https://maps.google.com/maps?z=16&q=" . $this->urlencode_address( $address ); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Builds map display HTML code from the supplied latitude and longitude. |
|
298
|
|
|
* |
|
299
|
|
|
* @param float $lat Map Latitude |
|
|
|
|
|
|
300
|
|
|
* @param float $lon Map Longitude |
|
|
|
|
|
|
301
|
|
|
* |
|
302
|
|
|
* @return string HTML of the map |
|
303
|
|
|
*/ |
|
304
|
|
|
function build_map( $address, $api_key = null ) { |
|
305
|
|
|
$this->enqueue_scripts(); |
|
306
|
|
|
$src = add_query_arg( 'q', urlencode( $address ), 'https://www.google.com/maps/embed/v1/place' ); |
|
307
|
|
|
if ( ! empty( $api_key ) ) { |
|
308
|
|
|
$src = add_query_arg( 'key', $api_key, $src ); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
return '<iframe width="600" height="216" frameborder="0" src="' . esc_url( $src ) . '" class="contact-map"></iframe>'; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Encode an URL |
|
316
|
|
|
* |
|
317
|
|
|
* @param string $address The URL to encode |
|
318
|
|
|
* |
|
319
|
|
|
* @return string The encoded URL |
|
320
|
|
|
*/ |
|
321
|
|
|
function urlencode_address( $address ) { |
|
322
|
|
|
|
|
323
|
|
|
$address = strtolower( $address ); |
|
324
|
|
|
$address = preg_replace( "/\s+/", " ", trim( $address ) ); // Get rid of any unwanted whitespace |
|
325
|
|
|
$address = str_ireplace( " ", "+", $address ); // Use + not %20 |
|
326
|
|
|
urlencode( $address ); |
|
327
|
|
|
|
|
328
|
|
|
return $address; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Check if the instance has a valid Map location. |
|
333
|
|
|
* |
|
334
|
|
|
* @param array $instance Widget instance configuration. |
|
335
|
|
|
* |
|
336
|
|
|
* @return bool Whether or not there is a valid map. |
|
337
|
|
|
*/ |
|
338
|
|
|
function has_good_map( $instance ) { |
|
339
|
|
|
// The lat and lon of an address that could not be plotted will have values of 0 and 0. |
|
340
|
|
|
return ! ( "0" == $instance['lat'] && "0" == $instance['lon'] ); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
} |
|
346
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.