PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.1
Jetpack – WP Security, Backup, Speed, & Growth v7.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / tweet.php

tweet.php in Jetpack – WP Security, Backup, Speed, & Growth 7.1, at modules/shortcodes/tweet.php

147 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public static function jetpack_tweet_shortcode( $atts ) {
35 global $wp_embed;
36 $default_atts = array(
37 'tweet' => '',
38 'align' => 'none',
39 'width' => '',
40 'lang' => 'en',
41 'hide_thread' => 'false',
42 'hide_media' => 'false',
43 );
44
45 $attr = shortcode_atts( $default_atts, $atts );
46
47 self::$provider_args = $attr;
48
49 // figure out the tweet id for the requested tweet
50 // supporting both omitted attributes and tweet="tweet_id"
51 // and supporting both an id and a URL
52 if ( empty( $attr['tweet'] ) && ! empty( $atts[0] ) ) {
53 $attr['tweet'] = $atts[0];
54 }
55
56 if ( ctype_digit( $attr['tweet'] ) ) {
57 $id = 'https://twitter.com/jetpack/status/' . $attr['tweet'];
58 } else {
59 preg_match( '/^http(s|):\/\/twitter\.com(\/\#\!\/|\/)([a-zA-Z0-9_]{1,20})\/status(es)*\/(\d+)$/', $attr['tweet'], $urlbits );
60
61 if ( isset( $urlbits[5] ) && intval( $urlbits[5] ) ) {
62 $id = 'https://twitter.com/' . $urlbits[3] . '/status/' . intval( $urlbits[5] );
63 } else {
64 return '<!-- Invalid tweet id -->';
65 }
66 }
67
68 // Add shortcode arguments to provider URL
69 add_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10, 3 );
70
71 // Fetch tweet
72 $output = $wp_embed->shortcode( $atts, $id );
73
74 // Clean up filter
75 remove_filter( 'oembed_fetch_url', array( 'Jetpack_Tweet', 'jetpack_tweet_url_extra_args' ), 10 );
76
77 // Add Twitter widgets.js script to the footer.
78 add_action( 'wp_footer', array( 'Jetpack_Tweet', 'jetpack_tweet_shortcode_script' ) );
79
80 /** This action is documented in modules/widgets/social-media-icons.php */
81 do_action( 'jetpack_bump_stats_extras', 'embeds', 'tweet' );
82
83 return $output;
84 }
85
86 /**
87 * Adds parameters to URL used to fetch the tweet.
88 *
89 * @since 4.5.0
90 *
91 * @param string $provider URL of provider that supplies the tweet we're requesting.
92 * @param string $url URL of tweet to embed.
93 * @param array $args Parameters supplied to shortcode and passed to wp_oembed_get
94 *
95 * @return string
96 */
97 public static function jetpack_tweet_url_extra_args( $provider, $url, $args = array() ) {
98 foreach ( self::$provider_args as $key => $value ) {
99 switch ( $key ) {
100 case 'align':
101 case 'lang':
102 case 'hide_thread':
103 case 'hide_media':
104 $provider = add_query_arg( $key, $value, $provider );
105 break;
106 }
107 }
108
109 // Disable script since we're enqueing it in our own way in the footer
110 $provider = add_query_arg( 'omit_script', 'true', $provider );
111
112 // Twitter doesn't support maxheight so don't send it
113 $provider = remove_query_arg( 'maxheight', $provider );
114
115 /**
116 * Filter the Twitter Partner ID.
117 *
118 * @module shortcodes
119 *
120 * @since 4.6.0
121 *
122 * @param string $partner_id Twitter partner ID.
123 */
124 $partner = apply_filters( 'jetpack_twitter_partner_id', 'jetpack' );
125
126 // Add Twitter partner ID to track embeds from Jetpack
127 if ( ! empty( $partner ) ) {
128 $provider = add_query_arg( 'partner', $partner, $provider );
129 }
130
131 return $provider;
132 }
133
134 /**
135 * Enqueue front end assets.
136 *
137 * @since 4.5.0
138 */
139 public static function jetpack_tweet_shortcode_script() {
140 if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) {
141 wp_register_script( 'twitter-widgets', 'https://platform.twitter.com/widgets.js', array(), JETPACK__VERSION, true );
142 wp_print_scripts( 'twitter-widgets' );
143 }
144 }
145
146 } // class end
147