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
|
|
|
add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Tweet', 'enqueue_block_editor_assets' ) ); |
21
|
|
|
|
22
|
|
|
class Jetpack_Tweet { |
23
|
|
|
|
24
|
|
|
static $provider_args; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Parse shortcode arguments and render its output. |
28
|
|
|
* |
29
|
|
|
* @since 4.5.0 |
30
|
|
|
* |
31
|
|
|
* @param array $atts Shortcode parameters. |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
static public function jetpack_tweet_shortcode( $atts ) { |
|
|
|
|
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_oembed_get( $id, $atts ); |
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
|
|
|
static public 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
|
|
|
static public function jetpack_tweet_shortcode_script() { |
|
|
|
|
140
|
|
|
if ( ! wp_script_is( 'twitter-widgets', 'registered' ) ) { |
141
|
|
|
wp_register_script( 'twitter-widgets', set_url_scheme( 'http://platform.twitter.com/widgets.js' ), array(), JETPACK__VERSION, true ); |
142
|
|
|
wp_print_scripts( 'twitter-widgets' ); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
static public function enqueue_block_editor_assets() { |
|
|
|
|
147
|
|
|
wp_register_script( |
148
|
|
|
'jetpack-shortcode-tweet-gutenberg', |
149
|
|
|
null, |
150
|
|
|
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'shortcode' ) |
151
|
|
|
); |
152
|
|
|
wp_enqueue_script( 'jetpack-shortcode-tweet-gutenberg' ); |
153
|
|
|
|
154
|
|
|
ob_start(); |
155
|
|
|
self::jetpack_shortcode_tweet_gutenberg_script(); |
|
|
|
|
156
|
|
|
$content = ob_get_clean(); |
157
|
|
|
|
158
|
|
|
wp_script_add_data( 'jetpack-shortcode-tweet-gutenberg', 'data', $content ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
static public function jetpack_shortcode_tweet_gutenberg_script() { |
|
|
|
|
162
|
|
|
?> |
163
|
|
|
// <script> |
164
|
|
|
( function( wp ) { |
165
|
|
|
wp.blocks.registerBlockType( 'jetpack/tweet', { |
166
|
|
|
title: wp.i18n.__( 'Tweet', 'jetpack' ), |
167
|
|
|
icon: 'twitter', |
168
|
|
|
category: 'layout', |
169
|
|
|
|
170
|
|
|
attributes : { |
171
|
|
|
tweet : function ( node ) { |
172
|
|
|
var shortcode = wp.shortcode.next( 'tweet', node.innerText ); |
173
|
|
|
if ( shortcode.attrs.named.tweet ) { |
174
|
|
|
return shortcode.attrs.named.tweet; |
175
|
|
|
} |
176
|
|
|
if ( shortcode.attrs.numeric[0] ) { |
177
|
|
|
return shortcode.attrs.numeric[0]; |
178
|
|
|
} |
179
|
|
|
return null; |
180
|
|
|
}, |
181
|
|
|
align : function ( node ) { |
182
|
|
|
var shortcode = wp.shortcode.next( 'tweet', node.innerText ); |
183
|
|
|
if ( shortcode.attrs.named.align ) { |
184
|
|
|
return shortcode.attrs.named.align; |
185
|
|
|
} |
186
|
|
|
return 'none'; |
187
|
|
|
}, |
188
|
|
|
width : function ( node ) { |
189
|
|
|
var shortcode = wp.shortcode.next( 'tweet', node.innerText ); |
190
|
|
|
if ( shortcode.attrs.named.width ) { |
191
|
|
|
return shortcode.attrs.named.width; |
192
|
|
|
} |
193
|
|
|
return ''; |
194
|
|
|
}, |
195
|
|
|
lang : function ( node ) { |
196
|
|
|
var shortcode = wp.shortcode.next( 'tweet', node.innerText ); |
197
|
|
|
if ( shortcode.attrs.named.lang ) { |
198
|
|
|
return shortcode.attrs.named.lang; |
199
|
|
|
} |
200
|
|
|
return 'en'; |
201
|
|
|
}, |
202
|
|
|
hide_thread : function ( node ) { |
203
|
|
|
var shortcode = wp.shortcode.next( 'tweet', node.innerText ); |
204
|
|
|
if ( shortcode.attrs.named.hide_thread ) { |
205
|
|
|
return shortcode.attrs.named.hide_thread; |
206
|
|
|
} |
207
|
|
|
return 'false'; |
208
|
|
|
}, |
209
|
|
|
hide_media : function ( node ) { |
210
|
|
|
var shortcode = wp.shortcode.next( 'tweet', node.innerText ); |
211
|
|
|
if ( shortcode.attrs.named.hide_media ) { |
212
|
|
|
return shortcode.attrs.named.hide_media; |
213
|
|
|
} |
214
|
|
|
return 'false'; |
215
|
|
|
} |
216
|
|
|
}, |
217
|
|
|
|
218
|
|
|
edit : function( props ) { |
219
|
|
|
return wp.element.createElement( |
220
|
|
|
'input', |
221
|
|
|
{ |
222
|
|
|
name : 'tweet', |
223
|
|
|
type : 'url', |
224
|
|
|
value : props.attributes.tweet, |
225
|
|
|
onChange: function( event ) { |
226
|
|
|
props.setAttributes({ |
227
|
|
|
tweet: event.target.value |
228
|
|
|
}); |
229
|
|
|
} |
230
|
|
|
}, |
231
|
|
|
null |
232
|
|
|
); |
233
|
|
|
}, |
234
|
|
|
|
235
|
|
|
save : function( props ) { |
236
|
|
|
var args = { |
237
|
|
|
tag : 'tweet', |
238
|
|
|
attrs : { |
239
|
|
|
named : {}, |
240
|
|
|
numeric : [ |
241
|
|
|
props.attributes.tweet |
242
|
|
|
] |
243
|
|
|
} |
244
|
|
|
}; |
245
|
|
|
|
246
|
|
|
// Populate optional attributes. |
247
|
|
|
if ( props.attributes.align && props.attributes.align !== 'none' ) { |
248
|
|
|
args.attrs.named.align = props.attributes.align; |
249
|
|
|
} |
250
|
|
|
if ( props.attributes.width && props.attributes.width !== '' ) { |
251
|
|
|
args.attrs.named.width = props.attributes.width; |
252
|
|
|
} |
253
|
|
|
if ( props.attributes.lang && props.attributes.lang !== 'en' ) { |
254
|
|
|
args.attrs.named.lang = props.attributes.lang; |
255
|
|
|
} |
256
|
|
|
if ( props.attributes.hide_thread && props.attributes.hide_thread !== 'none' ) { |
257
|
|
|
args.attrs.named.hide_thread = props.attributes.hide_thread; |
258
|
|
|
} |
259
|
|
|
if ( props.attributes.hide_media && props.attributes.hide_media !== 'none' ) { |
260
|
|
|
args.attrs.named.hide_media = props.attributes.hide_media; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
if ( props.className ) { |
264
|
|
|
return wp.element.createElement( |
265
|
|
|
'div', |
266
|
|
|
{ className : props.className }, |
267
|
|
|
wp.shortcode.string( args ) |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return wp.shortcode.string( args ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
} ); |
275
|
|
|
} )( window.wp ); |
276
|
|
|
// </script> |
277
|
|
|
<?php |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
} // class end |
281
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.