Changeset 213740 in webkit
- Timestamp:
- Mar 10, 2017 5:01:19 PM (7 years ago)
- Location:
- trunk/Websites/webkit.org
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Websites/webkit.org/ChangeLog
r213714 r213740 1 2017-03-10 Jon Davis <jond@apple.com> 2 3 Switch back to the Twitter API for the Tweet widget 4 https://bugs.webkit.org/show_bug.cgi?id=168749 5 6 Reviewed by Lucas Forschler. 7 8 Updates the Twitter widget tile implementation to prefer the Twitter API to populate tweets 9 displayed through the widget. If the API fails for any reason, it falls back to using the 10 Tweet listener for updated tweets. 11 12 The Twitter API provides the benefit of embedding media and being able to display the full 13 URLs rather than Twitter-shortened URLs. 14 15 * wp-content/plugins/tweet-listener.php: 16 * wp-content/themes/webkit/style.css: 17 (.twitter-tile .tile-content): 18 (.twitter-tile.text-only): 19 (.twitter-tile .media): 20 (.twitter-tile img): 21 * wp-content/themes/webkit/widgets/twitter.php: 22 1 23 2017-03-04 Filip Pizlo <fpizlo@apple.com> 2 24 -
trunk/Websites/webkit.org/wp-content/plugins/tweet-listener.php
r199491 r213740 13 13 14 14 private static $object = null; 15 15 16 16 const AUTH_TOKEN = TWITTER_LISTENER_AUTH; 17 17 const DATA_KEY = 'recent_tweet'; … … 29 29 30 30 public function listen() { 31 31 32 if (!isset($_POST['token']) || $_POST['token'] != TWITTER_LISTENER_AUTH) 33 wp_die(); 34 32 35 $defaults = array( 33 'text' => '', 36 'text' => '', 34 37 'username' => '@webkit', 35 38 'link' => '', … … 42 45 wp_die(); 43 46 } 44 47 45 48 public function tweet() { 46 49 $data = (object)get_option(self::DATA_KEY); 47 50 48 51 // Setup compatible Tweet structure 49 52 … … 65 68 } 66 69 } 67 70 68 71 return $Tweet; 69 72 } -
trunk/Websites/webkit.org/wp-content/themes/webkit/style.css
r203410 r213740 654 654 box-sizing: border-box; 655 655 font-size: 2rem; 656 text-align: center; 656 657 } 657 658 … … 660 661 display: flex; 661 662 justify-content: center; 663 text-align: left; 664 } 665 666 .twitter-tile .media { 667 text-align: center; 662 668 } 663 669 … … 671 677 672 678 .twitter-tile img { 673 width: 100%; 679 display: inline-block; 680 width: auto; 674 681 height: auto; 682 max-width: 100%; 675 683 max-height: 256px; 676 684 } … … 700 708 margin: 0 1.3rem; 701 709 } 710 702 711 .twitter-controls a { 703 712 opacity: 0.66; -
trunk/Websites/webkit.org/wp-content/themes/webkit/widgets/twitter.php
r199074 r213740 12 12 13 13 const CACHEKEY = 'webkit_twitter_feed'; 14 const ENDPOINT = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; 14 15 15 16 function __construct() { … … 31 32 return $this->follow_markup($options); 32 33 33 // Show "Follow @webkit" instead of tweet for blog URLs34 if ( ! empty($Tweet->entities)35 && ! empty($Tweet->entities->urls)36 && count($Tweet->entities->urls) == 137 && preg_match('!webkit.org/blog/\d+/!', $Tweet->entities->urls[0]->expanded_url) == 1)38 return $this->follow_markup($options);39 40 34 $classes = array('tile', 'third-tile', 'twitter-tile'); 41 42 35 $text = (string)$options['text']; 36 $link = "https://twitter.com/webkit/status/$Tweet->id"; 43 37 44 38 if ( ! empty($Tweet->text) ) 45 39 $text = $Tweet->text; 46 47 if ( ! empty($options['url']) )48 $url = (string)$options['url'];49 40 50 41 // Expand URLs … … 52 43 $expanded = '<a href="' . esc_url($entry->expanded_url) . '">' 53 44 . $entry->display_url . '</a>'; 45 46 // Don't show webkit.org links, just change the tile to use the link in the tweet 47 if ( preg_match('!webkit.org/.+?!', $entry->expanded_url) == 1 ) { 48 $expanded = ''; 49 $link = $entry->expanded_url; 50 } 51 54 52 $text = str_replace($entry->url, $expanded, $text); 55 53 } 56 54 57 55 $text = preg_replace('/RT @[^:]+:\s+/', '', $text, 1); 56 if ( ! ( empty($Tweet->entities) || empty($Tweet->entities->media) ) ) { 57 $Image = $Tweet->entities->media[0]; 58 58 59 if ( empty($Tweet->entities) || empty($Tweet->entities->media) ) {60 61 $Image = $Tweet->entities->media[0];62 59 if ( empty($Image) ) $classes[] = 'text-only'; 63 60 … … 66 63 $text = str_replace($entry->url, '', $text); 67 64 } 65 } else { 66 $classes[] = 'text-only'; 68 67 } 69 68 70 69 ?> 71 70 <div class="<?php echo esc_attr(join(' ', $classes)); ?>"> 72 <a href=" http://twitter.com/webkit/status/<?php echo esc_attr($Tweet->id); ?>" class="tile-link">Clickable link to tweet</a>71 <a href="<?php echo esc_url($link); ?>" class="tile-link">Clickable link to tweet</a> 73 72 <div class="tile-content"> 74 73 <?php if ( ! empty($Image) ): ?> … … 80 79 </div> 81 80 <ul class="twitter-controls"> 82 <li><a href="https://twitter.com/webkit" target="twitter-modal"><span class="twitter-icon">Twitter</span> 81 <li><a href="https://twitter.com/webkit" target="twitter-modal"><span class="twitter-icon">Twitter</span>@webkit</a></li> 83 82 <li><a href="https://twitter.com/intent/tweet?in-reply-to=<?php echo esc_attr($Tweet->id); ?>" class="twitter-icon reply-icon" target="twitter-modal">Reply</a></li> 84 83 <li><a href="https://twitter.com/intent/retweet?tweet_id=<?php echo esc_attr($Tweet->id); ?>" class="twitter-icon retweet-icon" target="twitter-modal">Retweet</a></li> … … 112 111 function tweet () { 113 112 114 // Prefer pushed tweet115 if ( class_exists('TweetListener') ) {116 $TweetListener = TweetListener::object();117 return $TweetListener->tweet();118 }119 120 // Poll for tweet121 113 if ( false !== ( $cached = get_transient(self::CACHEKEY) ) ) 122 114 return json_decode($cached); 123 115 124 $endpoint = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; 116 // Get pushed Tweet as a fallback 117 $pushedTweet = false; 118 if ( class_exists('TweetListener') ) { 119 $TweetListener = TweetListener::object(); 120 $pushedTweet = $TweetListener->tweet(); 121 } 122 123 // Connect to Twitter API 125 124 $parameters = array(); 126 125 $options = array( … … 128 127 ); 129 128 130 $oauth_consumer_key = 'ypSvRp37vmyt3ldMPhs0e62c9'; 131 $oauth_consumer_key_secret = 'mLuYD3AjUehUZKOgQIICA5Na69te45aSJTkdIDTGSg4cfHd6Lz'; 132 $oauth_token = '14315023-7pHbcI5bk2QZhNiHR9uFudaksBzMPubEuOEmYj7YQ'; 133 $oauth_token_secret = '0K9T9znxG4S9PUGunYZ5LwyKL9AR6v3eAXp6WKY2oi7Bg'; 129 list($oauth_consumer_key, $oauth_consumer_key_secret) = explode(':', TWITTER_CONSUMER_KEY); 130 list($oauth_token, $oauth_token_secret) = explode(':', TWITTER_OAUTH_TOKEN); 134 131 $oauth_timestamp = time(); 135 132 $oauth_nonce = sha1(rand() . $oauth_timestamp); … … 137 134 $fields = array( 138 135 'oauth_consumer_key' => $oauth_consumer_key, 139 'oauth_nonce' => sha1(rand() . time()),136 'oauth_nonce' => $oauth_nonce, 140 137 'oauth_signature_method' => 'HMAC-SHA1', 141 'oauth_timestamp' => time(),138 'oauth_timestamp' => $oauth_timestamp, 142 139 'oauth_token' => $oauth_token, 143 140 'oauth_version' => '1.0', … … 145 142 $fields = array_merge($parameters, $fields); 146 143 147 $base = array( 148 'GET', $endpoint, http_build_query($fields, '', '&') 149 ); 150 $base = join('&', array_map('rawurlencode', $base)); 144 $requestParts = array('GET', self::ENDPOINT, http_build_query($fields, '', '&')); 145 $request = join('&', array_map('rawurlencode', $requestParts)); 151 146 147 $authkeys = array($oauth_consumer_key_secret, $oauth_token_secret); 148 $auth = join('&', array_map('rawurlencode', $authkeys)); 152 149 153 $key = array( 154 $oauth_consumer_key_secret, $oauth_token_secret 155 ); 156 $key = join('&', array_map('rawurlencode', $key)); 157 158 $signature = base64_encode( hash_hmac('sha1', $base, $key, true) ); 150 $signature = base64_encode( hash_hmac('sha1', $request, $auth, true) ); 159 151 160 152 $oauth = array( … … 167 159 'oauth_version' => '1.0', 168 160 ); 161 162 // Wrap values in double-quotes. 169 163 $oauth = array_map(create_function('$h','return "\"$h\"";'), $oauth); 170 171 164 $oauth = http_build_query($oauth, '', ', '); 172 165 $oauth = str_replace('%22', '"', $oauth); … … 178 171 $options['headers'] = $headers; 179 172 180 $response = wp_remote_get( $endpoint, $options);173 $response = wp_remote_get(self::ENDPOINT, $options); 181 174 182 175 if ( is_a($response, 'WP_Error') ) 183 return false;176 return $pushedTweet; 184 177 185 178 if ( 200 == $response['response']['code'] && ! empty($response['body']) ) { … … 190 183 } 191 184 192 return false; 185 return $pushedTweet; 186 } 187 188 function isWebKitLink ($Tweet) { 189 return ( ! empty($Tweet->entities->urls[0]->expanded_url) 190 && preg_match('!webkit.org/.+?!', $Tweet->entities->urls[0]->expanded_url) == 1 ); 193 191 } 194 192
Note: See TracChangeset
for help on using the changeset viewer.