Completed
Push — feature/mailchimp-widget ( e85549...ecaa76 )
by
unknown
07:05
created

enqueue_admin_scripts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Assets;
4
5
if ( ! class_exists( 'Jetpack_MailChimp_Subscriber_Popup_Widget' ) ) {
6
7
	if ( ! class_exists( 'MailChimp_Subscriber_Popup' ) ) {
8
		include_once JETPACK__PLUGIN_DIR . 'modules/shortcodes/mailchimp.php';
9
	}
10
11
	//register MailChimp Subscriber Popup widget
12
	function jetpack_mailchimp_subscriber_popup_widget_init() {
13
		register_widget( 'Jetpack_MailChimp_Subscriber_Popup_Widget' );
14
	}
15
16
	add_action( 'widgets_init', 'jetpack_mailchimp_subscriber_popup_widget_init' );
17
18
	/**
19
	 * Add a MailChimp subscription form.
20
	 */
21
	class Jetpack_MailChimp_Subscriber_Popup_Widget extends WP_Widget {
22
23
		/**
24
		 * Constructor
25
		 */
26
		function __construct() {
27
			parent::__construct(
28
				'widget_mailchimp_subscriber_popup',
29
				/** This filter is documented in modules/widgets/facebook-likebox.php */
30
				apply_filters( 'jetpack_widget_name', __( 'MailChimp Subscriber Popup', 'jetpack' ) ),
31
				array(
32
					'classname'                   => 'widget_mailchimp_subscriber_popup',
33
					'description'                 => __( 'Allows displaying a popup subscription form to visitors.', 'jetpack' ),
34
					'customize_selective_refresh' => true,
35
				)
36
			);
37
38
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
39
		}
40
41
		/**
42
		 * Outputs the HTML for this widget.
43
		 *
44
		 * @param array $args     An array of standard parameters for widgets in this theme
45
		 * @param array $instance An array of settings for this widget instance
46
		 *
47
		 * @return void Echoes it's output
48
		 **/
49
		function widget( $args, $instance ) {
50
			$instance = wp_parse_args( $instance, array( 'code' => '' ) );
0 ignored issues
show
Documentation introduced by
array('code' => '') is of type array<string,string,{"code":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
52
			// Regular expresion that will match maichimp shortcode.
53
			$regex = '(\[mailchimp_subscriber_popup[^\]]+\])';
54
55
			// Check if the shortcode exists.
56
			preg_match( $regex, $instance['code'], $matches );
57
58
			// Process the shortcode only, if exists.
59
			if ( ! empty( $matches[0] ) ) {
60
				echo do_shortcode( $matches[0] );
61
			}
62
63
			/** This action is documented in modules/widgets/gravatar-profile.php */
64
			do_action( 'jetpack_stats_extra', 'widget_view', 'mailchimp_subscriber_popup' );
65
		}
66
67
68
		/**
69
		 * Deals with the settings when they are saved by the admin.
70
		 *
71
		 * @param array $new_instance New configuration values.
72
		 * @param array $old_instance Old configuration values.
73
		 *
74
		 * @return array
75
		 */
76
		public function update( $new_instance, $old_instance ) {
77
			$instance         = array();
78
			$instance['code'] = MailChimp_Subscriber_Popup::reversal( $new_instance['code'] );
79
80
			return $instance;
81
		}
82
83
		/**
84
		 * Output the mailchimp form.
85
		 *
86
		 * @return void
87
		 */
88
		public function mailchimp_form() {
89
			?>
90
			<div class="mailchimp_form">
91
				<div class="section">
92
					<div class="section_toggler">
93
						<span class="section_title"><?php echo esc_html__( 'Text Elements', 'jetpack' ); ?></span>
94
					</div>
95
					<div class="section_content">
96
						<div class="field">
97
							<label for="jetpack_mailchimp_email"><?php echo esc_html__( 'Email Placeholder', 'jetpack' ); ?></label>
98
							<input type="text" placeholder="<?php echo esc_html__( 'Enter your email', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_email">
99
						</div>
100
					</div>
101
				</div>
102
				<div class="section">
103
					<div class="section_toggler">
104
						<span class="section_title"><?php echo esc_html__( 'Notifications', 'jetpack' ); ?></span>
105
					</div>
106
					<div class="section_content">
107
						<div class="field">
108
							<label for="jetpack_mailchimp_processing_text"><?php echo esc_html__( 'Processing', 'jetpack' ); ?></label>
109
							<input type="text" placeholder="<?php echo esc_html__( 'Processing', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_processing_text">
110
						</div>
111
						<div class="field">
112
							<label for="jetpack_mailchimp_success_text"><?php echo esc_html__( 'Success text', 'jetpack' ); ?></label>
113
							<input type="text" placeholder="<?php echo esc_html__( 'Success! You\'re on the list.', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_success_text">
114
						</div>
115
						<div class="field">
116
							<label for="jetpack_mailchimp_error_text"><?php echo esc_html__( 'Error text', 'jetpack' ); ?></label>
117
							<input type="text" placeholder="<?php echo esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_error_text">
118
						</div>
119
					</div>
120
				</div>
121
				<div class="section">
122
					<div class="section_toggler">
123
						<span class="section_title"><?php echo esc_html__( 'Mailchimp Groups', 'jetpack' ); ?></span>
124
					</div>
125
					<div class="section_content">
126
						<a href=""><?php echo esc_html__( 'Learn about groups', 'jetpack' ); ?></a>
127
					</div>
128
				</div>
129
				<div class="section">
130
					<div class="section_toggler">
131
						<span class="section_title"><?php echo esc_html__( 'Signup Location Tracking', 'jetpack' ); ?></span>
132
					</div>
133
					<div class="section_content">
134
						<div class="field">
135
							<label for="jetpack_mailchimp_signup_tag"><?php echo esc_html__( 'Signup Field Tag', 'jetpack' ); ?></label>
136
							<input type="text" placeholder="<?php echo esc_html__( 'SIGNUP', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_signup_tag">
137
						</div>
138
						<div class="field">
139
							<label for="jetpack_mailchimp_signup_value"><?php echo esc_html__( 'Signup Field Value', 'jetpack' ); ?></label>
140
							<input type="text" placeholder="<?php echo esc_html__( 'website', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_signup_value">
141
						</div>
142
						<a href=""><?php echo esc_html__( 'Learn about signup location tracking(opens in a new tab)', 'jetpack' ); ?></a>
143
					</div>
144
				</div>
145
				<div class="section">
146
					<div class="section_toggler">
147
						<span class="section_title"><?php echo esc_html__( 'Mailchimp Groups', 'jetpack' ); ?></span>
148
					</div>
149
					<div class="section_content">
150
						<a href=""><?php echo esc_html__( 'Manage Connection', 'jetpack' ); ?></a>
151
					</div>
152
				</div>
153
			</div>
154
			<?php
155
		}
156
157
		/**
158
		 * Add the scripts for the widget.
159
		 *
160
		 * @return void
161
		 */
162
		public function enqueue_admin_scripts() {
163
			global $pagenow;
164
165
			if ( 'widgets.php' === $pagenow ) {
166
				wp_enqueue_script(
167
					'mailchimp-admin',
168
					Assets::get_file_url_for_environment(
169
						'_inc/build/widgets/mailchimp/js/admin.min.js',
170
						'modules/widgets/mailchimp/js/admin.js'
171
					),
172
					array(),
173
					'20200607',
174
					true
175
				);
176
			}
177
		}
178
179
180
		/**
181
		 * Displays the form for this widget on the Widgets page of the WP Admin area.
182
		 *
183
		 * @param array $instance Instance configuration.
184
		 *
185
		 * @return void
186
		 */
187
		public function form( $instance ) {
188
			$instance = wp_parse_args( $instance, array( 'code' => '' ) );
0 ignored issues
show
Documentation introduced by
array('code' => '') is of type array<string,string,{"code":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
190
			if ( empty( $instance['code'] ) ) {
191
				$this->mailchimp_form();
192
				return;
193
			}
194
195
			?>
196
197
			<p>
198
				<label for="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>">
199
					<?php printf( __( 'Code: <a href="%s" target="_blank">( ? )</a>', 'jetpack' ), 'https://en.support.wordpress.com/mailchimp/' ); ?>
200
				</label>
201
				<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'code' ) ); ?>" rows="3"><?php echo esc_textarea( $instance['code'] ); ?></textarea>
202
			</p>
203
			<p class="mailchimp_form_wrapper">
204
				<?php $this->mailchimp_form(); ?>
205
			</p>
206
207
			<?php
208
		}
209
210
	}
211
212
}
213