Completed
Push — try/gutenberg-separate-jetpack... ( e8dd3e...f0efb9 )
by Bernhard
39:26 queued 23:22
created

Jetpack_Twitter_Timeline_Widget::update()   D

Complexity

Conditions 12
Paths 288

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

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