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