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

mailchimp_form()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 68
rs 8.6981
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if ( ! class_exists( 'Jetpack_MailChimp_Subscriber_Popup_Widget' ) ) {
4
5
	if ( ! class_exists( 'MailChimp_Subscriber_Popup' ) ) {
6
		include_once JETPACK__PLUGIN_DIR . 'modules/shortcodes/mailchimp.php';
7
	}
8
9
	//register MailChimp Subscriber Popup widget
10
	function jetpack_mailchimp_subscriber_popup_widget_init() {
11
		register_widget( 'Jetpack_MailChimp_Subscriber_Popup_Widget' );
12
	}
13
14
	add_action( 'widgets_init', 'jetpack_mailchimp_subscriber_popup_widget_init' );
15
16
	/**
17
	 * Add a MailChimp subscription form.
18
	 */
19
	class Jetpack_MailChimp_Subscriber_Popup_Widget extends WP_Widget {
20
21
		/**
22
		 * Constructor
23
		 */
24 View Code Duplication
		function __construct() {
25
			parent::__construct(
26
				'widget_mailchimp_subscriber_popup',
27
				/** This filter is documented in modules/widgets/facebook-likebox.php */
28
				apply_filters( 'jetpack_widget_name', __( 'MailChimp Subscriber Popup', 'jetpack' ) ),
29
				array(
30
					'classname'                   => 'widget_mailchimp_subscriber_popup',
31
					'description'                 => __( 'Allows displaying a popup subscription form to visitors.', 'jetpack' ),
32
					'customize_selective_refresh' => true,
33
				)
34
			);
35
		}
36
37
		/**
38
		 * Outputs the HTML for this widget.
39
		 *
40
		 * @param array $args     An array of standard parameters for widgets in this theme
41
		 * @param array $instance An array of settings for this widget instance
42
		 *
43
		 * @return void Echoes it's output
44
		 **/
45
		function widget( $args, $instance ) {
46
			$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...
47
48
			// Regular expresion that will match maichimp shortcode.
49
			$regex = '(\[mailchimp_subscriber_popup[^\]]+\])';
50
51
			// Check if the shortcode exists.
52
			preg_match( $regex, $instance['code'], $matches );
53
54
			// Process the shortcode only, if exists.
55
			if ( ! empty( $matches[0] ) ) {
56
				echo do_shortcode( $matches[0] );
57
			}
58
59
			/** This action is documented in modules/widgets/gravatar-profile.php */
60
			do_action( 'jetpack_stats_extra', 'widget_view', 'mailchimp_subscriber_popup' );
61
		}
62
63
64
		/**
65
		 * Deals with the settings when they are saved by the admin.
66
		 *
67
		 * @param array $new_instance New configuration values
68
		 * @param array $old_instance Old configuration values
69
		 *
70
		 * @return array
71
		 */
72
		public function update( $new_instance, $old_instance ) {
73
			$instance         = array();
74
			$instance['code'] = MailChimp_Subscriber_Popup::reversal( $new_instance['code'] );
75
76
			return $instance;
77
		}
78
79
		/**
80
		 * Output the mailchimp form.
81
		 *
82
		 * @return void
83
		 */
84
		public function mailchimp_form() {
85
			?>
86
			<div class="mailchimp_form">
87
				<div class="section">
88
					<div class="section_toggler">
89
						<span class="section_title"><?php echo esc_html__( 'Text Elements', 'jetpack' ); ?></span>
90
					</div>
91
					<div class="section_content">
92
						<div class="field">
93
							<label for="jetpack_mailchimp_email"><?php echo esc_html__( 'Email Placeholder', 'jetpack' ); ?></label>
94
							<input type="text" placeholder="<?php echo esc_html__( 'Enter your email', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_email">
95
						</div>
96
					</div>
97
				</div>
98
				<div class="section">
99
					<div class="section_toggler">
100
						<span class="section_title"><?php echo esc_html__( 'Notifications', 'jetpack' ); ?></span>
101
					</div>
102
					<div class="section_content">
103
						<div class="field">
104
							<label for="jetpack_mailchimp_processing_text"><?php echo esc_html__( 'Processing', 'jetpack' ); ?></label>
105
							<input type="text" placeholder="<?php echo esc_html__( 'Processing', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_processing_text">
106
						</div>
107
						<div class="field">
108
							<label for="jetpack_mailchimp_success_text"><?php echo esc_html__( 'Success text', 'jetpack' ); ?></label>
109
							<input type="text" placeholder="<?php echo esc_html__( 'Success! You\'re on the list.', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_success_text">
110
						</div>
111
						<div class="field">
112
							<label for="jetpack_mailchimp_error_text"><?php echo esc_html__( 'Error text', 'jetpack' ); ?></label>
113
							<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">
114
						</div>
115
					</div>
116
				</div>
117
				<div class="section">
118
					<div class="section_toggler">
119
						<span class="section_title"><?php echo esc_html__( 'Mailchimp Groups', 'jetpack' ); ?></span>
120
					</div>
121
					<div class="section_content">
122
						<a href=""><?php echo esc_html__( 'Learn about groups', 'jetpack' ); ?></a>
123
					</div>
124
				</div>
125
				<div class="section">
126
					<div class="section_toggler">
127
						<span class="section_title"><?php echo esc_html__( 'Signup Location Tracking', 'jetpack' ); ?></span>
128
					</div>
129
					<div class="section_content">
130
						<div class="field">
131
							<label for="jetpack_mailchimp_signup_tag"><?php echo esc_html__( 'Signup Field Tag', 'jetpack' ); ?></label>
132
							<input type="text" placeholder="<?php echo esc_html__( 'SIGNUP', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_signup_tag">
133
						</div>
134
						<div class="field">
135
							<label for="jetpack_mailchimp_signup_value"><?php echo esc_html__( 'Signup Field Value', 'jetpack' ); ?></label>
136
							<input type="text" placeholder="<?php echo esc_html__( 'website', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_signup_value">
137
						</div>
138
						<a href=""><?php echo esc_html__( 'Learn about signup location tracking(opens in a new tab)', 'jetpack' ); ?></a>
139
					</div>
140
				</div>
141
				<div class="section">
142
					<div class="section_toggler">
143
						<span class="section_title"><?php echo esc_html__( 'Mailchimp Groups', 'jetpack' ); ?></span>
144
					</div>
145
					<div class="section_content">
146
						<a href=""><?php echo esc_html__( 'Manage Connection', 'jetpack' ); ?></a>
147
					</div>
148
				</div>
149
			</div>
150
			<?php
151
		}
152
153
154
		/**
155
		 * Displays the form for this widget on the Widgets page of the WP Admin area.
156
		 *
157
		 * @param array $instance Instance configuration.
158
		 *
159
		 * @return void
160
		 */
161
		public function form( $instance ) {
162
			$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...
163
164
			if ( empty( $instance['code'] ) ) {
165
				$this->mailchimp_form();
166
				return;
167
			}
168
169
			?>
170
171
			<p>
172
				<label for="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>">
173
					<?php printf( __( 'Code: <a href="%s" target="_blank">( ? )</a>', 'jetpack' ), 'https://en.support.wordpress.com/mailchimp/' ); ?>
174
				</label>
175
				<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>
176
			</p>
177
			<p class="mailchimp_form_wrapper">
178
				<?php $this->mailchimp_form(); ?>
179
			</p>
180
181
			<?php
182
		}
183
184
	}
185
186
}
187