Exemplo n.º 1
0
/**
 * Get one recent tweet
 * @param type $return_array
 * @return string|boolean
 */
function ts_get_recent_tweet($return_array = false)
{
    $username = ts_get_twitter_username();
    if ($username === false) {
        return '';
    }
    $cache = get_option('theme-recent-tweet');
    //display from cache, skip cache if username is changed
    if (ot_get_option('dont_cache_tweets') != 'yes' && is_array($cache) && $cache['username'] == $username && (int) $cache['time'] + 5 * 60 > time()) {
        if (isset($cache['tweets']) && !empty($cache['tweets'])) {
            return $cache;
        }
        return false;
    } else {
        require_once get_template_directory() . '/framework/class/TwitterAPIExchange.php';
        $settings = array('oauth_access_token' => ot_get_option('twitter_user_token'), 'oauth_access_token_secret' => ot_get_option('twitter_token_secret'), 'consumer_key' => ot_get_option('twitter_consumer_key'), 'consumer_secret' => ot_get_option('twitter_consumer_secret'));
        $url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
        $requestMethod = "GET";
        $getfield = '?screen_name=' . $username . '&count=20';
        $twitter = new TwitterAPIExchange($settings);
        $response = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
        $tweets = json_decode($response);
        if (is_array($tweets) && count($tweets) > 0) {
            $data = array('time' => time(), 'username' => $username, 'tweets' => $response, 'is_error' => false);
        } else {
            $data = array('time' => time(), 'username' => $username, 'tweets' => null, 'is_error' => false);
        }
        update_option('theme-recent-tweet', $data);
        return $data;
    }
}
Exemplo n.º 2
0
/**
 * Get one recent tweet
 * @param type $return_array
 * @return string|boolean
 */
function ts_get_recent_tweet($return_array = false)
{
    $username = ts_get_twitter_username();
    if ($username === false) {
        return '';
    }
    $cache = get_option('theme-recent-tweet');
    //display from cache, skip cache if username is changed
    if (is_array($cache) && $cache['username'] == $username && (int) $cache['time'] + 5 * 60 > time()) {
        if (isset($cache['tweets']) && !empty($cache['tweets'])) {
            return $cache;
        }
        return false;
    } else {
        require_once get_template_directory() . '/framework/class/tmhOAuth.php';
        require_once get_template_directory() . '/framework/class/tmhUtilities.php';
        $tmhOAuth = new tmhOAuth($a = array('consumer_key' => ot_get_option('twitter_consumer_key'), 'consumer_secret' => ot_get_option('twitter_consumer_secret'), 'user_token' => ot_get_option('twitter_user_token'), 'user_secret' => ot_get_option('twitter_token_secret'), 'curl_ssl_verifypeer' => false));
        $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/user_timeline'), array('screen_name' => $username));
        $response = $tmhOAuth->response;
        $tweets = null;
        if ($response['code'] == 200 && isset($response['response']) && !empty($response['response'])) {
            $tweets = json_decode($response['response']);
        } else {
            $tweets = json_decode($response['response']);
            return array('is_error' => true, 'error' => isset($tweets->errors[0]->message) ? $tweets->errors[0]->message : 'Unknown error');
        }
        if ($response['code'] == 200) {
            if (is_array($tweets) && count($tweets) > 0) {
                $data = array('time' => time(), 'username' => $username, 'tweets' => $response['response'], 'is_error' => false);
            } else {
                $data = array('time' => time(), 'username' => $username, 'tweets' => '', 'is_error' => false);
            }
            update_option('theme-recent-tweet', $data);
            return $data;
        }
    }
}