Completed
Push — fix/inline-docs-410 ( f96891...63b75c )
by
unknown
43:24 queued 33:40
created

Jetpack_Twitter_Timeline_Widget::widget()   C

Complexity

Conditions 10
Paths 72

Size

Total Lines 70
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
eloc 34
c 2
b 1
f 0
nc 72
nop 2
dl 0
loc 70
rs 5.9999

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