Completed
Push — update/twitter-remove-widget-i... ( bf821e )
by
unknown
156:29 queued 146:47
created

Jetpack_Twitter_Timeline_Widget::widget()   C

Complexity

Conditions 9
Paths 36

Size

Total Lines 69
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 33
c 1
b 0
f 0
nc 36
nop 2
dl 0
loc 69
rs 6.2192

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