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

twitter-timeline.php ➔ twitter_timeline_shortcode()   C

Complexity

Conditions 7
Paths 17

Size

Total Lines 41
Code Lines 24

Duplication

Lines 6
Ratio 14.63 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 24
c 1
b 0
f 0
nc 17
nop 1
dl 6
loc 41
rs 6.7272
1
<?php
2
add_shortcode( 'twitter-timeline', 'twitter_timeline_shortcode' );
3
4
function twitter_timeline_shortcode( $atts ) {
5
	$default_atts = array(
6
		'username' => '',
7
		'id'       => '',
8
		'width'    => '450',
9
		'height'   => '282',
10
	);
11
12
	$atts = shortcode_atts( $default_atts, $atts, 'twitter-timeline' );
13
14
	$atts['username'] = preg_replace( '/[^A-Za-z0-9_]+/', '', $atts['username'] );
15
16
	if ( empty( $atts['username'] ) && ! is_numeric( $atts['id'] ) ) {
17
		return '<!-- ' . __( 'Must specify Twitter Timeline id or username.', 'jetpack' ) . ' -->';
18
	}
19
20
	$output = '<a class="twitter-timeline"';
21
22
	if ( is_numeric( $atts['width'] ) ) {
23
		$output .= ' data-width="' . esc_attr( $atts['width'] ) . '"';
24
	}
25 View Code Duplication
	if ( is_numeric( $atts['height'] ) ) {
26
		$output .= ' data-height="' . esc_attr( $atts['height'] ) . '"';
27
	}
28 View Code Duplication
	if ( is_numeric( $atts['id'] ) ) {
29
		$output .= ' data-widget-id="' . esc_attr( $atts['id'] ) . '"';
30
	}
31
	if ( ! empty( $atts['username'] ) ) {
32
		$output .= ' href="' . esc_url( 'https://twitter.com/' . $atts['username'] ) . '"';
33
	}
34
35
	$output .= '>';
36
37
	$output .= sprintf( __( 'Tweets by @%s', 'jetpack' ), $atts['username'] );
38
39
	$output .= '</a>';
40
41
	wp_enqueue_script( 'jetpack-twitter-timeline' );
42
43
	return $output;
44
}
45
46
function twitter_timeline_js() {
47
	if ( is_customize_preview() ) {
48
		wp_enqueue_script( 'jetpack-twitter-timeline' );
49
	}
50
}
51
add_action( 'wp_enqueue_scripts', 'twitter_timeline_js' );
52