| 1 |
<?php |
| 2 |
/** |
| 3 |
* Twitter Timeline Shortcode. |
| 4 |
* |
| 5 |
* Examples: |
| 6 |
* [twitter-timeline username=jetpack] |
| 7 |
* |
| 8 |
* @package Jetpack |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Render the Twitter shortcode. |
| 13 |
* |
| 14 |
* @param array $atts Shortcode attributes. |
| 15 |
*/ |
| 16 |
function twitter_timeline_shortcode( $atts ) { |
| 17 |
$default_atts = array( |
| 18 |
'username' => '', |
| 19 |
'id' => '', |
| 20 |
'width' => '450', |
| 21 |
'height' => '282', |
| 22 |
); |
| 23 |
|
| 24 |
$atts = shortcode_atts( $default_atts, $atts, 'twitter-timeline' ); |
| 25 |
|
| 26 |
$atts['username'] = preg_replace( '/[^A-Za-z0-9_]+/', '', $atts['username'] ); |
| 27 |
|
| 28 |
if ( empty( $atts['username'] ) && ! is_numeric( $atts['id'] ) ) { |
| 29 |
return '<!-- ' . __( 'Must specify Twitter Timeline id or username.', 'jetpack' ) . ' -->'; |
| 30 |
} |
| 31 |
|
| 32 |
$output = '<a class="twitter-timeline"'; |
| 33 |
|
| 34 |
/** This filter is documented in modules/shortcodes/tweet.php */ |
| 35 |
$partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' ); |
| 36 |
if ( ! empty( $partner ) ) { |
| 37 |
$output .= ' data-partner="' . esc_attr( $partner ) . '"'; |
| 38 |
} |
| 39 |
if ( is_numeric( $atts['width'] ) ) { |
| 40 |
$output .= ' data-width="' . esc_attr( $atts['width'] ) . '"'; |
| 41 |
} |
| 42 |
if ( is_numeric( $atts['height'] ) ) { |
| 43 |
$output .= ' data-height="' . esc_attr( $atts['height'] ) . '"'; |
| 44 |
} |
| 45 |
if ( is_numeric( $atts['id'] ) ) { |
| 46 |
$output .= ' data-widget-id="' . esc_attr( $atts['id'] ) . '"'; |
| 47 |
} |
| 48 |
if ( ! empty( $atts['username'] ) ) { |
| 49 |
$output .= ' href="' . esc_url( 'https://twitter.com/' . $atts['username'] ) . '"'; |
| 50 |
} |
| 51 |
|
| 52 |
$output .= '>'; |
| 53 |
|
| 54 |
$output .= sprintf( |
| 55 |
/* Translators: placeholder is a Twitter username. */ |
| 56 |
__( 'Tweets by @%s', 'jetpack' ), |
| 57 |
$atts['username'] |
| 58 |
); |
| 59 |
|
| 60 |
$output .= '</a>'; |
| 61 |
|
| 62 |
wp_enqueue_script( 'jetpack-twitter-timeline' ); |
| 63 |
|
| 64 |
return $output; |
| 65 |
} |
| 66 |
add_shortcode( 'twitter-timeline', 'twitter_timeline_shortcode' ); |
| 67 |
|
| 68 |
/** |
| 69 |
* Enqueue the js used by the Twitter shortcode. |
| 70 |
*/ |
| 71 |
function twitter_timeline_js() { |
| 72 |
if ( is_customize_preview() ) { |
| 73 |
wp_enqueue_script( 'jetpack-twitter-timeline' ); |
| 74 |
} |
| 75 |
} |
| 76 |
add_action( 'wp_enqueue_scripts', 'twitter_timeline_js' ); |
| 77 |
|