|
1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
|
|
3
|
|
|
use Automattic\Jetpack\Assets; |
|
4
|
|
|
|
|
5
|
|
|
if ( ! class_exists( 'Jetpack_Contact_Info_Widget' ) ) { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Register Contact_Info_Widget widget |
|
9
|
|
|
*/ |
|
10
|
|
|
function jetpack_contact_info_widget_init() { |
|
11
|
|
|
register_widget( 'Jetpack_Contact_Info_Widget' ); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
add_action( 'widgets_init', 'jetpack_contact_info_widget_init' ); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Makes a custom Widget for displaying Restaurant Location/Map, Hours, and Contact Info available. |
|
18
|
|
|
* |
|
19
|
|
|
* @package WordPress |
|
20
|
|
|
*/ |
|
21
|
|
|
class Jetpack_Contact_Info_Widget extends WP_Widget { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct() { |
|
27
|
|
|
global $pagenow; |
|
28
|
|
|
|
|
29
|
|
|
$widget_ops = array( |
|
30
|
|
|
'classname' => 'widget_contact_info', |
|
31
|
|
|
'description' => __( 'Display a map with your location, hours, and contact information.', 'jetpack' ), |
|
32
|
|
|
'customize_selective_refresh' => true, |
|
33
|
|
|
); |
|
34
|
|
|
parent::__construct( |
|
35
|
|
|
'widget_contact_info', |
|
36
|
|
|
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
|
37
|
|
|
apply_filters( 'jetpack_widget_name', __( 'Contact Info & Map', 'jetpack' ) ), |
|
38
|
|
|
$widget_ops |
|
39
|
|
|
); |
|
40
|
|
|
$this->alt_option_name = 'widget_contact_info'; |
|
41
|
|
|
|
|
42
|
|
View Code Duplication |
if ( is_customize_preview() ) { |
|
43
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
44
|
|
|
} elseif ( 'widgets.php' === $pagenow ) { |
|
45
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
add_action( 'wp_ajax_customize-contact-info-api-key', array( $this, 'ajax_check_api_key' ) ); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Enqueue scripts and styles. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function enqueue_scripts() { |
|
55
|
|
|
wp_enqueue_style( |
|
56
|
|
|
'contact-info-map-css', |
|
57
|
|
|
plugins_url( 'contact-info/contact-info-map.css', __FILE__ ), |
|
58
|
|
|
array(), |
|
59
|
|
|
JETPACK__VERSION |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Return an associative array of default values |
|
66
|
|
|
* |
|
67
|
|
|
* These values are used in new widgets. |
|
68
|
|
|
* |
|
69
|
|
|
* @return array Array of default values for the Widget's options |
|
70
|
|
|
*/ |
|
71
|
|
|
public function defaults() { |
|
72
|
|
|
return array( |
|
73
|
|
|
'title' => __( 'Hours & Info', 'jetpack' ), |
|
74
|
|
|
'address' => __( "3999 Mission Boulevard,\nSan Diego CA 92109", 'jetpack' ), |
|
75
|
|
|
'phone' => _x( '1-202-555-1212', 'Example of a phone number', 'jetpack' ), |
|
76
|
|
|
'hours' => __( "Lunch: 11am - 2pm \nDinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am", 'jetpack' ), |
|
77
|
|
|
'email' => null, |
|
78
|
|
|
'showmap' => 0, |
|
79
|
|
|
'apikey' => null, |
|
80
|
|
|
'goodmap' => null, |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Outputs the HTML for this widget. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $args An array of standard parameters for widgets in this theme. |
|
88
|
|
|
* @param array $instance An array of settings for this widget instance. |
|
89
|
|
|
* |
|
90
|
|
|
* @return void Echoes it's output |
|
91
|
|
|
**/ |
|
92
|
|
|
public function widget( $args, $instance ) { |
|
93
|
|
|
$instance = wp_parse_args( $instance, $this->defaults() ); |
|
94
|
|
|
|
|
95
|
|
|
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
96
|
|
|
|
|
97
|
|
View Code Duplication |
if ( '' !== $instance['title'] ) { |
|
98
|
|
|
echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Fires at the beginning of the Contact Info widget, after the title. |
|
103
|
|
|
* |
|
104
|
|
|
* @module widgets |
|
105
|
|
|
* |
|
106
|
|
|
* @since 3.9.2 |
|
107
|
|
|
*/ |
|
108
|
|
|
do_action( 'jetpack_contact_info_widget_start' ); |
|
109
|
|
|
|
|
110
|
|
|
echo '<div itemscope itemtype="http://schema.org/LocalBusiness">'; |
|
111
|
|
|
|
|
112
|
|
|
if ( '' !== $instance['address'] ) { |
|
113
|
|
|
|
|
114
|
|
|
$showmap = $instance['showmap']; |
|
115
|
|
|
$goodmap = isset( $instance['goodmap'] ) ? $instance['goodmap'] : $this->has_good_map( $instance ); |
|
116
|
|
|
|
|
117
|
|
|
if ( $showmap && true === $goodmap ) { |
|
118
|
|
|
/** |
|
119
|
|
|
* Set a Google Maps API Key. |
|
120
|
|
|
* |
|
121
|
|
|
* @since 4.1.0 |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $api_key Google Maps API Key |
|
124
|
|
|
*/ |
|
125
|
|
|
$api_key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] ); |
|
126
|
|
|
echo $this->build_map( $instance['address'], $api_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
127
|
|
|
} elseif ( $showmap && is_customize_preview() && true !== $goodmap ) { |
|
128
|
|
|
printf( |
|
129
|
|
|
'<span class="contact-map-api-error" style="display: block;">%s</span>', |
|
130
|
|
|
esc_html( $instance['goodmap'] ) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$map_link = $this->build_map_link( $instance['address'] ); |
|
135
|
|
|
|
|
136
|
|
|
printf( |
|
137
|
|
|
'<div class="confit-address" itemscope itemtype="http://schema.org/PostalAddress" itemprop="address"><a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a></div>', |
|
138
|
|
|
esc_url( $map_link ), |
|
139
|
|
|
str_replace( "\n", '<br/>', esc_html( $instance['address'] ) ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ( '' !== $instance['phone'] ) { |
|
144
|
|
|
if ( wp_is_mobile() ) { |
|
145
|
|
|
echo '<div class="confit-phone"><span itemprop="telephone"><a href="' . esc_url( 'tel:' . $instance['phone'] ) . '">' . esc_html( $instance['phone'] ) . '</a></span></div>'; |
|
146
|
|
|
} else { |
|
147
|
|
|
echo '<div class="confit-phone"><span itemprop="telephone">' . esc_html( $instance['phone'] ) . '</span></div>'; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ( is_email( trim( $instance['email'] ) ) ) { |
|
152
|
|
|
printf( |
|
153
|
|
|
'<div class="confit-email"><a href="mailto:%1$s">%1$s</a></div>', |
|
154
|
|
|
esc_html( $instance['email'] ) |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if ( '' !== $instance['hours'] ) { |
|
159
|
|
|
printf( |
|
160
|
|
|
'<div class="confit-hours" itemprop="openingHours"></div>', |
|
161
|
|
|
str_replace( "\n", '<br/>', esc_html( $instance['hours'] ) ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
echo '</div>'; |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Fires at the end of Contact Info widget. |
|
169
|
|
|
* |
|
170
|
|
|
* @module widgets |
|
171
|
|
|
* |
|
172
|
|
|
* @since 3.9.2 |
|
173
|
|
|
*/ |
|
174
|
|
|
do_action( 'jetpack_contact_info_widget_end' ); |
|
175
|
|
|
|
|
176
|
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
177
|
|
|
|
|
178
|
|
|
/** This action is documented in modules/widgets/gravatar-profile.php */ |
|
179
|
|
|
do_action( 'jetpack_stats_extra', 'widget_view', 'contact_info' ); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Deals with the settings when they are saved by the admin. Here is |
|
185
|
|
|
* where any validation should be dealt with. |
|
186
|
|
|
* |
|
187
|
|
|
* @param array $new_instance New configuration values. |
|
188
|
|
|
* @param array $old_instance Old configuration values. |
|
189
|
|
|
* |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
|
|
public function update( $new_instance, $old_instance ) { |
|
193
|
|
|
|
|
194
|
|
|
$instance = array(); |
|
195
|
|
|
$instance['title'] = wp_kses( $new_instance['title'], array() ); |
|
196
|
|
|
$instance['address'] = wp_kses( $new_instance['address'], array() ); |
|
197
|
|
|
$instance['phone'] = wp_kses( $new_instance['phone'], array() ); |
|
198
|
|
|
$instance['email'] = wp_kses( $new_instance['email'], array() ); |
|
199
|
|
|
$instance['hours'] = wp_kses( $new_instance['hours'], array() ); |
|
200
|
|
|
$instance['apikey'] = wp_kses( isset( $new_instance['apikey'] ) ? $new_instance['apikey'] : $old_instance['apikey'], array() ); |
|
201
|
|
|
|
|
202
|
|
|
if ( ! isset( $new_instance['showmap'] ) ) { |
|
203
|
|
|
$instance['showmap'] = 0; |
|
204
|
|
|
} else { |
|
205
|
|
|
$instance['showmap'] = intval( $new_instance['showmap'] ); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$instance['goodmap'] = $this->update_goodmap( $old_instance, $instance ); |
|
209
|
|
|
|
|
210
|
|
|
return $instance; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Displays the form for this widget on the Widgets page of the WP Admin area. |
|
216
|
|
|
* |
|
217
|
|
|
* @param array $instance Instance configuration. |
|
218
|
|
|
* |
|
219
|
|
|
* @return void |
|
220
|
|
|
*/ |
|
221
|
|
|
public function form( $instance ) { |
|
222
|
|
|
$instance = wp_parse_args( $instance, $this->defaults() ); |
|
223
|
|
|
/** This filter is documented in modules/widgets/contact-info.php */ |
|
224
|
|
|
$apikey = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] ); |
|
225
|
|
|
|
|
226
|
|
|
wp_enqueue_script( |
|
227
|
|
|
'contact-info-admin', |
|
228
|
|
|
Assets::get_file_url_for_environment( |
|
229
|
|
|
'_inc/build/widgets/contact-info/contact-info-admin.min.js', |
|
230
|
|
|
'modules/widgets/contact-info/contact-info-admin.js' |
|
231
|
|
|
), |
|
232
|
|
|
array( 'jquery' ), |
|
233
|
|
|
20160727, |
|
234
|
|
|
false |
|
235
|
|
|
); |
|
236
|
|
|
|
|
237
|
|
|
if ( is_customize_preview() ) { |
|
238
|
|
|
$customize_contact_info_api_key_nonce = wp_create_nonce( 'customize_contact_info_api_key' ); |
|
239
|
|
|
wp_localize_script( |
|
240
|
|
|
'contact-info-admin', |
|
241
|
|
|
'contact_info_api_key_ajax_obj', |
|
242
|
|
|
array( 'nonce' => $customize_contact_info_api_key_nonce ) |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
?> |
|
247
|
|
|
<p> |
|
248
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label> |
|
249
|
|
|
<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'] ); ?>" /> |
|
250
|
|
|
</p> |
|
251
|
|
|
|
|
252
|
|
|
<p> |
|
253
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label> |
|
254
|
|
|
<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> |
|
255
|
|
|
|
|
256
|
|
|
<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 ); ?> /> |
|
257
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>"><?php esc_html_e( 'Show map', 'jetpack' ); ?></label> |
|
258
|
|
|
</p> |
|
259
|
|
|
|
|
260
|
|
|
<p class="jp-contact-info-admin-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>"> |
|
261
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>"> |
|
262
|
|
|
<?php esc_html_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( $apikey ); ?>" /> |
|
264
|
|
|
<br /> |
|
265
|
|
|
<small> |
|
266
|
|
|
<?php |
|
267
|
|
|
printf( |
|
268
|
|
|
wp_kses( |
|
269
|
|
|
/* Translators: placeholder is a URL to support documentation. */ |
|
270
|
|
|
__( '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.', 'jetpack' ), |
|
271
|
|
|
array( |
|
272
|
|
|
'a' => array( |
|
273
|
|
|
'href' => true, |
|
274
|
|
|
), |
|
275
|
|
|
) |
|
276
|
|
|
), |
|
277
|
|
|
'https://jetpack.com/support/extra-sidebar-widgets/contact-info-widget/' |
|
278
|
|
|
); |
|
279
|
|
|
?> |
|
280
|
|
|
</small> |
|
281
|
|
|
</label> |
|
282
|
|
|
</p> |
|
283
|
|
|
|
|
284
|
|
|
<p class="jp-contact-info-admin-map jp-contact-info-embed-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>"> |
|
285
|
|
|
<?php |
|
286
|
|
|
if ( ! is_customize_preview() && true === $instance['goodmap'] ) { |
|
287
|
|
|
echo $this->build_map( $instance['address'], $apikey ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
|
288
|
|
|
} elseif ( true !== $instance['goodmap'] && ! empty( $instance['goodmap'] ) ) { |
|
289
|
|
|
printf( |
|
290
|
|
|
'<span class="notice notice-warning" style="display: block;">%s</span>', |
|
291
|
|
|
esc_html( $instance['goodmap'] ) |
|
292
|
|
|
); |
|
293
|
|
|
} |
|
294
|
|
|
?> |
|
295
|
|
|
</p> |
|
296
|
|
|
|
|
297
|
|
|
<p> |
|
298
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label> |
|
299
|
|
|
<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'] ); ?>" /> |
|
300
|
|
|
</p> |
|
301
|
|
|
|
|
302
|
|
|
<p> |
|
303
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>"><?php esc_html_e( 'Email Address:', 'jetpack' ); ?></label> |
|
304
|
|
|
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'email' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['email'] ); ?>" /> |
|
305
|
|
|
</p> |
|
306
|
|
|
|
|
307
|
|
|
<p> |
|
308
|
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label> |
|
309
|
|
|
<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> |
|
310
|
|
|
</p> |
|
311
|
|
|
|
|
312
|
|
|
<?php |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* Generate a Google Maps link for the supplied address. |
|
318
|
|
|
* |
|
319
|
|
|
* @param string $address Address to link to. |
|
320
|
|
|
* |
|
321
|
|
|
* @return string |
|
322
|
|
|
*/ |
|
323
|
|
|
private function build_map_link( $address ) { |
|
324
|
|
|
// Google map urls have lots of available params but zoom (z) and query (q) are enough. |
|
325
|
|
|
return 'https://maps.google.com/maps?z=16&q=' . $this->urlencode_address( $address ); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
|
|
329
|
|
|
/** |
|
330
|
|
|
* Builds map display HTML code from the supplied address. |
|
331
|
|
|
* |
|
332
|
|
|
* @param string $address Address. |
|
333
|
|
|
* @param string $api_key API Key. |
|
|
|
|
|
|
334
|
|
|
* |
|
335
|
|
|
* @return string HTML of the map. |
|
336
|
|
|
*/ |
|
337
|
|
|
private function build_map( $address, $api_key = null ) { |
|
338
|
|
|
$this->enqueue_scripts(); |
|
339
|
|
|
$src = add_query_arg( 'q', rawurlencode( $address ), 'https://www.google.com/maps/embed/v1/place' ); |
|
340
|
|
|
if ( ! empty( $api_key ) ) { |
|
341
|
|
|
$src = add_query_arg( 'key', $api_key, $src ); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
$height = 216; |
|
345
|
|
|
|
|
346
|
|
|
$iframe_attributes = sprintf( |
|
347
|
|
|
' height="%d" frameborder="0" src="%s" class="contact-map"', |
|
348
|
|
|
esc_attr( $height ), |
|
349
|
|
|
esc_url( $src ) |
|
350
|
|
|
); |
|
351
|
|
|
|
|
352
|
|
|
$iframe_html = sprintf( '<iframe width="600" %s></iframe>', $iframe_attributes ); |
|
353
|
|
|
|
|
354
|
|
|
if ( |
|
355
|
|
|
! class_exists( 'Jetpack_AMP_Support' ) |
|
356
|
|
|
|| ! Jetpack_AMP_Support::is_amp_request() |
|
357
|
|
|
) { |
|
358
|
|
|
return $iframe_html; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
$amp_iframe_html = sprintf( '<amp-iframe layout="fixed-height" width="auto" sandbox="allow-scripts allow-same-origin" %s>', $iframe_attributes ); |
|
362
|
|
|
|
|
363
|
|
|
// Add placeholder to avoid AMP error: <amp-iframe> elements must be positioned outside the first 75% of the viewport or 600px from the top (whichever is smaller). |
|
364
|
|
|
$amp_iframe_html .= sprintf( '<span placeholder>%s</span>', esc_html__( 'Loading map…', 'jetpack' ) ); |
|
365
|
|
|
|
|
366
|
|
|
// Add original iframe as fallback in case JavaScript is disabled. |
|
367
|
|
|
$amp_iframe_html .= sprintf( '<noscript>%s</noscript>', $iframe_html ); |
|
368
|
|
|
|
|
369
|
|
|
$amp_iframe_html .= '</amp-iframe>'; |
|
370
|
|
|
return $amp_iframe_html; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* Encode an URL |
|
375
|
|
|
* |
|
376
|
|
|
* @param string $address The URL to encode. |
|
377
|
|
|
* |
|
378
|
|
|
* @return string The encoded URL |
|
379
|
|
|
*/ |
|
380
|
|
|
private function urlencode_address( $address ) { |
|
381
|
|
|
|
|
382
|
|
|
$address = strtolower( $address ); |
|
383
|
|
|
// Get rid of any unwanted whitespace. |
|
384
|
|
|
$address = preg_replace( '/\s+/', ' ', trim( $address ) ); |
|
385
|
|
|
// Use + not %20. |
|
386
|
|
|
$address = str_ireplace( ' ', '+', $address ); |
|
387
|
|
|
return rawurlencode( $address ); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* Returns the instance's updated 'goodmap' value. |
|
392
|
|
|
* |
|
393
|
|
|
* @param array $old_instance Old configuration values. |
|
394
|
|
|
* @param array $instance Current configuration values. |
|
395
|
|
|
* |
|
396
|
|
|
* @return bool|string The instance's updated 'goodmap' value. The value is true if |
|
397
|
|
|
* $instance can display a good map. If not, returns an error message. |
|
398
|
|
|
*/ |
|
399
|
|
|
private function update_goodmap( $old_instance, $instance ) { |
|
400
|
|
|
/* |
|
401
|
|
|
* If we have no address or don't want to show a map, |
|
402
|
|
|
* no need to check if the map is valid. |
|
403
|
|
|
*/ |
|
404
|
|
|
if ( empty( $instance['address'] ) || 0 === $instance['showmap'] ) { |
|
405
|
|
|
return false; |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/* |
|
409
|
|
|
* If there have been any changes that may impact the map in the widget |
|
410
|
|
|
* (adding an address, address changes, new API key, API key change) |
|
411
|
|
|
* then we want to check whether our map can be displayed again. |
|
412
|
|
|
*/ |
|
413
|
|
|
if ( |
|
414
|
|
|
! isset( $instance['goodmap'] ) |
|
415
|
|
|
|| ! isset( $old_instance['address'] ) |
|
416
|
|
|
|| $this->urlencode_address( $old_instance['address'] ) !== $this->urlencode_address( $instance['address'] ) |
|
417
|
|
|
|| ! isset( $old_instance['apikey'] ) |
|
418
|
|
|
|| $old_instance['apikey'] !== $instance['apikey'] |
|
419
|
|
|
) { |
|
420
|
|
|
return $this->has_good_map( $instance ); |
|
421
|
|
|
} else { |
|
422
|
|
|
return $instance['goodmap']; |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
|
|
/** |
|
427
|
|
|
* Check if the instance has a valid Map location. |
|
428
|
|
|
* |
|
429
|
|
|
* @param array $instance Widget instance configuration. |
|
430
|
|
|
* |
|
431
|
|
|
* @return bool|string Whether or not there is a valid map. If not, return an error message. |
|
432
|
|
|
*/ |
|
433
|
|
|
private function has_good_map( $instance ) { |
|
434
|
|
|
/** This filter is documented in modules/widgets/contact-info.php */ |
|
435
|
|
|
$api_key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] ); |
|
436
|
|
|
if ( ! empty( $api_key ) ) { |
|
437
|
|
|
$path = add_query_arg( |
|
438
|
|
|
array( |
|
439
|
|
|
'q' => rawurlencode( $instance['address'] ), |
|
440
|
|
|
'key' => $api_key, |
|
441
|
|
|
), |
|
442
|
|
|
'https://www.google.com/maps/embed/v1/place' |
|
443
|
|
|
); |
|
444
|
|
|
$wp_remote_get_args = array( |
|
445
|
|
|
'headers' => array( 'Referer' => home_url() ), |
|
446
|
|
|
); |
|
447
|
|
|
$response = wp_remote_get( esc_url_raw( $path ), $wp_remote_get_args ); |
|
448
|
|
|
|
|
449
|
|
|
if ( 200 === wp_remote_retrieve_response_code( $response ) ) { |
|
450
|
|
|
return true; |
|
451
|
|
|
} else { |
|
452
|
|
|
return wp_remote_retrieve_body( $response ); |
|
453
|
|
|
} |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
return __( 'Please enter a valid Google API Key.', 'jetpack' ); |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* Check the Google Maps API key after an Ajax call from the widget's admin form in |
|
461
|
|
|
* the Customizer preview. |
|
462
|
|
|
*/ |
|
463
|
|
|
public function ajax_check_api_key() { |
|
464
|
|
|
if ( isset( $_POST['apikey'] ) ) { |
|
465
|
|
|
if ( check_ajax_referer( 'customize_contact_info_api_key' ) && current_user_can( 'customize' ) ) { |
|
466
|
|
|
$apikey = wp_kses( $_POST['apikey'], array() ); |
|
467
|
|
|
$default_instance = $this->defaults(); |
|
468
|
|
|
$default_instance['apikey'] = $apikey; |
|
469
|
|
|
wp_send_json( array( 'result' => esc_html( $this->has_good_map( $default_instance ) ) ) ); |
|
470
|
|
|
} |
|
471
|
|
|
} else { |
|
472
|
|
|
wp_die(); |
|
473
|
|
|
} |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
} |
|
479
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.