Last active
December 23, 2015 07:28
-
-
Save PeterBooker/6600526 to your computer and use it in GitHub Desktop.
This is taken from my Kebo Twitter Feed plugin for WordPress and linkifies Tweet text (Hashtags, Mentions and URLs).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Converts Tweet text urls, account names and hashtags into HTML links. | |
* Accepts return data from Twitter API (v1.1) | |
*/ | |
function kebo_twitter_linkify( $tweets ) { | |
foreach ( $tweets as $tweet ) { | |
$hash_length = 45; // Length of HTML added to hashtags | |
$mention_length = 33; // Length of HTML added to mentions | |
$markers = array(); | |
/* | |
* Linkify Hashtags | |
*/ | |
if ( ! empty( $tweet->entities->hashtags ) ) { | |
// One Hashtag at a time | |
foreach ( $tweet->entities->hashtags as $hashtag ) { | |
// Start offset from 0 | |
$offset = 0; | |
// Calculate length of hastag - end minus start | |
$length = $hashtag->indices[1] - $hashtag->indices[0]; | |
// If no markers, no need to offset | |
if ( ! empty( $markers ) ) { | |
foreach ( $markers as $mark ) { | |
// If the start point is past a previous marker, we need to adjust for the characters added. | |
if ( $hashtag->indices[0] > $mark['point'] ) { | |
// Include previous offsets. | |
$offset = ( $offset + ( $mark['length'] ) ); | |
} | |
} | |
} | |
/* | |
* Replace hashtag text with an HTML link | |
*/ | |
$tweet->text = substr_replace( $tweet->text, '<a href="http://twitter.com/search?q=%23' . $hashtag->text . '">#' . $hashtag->text . '</a>', $hashtag->indices[0] + $offset, $length ); | |
// Set marker so we can take into account the characters we just added. | |
$markers[] = array( | |
'point' => $hashtag->indices[0], | |
'length' => $hash_length + $length, | |
); | |
} | |
} | |
/* | |
* Linkify Mentions | |
*/ | |
if ( ! empty( $tweet->entities->user_mentions ) ) { | |
// One Mention at a time | |
foreach ( $tweet->entities->user_mentions as $mention ) { | |
// Start offset from 0 | |
$offset = 0; | |
// Calculate length of mention - end minus start | |
$length = $mention->indices[1] - $mention->indices[0]; | |
// If no markers, no need to offset | |
if ( ! empty($markers) ) { | |
foreach ( $markers as $mark ) { | |
// If the start point is past a previous marker, we need to adjust for the characters added. | |
if ( $mention->indices[0] > $mark['point'] ) { | |
// Include previous offsets. | |
$offset = ( $offset + ( $mark['length'] ) ); | |
} | |
} | |
} | |
/* | |
* Replace mention text with an HTML link | |
*/ | |
$tweet->text = substr_replace( $tweet->text, '<a href="http://twitter.com/' . $mention->screen_name . '">@' . $mention->screen_name . '</a>', $mention->indices[0] + $offset, $length ); | |
// Set marker so we can take into account the characters we just added. | |
$markers[] = array( | |
'point' => $mention->indices[0], | |
'length' => $mention_length + $length, | |
); | |
} | |
} | |
/* | |
* Linkify text URLs | |
*/ | |
$tweet->text = make_clickable( $tweet->text ); | |
/* | |
* Add target="_blank" to all links | |
*/ | |
$tweet->text = links_add_target( $tweet->text, '_blank', array( 'a' ) ); | |
/* | |
* Decode HTML Chars like ' to ' | |
*/ | |
$tweet->text = htmlspecialchars_decode( $tweet->text, ENT_QUOTES ); | |
} | |
return $tweets; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment