Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
A8C\FSE\Mailerlite\mailerlite_register_widget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
WPCOM_Widget_Mailerlite
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 widget
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
30
 update
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 form
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
1<?php 
2//phpcs:ignoreFile WordPress.Files.FileName.InvalidClassFileName
3//phpcs:ignoreFile Universal.Files.SeparateFunctionsFromOO.Mixed
4
5
6namespace A8C\FSE\Mailerlite;
7
8use Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10/**
11 * Mailerlite widget class
12 * Display a subscriber popup for Mailerlite.
13 */
14class WPCOM_Widget_Mailerlite extends \WP_Widget {
15
16    /**
17     * WPCOM_Widget_Mailerlite constructor.
18     */
19    public function __construct() {
20        parent::__construct(
21            'wpcom-mailerlite',
22            /** This filter is documented in modules/widgets/facebook-likebox.php */
23            apply_filters( 'jetpack_widget_name', __( 'Mailerlite subscriber popup', 'jetpack-mu-wpcom' ) ),
24            array(
25                'classname'                   => 'widget_mailerlite',
26                'description'                 => __( 'Display Mailerlite subscriber popup', 'jetpack-mu-wpcom' ),
27                'customize_selective_refresh' => true,
28            )
29        );
30    }
31
32    /**
33     * Output the widget.
34     *
35     * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
36     * @param array $instance - The settings for the particular instance of the widget.
37     */
38    public function widget( $args, $instance ) {
39        /** This action is documented in modules/widgets/gravatar-profile.php */
40        do_action( 'jetpack_stats_extra', 'widget_view', 'mailerlite' );
41
42        if ( empty( $instance['account'] ) || empty( $instance['uuid'] ) ) {
43            if ( current_user_can( 'edit_theme_options' ) ) {
44                echo $args['before_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
45                echo '<p>' . sprintf(
46                    wp_kses(
47                        /* translators: %1$s - URL to manage the widget, %2$s - documentation URL. */
48                        __( 'You need to enter your numeric account ID and UUID for the <a href="%1$s">Mailerlite Widget</a> to work correctly. <a href="%2$s" target="_blank">Full instructions</a>.', 'jetpack-mu-wpcom' ),
49                        array(
50                            'a' => array(
51                                'href'  => array(),
52                                'title' => array(),
53                            ),
54                        )
55                    ),
56                    esc_url( admin_url( 'widgets.php' ) ),
57                    'https://wordpress.com/support/widgets/mailerlite/'
58                ) . '</p>';
59                echo $args['after_widget']; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
60            }
61            return;
62        }
63
64        if ( wp_script_is( 'mailerlite-subscriber-popup', 'enqueued' ) ) {
65            return;
66        }
67
68        wp_register_script( 'mailerlite-universal', 'https://static.mailerlite.com/js/universal.js', array(), '20200521', true );
69        $asset_file = include Jetpack_Mu_Wpcom::BASE_DIR . 'build/mailerlite-subscriber-popup/mailerlite-subscriber-popup.asset.php';
70        wp_enqueue_script(
71            'mailerlite-subscriber-popup',
72            plugins_url( 'build/mailerlite-subscriber-popup/mailerlite-subscriber-popup.js', Jetpack_Mu_Wpcom::BASE_FILE ),
73            array( 'mailerlite-universal' ),
74            $asset_file['version'] ?? filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/mailerlite-subscriber-popup/mailerlite-subscriber-popup.js' ),
75            true
76        );
77        wp_localize_script(
78            'mailerlite-subscriber-popup',
79            'jetpackMailerliteSettings',
80            array(
81                'account' => esc_attr( $instance['account'] ),
82                'uuid'    => esc_attr( $instance['uuid'] ),
83            )
84        );
85    }
86
87    /**
88     * Updates a particular instance of a widget.
89     *
90     * @param array $new_instance New settings for this instance as input by the user via WP_Widget::form().
91     * @param array $old_instance   Old settings for this instance.
92     *
93     * @return mixed
94     */
95    public function update( $new_instance, $old_instance ) { // @phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
96        return array(
97            'account' => wp_kses( stripslashes( $new_instance['account'] ), array() ),
98            'uuid'    => wp_kses( stripslashes( $new_instance['uuid'] ), array() ),
99        );
100    }
101
102    /**
103     * Editable form in WP-admin.
104     *
105     * @param array $instance - settings.
106     */
107    public function form( $instance ) {
108        $instance = wp_parse_args(
109            (array) $instance,
110            array(
111                'account' => '',
112                'uuid'    => '',
113            )
114        );
115
116    $html = '<p><label for="' . esc_attr( $this->get_field_id( 'account' ) ) . '">';
117    $html .= sprintf( wp_kses_post( __( 'Account ID <a href="%s" target="_blank">(instructions)</a>:', 'jetpack-mu-wpcom' ) ), 'https://wordpress.com/support/widgets/mailerlite/' );
118    $html .= '<input class="widefat" id="' . esc_attr( $this->get_field_id( 'account' ) ) . '" name="' . esc_attr( $this->get_field_name( 'account' ) ) . '" type="text" value="' . esc_attr( $instance['account'] ) . '" />
119    </label></p>
120    <p><label for="' . esc_attr( $this->get_field_id( 'shelf' ) ) . '">' . esc_html__( 'UUID:', 'jetpack-mu-wpcom' );
121    $html .= '<input class="widefat" id="' . esc_attr( $this->get_field_id( 'uuid' ) ) . '" name="' . esc_attr( $this->get_field_name( 'uuid' ) ) . '" type="text" value="' . esc_attr( $instance['uuid'] ) . '" />';
122    $html .= '</label></p>';
123
124        echo $html;
125        return $html;
126    }
127}
128
129/**
130 * Registers the widget via widgets_init hook.
131 */
132function mailerlite_register_widget() {
133    register_widget( '\A8C\FSE\Mailerlite\WPCOM_Widget_Mailerlite' );
134}
135add_action( 'widgets_init', '\A8C\FSE\Mailerlite\mailerlite_register_widget' );