tweet.php
| 1 | <?php |
| 2 | /** |
| 3 | * Tweet shortcode. |
| 4 | * Params map to key value pairs, and all but tweet are optional: |
| 5 | * tweet = id or permalink url* (Required) |
| 6 | * align = none|left|right|center |
| 7 | * width = number in pixels example: width="300" |
| 8 | * lang = en|fr|de|ko|etc... language country code. |
| 9 | * hide_thread = true | false ** |
| 10 | * hide_media = true | false ** |
| 11 | * |
| 12 | * Basic: |
| 13 | * [tweet https://twitter.com/jack/statuses/20 width="350"] |
| 14 | * |
| 15 | * More parameters and another tweet syntax admitted: |
| 16 | * [tweet tweet="https://twitter.com/jack/statuses/20" align="left" width="350" align="center" lang="es"] |
| 17 | */ |
| 18 | |
| 19 | add_shortcode( 'tweet', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode' ) ); |
| 20 | |
| 21 | class Jetpack_Tweet { |
| 22 | |
| 23 | static $provider_args; |
| 24 | |
| 25 | /** |
| 26 | * Parse shortcode arguments and render its output. |
| 27 | * |
| 28 | * @since 4.5.0 |
| 29 | * |
| 30 | * @param array $atts Shortcode parameters. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | static public function jetpack_tweet_shortcode( $atts ) { |
| 35 | $default_atts = array( |
| 36 | 'tweet' => '', |
| 37 | 'align' => 'none', |
| 38 | 'width' => '', |
| 39 | 'lang' => 'en', |
| 40 | 'hide_thread' => 'false', |
| 41 | 'hide_media' => 'false', |
| 42 | ); |
| 43 | |
| 44 | $attr = shortcode_atts( $default_atts, $atts ); |
| 45 | |
| 46 | self::$provider_args = $attr; |
| 47 | |
| 48 | // figure out the tweet id for the requested tweet |
| 49 | // supporting both omitted attributes and tweet="tweet_id" |
| 50 | // and supporting both an id and a URL |
| 51 | if ( empty( $attr['tweet'] ) && ! empty( $atts[0] ) ) { |
| 52 | $attr['tweet'] = $atts[0]; |
| 53 | } |
| 54 | |
| 55 | if ( ctype_digit( $attr['tweet'] ) ) { |
| 56 | $id = 'https://twitter.com/jetpack/status/' . $attr['tweet']; |
| 57 | } else { |
| 58 | preg_match( '/^http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)$/', $attr['tweet'], $urlbits ); |
| 59 | |
| 60 | if ( isset( $urlbits[5] ) && intval( $urlbits[5] ) ) { |
| 61 | $id = 'https://twitter.com/' . $urlbits[3] . '/status/' . intval( $urlbits[5] ); |
| 62 | } else { |
| 63 | return '<!-- Invalid tweet id -->'; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Add shortcode arguments to provider URL |
| 68 | add_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10, 3 ); |
| 69 | |
| 70 | // Fetch tweet |
| 71 | $output = wp_oembed_get( $id, $atts ); |
| 72 | |
| 73 | // Clean up filter |
| 74 | remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 ); |
| 75 | |
| 76 | // Add Twitter widgets.js script to the footer. |
| 77 | add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) ); |
| 78 | |
| 79 | /** This action is documented in modules/widgets/social-media-icons.php */ |
| 80 | do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' ); |
| 81 | |
| 82 | return $output; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Adds parameters to URL used to fetch the tweet. |
| 87 | * |
| 88 | * @since 4.5.0 |
| 89 | * |
| 90 | * @param string $provider URL of provider that supplies the tweet we're requesting. |
| 91 | * @param string $url URL of tweet to embed. |
| 92 | * @param array $args Parameters supplied to shortcode and passed to wp_oembed_get |
| 93 | * |
| 94 | * @return string |
| 95 | */ |
| 96 | static public function jetpack_tweet_url_extra_args( $provider, $url, $args = array() ) { |
| 97 | foreach ( self::$provider_args as $key => $value ) { |
| 98 | switch ( $key ) { |
| 99 | case 'align': |
| 100 | case 'lang': |
| 101 | case 'hide_thread': |
| 102 | case 'hide_media': |
| 103 | $provider = add_query_arg( $key, $value, $provider ); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Disable script since we're enqueing it in our own way in the footer |
| 109 | $provider = add_query_arg( 'omit_script', 'true', $provider ); |
| 110 | |
| 111 | // Twitter doesn't support maxheight so don't send it |
| 112 | $provider = remove_query_arg( 'maxheight', $provider ); |
| 113 | |
| 114 | /** |
| 115 | * Filter the Twitter Partner ID. |
| 116 | * |
| 117 | * @module shortcodes |
| 118 | * |
| 119 | * @since 4.6.0 |
| 120 | * |
| 121 | * @param string $partner_id Twitter partner ID. |
| 122 | */ |
| 123 | $partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' ); |
| 124 | |
| 125 | // Add Twitter partner ID to track embeds from Jetpack |
| 126 | if ( ! empty( $partner ) ) { |
| 127 | $provider = add_query_arg( 'partner', $partner, $provider ); |
| 128 | } |
| 129 | |
| 130 | return $provider; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Enqueue front end assets. |
| 135 | * |
| 136 | * @since 4.5.0 |
| 137 | */ |
| 138 | static public function jetpack_tweet_shortcode_script() { |
| 139 | if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) { |
| 140 | wp_register_script( 'twitter-widgets', 'https://platform.twitter.com/widgets.js', array(), JETPACK__VERSION, true ); |
| 141 | wp_print_scripts( 'twitter-widgets' ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | } // class end |
| 146 |