Completed
Push — fix/jetpack-banner-close-butto... ( 9d1938...11c4b4 )
by
unknown
21:19 queued 13:02
created

Jetpack_Twitter_Timeline_Widget::update()   F

Complexity

Conditions 13
Paths 576

Size

Total Lines 84
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 48
nc 576
nop 2
dl 0
loc 84
rs 2.605
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
 * (https://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
		$title = isset( $instance['title'] ) ? $instance['title'] : '';
84
		
85
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
86
		$title = apply_filters( 'widget_title', $title );
87
		if ( ! empty( $title ) ) {
88
			echo $args['before_title'] . $title . $args['after_title'];
89
		}
90
91
		// Start tag output
92
		// This tag is transformed into the widget markup by Twitter's
93
		// widgets.js code
94
		echo '<a class="twitter-timeline"';
95
96
		$data_attribs = array(
97
			'width',
98
			'height',
99
			'theme',
100
			'link-color',
101
			'border-color',
102
			'tweet-limit',
103
			'lang'
104
		);
105
		foreach ( $data_attribs as $att ) {
106
			if ( ! empty( $instance[ $att ] ) && ! is_array( $instance[ $att ] ) ) {
107
				echo ' data-' . esc_attr( $att ) . '="' . esc_attr( $instance[ $att ] ) . '"';
108
			}
109
		}
110
111
		/** This filter is documented in modules/shortcodes/tweet.php */
112
		$partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
113
		if ( ! empty( $partner ) ) {
114
			echo ' data-partner="' . esc_attr( $partner ) . '"';
115
		}
116
117
		if ( ! empty( $instance['chrome'] ) && is_array( $instance['chrome'] ) ) {
118
			echo ' data-chrome="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"';
119
		}
120
121
		$type      = ( isset( $instance['type'] ) ? $instance['type'] : '' );
122
		$widget_id = ( isset( $instance['widget-id'] ) ? $instance['widget-id'] : '' );
123
		switch ( $type ) {
124
			case 'profile':
125
				echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
126
				break;
127
			case 'widget-id':
128
			default:
129
				echo ' data-widget-id="' . esc_attr( $widget_id ) . '"';
130
				break;
131
		}
132
133
		// End tag output
134
		echo '>';
135
136
		$timeline_placeholder = __( 'My Tweets', 'jetpack' );
137
138
		/**
139
		 * Filter the Timeline placeholder text.
140
		 *
141
		 * @module widgets
142
		 *
143
		 * @since 3.4.0
144
		 *
145
		 * @param string $timeline_placeholder Timeline placeholder text.
146
		 */
147
		$timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder );
148
149
		echo esc_html( $timeline_placeholder ) . '</a>';
150
151
		// End tag output
152
153
		echo $args['after_widget'];
154
155
		/** This action is documented in modules/widgets/gravatar-profile.php */
156
		do_action( 'jetpack_stats_extra', 'widget_view', 'twitter_timeline' );
157
	}
158
159
160
	/**
161
	 * Sanitize widget form values as they are saved.
162
	 *
163
	 * @see WP_Widget::update()
164
	 *
165
	 * @param array $new_instance Values just sent to be saved.
166
	 * @param array $old_instance Previously saved values from database.
167
	 *
168
	 * @return array Updated safe values to be saved.
169
	 */
170
	public function update( $new_instance, $old_instance ) {
171
		$instance = array();
172
173
		$instance['title'] = sanitize_text_field( $new_instance['title'] );
174
175
		$width = (int) $new_instance['width'];
176
		if ( $width ) {
177
			// From publish.twitter.com: 220 <= width <= 1200
178
			$instance['width'] = min( max( $width, 220 ), 1200 );
179
		} else {
180
			$instance['width'] = '';
181
		}
182
183
		$height = (int) $new_instance['height'];
184
		if ( $height ) {
185
			// From publish.twitter.com: height >= 200
186
			$instance['height'] = max( $height, 200 );
187
		} else {
188
			$instance['height'] = '';
189
		}
190
191
		$tweet_limit = (int) $new_instance['tweet-limit'];
192
		if ( $tweet_limit ) {
193
			$instance['tweet-limit'] = min( max( $tweet_limit, 1 ), 20 );
194
			/**
195
			 * A timeline with a specified limit is expanded to the height of those Tweets.
196
			 * The specified height value no longer applies, so reject the height value
197
			 * when a valid limit is set: a widget attempting to save both limit 5 and
198
			 * height 400 would be saved with just limit 5.
199
			 */
200
			$instance['height'] = '';
201
		} else {
202
			$instance['tweet-limit'] = null;
203
		}
204
205
		// If they entered something that might be a full URL, try to parse it out
206
		if ( is_string( $new_instance['widget-id'] ) ) {
207
			if ( preg_match(
208
				'#https?://twitter\.com/settings/widgets/(\d+)#s',
209
				$new_instance['widget-id'],
210
				$matches
211
			) ) {
212
				$new_instance['widget-id'] = $matches[1];
213
			}
214
		}
215
216
		$instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] );
217
218
		$hex_regex = '/#([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/';
219
		foreach ( array( 'link-color', 'border-color' ) as $color ) {
220
			$new_color = sanitize_text_field( $new_instance[ $color ] );
221
			if ( preg_match( $hex_regex, $new_color ) ) {
222
				$instance[ $color ] = $new_color;
223
			}
224
225
		}
226
227
		$instance['type'] = 'widget-id';
228
		if ( in_array( $new_instance['type'], array( 'widget-id', 'profile' ) ) ) {
229
			$instance['type'] = $new_instance['type'];
230
		}
231
232
		$instance['theme'] = 'light';
233
		if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) {
234
			$instance['theme'] = $new_instance['theme'];
235
		}
236
237
		$instance['chrome'] = array();
238
		$chrome_settings = array(
239
			'noheader',
240
			'nofooter',
241
			'noborders',
242
			'transparent'
243
		);
244
		if ( isset( $new_instance['chrome'] ) ) {
245
			foreach ( $new_instance['chrome'] as $chrome ) {
246
				if ( in_array( $chrome, $chrome_settings ) ) {
247
					$instance['chrome'][] = $chrome;
248
				}
249
			}
250
		}
251
252
		return $instance;
253
	}
254
255
	/**
256
 	 * Returns a link to the documentation for a feature of this widget on
257
 	 * Jetpack or WordPress.com.
258
 	 */
259
	public function get_docs_link( $hash = '' ) {
260
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
261
			$base_url = 'https://support.wordpress.com/widgets/twitter-timeline-widget/';
262
		} else {
263
			$base_url = 'https://jetpack.com/support/extra-sidebar-widgets/twitter-timeline-widget/';
264
		}
265
		return '<a href="' . $base_url . $hash . '" target="_blank">( ? )</a>';
266
	}
267
268
	/**
269
	 * Back-end widget form.
270
	 *
271
	 * @see WP_Widget::form()
272
	 *
273
	 * @param array $instance Previously saved values from database.
274
	 */
275
	public function form( $instance ) {
276
		$defaults = array(
277
			'title'        => esc_html__( 'Follow me on Twitter', 'jetpack' ),
278
			'width'        => '',
279
			'height'       => '400',
280
			'widget-id'    => '',
281
			'link-color'   => '#f96e5b',
282
			'border-color' => '#e8e8e8',
283
			'theme'        => 'light',
284
			'chrome'       => array(),
285
			'tweet-limit'  => null,
286
		);
287
288
		$instance = wp_parse_args( (array) $instance, $defaults );
289
290
		if ( empty( $instance['type'] ) ) {
291
			// Decide the correct widget type.  If this is a pre-existing
292
			// widget with a numeric widget ID, then the type should be
293
			// 'widget-id', otherwise it should be 'profile'.
294
			if ( ! empty( $instance['widget-id'] ) && is_numeric( $instance['widget-id'] ) ) {
295
				$instance['type'] = 'widget-id';
296
			} else {
297
				$instance['type'] = 'profile';
298
			}
299
		}
300
		?>
301
302
		<p>
303
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
304
				<?php esc_html_e( 'Title:', 'jetpack' ); ?>
305
			</label>
306
			<input
307
				class="widefat"
308
				id="<?php echo $this->get_field_id( 'title' ); ?>"
309
				name="<?php echo $this->get_field_name( 'title' ); ?>"
310
				type="text"
311
				value="<?php echo esc_attr( $instance['title'] ); ?>"
312
			/>
313
		</p>
314
315
		<p>
316
			<label for="<?php echo $this->get_field_id( 'width' ); ?>">
317
				<?php esc_html_e( 'Maximum Width (px; 220 to 1200):', 'jetpack' ); ?>
318
			</label>
319
			<input
320
				class="widefat"
321
				id="<?php echo $this->get_field_id( 'width' ); ?>"
322
				name="<?php echo $this->get_field_name( 'width' ); ?>"
323
				type="number" min="220" max="1200"
324
				value="<?php echo esc_attr( $instance['width'] ); ?>"
325
			/>
326
		</p>
327
328
		<p>
329
			<label for="<?php echo $this->get_field_id( 'height' ); ?>">
330
				<?php esc_html_e( 'Height (px; at least 200):', 'jetpack' ); ?>
331
			</label>
332
			<input
333
				class="widefat"
334
				id="<?php echo $this->get_field_id( 'height' ); ?>"
335
				name="<?php echo $this->get_field_name( 'height' ); ?>"
336
				type="number" min="200"
337
				value="<?php echo esc_attr( $instance['height'] ); ?>"
338
			/>
339
		</p>
340
341
		<p>
342
			<label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>">
343
				<?php esc_html_e( '# of Tweets Shown (1 to 20):', 'jetpack' ); ?>
344
			</label>
345
			<input
346
				class="widefat"
347
				id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"
348
				name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>"
349
				type="number" min="1" max="20"
350
				value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>"
351
			/>
352
		</p>
353
354
		<p class="jetpack-twitter-timeline-widget-type-container">
355
			<label for="<?php echo $this->get_field_id( 'type' ); ?>">
356
				<?php esc_html_e( 'Widget Type:', 'jetpack' ); ?>
357
				<?php echo $this->get_docs_link( '#widget-type' ); ?>
358
			</label>
359
			<select
360
				name="<?php echo $this->get_field_name( 'type' ); ?>"
361
				id="<?php echo $this->get_field_id( 'type' ); ?>"
362
				class="jetpack-twitter-timeline-widget-type widefat"
363
			>
364
				<option value="profile"<?php selected( $instance['type'], 'profile' ); ?>>
365
					<?php esc_html_e( 'Profile', 'jetpack' ); ?>
366
				</option>
367
				<option value="widget-id"<?php selected( $instance['type'], 'widget-id' ); ?>>
368
					<?php esc_html_e( 'Widget ID', 'jetpack' ); ?>
369
				</option>
370
			</select>
371
		</p>
372
373
		<p class="jetpack-twitter-timeline-widget-id-container">
374
			<label
375
				for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
376
				data-widget-type="widget-id"
377
				<?php echo ( 'widget-id' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
378
			>
379
				<?php esc_html_e( 'Widget ID:', 'jetpack' ); ?>
380
				<?php echo $this->get_docs_link( '#widget-id' ); ?>
381
			</label>
382
			<label
383
				for="<?php echo $this->get_field_id( 'widget-id' ); ?>"
384
				data-widget-type="profile"
385
				<?php echo ( 'profile' === $instance['type'] ? '' : 'style="display: none;"' ); ?>
386
			>
387
				<?php esc_html_e( 'Twitter Username:', 'jetpack' ); ?>
388
				<?php echo $this->get_docs_link( '#twitter-username' ); ?>
389
			</label>
390
			<input
391
				class="widefat"
392
				id="<?php echo $this->get_field_id( 'widget-id' ); ?>"
393
				name="<?php echo $this->get_field_name( 'widget-id' ); ?>"
394
				type="text"
395
				value="<?php echo esc_attr( $instance['widget-id'] ); ?>"
396
			/>
397
		</p>
398
399
		<p>
400
			<label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
401
				<?php esc_html_e( 'Layout Options:', 'jetpack' ); ?>
402
			</label>
403
			<br />
404
			<input
405
				type="checkbox"<?php checked( in_array( 'noheader', $instance['chrome'] ) ); ?>
406
				id="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>"
407
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
408
				value="noheader"
409
			/>
410
			<label for="<?php echo $this->get_field_id( 'chrome-noheader' ); ?>">
411
				<?php esc_html_e( 'No Header', 'jetpack' ); ?>
412
			</label>
413
			<br />
414
			<input
415
				type="checkbox"<?php checked( in_array( 'nofooter', $instance['chrome'] ) ); ?>
416
				id="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>"
417
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
418
				value="nofooter"
419
			/>
420
			<label for="<?php echo $this->get_field_id( 'chrome-nofooter' ); ?>">
421
				<?php esc_html_e( 'No Footer', 'jetpack' ); ?>
422
			</label>
423
			<br />
424
			<input
425
				type="checkbox"<?php checked( in_array( 'noborders', $instance['chrome'] ) ); ?>
426
				id="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>"
427
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
428
				value="noborders"
429
			/>
430
			<label for="<?php echo $this->get_field_id( 'chrome-noborders' ); ?>">
431
				<?php esc_html_e( 'No Borders', 'jetpack' ); ?>
432
			</label>
433
			<br />
434
			<input
435
				type="checkbox"<?php checked( in_array( 'transparent', $instance['chrome'] ) ); ?>
436
				id="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>"
437
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
438
				value="transparent"
439
			/>
440
			<label for="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>">
441
				<?php esc_html_e( 'Transparent Background', 'jetpack' ); ?>
442
			</label>
443
		</p>
444
445
		<p>
446
			<label for="<?php echo $this->get_field_id( 'link-color' ); ?>">
447
				<?php _e( 'Link Color (hex):', 'jetpack' ); ?>
448
			</label>
449
			<input
450
				class="widefat"
451
				id="<?php echo $this->get_field_id( 'link-color' ); ?>"
452
				name="<?php echo $this->get_field_name( 'link-color' ); ?>"
453
				type="text"
454
				value="<?php echo esc_attr( $instance['link-color'] ); ?>"
455
			/>
456
		</p>
457
458
		<p>
459
			<label for="<?php echo $this->get_field_id( 'border-color' ); ?>">
460
				<?php _e( 'Border Color (hex):', 'jetpack' ); ?>
461
			</label>
462
			<input
463
				class="widefat"
464
				id="<?php echo $this->get_field_id( 'border-color' ); ?>"
465
				name="<?php echo $this->get_field_name( 'border-color' ); ?>"
466
				type="text"
467
				value="<?php echo esc_attr( $instance['border-color'] ); ?>"
468
			/>
469
		</p>
470
471
		<p>
472
			<label for="<?php echo $this->get_field_id( 'theme' ); ?>">
473
				<?php _e( 'Timeline Theme:', 'jetpack' ); ?>
474
			</label>
475
			<select
476
				name="<?php echo $this->get_field_name( 'theme' ); ?>"
477
				id="<?php echo $this->get_field_id( 'theme' ); ?>"
478
				class="widefat"
479
			>
480
				<option value="light"<?php selected( $instance['theme'], 'light' ); ?>>
481
					<?php esc_html_e( 'Light', 'jetpack' ); ?>
482
				</option>
483
				<option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>>
484
					<?php esc_html_e( 'Dark', 'jetpack' ); ?>
485
				</option>
486
			</select>
487
		</p>
488
	<?php
489
	}
490
}
491