Completed
Push — instant-search-master ( 9522da...404687 )
by
unknown
07:50 queued 10s
created

Jetpack_Twitter_Timeline_Widget::update()   C

Complexity

Conditions 11
Paths 192

Size

Total Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 192
nop 2
dl 0
loc 78
rs 5.72
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
use Automattic\Jetpack\Assets;
12
13
/**
14
 * Register the widget for use in Appearance -> Widgets
15
 */
16
add_action( 'widgets_init', 'jetpack_twitter_timeline_widget_init' );
17
18
function jetpack_twitter_timeline_widget_init() {
19
	register_widget( 'Jetpack_Twitter_Timeline_Widget' );
20
}
21
22
class Jetpack_Twitter_Timeline_Widget extends WP_Widget {
23
	/**
24
	 * Register widget with WordPress.
25
	 */
26
	public function __construct() {
27
		parent::__construct(
28
			'twitter_timeline',
29
			/** This filter is documented in modules/widgets/facebook-likebox.php */
30
			apply_filters( 'jetpack_widget_name', esc_html__( 'Twitter Timeline', 'jetpack' ) ),
31
			array(
32
				'classname'                   => 'widget_twitter_timeline',
33
				'description'                 => __( 'Display an official Twitter Embedded Timeline widget.', 'jetpack' ),
34
				'customize_selective_refresh' => true,
35
			)
36
		);
37
38 View Code Duplication
		if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
39
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
40
		}
41
42
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
43
	}
44
45
	/**
46
	 * Enqueue scripts.
47
	 */
48
	public function enqueue_scripts() {
49
		wp_enqueue_script( 'jetpack-twitter-timeline' );
50
	}
51
52
	/**
53
	 * Enqueue Twitter's widget library.
54
	 *
55
	 * @deprecated
56
	 */
57
	public function library() {
58
		_deprecated_function( __METHOD__, '4.0.0' );
59
		wp_print_scripts( array( 'jetpack-twitter-timeline' ) );
60
	}
61
62
	/**
63
	 * Enqueue script to improve admin UI
64
	 */
65
	public function admin_scripts( $hook ) {
66
		// This is still 'widgets.php' when managing widgets via the Customizer.
67
		if ( 'widgets.php' === $hook ) {
68
			wp_enqueue_script(
69
				'twitter-timeline-admin',
70
				Assets::get_file_url_for_environment(
71
					'_inc/build/widgets/twitter-timeline-admin.min.js',
72
					'modules/widgets/twitter-timeline-admin.js'
73
				)
74
			);
75
		}
76
	}
77
78
	/**
79
	 * Front-end display of widget.
80
	 *
81
	 * @see WP_Widget::widget()
82
	 *
83
	 * @param array $args     Widget arguments.
84
	 * @param array $instance Saved values from database.
85
	 */
86
	public function widget( $args, $instance ) {
87
		// Twitter deprecated `data-widget-id` on 2018-05-25,
88
		// with cease support deadline on 2018-07-27.
89
		if ( isset( $instance['type'] ) && 'widget-id' === $instance['type'] ) {
90
			if ( current_user_can( 'edit_theme_options' ) ) {
91
				echo $args['before_widget'];
92
				echo $args['before_title'] . esc_html__( 'Twitter Timeline', 'jetpack' ) . $args['after_title'];
93
				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>';
94
				echo '<p>' . esc_html__( '(Only administrators will see this message.)', 'jetpack' ) . '</p>';
95
				echo $args['after_widget'];
96
			}
97
			return;
98
		}
99
100
		$instance['lang'] = substr( strtoupper( get_locale() ), 0, 2 );
101
102
		echo $args['before_widget'];
103
104
		$title = isset( $instance['title'] ) ? $instance['title'] : '';
105
106
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
107
		$title = apply_filters( 'widget_title', $title );
108
		if ( ! empty( $title ) ) {
109
			echo $args['before_title'] . $title . $args['after_title'];
110
		}
111
112
		if ( isset( $instance['type'] ) && 'widget-id' === $instance['type'] && current_user_can( 'edit_theme_options' ) ) {
113
			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>';
114
			echo '<p>' . esc_html__( '(Only administrators will see this message.)', 'jetpack' ) . '</p>';
115
		}
116
117
		// Start tag output
118
		// This tag is transformed into the widget markup by Twitter's
119
		// widgets.js code
120
		echo '<a class="twitter-timeline"';
121
122
		$data_attribs = array(
123
			'width',
124
			'height',
125
			'theme',
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
		/**
143
		 * Allow the activation of Do Not Track for the Twitter Timeline Widget.
144
		 *
145
		 * @see https://developer.twitter.com/en/docs/twitter-for-websites/timelines/guides/parameter-reference.html
146
		 *
147
		 * @module widgets
148
		 *
149
		 * @since 6.9.0
150
		 *
151
		 * @param bool false Should the Twitter Timeline use the DNT attribute? Default to false.
152
		 */
153
		$dnt = apply_filters( 'jetpack_twitter_timeline_default_dnt', false );
154
		if ( true === $dnt ) {
155
			echo ' data-dnt="true"';
156
		}
157
158
		if ( ! empty( $instance['chrome'] ) && is_array( $instance['chrome'] ) ) {
159
			echo ' data-chrome="' . esc_attr( join( ' ', $instance['chrome'] ) ) . '"';
160
		}
161
162
		$type      = ( isset( $instance['type'] ) ? $instance['type'] : '' );
163
		$widget_id = ( isset( $instance['widget-id'] ) ? $instance['widget-id'] : '' );
164
		switch ( $type ) {
165
			case 'profile':
166
				echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
167
				break;
168
			case 'widget-id':
169
			default:
170
				echo ' data-widget-id="' . esc_attr( $widget_id ) . '"';
171
				break;
172
		}
173
		echo ' href="https://twitter.com/' . esc_attr( $widget_id ) . '"';
174
175
		// End tag output
176
		echo '>';
177
178
		$timeline_placeholder = __( 'My Tweets', 'jetpack' );
179
180
		/**
181
		 * Filter the Timeline placeholder text.
182
		 *
183
		 * @module widgets
184
		 *
185
		 * @since 3.4.0
186
		 *
187
		 * @param string $timeline_placeholder Timeline placeholder text.
188
		 */
189
		$timeline_placeholder = apply_filters( 'jetpack_twitter_timeline_placeholder', $timeline_placeholder );
190
191
		echo esc_html( $timeline_placeholder ) . '</a>';
192
193
		// End tag output
194
195
		echo $args['after_widget'];
196
197
		/** This action is documented in modules/widgets/gravatar-profile.php */
198
		do_action( 'jetpack_stats_extra', 'widget_view', 'twitter_timeline' );
199
	}
200
201
202
	/**
203
	 * Sanitize widget form values as they are saved.
204
	 *
205
	 * @see WP_Widget::update()
206
	 *
207
	 * @param array $new_instance Values just sent to be saved.
208
	 * @param array $old_instance Previously saved values from database.
209
	 *
210
	 * @return array Updated safe values to be saved.
211
	 */
212
	public function update( $new_instance, $old_instance ) {
213
		$instance = array();
214
215
		$instance['title'] = sanitize_text_field( $new_instance['title'] );
216
217
		$width = (int) $new_instance['width'];
218
		if ( $width ) {
219
			// From publish.twitter.com: 220 <= width <= 1200
220
			$instance['width'] = min( max( $width, 220 ), 1200 );
221
		} else {
222
			$instance['width'] = '';
223
		}
224
225
		$height = (int) $new_instance['height'];
226
		if ( $height ) {
227
			// From publish.twitter.com: height >= 200
228
			$instance['height'] = max( $height, 200 );
229
		} else {
230
			$instance['height'] = '';
231
		}
232
233
		$tweet_limit = (int) $new_instance['tweet-limit'];
234
		if ( $tweet_limit ) {
235
			$instance['tweet-limit'] = min( max( $tweet_limit, 1 ), 20 );
236
			/**
237
			 * A timeline with a specified limit is expanded to the height of those Tweets.
238
			 * The specified height value no longer applies, so reject the height value
239
			 * when a valid limit is set: a widget attempting to save both limit 5 and
240
			 * height 400 would be saved with just limit 5.
241
			 */
242
			$instance['height'] = '';
243
		} else {
244
			$instance['tweet-limit'] = null;
245
		}
246
247
		// If they entered something that might be a full URL, try to parse it out
248
		if ( is_string( $new_instance['widget-id'] ) ) {
249
			if ( preg_match(
250
				'#https?://twitter\.com/settings/widgets/(\d+)#s',
251
				$new_instance['widget-id'],
252
				$matches
253
			) ) {
254
				$new_instance['widget-id'] = $matches[1];
255
			}
256
		}
257
258
		$instance['widget-id'] = sanitize_text_field( $new_instance['widget-id'] );
259
260
		$new_border_color = sanitize_hex_color( $new_instance['border-color'] );
261
		if ( ! empty( $new_border_color ) ) {
262
			$instance['border-color'] = $new_border_color;
263
		}
264
265
		$instance['type'] = 'profile';
266
267
		$instance['theme'] = 'light';
268
		if ( in_array( $new_instance['theme'], array( 'light', 'dark' ) ) ) {
269
			$instance['theme'] = $new_instance['theme'];
270
		}
271
272
		$instance['chrome'] = array();
273
		$chrome_settings    = array(
274
			'noheader',
275
			'nofooter',
276
			'noborders',
277
			'transparent',
278
			'noscrollbar',
279
		);
280
		if ( isset( $new_instance['chrome'] ) ) {
281
			foreach ( $new_instance['chrome'] as $chrome ) {
282
				if ( in_array( $chrome, $chrome_settings ) ) {
283
					$instance['chrome'][] = $chrome;
284
				}
285
			}
286
		}
287
288
		return $instance;
289
	}
290
291
	/**
292
	 * Returns a link to the documentation for a feature of this widget on
293
	 * Jetpack or WordPress.com.
294
	 */
295
	public function get_docs_link( $hash = '' ) {
296
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
297
			$base_url = 'https://support.wordpress.com/widgets/twitter-timeline-widget/';
298
		} else {
299
			$base_url = 'https://jetpack.com/support/extra-sidebar-widgets/twitter-timeline-widget/';
300
		}
301
		return '<a href="' . $base_url . $hash . '" target="_blank">( ? )</a>';
302
	}
303
304
	/**
305
	 * Back end widget form.
306
	 *
307
	 * @see WP_Widget::form()
308
	 *
309
	 * @param array $instance Previously saved values from database.
310
	 */
311
	public function form( $instance ) {
312
		$defaults = array(
313
			'title'        => esc_html__( 'Follow me on Twitter', 'jetpack' ),
314
			'width'        => '',
315
			'height'       => '400',
316
			'type'         => 'profile',
317
			'widget-id'    => '',
318
			'border-color' => '#e8e8e8',
319
			'theme'        => 'light',
320
			'chrome'       => array(),
321
			'tweet-limit'  => null,
322
		);
323
324
		$instance = wp_parse_args( (array) $instance, $defaults );
325
326
		if ( 'widget-id' === $instance['type'] ) {
327
			$instance['widget-id'] = '';
328
		}
329
330
		$instance['type'] = 'profile';
331
		?>
332
333
		<p>
334
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
335
				<?php esc_html_e( 'Title:', 'jetpack' ); ?>
336
			</label>
337
			<input
338
				class="widefat"
339
				id="<?php echo $this->get_field_id( 'title' ); ?>"
340
				name="<?php echo $this->get_field_name( 'title' ); ?>"
341
				type="text"
342
				value="<?php echo esc_attr( $instance['title'] ); ?>"
343
			/>
344
		</p>
345
346
		<p>
347
			<label for="<?php echo $this->get_field_id( 'width' ); ?>">
348
				<?php esc_html_e( 'Maximum Width (px; 220 to 1200):', 'jetpack' ); ?>
349
			</label>
350
			<input
351
				class="widefat"
352
				id="<?php echo $this->get_field_id( 'width' ); ?>"
353
				name="<?php echo $this->get_field_name( 'width' ); ?>"
354
				type="number" min="220" max="1200"
355
				value="<?php echo esc_attr( $instance['width'] ); ?>"
356
			/>
357
		</p>
358
359
		<p>
360
			<label for="<?php echo $this->get_field_id( 'height' ); ?>">
361
				<?php esc_html_e( 'Height (px; at least 200):', 'jetpack' ); ?>
362
			</label>
363
			<input
364
				class="widefat"
365
				id="<?php echo $this->get_field_id( 'height' ); ?>"
366
				name="<?php echo $this->get_field_name( 'height' ); ?>"
367
				type="number" min="200"
368
				value="<?php echo esc_attr( $instance['height'] ); ?>"
369
			/>
370
		</p>
371
372
		<p>
373
			<label for="<?php echo $this->get_field_id( 'tweet-limit' ); ?>">
374
				<?php esc_html_e( '# of Tweets Shown (1 to 20):', 'jetpack' ); ?>
375
			</label>
376
			<input
377
				class="widefat"
378
				id="<?php echo $this->get_field_id( 'tweet-limit' ); ?>"
379
				name="<?php echo $this->get_field_name( 'tweet-limit' ); ?>"
380
				type="number" min="1" max="20"
381
				value="<?php echo esc_attr( $instance['tweet-limit'] ); ?>"
382
			/>
383
		</p>
384
385
		<p class="jetpack-twitter-timeline-widget-id-container">
386
			<label for="<?php echo $this->get_field_id( 'widget-id' ); ?>">
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( 'noscrollbar', $instance['chrome'] ) ); ?>
436
				id="<?php echo $this->get_field_id( 'chrome-noscrollbar' ); ?>"
437
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
438
				value="noscrollbar"
439
			/>
440
			<label for="<?php echo $this->get_field_id( 'chrome-noscrollbar' ); ?>">
441
				<?php esc_html_e( 'No Scrollbar', 'jetpack' ); ?>
442
			</label>
443
			<br />
444
			<input
445
				type="checkbox"<?php checked( in_array( 'transparent', $instance['chrome'] ) ); ?>
446
				id="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>"
447
				name="<?php echo $this->get_field_name( 'chrome' ); ?>[]"
448
				value="transparent"
449
			/>
450
			<label for="<?php echo $this->get_field_id( 'chrome-transparent' ); ?>">
451
				<?php esc_html_e( 'Transparent Background', 'jetpack' ); ?>
452
			</label>
453
		</p>
454
455
		<p>
456
			<label for="<?php echo $this->get_field_id( 'border-color' ); ?>">
457
				<?php _e( 'Border Color (hex):', 'jetpack' ); ?>
458
			</label>
459
			<input
460
				class="widefat"
461
				id="<?php echo $this->get_field_id( 'border-color' ); ?>"
462
				name="<?php echo $this->get_field_name( 'border-color' ); ?>"
463
				type="text"
464
				value="<?php echo esc_attr( $instance['border-color'] ); ?>"
465
			/>
466
		</p>
467
468
		<p>
469
			<label for="<?php echo $this->get_field_id( 'theme' ); ?>">
470
				<?php _e( 'Timeline Theme:', 'jetpack' ); ?>
471
			</label>
472
			<select
473
				name="<?php echo $this->get_field_name( 'theme' ); ?>"
474
				id="<?php echo $this->get_field_id( 'theme' ); ?>"
475
				class="widefat"
476
			>
477
				<option value="light"<?php selected( $instance['theme'], 'light' ); ?>>
478
					<?php esc_html_e( 'Light', 'jetpack' ); ?>
479
				</option>
480
				<option value="dark"<?php selected( $instance['theme'], 'dark' ); ?>>
481
					<?php esc_html_e( 'Dark', 'jetpack' ); ?>
482
				</option>
483
			</select>
484
		</p>
485
	<?php
486
	}
487
}
488