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