Completed
Push — fix/gulp-env ( ec4107...cf0b47 )
by
unknown
133:51 queued 124:05
created

Jetpack_Twitter_Timeline_Widget::update()   F

Complexity

Conditions 13
Paths 576

Size

Total Lines 73
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 44
nc 576
nop 2
dl 0
loc 73
rs 2.9041
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
/*
4
 * Based on Evolution Twitter Timeline
5
 * (http://wordpress.org/extend/plugins/evolution-twitter-timeline/)
6
 * For details on Twitter Timelines see:
7
 *  - https://twitter.com/settings/widgets
8
 *  - https://dev.twitter.com/docs/embedded-timelines
9
 */
10
11
/**
12
 * Register the widget for use in Appearance -> Widgets
13
 */
14
add_action( 'widgets_init', 'jetpack_twitter_timeline_widget_init' );
15
16
function jetpack_twitter_timeline_widget_init() {
17
	register_widget( 'Jetpack_Twitter_Timeline_Widget' );
18
}
19
20
class Jetpack_Twitter_Timeline_Widget extends WP_Widget {
21
	/**
22
	 * Register widget with WordPress.
23
	 */
24
	public function __construct() {
25
		parent::__construct(
26
			'twitter_timeline',
27
			/** This filter is documented in modules/widgets/facebook-likebox.php */
28
			apply_filters( 'jetpack_widget_name', esc_html__( 'Twitter Timeline', 'jetpack' ) ),
29
			array(
30
				'classname' => 'widget_twitter_timeline',
31
				'description' => __( 'Display an official Twitter Embedded Timeline widget.', 'jetpack' ),
32
				'customize_selective_refresh' => true,
33
			)
34
		);
35
36
		if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
37
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
38
		}
39
40
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
41
	}
42
43
	/**
44
	 * Enqueue scripts.
45
	 */
46
	public function enqueue_scripts() {
47
		wp_enqueue_script( 'jetpack-twitter-timeline' );
48
	}
49
50
	/**
51
	 * Enqueue Twitter's widget library.
52
	 *
53
	 * @deprecated
54
	 */
55
	public function library() {
56
		_deprecated_function( __METHOD__, '4.0.0' );
57
		wp_print_scripts( array( 'jetpack-twitter-timeline' ) );
58
	}
59
60
	/**
61
	 * Enqueue script to improve admin UI
62
	 */
63
	public function admin_scripts( $hook ) {
64
		// This is still 'widgets.php' when managing widgets via the Customizer.
65
		if ( 'widgets.php' === $hook ) {
66
			wp_enqueue_script( 'twitter-timeline-admin', plugins_url( 'twitter-timeline-admin.js', __FILE__ ) );
67
		}
68
	}
69
70
	/**
71
	 * Front-end display of widget.
72
	 *
73
	 * @see WP_Widget::widget()
74
	 *
75
	 * @param array $args     Widget arguments.
76
	 * @param array $instance Saved values from database.
77
	 */
78
	public function widget( $args, $instance ) {
79
		$instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 );
80
81
		echo $args['before_widget'];
82
83
		if ( isset( $instance['title'] ) ) {
84
			/** This filter is documented in core/src/wp-includes/default-widgets.php */
85
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
86
		}
87
88
		// Start tag output
89
		// This tag is transformed into the widget markup by Twitter's
90
		// widgets.js code
91
		echo '<a class="twitter-timeline"';
92
93
		$data_attribs = array(
94
			'width',
95
			'height',
96
			'theme',
97
			'link-color',
98
			'border-color',
99
			'tweet-limit',
100
			'lang'
101
		);
102
		foreach ( $data_attribs as $att ) {
103
			if ( ! empty( $instance[ $att ] ) && ! is_array( $instance[ $att ] ) ) {
104
				echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[ $att ] ) . '"';
105
			}
106
		}
107
108
		if ( ! empty( $instance['chrome'] ) && is_array( $instance['chrome'] ) ) {
109
			echo ' data-chrome="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"';
110
		}
111
112
		$type      = ( isset( $instance['type'] ) ? $instance['type'] : '' );
113
		$widget_id = ( isset( $instance['widget-id'] ) ? $instance['widget-id'] : '' );
114
		switch ( $type ) {
115
			case 'profile':
116
				echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
117
				break;
118
			case 'widget-id':
119
			default:
120
				echo ' data-widget-id="' . esc_attr( $widget_id ) . '"';
121
				break;
122
		}
123
124
		// End tag output
125
		echo '>';
126
127
		$timeline_placeholder = __( 'My Tweets', 'jetpack' );
128
129
		/**
130
		 * Filter the Timeline placeholder text.
131
		 *
132
		 * @module widgets
133
		 *
134
		 * @since 3.4.0
135
		 *
136
		 * @param string $timeline_placeholder Timeline placeholder text.
137
		 */
138
		$timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder );
139
140
		echo esc_html( $timeline_placeholder ) . '</a>';
141
142
		// End tag output
143
144
		echo $args['after_widget'];
145
146
		/** This action is documented in modules/widgets/social-media-icons.php */
147
		do_action( 'jetpack_bump_stats_extras', 'widget', 'twitter_timeline' );
148
	}
149
150
151
	/**
152
	 * Sanitize widget form values as they are saved.
153
	 *
154
	 * @see WP_Widget::update()
155
	 *
156
	 * @param array $new_instance Values just sent to be saved.
157
	 * @param array $old_instance Previously saved values from database.
158
	 *
159
	 * @return array Updated safe values to be saved.
160
	 */
161
	public function update( $new_instance, $old_instance ) {
162
		$instance = array();
163
164
		$instance['title'] = sanitize_text_field( $new_instance['title'] );
165
166
		$width = (int) $new_instance['width'];
167
		if ( $width ) {
168
			// From publish.twitter.com: 220 <= width <= 1200
169
			$instance['width'] = min( max( $width, 220 ), 1200 );
170
		} else {
171
			$instance['width'] = '';
172
		}
173
174
		$height = (int) $new_instance['height'];
175
		if ( $height ) {
176
			// From publish.twitter.com: height >= 200
177
			$instance['height'] = max( $height, 200 );
178
		} else {
179
			$instance['height'] = '';
180
		}
181
182
		$tweet_limit = (int) $new_instance['tweet-limit'];
183
		$instance['tweet-limit'] = ( $tweet_limit ? $tweet_limit : null );
184
185
		// If they entered something that might be a full URL, try to parse it out
186
		if ( is_string( $new_instance['widget-id'] ) ) {
187
			if ( preg_match(
188
				'#https?://twitter\.com/settings/widgets/(\d+)#s',
189
				$new_instance['widget-id'],
190
				$matches
191
			) ) {
192
				$new_instance['widget-id'] = $matches[1];
193
			}
194
		}
195
196
		$instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] );
197
198
		$hex_regex = '/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/';
199
		foreach ( array( 'link-color', 'border-color' ) as $color ) {
200
			$new_color = sanitize_text_field( $new_instance[ $color ] );
201
			if ( preg_match( $hex_regex, $new_color ) ) {
202
				$instance[ $color ] = $new_color;
203
			}
204
205
		}
206
207
		$instance['type'] = 'widget-id';
208
		if ( in_array( $new_instance['type'], array( 'widget-id', 'profile' ) ) ) {
209
			$instance['type'] = $new_instance['type'];
210
		}
211
212
		$instance['theme'] = 'light';
213
		if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) {
214
			$instance['theme'] = $new_instance['theme'];
215
		}
216
217
		$instance['chrome'] = array();
218
		$chrome_settings = array(
219
			'noheader',
220
			'nofooter',
221
			'noborders',
222
			'transparent'
223
		);
224
		if ( isset( $new_instance['chrome'] ) ) {
225
			foreach ( $new_instance['chrome'] as $chrome ) {
226
				if ( in_array( $chrome, $chrome_settings ) ) {
227
					$instance['chrome'][] = $chrome;
228
				}
229
			}
230
		}
231
232
		return $instance;
233
	}
234
235
	/**
236
 	 * Returns a link to the documentation for a feature of this widget on
237
 	 * Jetpack or WordPress.com.
238
 	 */
239
	public function get_docs_link( $hash = '' ) {
240
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
241
			$base_url = 'https://support.wordpress.com/widgets/twitter-timeline-widget/';
242
		} else {
243
			$base_url = 'https://jetpack.com/support/extra-sidebar-widgets/twitter-timeline-widget/';
244
		}
245
		return '<a href="' . $base_url . $hash . '" target="_blank">( ? )</a>';
246
	}
247
248
	/**
249
	 * Back-end widget form.
250
	 *
251
	 * @see WP_Widget::form()
252
	 *
253
	 * @param array $instance Previously saved values from database.
254
	 */
255
	public function form( $instance ) {
256
		$defaults = array(
257
			'title'        => esc_html__( 'Follow me on Twitter', 'jetpack' ),
258
			'width'        => '',
259
			'height'       => '400',
260
			'widget-id'    => '',
261
			'link-color'   => '#f96e5b',
262
			'border-color' => '#e8e8e8',
263
			'theme'        => 'light',
264
			'chrome'       => array(),
265
			'tweet-limit'  => null,
266
		);
267
268
		$instance = wp_parse_args( (array) $instance, $defaults );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $instance. This often makes code more readable.
Loading history...
269
270
		if ( empty( $instance['type'] ) ) {
271
			// Decide the correct widget type.  If this is a pre-existing
272
			// widget with a numeric widget ID, then the type should be
273
			// 'widget-id', otherwise it should be 'profile'.
274
			if ( ! empty( $instance['widget-id'] ) && is_numeric( $instance['widget-id'] ) ) {
275
				$instance['type'] = 'widget-id';
276
			} else {
277
				$instance['type'] = 'profile';
278
			}
279
		}
280
		?>
281
282
		<p>
283
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
284
				<?php esc_html_e( 'Title:', 'jetpack' ); ?>
285
			</label>
286
			<input
287
				class="widefat"
288
				id="<?php echo $this->get_field_id( 'title' ); ?>"
289
				name="<?php echo $this->get_field_name( 'title' ); ?>"
290
				type="text"
291
				value="<?php echo esc_attr( $instance['title'] ); ?>"
292
			/>
293
		</p>
294
295
		<p>
296
			<label for="<?php echo $this->get_field_id( 'width' ); ?>">
297
				<?php esc_html_e( 'Maximum Width (px; 220 to 1200):', 'jetpack' ); ?>
298
			</label>
299
			<input
300
				class="widefat"
301
				id="<?php echo $this->get_field_id( 'width' ); ?>"
302
				name="<?php echo $this->get_field_name( 'width' ); ?>"
303
				type="number" min="220" max="1200"
304
				value="<?php echo esc_attr( $instance['width'] ); ?>"
305
			/>
306
		</p>
307
308
		<p>
309
			<label for="<?php echo $this->get_field_id( 'height' ); ?>">
310
				<?php esc_html_e( 'Height (px; at least 200):', 'jetpack' ); ?>
311
			</label>
312
			<input
313
				class="widefat"
314
				id="<?php echo $this->get_field_id( 'height' ); ?>"
315
				name="<?php echo $this->get_field_name( 'height' ); ?>"
316
				type="number" min="200"
317
				value="<?php echo esc_attr( $instance['height'] ); ?>"
318
			/>
319
		</p>
320
321
		<p>
322
			<label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>">
323
				<?php esc_html_e( '# of Tweets Shown:', 'jetpack' ); ?>
324
			</label>
325
			<input
326
				class="widefat"
327
				id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"
328
				name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>"
329
				type="number" min="1" max="20"
330
				value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>"
331
			/>
332
		</p>
333
334
		<p class="jetpack-twitter-timeline-widget-type-container">
335
			<label for="<?php echo $this->get_field_id( 'type' ); ?>">
336
				<?php esc_html_e( 'Widget Type:', 'jetpack' ); ?>
337
				<?php echo $this->get_docs_link( '#widget-type' ); ?>
338
			</label>
339
			<select
340
				name="<?php echo $this->get_field_name( 'type' ); ?>"
341
				id="<?php echo $this->get_field_id( 'type' ); ?>"
342
				class="jetpack-twitter-timeline-widget-type widefat"
343
			>
344
				<option value="profile"<?php selected( $instance['type'], 'profile' ); ?>>
345
					<?php esc_html_e( 'Profile', 'jetpack' ); ?>
346
				</option>
347
				<option value="widget-id"<?php selected( $instance['type'], 'widget-id' ); ?>>
348
					<?php esc_html_e( 'Widget ID', 'jetpack' ); ?>
349
				</option>
350
			</select>
351
		</p>
352
353
		<p class="jetpack-twitter-timeline-widget-id-container">
354
			<label
355
				for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
356
				data-widget-type="widget-id"
357
				<?php echo ( 'widget-id' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
358
			>
359
				<?php esc_html_e( 'Widget ID:', 'jetpack' ); ?>
360
				<?php echo $this->get_docs_link( '#widget-id' ); ?>
361
			</label>
362
			<label
363
				for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
364
				data-widget-type="profile"
365
				<?php echo ( 'profile' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
366
			>
367
				<?php esc_html_e( 'Twitter Username:', 'jetpack' ); ?>
368
				<?php echo $this->get_docs_link( '#twitter-username' ); ?>
369
			</label>
370
			<input
371
				class="widefat"
372
				id="<?php echo $this->get_field_id( 'widget-id' ); ?>"
373
				name="<?php echo $this->get_field_name( 'widget-id' ); ?>"
374
				type="text"
375
				value="<?php echo esc_attr( $instance['widget-id'] ); ?>"
376
			/>
377
		</p>
378
379
		<p>
380
			<label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
381
				<?php esc_html_e( 'Layout Options:', 'jetpack' ); ?>
382
			</label>
383
			<br />
384
			<input
385
				type="checkbox"<?php checked( in_array( 'noheader', $instance['chrome'] ) ); ?>
386
				id="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"
387
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
388
				value="noheader"
389
			/>
390
			<label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
391
				<?php esc_html_e( 'No Header', 'jetpack' ); ?>
392
			</label>
393
			<br />
394
			<input
395
				type="checkbox"<?php checked( in_array( 'nofooter', $instance['chrome'] ) ); ?>
396
				id="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>"
397
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
398
				value="nofooter"
399
			/>
400
			<label for="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>">
401
				<?php esc_html_e( 'No Footer', 'jetpack' ); ?>
402
			</label>
403
			<br />
404
			<input
405
				type="checkbox"<?php checked( in_array( 'noborders', $instance['chrome'] ) ); ?>
406
				id="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>"
407
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
408
				value="noborders"
409
			/>
410
			<label for="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>">
411
				<?php esc_html_e( 'No Borders', 'jetpack' ); ?>
412
			</label>
413
			<br />
414
			<input
415
				type="checkbox"<?php checked( in_array( 'transparent', $instance['chrome'] ) ); ?>
416
				id="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>"
417
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
418
				value="transparent"
419
			/>
420
			<label for="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>">
421
				<?php esc_html_e( 'Transparent Background', 'jetpack' ); ?>
422
			</label>
423
		</p>
424
425
		<p>
426
			<label for="<?php echo $this->get_field_id( 'link-color' ); ?>">
427
				<?php _e( 'Link Color (hex):', 'jetpack' ); ?>
428
			</label>
429
			<input
430
				class="widefat"
431
				id="<?php echo $this->get_field_id( 'link-color' ); ?>"
432
				name="<?php echo $this->get_field_name( 'link-color' ); ?>"
433
				type="text"
434
				value="<?php echo esc_attr( $instance['link-color'] ); ?>"
435
			/>
436
		</p>
437
438
		<p>
439
			<label for="<?php echo $this->get_field_id( 'border-color' ); ?>">
440
				<?php _e( 'Border Color (hex):', 'jetpack' ); ?>
441
			</label>
442
			<input
443
				class="widefat"
444
				id="<?php echo $this->get_field_id( 'border-color' ); ?>"
445
				name="<?php echo $this->get_field_name( 'border-color' ); ?>"
446
				type="text"
447
				value="<?php echo esc_attr( $instance['border-color'] ); ?>"
448
			/>
449
		</p>
450
451
		<p>
452
			<label for="<?php echo $this->get_field_id( 'theme' ); ?>">
453
				<?php _e( 'Timeline Theme:', 'jetpack' ); ?>
454
			</label>
455
			<select
456
				name="<?php echo $this->get_field_name( 'theme' ); ?>"
457
				id="<?php echo $this->get_field_id( 'theme' ); ?>"
458
				class="widefat"
459
			>
460
				<option value="light"<?php selected( $instance['theme'], 'light' ); ?>>
461
					<?php esc_html_e( 'Light', 'jetpack' ); ?>
462
				</option>
463
				<option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>>
464
					<?php esc_html_e( 'Dark', 'jetpack' ); ?>
465
				</option>
466
			</select>
467
		</p>
468
	<?php
469
	}
470
}
471