public static function object() { if (self::$object === null) { self::$object = new self(); } return self::$object; }
function tweet() { // Prefer pushed tweet if (class_exists('TweetListener')) { $TweetListener = TweetListener::object(); return $TweetListener->tweet(); } // Poll for tweet if (false !== ($cached = get_transient(self::CACHEKEY))) { return json_decode($cached); } $endpoint = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; $parameters = array(); $options = array('method' => 'GET'); $oauth_consumer_key = 'ypSvRp37vmyt3ldMPhs0e62c9'; $oauth_consumer_key_secret = 'mLuYD3AjUehUZKOgQIICA5Na69te45aSJTkdIDTGSg4cfHd6Lz'; $oauth_token = '14315023-7pHbcI5bk2QZhNiHR9uFudaksBzMPubEuOEmYj7YQ'; $oauth_token_secret = '0K9T9znxG4S9PUGunYZ5LwyKL9AR6v3eAXp6WKY2oi7Bg'; $oauth_timestamp = time(); $oauth_nonce = sha1(rand() . $oauth_timestamp); $fields = array('oauth_consumer_key' => $oauth_consumer_key, 'oauth_nonce' => sha1(rand() . time()), 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => time(), 'oauth_token' => $oauth_token, 'oauth_version' => '1.0'); $fields = array_merge($parameters, $fields); $base = array('GET', $endpoint, http_build_query($fields, '', '&')); $base = join('&', array_map('rawurlencode', $base)); $key = array($oauth_consumer_key_secret, $oauth_token_secret); $key = join('&', array_map('rawurlencode', $key)); $signature = base64_encode(hash_hmac('sha1', $base, $key, true)); $oauth = array('oauth_consumer_key' => $oauth_consumer_key, 'oauth_nonce' => $fields['oauth_nonce'], 'oauth_signature' => $signature, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => $fields['oauth_timestamp'], 'oauth_token' => $oauth_token, 'oauth_version' => '1.0'); $oauth = array_map(create_function('$h', 'return "\\"$h\\"";'), $oauth); $oauth = http_build_query($oauth, '', ', '); $oauth = str_replace('%22', '"', $oauth); $headers = array('Authorization' => "OAuth {$oauth}"); $options['headers'] = $headers; $response = wp_remote_get($endpoint, $options); if (is_a($response, 'WP_Error')) { return false; } if (200 == $response['response']['code'] && !empty($response['body'])) { $body = json_decode($response['body']); $data = $body[0]; set_transient(self::CACHEKEY, json_encode($data), DAY_IN_SECONDS / 2); return $data; } return false; }