Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
reservations_widget_register
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
12
reservations_widget_style
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
WPCOM_Widget_Reservations
0.00% covered (danger)
0.00%
0 / 81
0.00% covered (danger)
0.00%
0 / 4
506
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 form
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
 widget
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
210
 update
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2
3/**
4 * A WordPress.com Reservations widget.
5 */
6class WPCOM_Widget_Reservations extends WP_Widget {
7    /**
8     * Widget config fields.
9     *
10     * @var array $defaults
11     */
12    private $defaults = array();
13
14    /**
15     * Reservation form fields.
16     *
17     * @var array $fields
18     */
19    private $fields = array();
20
21    /**
22     * Constructor
23     */
24    public function __construct() {
25        parent::__construct(
26            'reservations',
27            __( 'Reservations', 'wpcomsh' ),
28            array( 'description' => 'Allow visitors to submit a reservation inquiry.' )
29        );
30
31        $this->defaults = array(
32            'title'    => __( 'Reservations', 'wpcomsh' ),
33            'subject'  => __( 'Reservation Inquiry', 'wpcomsh' ),
34            'email_to' => get_option( 'admin_email' ),
35            'show'     => array( 'name', 'email', 'adults', 'children', 'arrival', 'departure', 'message' ),
36        );
37
38        $this->fields = array(
39            'name'      => __( 'Name', 'wpcomsh' ),
40            'email'     => __( 'Email', 'wpcomsh' ),
41            'phone'     => __( 'Phone', 'wpcomsh' ),
42            'message'   => __( 'Message', 'wpcomsh' ),
43            'adults'    => __( '# Adults', 'wpcomsh' ),
44            'children'  => __( '# Children', 'wpcomsh' ),
45            'arrival'   => __( 'Arrival', 'wpcomsh' ),
46            'departure' => __( 'Departure', 'wpcomsh' ),
47        );
48    }
49
50    /**
51     * Display the widget settings form.
52     *
53     * @param array $instance Current settings.
54     * @return never
55     */
56    public function form( $instance ) {
57        $instance = wp_parse_args( (array) $instance, $this->defaults );
58        ?>
59        <p>
60            <label>
61                <?php esc_html_e( 'Title:', 'wpcomsh' ); ?>
62                <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
63            </label>
64        </p>
65        <p>
66            <label>
67                <?php esc_html_e( 'Recipient E-mail Address:', 'wpcomsh' ); ?>
68                <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'email_to' ) ); ?>" value="<?php echo esc_attr( $instance['email_to'] ); ?>" />
69            </label>
70        </p>
71        <p>
72            <label>
73                <?php esc_html_e( 'E-mail Subject:', 'wpcomsh' ); ?>
74                <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'subject' ) ); ?>" value="<?php echo esc_attr( $instance['subject'] ); ?>" />
75            </label>
76        </p>
77        <p>
78            <?php esc_html_e( 'Show:', 'wpcomsh' ); ?>
79            <fieldset style="padding: 0 10px;">
80                <?php foreach ( $this->fields as $key => $label ) { ?>
81                    <label style="display: block; float: left; width: 50%; margin-bottom: 5px;">
82                        <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show-' . $key ) ); ?>" value="<?php echo esc_attr( $key ); ?><?php checked( in_array( $key, $instance['show'], true ) ); ?> /> <?php echo esc_html( $label ); ?>
83                    </label>
84                <?php } ?>
85            </fieldset>
86        </p>
87        <?php
88    }
89
90    /**
91     * Display the widget.
92     *
93     * @param array $args     Widget arguments.
94     * @param array $instance Widget instance.
95     */
96    public function widget( $args, $instance ) {
97        $instance = wp_parse_args( $instance, $this->defaults );
98
99        if ( empty( $instance['show'] ) ) {
100            return;
101        }
102
103        $contact_form_shortcode = '[contact-form';
104
105        if ( isset( $instance['email_to'] ) && ! empty( $instance['email_to'] ) ) {
106            $contact_form_shortcode .= " to='" . esc_attr( $instance['email_to'] ) . "'";
107        }
108
109        if ( isset( $instance['subject'] ) && ! empty( $instance['subject'] ) ) {
110            $contact_form_shortcode .= " subject='" . esc_attr( $instance['subject'] ) . "'";
111        }
112
113        $contact_form_shortcode .= ']';
114
115        if ( in_array( 'name', $instance['show'], true ) ) {
116            $contact_form_shortcode .= "[contact-field label='" . esc_attr( $this->fields['name'] ) . "' type='name'/]";
117        }
118        if ( in_array( 'email', $instance['show'], true ) ) {
119            $contact_form_shortcode .= "[contact-field label='" . esc_attr( $this->fields['email'] ) . "' type='email' required='1'/]";
120        }
121        if ( in_array( 'phone', $instance['show'], true ) ) {
122            $contact_form_shortcode .= "[contact-field label='" . esc_attr( $this->fields['phone'] ) . "' type='text'/]";
123        }
124        if ( in_array( 'adults', $instance['show'], true ) ) {
125            $contact_form_shortcode .= "[contact-field label='" . esc_attr( $this->fields['adults'] ) . "' type='text' data='" . esc_attr( wp_json_encode( array( 'field-size' => 'small' ), JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ) . "'/]";
126        }
127        if ( in_array( 'children', $instance['show'], true ) ) {
128            $contact_form_shortcode .= "[contact-field label='" . esc_attr( $this->fields['children'] ) . "' type='text' data='" . esc_attr( wp_json_encode( array( 'field-size' => 'small' ), JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ) . "'/]";
129        }
130        if ( in_array( 'arrival', $instance['show'], true ) ) {
131            $contact_form_shortcode .= "[contact-field label='" . esc_attr( $this->fields['arrival'] ) . "' type='date' data='" . esc_attr( wp_json_encode( array( 'field-size' => 'small' ), JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ) . "'/]";
132        }
133        if ( in_array( 'departure', $instance['show'], true ) ) {
134            $contact_form_shortcode .= "[contact-field label='" . esc_attr( $this->fields['departure'] ) . "' type='date' data='" . esc_attr( wp_json_encode( array( 'field-size' => 'small' ), JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ) . "'/]";
135        }
136        if ( in_array( 'message', $instance['show'], true ) ) {
137            $contact_form_shortcode .= "[contact-field label='" . esc_attr( $this->fields['message'] ) . "' type='textarea' /]";
138        }
139
140        $contact_form_shortcode .= '[/contact-form]';
141
142        echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
143        echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
144        echo do_shortcode( apply_filters( 'widget_text', $contact_form_shortcode ) );
145        echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
146
147        do_action( 'jetpack_stats_extra', 'widget_view', 'reservations' );
148    }
149
150    /**
151     * Update the widget settings.
152     *
153     * @param array $new_instance New settings.
154     * @param array $old_instance Old settings.
155     */
156    public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
157        $new_instance['show'] = array();
158
159        foreach ( $this->fields as $field_key => $unused ) {
160            if ( isset( $new_instance[ 'show-' . $field_key ] ) ) {
161                $new_instance['show'][] = $field_key;
162                unset( $new_instance[ 'show-' . $field_key ] );
163            }
164        }
165
166        if ( ! is_email( $new_instance['email_to'] ) ) {
167            $new_instance['email_to'] = $this->defaults['email_to'];
168        }
169
170        if ( empty( $new_instance['subject'] ) ) {
171            $new_instance['subject'] = $this->defaults['subject'];
172        }
173
174        return $new_instance;
175    }
176}
177
178/**
179 * Register the Reservations widget for all sites running Stay
180 * or with an active widget.
181 */
182function reservations_widget_register() { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
183    if ( 'stay' === get_stylesheet() || is_active_widget( false, false, 'reservations', false ) ) {
184        register_widget( 'WPCOM_Widget_Reservations' );
185    }
186}
187add_action( 'widgets_init', 'reservations_widget_register' );
188
189/**
190 * Enqueue widget styles.
191 */
192function reservations_widget_style() {
193    if ( is_active_widget( false, false, 'reservations' ) ) {
194        wp_enqueue_style( 'widget-reservations', plugins_url( 'reservations/css/reservations.css', __FILE__ ), array(), WPCOMSH_VERSION );
195        wp_enqueue_script( 'widget-reservations', plugins_url( 'reservations/js/reservations.js', __FILE__ ), array( 'jquery' ), WPCOMSH_VERSION, true );
196    }
197}
198add_action( 'wp_enqueue_scripts', 'reservations_widget_style' );