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