summaryrefslogtreecommitdiff
blob: bce9d2a9eac073557150bc1fb2a44f733a89f7f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
add_shortcode( 'twitter-timeline', 'twitter_timeline_shortcode' );

function twitter_timeline_shortcode( $atts ) {
	$default_atts = array(
		'username' => '',
		'id'       => '',
		'width'    => '450',
		'height'   => '282',
	);

	$atts = shortcode_atts( $default_atts, $atts, 'twitter-timeline' );

	$atts['username'] = preg_replace( '/[^A-Za-z0-9_]+/', '', $atts['username'] );

	if ( empty( $atts['username'] ) && ! is_numeric( $atts['id'] ) ) {
		return '<!-- ' . __( 'Must specify Twitter Timeline id or username.', 'jetpack' ) . ' -->';
	}

	$output = '<a class="twitter-timeline"';

	if ( is_numeric( $atts['width'] ) ) {
		$output .= ' data-width="' . esc_attr( $atts['width'] ) . '"';
	}
	if ( is_numeric( $atts['height'] ) ) {
		$output .= ' data-height="' . esc_attr( $atts['height'] ) . '"';
	}
	if ( is_numeric( $atts['id'] ) ) {
		$output .= ' data-widget-id="' . esc_attr( $atts['id'] ) . '"';
	}
	if ( ! empty( $atts['username'] ) ) {
		$output .= ' href="' . esc_url( 'https://twitter.com/' . $atts['username'] ) . '"';
	}

	$output .= '>';

	$output .= sprintf( __( 'Tweets by @%s', 'jetpack' ), $atts['username'] );

	$output .= '</a>';

	wp_enqueue_script( 'jetpack-twitter-timeline' );

	return $output;
}

function twitter_timeline_js() {
	if ( is_customize_preview() ) {
		wp_enqueue_script( 'jetpack-twitter-timeline' );
	}
}
add_action( 'wp_enqueue_scripts', 'twitter_timeline_js' );