PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.7
Jetpack – WP Security, Backup, Speed, & Growth v9.7
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / widgets / contact-info.php

contact-info.php in Jetpack – WP Security, Backup, Speed, & Growth 9.7, at modules/widgets/contact-info.php

486 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 use Automattic\Jetpack\Assets;
4 use Automattic\Jetpack\Redirect;
5
6 if ( ! class_exists( 'Jetpack_Contact_Info_Widget' ) ) {
7
8 /**
9 * Register Contact_Info_Widget widget
10 */
11 function jetpack_contact_info_widget_init() {
12 register_widget( 'Jetpack_Contact_Info_Widget' );
13 }
14
15 add_action( 'widgets_init', 'jetpack_contact_info_widget_init' );
16
17 /**
18 * Makes a custom Widget for displaying Restaurant Location/Map, Hours, and Contact Info available.
19 *
20 * @package WordPress
21 */
22 class Jetpack_Contact_Info_Widget extends WP_Widget {
23
24 /**
25 * Constructor
26 */
27 public function __construct() {
28 global $pagenow;
29
30 $widget_ops = array(
31 'classname' => 'widget_contact_info',
32 'description' => __( 'Display a map with your location, hours, and contact information.', 'jetpack' ),
33 'customize_selective_refresh' => true,
34 );
35 parent::__construct(
36 'widget_contact_info',
37 /** This filter is documented in modules/widgets/facebook-likebox.php */
38 apply_filters( 'jetpack_widget_name', __( 'Contact Info & Map', 'jetpack' ) ),
39 $widget_ops
40 );
41 $this->alt_option_name = 'widget_contact_info';
42
43 if ( is_customize_preview() ) {
44 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
45 } elseif ( 'widgets.php' === $pagenow ) {
46 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
47 }
48
49 add_action( 'wp_ajax_customize-contact-info-api-key', array( $this, 'ajax_check_api_key' ) );
50 }
51
52 /**
53 * Enqueue scripts and styles.
54 */
55 public function enqueue_scripts() {
56 wp_enqueue_style(
57 'contact-info-map-css',
58 plugins_url( 'contact-info/contact-info-map.css', __FILE__ ),
59 array(),
60 JETPACK__VERSION
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 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">%s</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 * Deals with the settings when they are saved by the admin. Here is
184 * where any validation should be dealt with.
185 *
186 * @param array $new_instance New configuration values.
187 * @param array $old_instance Old configuration values.
188 *
189 * @return array
190 */
191 public function update( $new_instance, $old_instance ) {
192
193 $instance = array();
194 $instance['title'] = wp_kses( $new_instance['title'], array() );
195 $instance['address'] = wp_kses( $new_instance['address'], array() );
196 $instance['phone'] = wp_kses( $new_instance['phone'], array() );
197 $instance['email'] = wp_kses( $new_instance['email'], array() );
198 $instance['hours'] = wp_kses( $new_instance['hours'], array() );
199 $instance['apikey'] = wp_kses( isset( $new_instance['apikey'] ) ? $new_instance['apikey'] : $old_instance['apikey'], array() );
200
201 if ( ! isset( $new_instance['showmap'] ) ) {
202 $instance['showmap'] = 0;
203 } else {
204 $instance['showmap'] = (int) $new_instance['showmap'];
205 }
206
207 $instance['goodmap'] = $this->update_goodmap( $old_instance, $instance );
208
209 return $instance;
210 }
211
212 /**
213 * Displays the form for this widget on the Widgets page of the WP Admin area.
214 *
215 * @param array $instance Instance configuration.
216 *
217 * @return void
218 */
219 public function form( $instance ) {
220 $instance = wp_parse_args( $instance, $this->defaults() );
221 /** This filter is documented in modules/widgets/contact-info.php */
222 $apikey = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
223
224 wp_enqueue_script(
225 'contact-info-admin',
226 Assets::get_file_url_for_environment(
227 '_inc/build/widgets/contact-info/contact-info-admin.min.js',
228 'modules/widgets/contact-info/contact-info-admin.js'
229 ),
230 array( 'jquery' ),
231 20160727,
232 false
233 );
234
235 if ( is_customize_preview() ) {
236 $customize_contact_info_api_key_nonce = wp_create_nonce( 'customize_contact_info_api_key' );
237 wp_localize_script(
238 'contact-info-admin',
239 'contact_info_api_key_ajax_obj',
240 array( 'nonce' => $customize_contact_info_api_key_nonce )
241 );
242 }
243
244 ?>
245 <p>
246 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
247 <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'] ); ?>" />
248 </p>
249
250 <p>
251 <label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label>
252 <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>
253 </p>
254
255 <p>
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 <?php if ( ! has_filter( 'jetpack_google_maps_api_key' ) || false === apply_filters( 'jetpack_google_maps_api_key', false ) ) { ?>
261
262 <p class="jp-contact-info-admin-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>">
263 <label for="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>">
264 <?php esc_html_e( 'Google Maps API Key', 'jetpack' ); ?>
265 <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 ); ?>" />
266 <br />
267 <small>
268 <?php
269 printf(
270 wp_kses(
271 /* Translators: placeholder is a URL to support documentation. */
272 __( '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' ),
273 array(
274 'a' => array(
275 'href' => true,
276 ),
277 )
278 ),
279 ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'https://wordpress.com/support/widgets/contact-info/' : esc_url( Redirect::get_url( 'jetpack-support-extra-sidebar-widgets-contact-info-widget' ) )
280 );
281 ?>
282 </small>
283 </label>
284 </p>
285
286 <?php } else { ?>
287
288 <input type="hidden" id="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'apikey' ) ); ?>" value="<?php echo esc_attr( $apikey ); ?>" />
289
290 <?php } // end if jetpack_google_maps_api_key check. ?>
291
292 <p class="jp-contact-info-admin-map jp-contact-info-embed-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>">
293 <?php
294 if ( ! is_customize_preview() && true === $instance['goodmap'] ) {
295 echo $this->build_map( $instance['address'], $apikey ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
296 } elseif ( true !== $instance['goodmap'] && ! empty( $instance['goodmap'] ) ) {
297 printf(
298 '<span class="button-link-delete">%s</span>',
299 esc_html( $instance['goodmap'] )
300 );
301 }
302 ?>
303 </p>
304
305 <p>
306 <label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label>
307 <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'] ); ?>" />
308 </p>
309
310 <p>
311 <label for="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>"><?php esc_html_e( 'Email Address:', 'jetpack' ); ?></label>
312 <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'] ); ?>" />
313 </p>
314
315 <p>
316 <label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label>
317 <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>
318 </p>
319
320 <?php
321 }
322
323 /**
324 * Generate a Google Maps link for the supplied address.
325 *
326 * @param string $address Address to link to.
327 *
328 * @return string
329 */
330 private function build_map_link( $address ) {
331 // Google map urls have lots of available params but zoom (z) and query (q) are enough.
332 return 'https://maps.google.com/maps?z=16&q=' . $this->urlencode_address( $address );
333 }
334
335 /**
336 * Builds map display HTML code from the supplied address.
337 *
338 * @param string $address Address.
339 * @param string $api_key API Key.
340 *
341 * @return string HTML of the map.
342 */
343 private function build_map( $address, $api_key = null ) {
344 $this->enqueue_scripts();
345 $src = add_query_arg( 'q', rawurlencode( $address ), 'https://www.google.com/maps/embed/v1/place' );
346 if ( ! empty( $api_key ) ) {
347 $src = add_query_arg( 'key', $api_key, $src );
348 }
349
350 $height = 216;
351
352 $iframe_attributes = sprintf(
353 ' height="%d" frameborder="0" src="%s" title="%s" class="contact-map"',
354 esc_attr( $height ),
355 esc_url( $src ),
356 __( 'Google Map Embed', 'jetpack' )
357 );
358
359 $iframe_html = sprintf( '<iframe width="600" %s></iframe>', $iframe_attributes );
360
361 if (
362 ! class_exists( 'Jetpack_AMP_Support' )
363 || ! Jetpack_AMP_Support::is_amp_request()
364 ) {
365 return $iframe_html;
366 }
367
368 $amp_iframe_html = sprintf( '<amp-iframe layout="fixed-height" width="auto" sandbox="allow-scripts allow-same-origin" %s>', $iframe_attributes );
369
370 // 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).
371 $amp_iframe_html .= sprintf( '<span placeholder>%s</span>', esc_html__( 'Loading map&hellip;', 'jetpack' ) );
372
373 // Add original iframe as fallback in case JavaScript is disabled.
374 $amp_iframe_html .= sprintf( '<noscript>%s</noscript>', $iframe_html );
375
376 $amp_iframe_html .= '</amp-iframe>';
377 return $amp_iframe_html;
378 }
379
380 /**
381 * Encode an URL
382 *
383 * @param string $address The URL to encode.
384 *
385 * @return string The encoded URL
386 */
387 private function urlencode_address( $address ) {
388
389 $address = strtolower( $address );
390 // Get rid of any unwanted whitespace.
391 $address = preg_replace( '/\s+/', ' ', trim( $address ) );
392 // Use + not %20.
393 $address = str_ireplace( ' ', '+', $address );
394 return rawurlencode( $address );
395 }
396
397 /**
398 * Returns the instance's updated 'goodmap' value.
399 *
400 * @param array $old_instance Old configuration values.
401 * @param array $instance Current configuration values.
402 *
403 * @return bool|string The instance's updated 'goodmap' value. The value is true if
404 * $instance can display a good map. If not, returns an error message.
405 */
406 private function update_goodmap( $old_instance, $instance ) {
407 /*
408 * If we have no address or don't want to show a map,
409 * no need to check if the map is valid.
410 */
411 if ( empty( $instance['address'] ) || 0 === $instance['showmap'] ) {
412 return false;
413 }
414
415 /*
416 * If there have been any changes that may impact the map in the widget
417 * (adding an address, address changes, new API key, API key change)
418 * then we want to check whether our map can be displayed again.
419 */
420 if (
421 ! isset( $instance['goodmap'] )
422 || ! isset( $old_instance['address'] )
423 || $this->urlencode_address( $old_instance['address'] ) !== $this->urlencode_address( $instance['address'] )
424 || ! isset( $old_instance['apikey'] )
425 || $old_instance['apikey'] !== $instance['apikey']
426 ) {
427 return $this->has_good_map( $instance );
428 } else {
429 return $instance['goodmap'];
430 }
431 }
432
433 /**
434 * Check if the instance has a valid Map location.
435 *
436 * @param array $instance Widget instance configuration.
437 *
438 * @return bool|string Whether or not there is a valid map. If not, return an error message.
439 */
440 private function has_good_map( $instance ) {
441 /** This filter is documented in modules/widgets/contact-info.php */
442 $api_key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
443 if ( ! empty( $api_key ) ) {
444 $path = add_query_arg(
445 array(
446 'q' => rawurlencode( $instance['address'] ),
447 'key' => $api_key,
448 ),
449 'https://www.google.com/maps/embed/v1/place'
450 );
451 $wp_remote_get_args = array(
452 'headers' => array( 'Referer' => home_url() ),
453 );
454 $response = wp_remote_get( esc_url_raw( $path ), $wp_remote_get_args );
455
456 if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
457 return true;
458 } else {
459 return wp_remote_retrieve_body( $response );
460 }
461 }
462
463 return __( 'Please enter a valid Google API Key.', 'jetpack' );
464 }
465
466 /**
467 * Check the Google Maps API key after an Ajax call from the widget's admin form in
468 * the Customizer preview.
469 */
470 public function ajax_check_api_key() {
471 if ( isset( $_POST['apikey'] ) ) {
472 if ( check_ajax_referer( 'customize_contact_info_api_key' ) && current_user_can( 'customize' ) ) {
473 $apikey = wp_kses( $_POST['apikey'], array() );
474 $default_instance = $this->defaults();
475 $default_instance['apikey'] = $apikey;
476 wp_send_json( array( 'result' => esc_html( $this->has_good_map( $default_instance ) ) ) );
477 }
478 } else {
479 wp_die();
480 }
481 }
482
483 }
484
485 }
486