Extension WordPress Twitter

  • par

Code simple qui permet d’afficher les derniers messages de votre compte Twitter dans une section choisie de votre carnet wordpress.


// Affiche vos messages Twitter dans WordPress. Insérez tout
// simplement la fonction « ?php twitter_messages("twitterusername"); ? »
// à l'endroit voulu sur une section de votre theme.
// Préformté si list=true. Remplacer TwitterUserName par votre
// nom usager Twitter.  License: GPL
//

define( 'MAGPIE_CACHE_ON', 1 );
define( 'MAGPIE_CACHE_AGE', 180 );
define( 'MAGPIE_INPUT_ENCODING', 'UTF-8' );
define( 'MAGPIE_OUTPUT_ENCODING', 'UTF-8' );
$twitter_options[ 'prefix' ] = 'twitter';
function fetch_rss_with_request_headers( $url, $request_headers = array() )
{
// initialise constante
init();
if ( !isset($url) ) {
return false;
}
// if cache is disabled
if ( !MAGPIE_CACHE_ON ) {
// fetch file, parse it
$resp = _fetch_remote_file( $url );
if ( is_success( $resp->status ) ) {
return _response_to_rss( $resp );
}
else {
return false;
}
}
// else cache est ON
else {

$cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );

if (MAGPIE_DEBUG and $cache->ERROR) {
debug($cache->ERROR, E_USER_WARNING);
}
$cache_status 	 = 0;	// reponse de check_cache
$rss 			 = 0;	// parsed RSS object
$errormsg		 = 0;	// erreur !

if (!$cache->ERROR) {
$cache_status = $cache->check_cache( $url );
}
// if object cached, and cache is fresh, return cached object
if ( $cache_status == 'HIT' ) {
$rss = $cache->get( $url );
if ( isset($rss) and $rss ) {
$rss->from_cache = 1;
if ( MAGPIE_DEBUG > 1) {
debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
}
return $rss;
}
}
// else attempt a conditional get
// setup headers
if ( $cache_status == 'STALE' ) {
$rss = $cache->get( $url );
if ( isset($rss->etag) and $rss->last_modified ) {
$request_headers['If-None-Match'] = $rss->etag;
$request_headers['If-Last-Modified'] = $rss->last_modified;
}
}
$resp = _fetch_remote_file( $url, $request_headers );

if (isset($resp) and $resp) {
if ($resp->status == '304' ) {
// copie la plus actuelle
if ( MAGPIE_DEBUG > 1) {
debug("Got 304 for $url");
}
// reset cache sur 304
$cache->set($url, $rss);
return $rss;
}
elseif ( is_success( $resp->status ) ) {
$rss = _response_to_rss( $resp );
if ( $rss ) {
if (MAGPIE_DEBUG > 1) {
debug("Fetch successful");
}
// ajouter objet a la cache
$cache->set( $url, $rss );
return $rss;
}
}
else {
$errormsg = "Failed to fetch $url. ";
if ( $resp->error ) {
// compense pour 'n'
$http_error = substr($resp->error, 0, -2);
$errormsg .= "(HTTP Error: $http_error)";
}
else {
$errormsg .=  "(HTTP Response: " . $resp->response_code .')';
}
}
}
else {
$errormsg = "incapable de trouver le fil RSS pour une raison inconnue.";
}
// autrement fetch erreur
// retourner objet caché
if ($rss) {
if ( MAGPIE_DEBUG ) {
debug("Returning STALE object for $url");
}
return $rss;
}
// echec total
error( $errormsg );
return false;
} // end if ( !MAGPIE_CACHE_ON ) {
}
// Afficher les messages Twitter
function twitter_messages( $username = 'TwitterUserName', $num = 5, $list = true, $update = true, $linked = '# Twitter', $hyperlinks = true, $twitter_users = true, $encode_utf8 = true )
{
global $twitter_options;
include_once( ABSPATH . WPINC . '/rss.php' );
$messages = fetch_rss( 'http://twitter.com/statuses/user_timeline/' . $username . '.rss' );
if( $list )
echo
'
«ul class="twitter"»;
if( $username == '' )
{
if( $list )
echo
'
«li»';
echo 'RSS not configured';
if( $list )
echo
'«/li»
';
}
else
{
if( empty( $messages -> items ) )
{
if( $list )
echo
'
«li»';
echo 'Aucun message.';
if( $list )
echo
'«/li»
';
}
else
{
$i = 0;
foreach( $messages -> items AS $message )
{
$msg = " " . substr(strstr( $message[ 'description' ], ': '), 2, strlen( $message[ 'description' ])) . " ";
if( $encode_utf8 )
$msg = utf8_encode( $msg );
$link = $message[ 'link' ];
if( $list )
echo
'
«li class="twitter-item"»';
else
if( $num != 1 )
echo
'
';
if( $linked != '' || $linked != false )
{
if( $linked == 'all' )
$msg = '<a class="twitter-link" href="' . $link . '">' . $msg . '</a>';
else
$msg = $msg . '<a class="twitter-link" href="' . $link . '">' . $linked . '</a>';
}
if( $hyperlinks )
$msg = hyperlinks( $msg );
if( $twitter_users )
$msg = twitter_users( $msg );
echo $msg;
if( $update )
{
$time = strtotime( $message[ 'pubdate' ] );
if( (abs( time() - $time) ) &lt; 86400 )
$h_time = sprintf( __( '%s ago'), human_time_diff( $time ) );
else
$h_time = date(__( 'Y/m/d'), $time );
echo
sprintf( __( '%s', 'twitter-for-wordpress'), ' <span class="twitter-timestamp"><abbr title="' . date(__( 'Y/m/d H:i:s'), $time ) . '">' . $h_time . '</abbr></span>' );
}
if( $list )
echo
'
';
else
if( $num != 1 )
echo
'
';
$i++;
if( $i &gt;= $num )
break;
}
}
}
if( $list )
echo
'
';
}
// Links
function hyperlinks( $text )
{
// match protocol://address/path/file.extension?some=variable&amp;another=asf%
$text = preg_replace("/s([a-zA-Z]+://[a-z][a-z0-9_.-]*[a-z]{2,6}[a-zA-Z0-9/*-?&amp;%]*)([s|.|,])/i"," <a class="&quot;twitter-link&quot;" href="">$1</a>$2", $text );
// match www.something.domain/path/file.extension?some=variable&amp;another=asf%
$text = preg_replace("/s(www.[a-z][a-z0-9_.-]*[a-z]{2,6}[a-zA-Z0-9/*-?&amp;%]*)([s|.|,])/i"," <a class="&quot;twitter-link&quot;" href="">$1</a>$2", $text );
// match name@address
$text = preg_replace("/s([a-zA-Z][a-zA-Z0-9_.-]*[a-zA-Z]*@[a-zA-Z][a-zA-Z0-9_.-]*[a-zA-Z]{2,6})([s|.|,])/i"," <a class="&quot;twitter-link&quot;" href="">$1</a>$2", $text );
return $text;
}
function twitter_users( $text )
{
$text = preg_replace( '/([.|,|:|¡|¿|&gt;|{|(]?)@{1}(w*)([.|,|:|!|?|&gt;|}|)]?)s/i', "$1<a class="&quot;twitter-user&quot;" href="">@$2</a>$3 ", $text );
return $text;
}
?&gt;

Note: dans le listing, remplacez les balises « et » par < et >. On peut prendre ce code [twitter_ex.php] dans le bon format  *ici*.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *