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