<?php
if(!class_exists('WP_Widget_Twitterupdate')){
/**
* Twitter Update Widget class
*
*/
class WP_Widget_Twitterupdate extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'widget_twitterupdate', 'description' => __('Twitter Widget'));
parent::__construct('twitterupdate', __('WD Twitter'), $widget_ops);
}
function widget( $args, $instance ) {
extract($args);
$title = apply_filters( 'widget_title', empty($instance['title']) ? __('Twitter Update') : $instance['title'], $instance, $this->id_base);
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
if($instance['username']){
$cache_file = get_template_directory().'/cache_theme/twitter_'.$instance['username'].'.txt';
// Seconds to cache feed (1 hour).
$cachetime = 60*60;
// Time that the cache was last filled.
$cache_file_created = ((@file_exists($cache_file))) ? @filemtime($cache_file) : 0;
// Show file from cache if still valid.
if (time() - $cachetime < $cache_file_created) {
@readfile($cache_file);
} else {
try{
if(!$limit = $instance['limit'])
$limit = 5;
//$url = "http://twitter.com/statuses/user_timeline/".$instance['username'].".xml?count=".$limit;
$url = "http://api.twitter.com/1/statuses/user_timeline.xml?include_entities=0&include_rts=1&screen_name=".$instance['username']."&count=".$limit;
$timeout = 5;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$statuses = curl_exec($ch);
curl_close($ch);
$statuses = simplexml_load_string($statuses);
ob_start();
?>
<div id="twitter-box" style="overflow: hidden;">
<?php if(!empty($statuses->status)):?>
<ul id="twitter-list">
<?php
$i = 0 ;
$class = "";
foreach($statuses->status as $status){
$class = ($i == 0 ? 'first' : ($i==(count($statuses->status)-1) ? "last" : ''));
$i++;
?>
<li class="status-item <?php echo $class;?>">
<span class="tweet-content"><?php echo twitterify($status->text);?></span>
<div class="name-time"></div>
<div class="date-time"><a href="<?php echo 'http://twitter.com/'.$status->user->screen_name.'/statuses/'.$status->id; ?>"><?php echo $this->relativeTime($status->created_at)?></a></div>
</li>
<?php };?>
</ul>
<a href="http://twitter.com/<?php echo $instance['username']?>"><span class="twitter-follow"></span></a>
<?php endif;?>
</div><!-- #ticker-wrapper -->
<?php
$file = @fopen($cache_file, 'w');
// Save the contents of output buffer to the file, and flush the buffer.
@fwrite($file, ob_get_contents());
@fclose($file);
ob_end_flush();
}catch(Excetion $e){
$result = new StdClass();
$result->status = array();
return $result;
}
}
}
echo $after_widget;
}
function relativeTime($time)
{
//echo $time,'<br>';
//echo $time = strtotime($time);
$second = 1;
$minute = 60 * $second;
$hour = 60 * $minute;
$day = 24 * $hour;
$month = 30 * $day;
$delta = strtotime('+0 hours') - strtotime($time);
if ($delta < 2 * $minute) {
return "1 min ago";
}
if ($delta < 45 * $minute) {
return floor($delta / $minute) . " min ago";
}
if ($delta < 90 * $minute) {
return "1 hour ago";
}
if ($delta < 24 * $hour) {
return floor($delta / $hour) . " hours ago";
}
if ($delta < 48 * $hour) {
return "yesterday";
}
if ($delta < 30 * $day) {
return floor($delta / $day) . " days ago";
}
if ($delta < 12 * $month) {
$months = floor($delta / $day / 30);
return $months <= 1 ? "1 month ago" : $months . " months ago";
} else {
$years = floor($delta / $day / 365);
return $years <= 1 ? "1 year ago" : $years . " years ago";
}
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['username'] = strip_tags($new_instance['username']);
$instance['limit'] = strip_tags($new_instance['limit']);
$cache_file = get_template_directory().'/cache_theme/twitter_'.$instance['username'].'.txt';
if(file_exists($cache_file))
unlink($cache_file);
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$title = strip_tags($instance['title']);
$username = esc_attr($instance['username']);
$limit = esc_attr($instance['limit']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Username twitter:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('username'); ?>" name="<?php echo $this->get_field_name('username'); ?>" type="text" value="<?php echo esc_attr($username); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e('Limit:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo esc_attr($limit); ?>" /></p>
<?php
}
}
}
?> Wordpress Twitter Eklentisi [Yardım]
3
●584
- 09-02-2014, 12:06:08Merhabalar. Temanın şöyle bir widget sistemi var fakat bit türlü çalıştıramadım. Twitter api 1.1 versiyonuna geçti diyemi çalışmıyor anlamıyorum. Yardımcı olabilecek arkadaş varmı. Kodun neresini değiştirmem gerekiyor
- 10-02-2014, 07:27:12Seninde belirttiğin gibi bir widget bu. Yani bileşen alanlarına ekledikten sonra kullanabiliyorsun tabi dediğin gibi widgette herhangi bir sorun yoksa.mesut16 adlı üyeden alıntı: mesajı görüntüle
Wordpress Twitter eklentilerini kullanabilmen için öncelikle Twitter üzerinden bir uygulama yapmanız gerekiyor. Daha sonra kullandığınız eklentiye göre hazırladığınız uygulamaya okuma, okuma yazma gibi izinler verdikten sonra uygulama api ve secret kodlarını bileşen alanına girmeniz gerekiyor Twitter kullanıcı adınızla birlikte.