No Description

twitter-timeline.php 1.9KB

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