Skip to content

Instantly share code, notes, and snippets.

@chaance
Last active February 7, 2018 17:15
Show Gist options
  • Save chaance/5b57981c737b2f297fa5ff547d9bea91 to your computer and use it in GitHub Desktop.
Save chaance/5b57981c737b2f297fa5ff547d9bea91 to your computer and use it in GitHub Desktop.
Uses twitteroauth PHP library to render latest tweets from a user's feed. https://github.com/abraham/twitteroauth
<?php
/**
* Wrap input inside a link to include in the body of the tweet.
*
* @todo more validation (URL?)
*
* @param string $url URL to use as a link.
* @param string $text Text to wrap.
* @return [type] Formatted HTML link.
*/
function xx_tweet_link( $url = '', $text = '' ) {
if ( empty( $url ) || empty( $text ) )
return;
return '<a class="tweet__body-link" href="' . esc_url( $url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $text ) . '</a>';
}
/**
* Format Tweets by adding links to any user mentions, hashtags,
* media, or URL refs
*
* @todo Test and refine validation.
*
* @param array $arr Array returned by the Twitter status object.
* @param string $type The type of content to format.
* @param string $tweet_text Tweet text provided by the Twitter status object.
* @return string Formatted Tweet text including HTML links.
*/
function xx_format_tweet( $arr = [], $type = 'link', $tweet_text = '' ) {
// Errrrrr
if ( empty( $arr ) || count( $arr ) < 1 || empty( $tweet_text ) || ! is_object( $arr[0] ) )
return $tweet_text;
foreach ( $arr as $item ) {
switch ( $type ) {
case 'user' :
$item = $item->screen_name;
$search = "@$item";
$replace = xx_tweet_link( "https://twitter.com/$item", "@$item" );
break;
case 'tag' :
$item = $item->text;
$search = "#$item";
$replace = xx_tweet_link( "https://twitter.com/hashtag/$item?src=hash", "#$item" );
break;
default :
$item = $item->url;
$search = $item;
$replace = xx_tweet_link( 'https://' . str_replace( [ 'https://', 'http://' ], '', $item ), $item );
break;
}
return str_replace( $search, $replace, $tweet_text );
}
}
/**
* Format and render the Tweet text to the screen
*
* @param string $handle Twitter handle to use.
* @param string $tweet_text Tweet text provided by the Twitter status object.
* @param array $links URLs returned by the Twitter status object.
* @param array $hashtags Hashtags returned by the Twitter status object.
* @param array $users User mentions returned by the Twitter status object.
* @param array $media Media links returned by the Twitter status object.
*/
function xx_tweet( $hanlde = 'TheNameIsChance', $tweet_text = '', $links = [], $hashtags = [], $users = [], $media = [] ) {
// @todo Validate some more here
if ( empty( $tweet_text ) ) {
return;
}
// May be a better DRY way to do this
// Seems like a lot of looping - might not be most performant method
$tweet_text = esc_html( $tweet_text );
$tweet_text = xx_format_tweet( $links, 'link', $tweet_text );
$tweet_text = xx_format_tweet( $media, 'link', $tweet_text );
$tweet_text = xx_format_tweet( $hashtags, 'tag', $tweet_text );
$tweet_text = xx_format_tweet( $users, 'user', $tweet_text );
?>
<article class="tweet">
<h3 class="tweet__author-handle"><?php echo esc_html( "@$handle" ); ?></h3>
<p class="tweet__body"><?php echo $tweet_text; ?></p>
</article>
<?php
}
<?php
$twitter_handle = 'TheNameIsChance';
require_once 'twitteroauth.php';
$statuses = $connection->get( 'statuses/user_timeline', array(
'count' => 2,
'exclude_replies' => TRUE,
'screen_name' => $twitter_handle,
) );
if ( ! empty( statuses ) ) {
echo '<section class="news__tweets">';
foreach ( $statuses as $status ) {
echo xx_tweet(
$twitter_handle,
$status->text,
$status->entities->urls,
$status->entities->hashtags,
$status->entities->user_mentions,
$status->entities->media
);
}
echo '</section>';
}
<?php
// Download the library then require it
// @link https://github.com/abraham/twitteroauth
require 'lib/twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
// First, create an app on Twitter and get your keys
// @link https://apps.twitter.com/
define( CONSUMER_KEY, '[KEY GOES HERE]' );
define( CONSUMER_SECRET, '[KEY GOES HERE]' );
$connection = new TwitterOAuth( CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret );
$content = $connection->get( 'account/verify_credentials' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment