PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 12.3.2
Jetpack – WP Security, Backup, Speed, & Growth v12.3.2
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 14.4.2 14.5.1 14.6.1 14.7.1 14.8.1 14.9.2 15.0.3 15.1.2 15.2.1 15.3.2 15.4.1 15.5.1 15.6.1 15.7.2 15.8.1 15.9.2 16.0.2 16.1.3 16.2-a.5 16.2-a.3 16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8.0.1 8.0.2 8.0.3 8.1 8.1.1 8.1.2 8.1.3 8.1.4 8.2 8.2.0.1 8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.3 8.3.1 8.3.2 8.3.3 8.4 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.5 8.5.1 8.5.2 8.5.3 8.6 8.6.1 8.6.2 8.6.3 8.6.4 8.7 8.7.0.1 8.7.1 8.7.2 8.7.3 8.7.4 8.8 8.8.1 8.8.2 8.8.3 8.8.4 8.8.5 8.9 8.9.1 8.9.2 8.9.3 8.9.4 9.0 9.0.1 9.0.2 9.0.3 9.0.4 9.0.5 9.1 9.1.1 9.1.2 9.1.3 9.2 9.2.1 9.2.2 9.2.3 9.2.4 9.3 9.3.1 9.3.2 9.3.3 9.3.4 9.3.5 9.4 9.4.1 9.4.2 9.4.3 9.4.4 9.5 9.5.1 9.5.2 9.5.3 9.5.4 9.5.5 9.6 9.6.1 9.6.2 9.6.3 9.6.4 9.7 9.7.1 9.7.2 15.7-beta.2 9.7.3 15.7.1 9.8 15.8-a.1 9.8.1 15.8-a.3 9.8.2 2.0.9 9.8.3 2.1.7 9.9 2.2.10 9.9.1 2.3.10 9.9.2 2.4.7 9.9.3 2.5.5 2.6.6 2.7.5 2.8.5 2.9.6 3.0.6 3.1.5 3.2.5 3.3.6 3.4.6 3.5.6 3.6.4 3.7.5 3.8.5 3.9.10 4.0.7 4.1.4 4.2.5 4.3.5 4.4.5 4.5.3 4.6.3 4.7.4 4.8.5 4.9.3 5.0.3 5.1.4 trunk 10.0 10.0.1 10.0.2 10.1 10.1.1 10.1.2 10.2 10.2.1 10.2.2 10.2.3 10.3 10.3.1 10.3.2 10.4 10.4.1 10.4.2 10.5 10.5.1 10.5.2 10.5.3 10.6 10.6.1 10.6.2 10.7 10.7.1 10.7.2 10.8 10.8.1 10.8.2 10.9 10.9.1 10.9.2 10.9.3 11.0 11.0.1 11.0.2 11.1 11.1.1 11.1.2 11.1.3 11.1.4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / modules / widgets / contact-info.php
jetpack / modules / widgets Last commit date
authors 8 years ago contact-info 5 years ago eu-cookie-law 4 years ago facebook-likebox 11 years ago flickr 4 years ago gallery 4 years ago goodreads 5 years ago google-translate 4 years ago image-widget 8 years ago instagram 6 years ago migrate-to-core 3 years ago milestone 3 years ago my-community 8 years ago simple-payments 4 years ago social-icons 4 years ago social-media-icons 5 years ago top-posts 8 years ago wordpress-post-widget 3 years ago authors.php 3 years ago blog-stats.php 3 years ago class-jetpack-eu-cookie-law-widget.php 3 years ago class-jetpack-instagram-widget.php 3 years ago contact-info.php 3 years ago customizer-controls.css 9 years ago customizer-utils.js 6 years ago facebook-likebox.php 3 years ago flickr.php 3 years ago gallery.php 3 years ago goodreads.php 3 years ago google-translate.php 3 years ago gravatar-profile.css 10 years ago gravatar-profile.php 3 years ago image-widget.php 3 years ago internet-defense-league.php 3 years ago mailchimp.php 3 years ago milestone.php 5 years ago my-community.php 3 years ago rsslinks-widget.php 3 years ago simple-payments.php 3 years ago social-icons.php 3 years ago social-media-icons.php 3 years ago top-posts.php 3 years ago twitter-timeline-admin.js 6 years ago twitter-timeline.php 3 years ago upcoming-events.php 3 years ago wordpress-post-widget.php 4 years ago
contact-info.php
501 lines
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
4
5 use Automattic\Jetpack\Assets;
6 use Automattic\Jetpack\Redirect;
7
8 if ( ! class_exists( 'Jetpack_Contact_Info_Widget' ) ) {
9
10 /**
11 * Register Contact_Info_Widget widget
12 */
13 function jetpack_contact_info_widget_init() {
14 register_widget( 'Jetpack_Contact_Info_Widget' );
15 }
16
17 add_action( 'widgets_init', 'jetpack_contact_info_widget_init' );
18
19 /**
20 * Makes a custom Widget for displaying Restaurant Location/Map, Hours, and Contact Info available.
21 *
22 * @package WordPress
23 */
24 class Jetpack_Contact_Info_Widget extends WP_Widget {
25
26 /**
27 * Constructor
28 */
29 public function __construct() {
30 global $pagenow;
31
32 $widget_ops = array(
33 'classname' => 'widget_contact_info',
34 'description' => __( 'Display a map with your location, hours, and contact information.', 'jetpack' ),
35 'customize_selective_refresh' => true,
36 'show_instance_in_rest' => true,
37 );
38 parent::__construct(
39 'widget_contact_info',
40 /** This filter is documented in modules/widgets/facebook-likebox.php */
41 apply_filters( 'jetpack_widget_name', __( 'Contact Info & Map', 'jetpack' ) ),
42 $widget_ops
43 );
44 $this->alt_option_name = 'widget_contact_info';
45
46 if ( is_customize_preview() ) {
47 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
48 } elseif ( 'widgets.php' === $pagenow ) {
49 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
50 }
51
52 add_action( 'wp_ajax_customize-contact-info-api-key', array( $this, 'ajax_check_api_key' ) );
53 add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
54 }
55
56 /**
57 * Remove the "Contact info and Map" widget from the Legacy Widget block
58 *
59 * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
60 * @return array $widget_types New list of widgets that will be removed.
61 */
62 public function hide_widget_in_block_editor( $widget_types ) {
63 $widget_types[] = 'widget_contact_info';
64 return $widget_types;
65 }
66
67 /**
68 * Enqueue scripts and styles.
69 */
70 public function enqueue_scripts() {
71 wp_enqueue_style(
72 'contact-info-map-css',
73 plugins_url( 'contact-info/contact-info-map.css', __FILE__ ),
74 array(),
75 JETPACK__VERSION
76 );
77 }
78
79 /**
80 * Return an associative array of default values
81 *
82 * These values are used in new widgets.
83 *
84 * @return array Array of default values for the Widget's options
85 */
86 public function defaults() {
87 return array(
88 'title' => __( 'Hours & Info', 'jetpack' ),
89 'address' => __( "3999 Mission Boulevard,\nSan Diego CA 92109", 'jetpack' ),
90 'phone' => _x( '1-202-555-1212', 'Example of a phone number', 'jetpack' ),
91 'hours' => __( "Lunch: 11am - 2pm \nDinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am", 'jetpack' ),
92 'email' => null,
93 'showmap' => 0,
94 'apikey' => null,
95 'goodmap' => null,
96 );
97 }
98
99 /**
100 * Outputs the HTML for this widget.
101 *
102 * @param array $args An array of standard parameters for widgets in this theme.
103 * @param array $instance An array of settings for this widget instance.
104 *
105 * @return void Echoes it's output
106 **/
107 public function widget( $args, $instance ) {
108 $instance = wp_parse_args( $instance, $this->defaults() );
109
110 echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
111
112 if ( '' !== $instance['title'] ) {
113 echo $args['before_title'] . $instance['title'] . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
114 }
115
116 /**
117 * Fires at the beginning of the Contact Info widget, after the title.
118 *
119 * @module widgets
120 *
121 * @since 3.9.2
122 */
123 do_action( 'jetpack_contact_info_widget_start' );
124
125 echo '<div itemscope itemtype="http://schema.org/LocalBusiness">';
126
127 if ( '' !== $instance['address'] ) {
128
129 $showmap = $instance['showmap'];
130 $goodmap = isset( $instance['goodmap'] ) ? $instance['goodmap'] : $this->has_good_map( $instance );
131
132 if ( $showmap && true === $goodmap ) {
133 /**
134 * Set a Google Maps API Key.
135 *
136 * @since 4.1.0
137 *
138 * @param string $api_key Google Maps API Key
139 */
140 $api_key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
141 echo $this->build_map( $instance['address'], $api_key ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
142 } elseif ( $showmap && is_customize_preview() && true !== $goodmap ) {
143 printf(
144 '<span class="contact-map-api-error" style="display: block;">%s</span>',
145 esc_html( $instance['goodmap'] )
146 );
147 }
148
149 $map_link = $this->build_map_link( $instance['address'] );
150
151 printf(
152 '<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>',
153 esc_url( $map_link ),
154 str_replace( "\n", '<br/>', esc_html( $instance['address'] ) ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
155 );
156 }
157
158 if ( '' !== $instance['phone'] ) {
159 if ( wp_is_mobile() ) {
160 echo '<div class="confit-phone"><span itemprop="telephone"><a href="' . esc_url( 'tel:' . $instance['phone'] ) . '">' . esc_html( $instance['phone'] ) . '</a></span></div>';
161 } else {
162 echo '<div class="confit-phone"><span itemprop="telephone">' . esc_html( $instance['phone'] ) . '</span></div>';
163 }
164 }
165
166 if ( is_email( trim( $instance['email'] ) ) ) {
167 printf(
168 '<div class="confit-email"><a href="mailto:%1$s">%1$s</a></div>',
169 esc_html( $instance['email'] )
170 );
171 }
172
173 if ( '' !== $instance['hours'] ) {
174 printf(
175 '<div class="confit-hours" itemprop="openingHours">%s</div>',
176 str_replace( "\n", '<br/>', esc_html( $instance['hours'] ) ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
177 );
178 }
179
180 echo '</div>';
181
182 /**
183 * Fires at the end of Contact Info widget.
184 *
185 * @module widgets
186 *
187 * @since 3.9.2
188 */
189 do_action( 'jetpack_contact_info_widget_end' );
190
191 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
192
193 /** This action is documented in modules/widgets/gravatar-profile.php */
194 do_action( 'jetpack_stats_extra', 'widget_view', 'contact_info' );
195 }
196
197 /**
198 * Deals with the settings when they are saved by the admin. Here is
199 * where any validation should be dealt with.
200 *
201 * @param array $new_instance New configuration values.
202 * @param array $old_instance Old configuration values.
203 *
204 * @return array
205 */
206 public function update( $new_instance, $old_instance ) {
207
208 $instance = array();
209 $instance['title'] = wp_kses( $new_instance['title'], array() );
210 $instance['address'] = wp_kses( $new_instance['address'], array() );
211 $instance['phone'] = wp_kses( $new_instance['phone'], array() );
212 $instance['email'] = wp_kses( $new_instance['email'], array() );
213 $instance['hours'] = wp_kses( $new_instance['hours'], array() );
214 $instance['apikey'] = wp_kses( isset( $new_instance['apikey'] ) ? $new_instance['apikey'] : $old_instance['apikey'], array() );
215
216 if ( ! isset( $new_instance['showmap'] ) ) {
217 $instance['showmap'] = 0;
218 } else {
219 $instance['showmap'] = (int) $new_instance['showmap'];
220 }
221
222 $instance['goodmap'] = $this->update_goodmap( $old_instance, $instance );
223
224 return $instance;
225 }
226
227 /**
228 * Displays the form for this widget on the Widgets page of the WP Admin area.
229 *
230 * @param array $instance Instance configuration.
231 *
232 * @return void
233 */
234 public function form( $instance ) {
235 $instance = wp_parse_args( $instance, $this->defaults() );
236 /** This filter is documented in modules/widgets/contact-info.php */
237 $apikey = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
238
239 wp_enqueue_script(
240 'contact-info-admin',
241 Assets::get_file_url_for_environment(
242 '_inc/build/widgets/contact-info/contact-info-admin.min.js',
243 'modules/widgets/contact-info/contact-info-admin.js'
244 ),
245 array( 'jquery' ),
246 20160727,
247 false
248 );
249
250 if ( is_customize_preview() ) {
251 $customize_contact_info_api_key_nonce = wp_create_nonce( 'customize_contact_info_api_key' );
252 wp_localize_script(
253 'contact-info-admin',
254 'contact_info_api_key_ajax_obj',
255 array( 'nonce' => $customize_contact_info_api_key_nonce )
256 );
257 }
258
259 ?>
260 <p>
261 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
262 <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'] ); ?>" />
263 </p>
264
265 <p>
266 <label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label>
267 <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>
268 </p>
269
270 <p>
271 <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 ); ?> />
272 <label for="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>"><?php esc_html_e( 'Show map', 'jetpack' ); ?></label>
273 </p>
274
275 <?php if ( ! has_filter( 'jetpack_google_maps_api_key' ) || false === apply_filters( 'jetpack_google_maps_api_key', false ) ) { ?>
276
277 <p class="jp-contact-info-admin-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>">
278 <label for="<?php echo esc_attr( $this->get_field_id( 'apikey' ) ); ?>">
279 <?php esc_html_e( 'Google Maps API Key', 'jetpack' ); ?>
280 <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 ); ?>" />
281 <br />
282 <small>
283 <?php
284 printf(
285 wp_kses(
286 /* Translators: placeholder is a URL to support documentation. */
287 __( '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' ),
288 array(
289 'a' => array(
290 'href' => true,
291 ),
292 )
293 ),
294 ( 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' ) )
295 );
296 ?>
297 </small>
298 </label>
299 </p>
300
301 <?php } else { ?>
302
303 <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 ); ?>" />
304
305 <?php } // end if jetpack_google_maps_api_key check. ?>
306
307 <p class="jp-contact-info-admin-map jp-contact-info-embed-map" style="<?php echo $instance['showmap'] ? '' : 'display: none;'; ?>">
308 <?php
309 if ( ! is_customize_preview() && true === $instance['goodmap'] ) {
310 echo $this->build_map( $instance['address'], $apikey ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
311 } elseif ( true !== $instance['goodmap'] && ! empty( $instance['goodmap'] ) ) {
312 printf(
313 '<span class="button-link-delete">%s</span>',
314 esc_html( $instance['goodmap'] )
315 );
316 }
317 ?>
318 </p>
319
320 <p>
321 <label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label>
322 <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'] ); ?>" />
323 </p>
324
325 <p>
326 <label for="<?php echo esc_attr( $this->get_field_id( 'email' ) ); ?>"><?php esc_html_e( 'Email Address:', 'jetpack' ); ?></label>
327 <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'] ); ?>" />
328 </p>
329
330 <p>
331 <label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label>
332 <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>
333 </p>
334
335 <?php
336 }
337
338 /**
339 * Generate a Google Maps link for the supplied address.
340 *
341 * @param string $address Address to link to.
342 *
343 * @return string
344 */
345 private function build_map_link( $address ) {
346 // Google map urls have lots of available params but zoom (z) and query (q) are enough.
347 return 'https://maps.google.com/maps?z=16&q=' . $this->urlencode_address( $address );
348 }
349
350 /**
351 * Builds map display HTML code from the supplied address.
352 *
353 * @param string $address Address.
354 * @param string $api_key API Key.
355 *
356 * @return string HTML of the map.
357 */
358 private function build_map( $address, $api_key = null ) {
359 $this->enqueue_scripts();
360 $src = add_query_arg( 'q', rawurlencode( $address ), 'https://www.google.com/maps/embed/v1/place' );
361 if ( ! empty( $api_key ) ) {
362 $src = add_query_arg( 'key', $api_key, $src );
363 }
364
365 $height = 216;
366
367 $iframe_attributes = sprintf(
368 ' height="%d" frameborder="0" src="%s" title="%s" class="contact-map"',
369 esc_attr( $height ),
370 esc_url( $src ),
371 __( 'Google Map Embed', 'jetpack' )
372 );
373
374 $iframe_html = sprintf( '<iframe width="600" %s></iframe>', $iframe_attributes );
375
376 if (
377 ! class_exists( 'Jetpack_AMP_Support' )
378 || ! Jetpack_AMP_Support::is_amp_request()
379 ) {
380 return $iframe_html;
381 }
382
383 $amp_iframe_html = sprintf( '<amp-iframe layout="fixed-height" width="auto" sandbox="allow-scripts allow-same-origin" %s>', $iframe_attributes );
384
385 // 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).
386 $amp_iframe_html .= sprintf( '<span placeholder>%s</span>', esc_html__( 'Loading map&hellip;', 'jetpack' ) );
387
388 // Add original iframe as fallback in case JavaScript is disabled.
389 $amp_iframe_html .= sprintf( '<noscript>%s</noscript>', $iframe_html );
390
391 $amp_iframe_html .= '</amp-iframe>';
392 return $amp_iframe_html;
393 }
394
395 /**
396 * Encode an URL
397 *
398 * @param string $address The URL to encode.
399 *
400 * @return string The encoded URL
401 */
402 private function urlencode_address( $address ) {
403
404 $address = strtolower( $address );
405 // Get rid of any unwanted whitespace.
406 $address = preg_replace( '/\s+/', ' ', trim( $address ) );
407 // Use + not %20.
408 $address = str_ireplace( ' ', '+', $address );
409 return rawurlencode( $address );
410 }
411
412 /**
413 * Returns the instance's updated 'goodmap' value.
414 *
415 * @param array $old_instance Old configuration values.
416 * @param array $instance Current configuration values.
417 *
418 * @return bool|string The instance's updated 'goodmap' value. The value is true if
419 * $instance can display a good map. If not, returns an error message.
420 */
421 private function update_goodmap( $old_instance, $instance ) {
422 /*
423 * If we have no address or don't want to show a map,
424 * no need to check if the map is valid.
425 */
426 if ( empty( $instance['address'] ) || 0 === $instance['showmap'] ) {
427 return false;
428 }
429
430 /*
431 * If there have been any changes that may impact the map in the widget
432 * (adding an address, address changes, new API key, API key change)
433 * then we want to check whether our map can be displayed again.
434 */
435 if (
436 ! isset( $instance['goodmap'] )
437 || ! isset( $old_instance['address'] )
438 || $this->urlencode_address( $old_instance['address'] ) !== $this->urlencode_address( $instance['address'] )
439 || ! isset( $old_instance['apikey'] )
440 || $old_instance['apikey'] !== $instance['apikey']
441 ) {
442 return $this->has_good_map( $instance );
443 } else {
444 return $instance['goodmap'];
445 }
446 }
447
448 /**
449 * Check if the instance has a valid Map location.
450 *
451 * @param array $instance Widget instance configuration.
452 *
453 * @return bool|string Whether or not there is a valid map. If not, return an error message.
454 */
455 private function has_good_map( $instance ) {
456 /** This filter is documented in modules/widgets/contact-info.php */
457 $api_key = apply_filters( 'jetpack_google_maps_api_key', $instance['apikey'] );
458 if ( ! empty( $api_key ) ) {
459 $path = add_query_arg(
460 array(
461 'q' => rawurlencode( $instance['address'] ),
462 'key' => $api_key,
463 ),
464 'https://www.google.com/maps/embed/v1/place'
465 );
466 $wp_remote_get_args = array(
467 'headers' => array( 'Referer' => home_url() ),
468 );
469 $response = wp_remote_get( esc_url_raw( $path ), $wp_remote_get_args );
470
471 if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
472 return true;
473 } else {
474 return wp_remote_retrieve_body( $response );
475 }
476 }
477
478 return __( 'Please enter a valid Google API Key.', 'jetpack' );
479 }
480
481 /**
482 * Check the Google Maps API key after an Ajax call from the widget's admin form in
483 * the Customizer preview.
484 */
485 public function ajax_check_api_key() {
486 if ( isset( $_POST['apikey'] ) ) {
487 if ( check_ajax_referer( 'customize_contact_info_api_key' ) && current_user_can( 'customize' ) ) {
488 $apikey = wp_kses( wp_unslash( $_POST['apikey'] ), array() );
489 $default_instance = $this->defaults();
490 $default_instance['apikey'] = $apikey;
491 wp_send_json( array( 'result' => esc_html( $this->has_good_map( $default_instance ) ) ) );
492 }
493 } else {
494 wp_die();
495 }
496 }
497
498 }
499
500 }
501