Twitter - Version 1.2.0

Version Description

Support Twitter Moment embeds.

Download this release

Release Info

Developer niallkennedy
Plugin Icon 128x128 Twitter
Version 1.2.0
Comparing to
See all releases

Code changes from version 1.1.0 to 1.2.0

readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Plugin Name ===
2
  Contributors: Twitter, niallkennedy
3
- Tags: twitter, embedded tweet, twitter video, twitter cards, tweet button, follow button, twitter analytics, twitter ads
4
  Requires at least: 3.9
5
- Tested up to: 4.3
6
- Stable tag: 1.1.0
7
  License: MIT
8
  License URI: http://opensource.org/licenses/MIT
9
 
@@ -15,6 +15,7 @@ The Twitter plugin for WordPress optimizes your website for a Twitter audience t
15
 
16
  Requires PHP version 5.4 or greater.
17
 
 
18
  * [Embedded Tweet](https://dev.twitter.com/web/embedded-tweets) - customize backgrounds and color schemes to match your site's theme
19
  * [Embedded Twitter video](https://dev.twitter.com/web/embedded-video) - showcase video uploaded to Twitter
20
  * [Tweet button](https://dev.twitter.com/web/tweet-button) - simple sharing of your site's content on Twitter
@@ -27,6 +28,9 @@ Contribute to the plugin, submit pull requests, or run test suites through the [
27
 
28
  == Upgrade Notice ==
29
 
 
 
 
30
  = 1.1.0 =
31
  Shortcode improvements for ajax-loaded posts. Remove photo, gallery, and product Twitter Cards no longer supported by Twitter.
32
 
@@ -35,6 +39,10 @@ Display admin notice if current PHP version does not meet minimum requirements.
35
 
36
  == Changelog ==
37
 
 
 
 
 
38
  = 1.1.0 =
39
  * Shortcodes now include inline asynchronous JavaScript loaders for improved compatibility with ajax-loaded content
40
  * Twitter announced photo, gallery, and product Twitter Cards are [no longer supported](https://twittercommunity.com/t/deprecating-the-photo-gallery-and-product-cards/38961 "Twitter announcement: deprecation of photo, gallery, product cards"). Removed from plugin
1
  === Plugin Name ===
2
  Contributors: Twitter, niallkennedy
3
+ Tags: twitter, embedded tweet, twitter moment, twitter video, twitter cards, tweet button, follow button, twitter analytics, twitter ads
4
  Requires at least: 3.9
5
+ Tested up to: 4.3.1
6
+ Stable tag: 1.2.0
7
  License: MIT
8
  License URI: http://opensource.org/licenses/MIT
9
 
15
 
16
  Requires PHP version 5.4 or greater.
17
 
18
+ * [Embedded Moment](https://dev.twitter.com/web/embedded-moments) - showcase multiple Tweets in a media rich grid layout
19
  * [Embedded Tweet](https://dev.twitter.com/web/embedded-tweets) - customize backgrounds and color schemes to match your site's theme
20
  * [Embedded Twitter video](https://dev.twitter.com/web/embedded-video) - showcase video uploaded to Twitter
21
  * [Tweet button](https://dev.twitter.com/web/tweet-button) - simple sharing of your site's content on Twitter
28
 
29
  == Upgrade Notice ==
30
 
31
+ = 1.2.0 =
32
+ Support Twitter Moment embeds.
33
+
34
  = 1.1.0 =
35
  Shortcode improvements for ajax-loaded posts. Remove photo, gallery, and product Twitter Cards no longer supported by Twitter.
36
 
39
 
40
  == Changelog ==
41
 
42
+ = 1.2.0 =
43
+ * Embed a Twitter Moment by simply pasting a URL
44
+ * Always load Twitter ads conversion tracking JavaScript over HTTPS
45
+
46
  = 1.1.0 =
47
  * Shortcodes now include inline asynchronous JavaScript loaders for improved compatibility with ajax-loaded content
48
  * Twitter announced photo, gallery, and product Twitter Cards are [no longer supported](https://twittercommunity.com/t/deprecating-the-photo-gallery-and-product-cards/38961 "Twitter announcement: deprecation of photo, gallery, product cards"). Removed from plugin
src/Twitter/WordPress/Admin/Post/TwitterCard.php CHANGED
@@ -137,10 +137,11 @@ class TwitterCard
137
 
138
  echo '<h4>' . esc_html( __( 'Twitter Card', 'twitter' ) ) . '</h4>';
139
 
140
- if ( isset( $cards_fields_supported_by_post_type[ static::TITLE_KEY ] ) ) {
141
- echo '<table id="twitter-card">';
142
- echo '<thead><tr><th scope="col">' . esc_html( _x( 'Property', 'Object component, such as a title and description of an article', 'twitter' ) ) . '</th><th scope="col">' . esc_html( __( 'Value' ) ) . '</th></tr></thead><tbody>';
143
 
 
144
  echo '<tr>';
145
  echo '<th scope="row" class="left"><label for="twitter-card-title">' . esc_html( __( 'Title' ) ) . '</label></th>';
146
  echo '<td><input type="text" id="twitter-card-title" name="' . esc_attr( static::META_KEY . '[' . static::TITLE_KEY . ']' ) . '" maxlength="70" autocomplete="off"';
@@ -163,10 +164,11 @@ class TwitterCard
163
  }
164
  echo \Twitter\WordPress\Helpers\HTMLBuilder::closeVoidHTMLElement() . '></td>';
165
  echo '</tr>';
166
-
167
- echo '</tbody></table>';
168
- echo '<p class="description">' . esc_html( __( 'Customize Twitter link previews', 'twitter' ) ) . '</p>';
169
  }
 
 
 
 
170
  }
171
 
172
  /**
137
 
138
  echo '<h4>' . esc_html( __( 'Twitter Card', 'twitter' ) ) . '</h4>';
139
 
140
+ // set up the table
141
+ echo '<table id="twitter-card">';
142
+ echo '<thead><tr><th scope="col">' . esc_html( _x( 'Property', 'Object component, such as a title and description of an article', 'twitter' ) ) . '</th><th scope="col">' . esc_html( __( 'Value' ) ) . '</th></tr></thead><tbody>';
143
 
144
+ if ( isset( $cards_fields_supported_by_post_type[ static::TITLE_KEY ] ) ) {
145
  echo '<tr>';
146
  echo '<th scope="row" class="left"><label for="twitter-card-title">' . esc_html( __( 'Title' ) ) . '</label></th>';
147
  echo '<td><input type="text" id="twitter-card-title" name="' . esc_attr( static::META_KEY . '[' . static::TITLE_KEY . ']' ) . '" maxlength="70" autocomplete="off"';
164
  }
165
  echo \Twitter\WordPress\Helpers\HTMLBuilder::closeVoidHTMLElement() . '></td>';
166
  echo '</tr>';
 
 
 
167
  }
168
+
169
+ // close the table, describe its contents
170
+ echo '</tbody></table>';
171
+ echo '<p class="description">' . esc_html( __( 'Customize Twitter link previews', 'twitter' ) ) . '</p>';
172
  }
173
 
174
  /**
src/Twitter/WordPress/Admin/Settings/SinglePage.php CHANGED
@@ -133,7 +133,7 @@ class SinglePage
133
 
134
  echo '<div class="wrap">';
135
 
136
- echo '<header><h2>' . esc_html( static::featureName() ) . '</h2></header>';
137
  // handle general messages such as settings updated up top
138
  // place individual settings errors alongside their fields
139
  settings_errors( 'general' );
133
 
134
  echo '<div class="wrap">';
135
 
136
+ echo '<header><h1>' . esc_html( static::featureName() ) . '</h1></header>';
137
  // handle general messages such as settings updated up top
138
  // place individual settings errors alongside their fields
139
  settings_errors( 'general' );
src/Twitter/WordPress/Helpers/TwitterOEmbed.php ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ The MIT License (MIT)
4
+
5
+ Copyright (c) 2015 Twitter Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+ */
25
+
26
+ namespace Twitter\WordPress\Helpers;
27
+
28
+ /**
29
+ * Request oEmbeds from Twitter's publisher endpoints
30
+ *
31
+ * @since 1.2.0
32
+ */
33
+ class TwitterOEmbed extends TwitterAPI
34
+ {
35
+
36
+ /**
37
+ * Twitter Publisher FQDN
38
+ *
39
+ * @since 1.2.0
40
+ *
41
+ * @type string
42
+ */
43
+ const HOST = 'publish.twitter.com';
44
+
45
+ /**
46
+ * Response formats supported by Twitter publisher oEmbed endpoints
47
+ *
48
+ * @since 1.2.0
49
+ *
50
+ * @type array supported formats {
51
+ * @type string filename extension
52
+ * @type string media type
53
+ * }
54
+ */
55
+ public static $RESPONSE_FORMATS = array( 'json' => 'application/json' );
56
+
57
+ /**
58
+ * Build a Twitter REST API URL
59
+ *
60
+ * @since 1.2.0
61
+ *
62
+ * @param string $relative_path Publisher oEmbed path
63
+ * @param array $query_parameters query parameters to append to the URL
64
+ * @param string $response_format requested response format
65
+ *
66
+ * @return string absolute API URL or empty string if invalid relative_path passed
67
+ */
68
+ public static function getAPIURL( $relative_path = 'oembed', $query_parameters = null, $response_format = 'json' )
69
+ {
70
+ if ( ! is_array( $query_parameters ) ) {
71
+ return '';
72
+ }
73
+
74
+ // Minimal information not provided
75
+ if ( ! ( isset( $query_parameters['id'] ) || isset( $query_parameters['url'] ) ) ) {
76
+ return '';
77
+ }
78
+
79
+ if ( ! is_string( $relative_path ) ) {
80
+ $relative_path = '';
81
+ } else {
82
+ $relative_path = trim( trim( $relative_path, '/' ) );
83
+ }
84
+ if ( ! $relative_path ) {
85
+ $relative_path = 'oembed';
86
+ }
87
+
88
+ if ( ! ( is_string( $response_format ) && $response_format && isset( static::$RESPONSE_FORMATS[ $response_format ] ) ) ) {
89
+ $response_format = '';
90
+ }
91
+
92
+ $url = 'https://' . implode( '/', array( static::HOST, $relative_path ) );
93
+ if ( $response_format ) {
94
+ $url .= '.' . $response_format;
95
+ }
96
+ $url .= '?' . http_build_query( $query_parameters, '', '&' );
97
+
98
+ return $url;
99
+ }
100
+ }
src/Twitter/WordPress/JavaScriptLoaders/Tracking.php CHANGED
@@ -43,6 +43,15 @@ class Tracking
43
  */
44
  const QUEUE_HANDLE = 'twitter-tracking';
45
 
 
 
 
 
 
 
 
 
 
46
  /**
47
  * Proactively resolve Twitter advertising JS FQDN asynchronously before later use
48
  *
@@ -71,7 +80,7 @@ class Tracking
71
  {
72
  wp_register_script(
73
  self::QUEUE_HANDLE,
74
- static::getAbsoluteURI(),
75
  array(), // no dependencies
76
  null, // no not add extra query parameters for cache busting
77
  true // in footer
@@ -91,18 +100,4 @@ class Tracking
91
  {
92
  wp_enqueue_script( self::QUEUE_HANDLE );
93
  }
94
-
95
- /**
96
- * The absolute URI of the Twitter advertising tracker JavaScript file
97
- *
98
- * Prefer absolute URI over scheme-relative URI
99
- *
100
- * @since 1.0.0
101
- *
102
- * @return string absolute URI for the Twitter advertising tracker JavaScript file
103
- */
104
- public static function getAbsoluteURI()
105
- {
106
- return 'http' . ( is_ssl() ? 's' : '' ) . '://platform.twitter.com/oct.js';
107
- }
108
  }
43
  */
44
  const QUEUE_HANDLE = 'twitter-tracking';
45
 
46
+ /**
47
+ * Twitter advertising JavaScript absolute URI
48
+ *
49
+ * @since 1.2.0
50
+ *
51
+ * @type string
52
+ */
53
+ const URI = 'https://platform.twitter.com/oct.js';
54
+
55
  /**
56
  * Proactively resolve Twitter advertising JS FQDN asynchronously before later use
57
  *
80
  {
81
  wp_register_script(
82
  self::QUEUE_HANDLE,
83
+ self::URI,
84
  array(), // no dependencies
85
  null, // no not add extra query parameters for cache busting
86
  true // in footer
100
  {
101
  wp_enqueue_script( self::QUEUE_HANDLE );
102
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  }
src/Twitter/WordPress/PluginLoader.php CHANGED
@@ -41,7 +41,7 @@ class PluginLoader
41
  *
42
  * @type string
43
  */
44
- const VERSION = '1.1.0';
45
 
46
  /**
47
  * Unique domain of the plugin's translated text
@@ -219,6 +219,7 @@ class PluginLoader
219
  5,
220
  0
221
  );
 
222
  // Twitter embedded videos
223
  add_action(
224
  'plugins_loaded',
@@ -226,6 +227,14 @@ class PluginLoader
226
  5,
227
  0
228
  );
 
 
 
 
 
 
 
 
229
  }
230
 
231
  // Follow button
41
  *
42
  * @type string
43
  */
44
+ const VERSION = '1.2.0';
45
 
46
  /**
47
  * Unique domain of the plugin's translated text
219
  5,
220
  0
221
  );
222
+
223
  // Twitter embedded videos
224
  add_action(
225
  'plugins_loaded',
227
  5,
228
  0
229
  );
230
+
231
+ // Twitter Moment
232
+ add_action(
233
+ 'plugins_loaded',
234
+ array( '\Twitter\WordPress\Shortcodes\Moment', 'init' ),
235
+ 5,
236
+ 0
237
+ );
238
  }
239
 
240
  // Follow button
src/Twitter/WordPress/Shortcodes/Moment.php ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ The MIT License (MIT)
4
+
5
+ Copyright (c) 2015 Twitter Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
24
+ */
25
+
26
+ namespace Twitter\WordPress\Shortcodes;
27
+
28
+ /**
29
+ * Display a Moment
30
+ *
31
+ * @since 1.2.0
32
+ */
33
+ class Moment
34
+ {
35
+
36
+ /**
37
+ * Shortcode tag to be matched
38
+ *
39
+ * @since 1.2.0
40
+ *
41
+ * @type string
42
+ */
43
+ const SHORTCODE_TAG = 'twitter_moment';
44
+
45
+ /**
46
+ * Relative path for the oEmbed API relative to Twitter publishers base path
47
+ *
48
+ * @since 1.2.0
49
+ *
50
+ * @type string
51
+ */
52
+ const OEMBED_API_ENDPOINT = 'oembed';
53
+
54
+ /**
55
+ * Regex used to match a Moment in text
56
+ *
57
+ * @since 1.2.0
58
+ *
59
+ * @type string
60
+ */
61
+ const URL_REGEX = '#^https://twitter\.com/i/moments/([0-9]+)#i';
62
+
63
+ /**
64
+ * Base URL used to reconstruct a Moment URL
65
+ *
66
+ * @since 1.2.0
67
+ *
68
+ * @type string
69
+ */
70
+ const BASE_URL = 'https://twitter.com/i/moments/';
71
+
72
+ /**
73
+ * Accepted shortcode attributes and their default values
74
+ *
75
+ * @since 1.2.0
76
+ *
77
+ * @type array
78
+ */
79
+ public static $SHORTCODE_DEFAULTS = array( 'id' => '' );
80
+
81
+ /**
82
+ * Attach handlers for Twitter Moment
83
+ *
84
+ * @since 1.2.0
85
+ *
86
+ * @return void
87
+ */
88
+ public static function init()
89
+ {
90
+ // register our shortcode and its handler
91
+ add_shortcode( self::SHORTCODE_TAG, array( __CLASS__, 'shortcodeHandler' ) );
92
+
93
+ wp_embed_register_handler(
94
+ self::SHORTCODE_TAG,
95
+ static::URL_REGEX,
96
+ array( __CLASS__, 'linkHandler' ),
97
+ 1
98
+ );
99
+
100
+ if ( is_admin() ) {
101
+ // Shortcake UI
102
+ if ( function_exists( 'shortcode_ui_register_for_shortcode' ) ) {
103
+ add_action(
104
+ 'admin_init',
105
+ array( __CLASS__, 'shortcodeUI' ),
106
+ 5,
107
+ 0
108
+ );
109
+ }
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Describe shortcode for Shortcake UI
115
+ *
116
+ * @since 1.2.0
117
+ *
118
+ * @link https://github.com/fusioneng/Shortcake Shortcake UI
119
+ *
120
+ * @return void
121
+ */
122
+ public static function shortcodeUI()
123
+ {
124
+ // Shortcake required
125
+ if ( ! function_exists( 'shortcode_ui_register_for_shortcode' ) ) {
126
+ return;
127
+ }
128
+
129
+ shortcode_ui_register_for_shortcode(
130
+ self::SHORTCODE_TAG,
131
+ array(
132
+ 'label' => __( 'Twitter Moment', 'twitter' ),
133
+ 'listItemImage' => 'dashicons-twitter',
134
+ 'attrs' => array(
135
+ array(
136
+ 'attr' => 'id',
137
+ 'label' => 'ID',
138
+ 'type' => 'text',
139
+ 'meta' => array(
140
+ 'required' => true,
141
+ 'pattern' => '[0-9]+',
142
+ ),
143
+ ),
144
+ ),
145
+ )
146
+ );
147
+ }
148
+
149
+ /**
150
+ * Handle a URL matched by a Moment embed handler
151
+ *
152
+ * @since 1.2.0
153
+ *
154
+ * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
155
+ * @param array $attr Embed attributes. Not used.
156
+ * @param string $url The original URL that was matched by the regex. Not used.
157
+ * @param array $rawattr The original unmodified attributes. Not used.
158
+ *
159
+ * @return string HTML markup for the Tweet or an empty string if requirements not met
160
+ */
161
+ public static function linkHandler( $matches, $attr, $url, $rawattr )
162
+ {
163
+ if ( ! ( is_array( $matches ) && isset( $matches[1] ) && $matches[1] ) ) {
164
+ return '';
165
+ }
166
+
167
+ return static::shortcodeHandler( array( 'id' => $matches[1] ) );
168
+ }
169
+
170
+ /**
171
+ * Handle shortcode macro
172
+ *
173
+ * @since 1.2.0
174
+ *
175
+ * @param array $attributes set of shortcode attribute-value pairs or positional content matching the WordPress shortcode regex {
176
+ * @type string|int attribute name or positional int
177
+ * @type mixed shortcode value
178
+ * }
179
+ * @param string $content content inside a shortcode macro. no effect on this shortcode
180
+ *
181
+ * @return string HTML markup. empty string if parameter requirement not met or no Moment info found
182
+ */
183
+ public static function shortcodeHandler( $attributes, $content = '' )
184
+ {
185
+ $options = shortcode_atts(
186
+ static::$SHORTCODE_DEFAULTS,
187
+ $attributes,
188
+ static::SHORTCODE_TAG
189
+ );
190
+
191
+ $moment_id = trim( $options['id'] );
192
+ if ( ! $moment_id ) {
193
+ return '';
194
+ }
195
+ $query_parameters = static::getBaseOEmbedParams( $moment_id );
196
+
197
+ // fetch HTML markup from Twitter oEmbed endpoint for the given parameters
198
+ $html = trim( static::getOEmbedMarkup( $query_parameters ) );
199
+ if ( ! $html ) {
200
+ return '';
201
+ }
202
+
203
+ $html = '<div class="twitter-moment">' . $html . '</div>';
204
+
205
+ $inline_js = \Twitter\WordPress\JavaScriptLoaders\Widgets::enqueue();
206
+ if ( $inline_js ) {
207
+ return $html . $inline_js;
208
+ }
209
+
210
+ return $html;
211
+ }
212
+
213
+ /**
214
+ * Get the base set of oEmbed params before applying shortcode customizations
215
+ *
216
+ * @since 1.2.0
217
+ *
218
+ * @param string $moment_id Moment identifier
219
+ *
220
+ * @return array associative array of query parameters ready for http_build_query {
221
+ * @type string query parameter name
222
+ * @type string|bool query parameter value
223
+ * }
224
+ */
225
+ public static function getBaseOEmbedParams( $moment_id )
226
+ {
227
+ $moment_id = trim( $moment_id );
228
+ if ( ! $moment_id ) {
229
+ return array();
230
+ }
231
+
232
+ // omit JavaScript. enqueue separately
233
+ $query_parameters = array(
234
+ 'id' => $moment_id,
235
+ 'omit_script' => true,
236
+ );
237
+
238
+ // attempt to customize text for site language
239
+ $lang = \Twitter\WordPress\Language::localeToTwitterLang();
240
+ if ( $lang ) {
241
+ $query_parameters['lang'] = $lang;
242
+ }
243
+
244
+ return $query_parameters;
245
+ }
246
+
247
+ /**
248
+ * Construct a cache key for the oEmbed response. Account for query parameters needing to bust cache
249
+ *
250
+ * @since 1.2.0
251
+ *
252
+ * @param array $query_parameters oEmbed API query parameters
253
+ *
254
+ * @return string cache key
255
+ */
256
+ public static function oEmbedCacheKey( array $query_parameters )
257
+ {
258
+ if ( ! ( isset( $query_parameters['id'] ) && $query_parameters['id'] ) ) {
259
+ return '';
260
+ }
261
+
262
+ $key_pieces = array( self::SHORTCODE_TAG, $query_parameters['id'] );
263
+
264
+ // separate cache for each explicitly-defined display language
265
+ if ( isset( $query_parameters['lang'] ) && $query_parameters['lang'] ) {
266
+ $key_pieces[] = $query_parameters['lang'];
267
+ }
268
+
269
+ return implode( '_', $key_pieces );
270
+ }
271
+
272
+ /**
273
+ * Request and parse Moment oEmbed markup from Twitter's servers
274
+ *
275
+ * @since 1.2.0
276
+ *
277
+ * @param string $moment_id Moment identifier
278
+ *
279
+ * @todo make this not mocked
280
+ * @return string HTML markup returned by the oEmbed endpoint
281
+ */
282
+ public static function getOEmbedMarkup( $query_parameters ) {
283
+ $cache_key = static::oEmbedCacheKey( $query_parameters );
284
+ if ( ! $cache_key ) {
285
+ return '';
286
+ }
287
+
288
+ $cache_key = static::oEmbedCacheKey( $query_parameters );
289
+ if ( ! $cache_key ) {
290
+ return '';
291
+ }
292
+
293
+ // check for cached result
294
+ $html = get_transient( $cache_key );
295
+ if ( $html ) {
296
+ return $html;
297
+ }
298
+
299
+ $ttl = DAY_IN_SECONDS;
300
+
301
+ // convert ID to full URL to allow more flexibility for oEmbed endpoint as embed types expand
302
+ $query_parameters['url'] = self::BASE_URL . $query_parameters['id'];
303
+ unset( $query_parameters['id'] );
304
+
305
+ $oembed_response = \Twitter\WordPress\Helpers\TwitterOEmbed::getJSON( self::OEMBED_API_ENDPOINT, $query_parameters );
306
+ if ( ! $oembed_response || ! isset( $oembed_response->type ) || 'rich' !== $oembed_response->type || ! ( isset( $oembed_response->html ) && $oembed_response->html ) ) {
307
+ // do not rerequest errors with every page request
308
+ set_transient( $cache_key, ' ', $ttl );
309
+ return '';
310
+ }
311
+
312
+ $html = $oembed_response->html;
313
+
314
+ if ( isset( $oembed_response->cache_age ) ) {
315
+ $ttl = absint( $oembed_response->cache_age );
316
+ }
317
+ set_transient( $cache_key, $html, $ttl );
318
+
319
+ return $html;
320
+ }
321
+ }
twitter.php CHANGED
@@ -30,7 +30,7 @@ THE SOFTWARE.
30
  Plugin Name: Twitter
31
  Plugin URI: http://wordpress.org/plugins/twitter/
32
  Description: Official Twitter plugin for WordPress. Embed Twitter content and grow your audience on Twitter. Requires PHP 5.4 or greater.
33
- Version: 1.1.0
34
  Author: Twitter
35
  Author URI: https://dev.twitter.com/
36
  License: MIT
30
  Plugin Name: Twitter
31
  Plugin URI: http://wordpress.org/plugins/twitter/
32
  Description: Official Twitter plugin for WordPress. Embed Twitter content and grow your audience on Twitter. Requires PHP 5.4 or greater.
33
+ Version: 1.2.0
34
  Author: Twitter
35
  Author URI: https://dev.twitter.com/
36
  License: MIT