/**
 * Construct Twitter tab
 *
 * @since 1.0
 */
function ts_fab_show_twitter($context = '', $authorid = '')
{
    // Grab settings
    $ts_fab_settings = ts_fab_get_tabs_settings();
    if ($authorid == '') {
        global $authordata;
        $author = $authordata;
    } else {
        $author = get_userdata($authorid);
    }
    // Check if author has entered twitter username into WordPress profile
    if (get_user_meta($author->ID, 'ts_fab_twitter', true)) {
        $screen_name = get_user_meta($author->ID, 'ts_fab_twitter', true);
        // Check if Twitter request response is cached
        $latest_tweet = get_transient('ts_fab_twitter-' . $author->ID);
        // Get latest tweet username, if it exists, to avoid PHP notice
        if (isset($latest_tweet->user->screen_name)) {
            $latest_tweet_user = $latest_tweet->user->screen_name;
        } else {
            $latest_tweet_user = false;
        }
        // If not, make a Twitter API call, cache the response
        if (false === $latest_tweet || strtolower(get_user_meta($author->ID, 'ts_fab_twitter', true)) != strtolower($latest_tweet_user)) {
            $api_call = 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . get_user_meta($author->ID, 'ts_fab_twitter', true);
            // Unsetting the variable, if user changes Twitter username
            unset($latest_tweet);
            $response = wp_remote_get($api_call, array('sslverify' => false));
            if (!is_wp_error($response) && isset($response['body']) && !isset($response['error'])) {
                $body = $response['body'];
                $twitter_info = json_decode($body);
                // Check if Twitter API returned an error by making sure latest tweet text exists
                if (is_array($twitter_info) && isset($twitter_info[0]->text)) {
                    $latest_tweet = $twitter_info[0];
                    $cache_int = $ts_fab_settings['twitter_cache_interval'] * 60;
                    set_transient('ts_fab_twitter-' . $author->ID, $latest_tweet, $cache_int);
                }
            }
        }
        // Store information we plan to use as variables
        if (isset($latest_tweet) && '' != $latest_tweet) {
            $status = $latest_tweet->text;
            $tweet_time = $latest_tweet->created_at;
            if (isset($latest_tweet->user->description)) {
                $description = $latest_tweet->user->description;
            }
        } else {
            $status = __('<!-- Couldn\'t fetch latest tweet -->', 'ts-fab');
        }
        // Create Fanciest Author Box output
        $ts_fab_twitter = '
		<div class="ts-fab-tab" id="ts-fab-twitter-' . $context . '">
			<div class="ts-fab-avatar">' . ts_fab_get_author_image($author) . '</div>
			<div class="ts-fab-text">
				<div class="ts-fab-header">
					<h4><a href="http://twitter.com/' . $screen_name . '">@' . $screen_name . '</a></h4>';
        if ($ts_fab_settings['twitter_bio'] == 1 && isset($description)) {
            $ts_fab_twitter .= '<div class="ts-fab-description">' . ts_fab_link_twitter($description) . '</div>';
        }
        $ts_fab_twitter .= '</div><!-- /.ts-fab-header -->';
        if ($ts_fab_settings['twitter_tweet'] == 1) {
            $ts_fab_twitter .= '<div class="ts-fab-content">';
            $ts_fab_twitter .= ts_fab_link_twitter($status);
            if (isset($tweet_time)) {
                $ts_fab_twitter .= '<span class="ts-fab-twitter-time"> - ' . human_time_diff(strtotime($tweet_time), time('U')) . ' ago</span>';
            }
            $ts_fab_twitter .= '</div>';
        }
        $show_count = $ts_fab_settings['twitter_count'] == 1 ? ' data-show-count="true" ' : ' data-show-count="false" ';
        $ts_fab_twitter .= '<div class="ts-fab-follow"><a href="https://twitter.com/' . $screen_name . '" class="twitter-follow-button"' . $show_count . 'data-lang="' . get_locale() . '">Follow @' . $screen_name . '</a></div>
				<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
			</div>
		</div>';
        return $ts_fab_twitter;
    }
}
Exemplo n.º 2
0
/**
 * Construct Twitter tab
 *
 * @since 1.0
 */
function ts_fab_show_twitter($context = '', $authorid = '')
{
    // Grab settings
    $ts_fab_settings = ts_fab_get_tabs_settings();
    if ($authorid == '') {
        global $authordata;
        $author = $authordata;
    } else {
        $author = get_userdata($authorid);
    }
    // Check if author has entered twitter username into WordPress profile
    if (get_user_meta($author->ID, 'ts_fab_twitter', true)) {
        $screen_name = get_user_meta($author->ID, 'ts_fab_twitter', true);
        if (isset($ts_fab_settings['twitter_consumer_key']) && isset($ts_fab_settings['twitter_consumer_secret'])) {
            // Include Twitter API 1.1 Client for WordPress
            require_once dirname(__FILE__) . '/class-wp-twitter-api.php';
            // Set your personal data retrieved at https://dev.twitter.com/apps
            $credentials = array('consumer_key' => $ts_fab_settings['twitter_consumer_key'], 'consumer_secret' => $ts_fab_settings['twitter_consumer_secret']);
            // Let's instantiate our class with our credentials
            $twitter_api = new Wp_Twitter_Api($credentials);
            // Example b - Retrieve my follower with a cache of 24 hour (default 30 minutes)
            $query = 'count=1&include_entities=true&screen_name=' . $screen_name;
            $args = array('type' => 'statuses/user_timeline', 'cache' => $ts_fab_settings['twitter_cache_interval'] * 60);
            $result = $twitter_api->query($query, $args);
        }
        // Store information we plan to use as variables
        if (isset($result[0]->text) && '' != $result[0]->text) {
            $status = $result[0]->text;
            $tweet_time = $result[0]->created_at;
            if (isset($result[0]->user->description)) {
                $description = $result[0]->user->description;
            }
        } else {
            $status = __('<!-- Couldn\'t fetch latest tweet -->', 'ts-fab');
        }
        // Create Fanciest Author Box output
        $ts_fab_twitter = '
		<div class="ts-fab-tab" id="ts-fab-twitter-' . $context . '">
			<div class="ts-fab-avatar">' . ts_fab_get_author_image($author) . '</div>
			<div class="ts-fab-text">
				<div class="ts-fab-header">
					<h4><a href="http://twitter.com/' . $screen_name . '">@' . $screen_name . '</a></h4>';
        if ($ts_fab_settings['twitter_bio'] == 1 && isset($description)) {
            $ts_fab_twitter .= '<div class="ts-fab-description">' . ts_fab_link_twitter($description) . '</div>';
        }
        $ts_fab_twitter .= '</div><!-- /.ts-fab-header -->';
        if ($ts_fab_settings['twitter_tweet'] == 1) {
            $ts_fab_twitter .= '<div class="ts-fab-content">';
            $ts_fab_twitter .= ts_fab_link_twitter($status);
            if (isset($tweet_time)) {
                $ts_fab_twitter .= '<span class="ts-fab-twitter-time"> - ' . human_time_diff(strtotime($tweet_time), time('U')) . ' ago</span>';
            }
            $ts_fab_twitter .= '</div>';
        }
        $show_count = $ts_fab_settings['twitter_count'] == 1 ? ' data-show-count="true" ' : ' data-show-count="false" ';
        $ts_fab_twitter .= '<div class="ts-fab-follow"><a href="https://twitter.com/' . $screen_name . '" class="twitter-follow-button"' . $show_count . 'data-lang="' . get_locale() . '">Follow @' . $screen_name . '</a></div>
				<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
			</div>
		</div>';
        return $ts_fab_twitter;
    }
}