* @license MIT * @link http://www.florian.girardey.net */ class twidget_widget extends WP_widget { /** * Init the widget in WordPress * * @access public * * @since 0.1 * * @return mixed Value. */ public function twidget_widget() { $options = array( 'classname' => 'twidget_widget', 'description' => __('Display your twitter feed.', 'twidget') ); $this->WP_widget('twidget', 'Twidget', $options); } /** * Generate HTML code which is displayed in the front-end. * * @param mixed $args Default arguments of the widget. * @param mixed $instance The instance of the widget. * * @access public * * @since 0.1 * * @return string Value. */ public function widget($args, $instance) { // Register the widget id in a variable class to avoid troubleshoots $this->id = $args['widget_id']; // Register Twidget stylesheets and scripts wp_register_style( 'twidget', plugins_url('css/twidget.css', __FILE__), array(), VERSION, null ); wp_enqueue_style( 'twidget' ); $tweets = $this->getTweets($instance); echo $args['before_widget']; echo $args['before_title'] . $instance['title'] . $args['after_title']; ?>
$tweet): ?> text; $txt = $this->encode_tweet($txt); $txt = $this->hyperlinks($txt); $txt = $this->twitter_users($txt); $txt = utf8_encode($txt); // var_dump($this); ?>

'Twidget' ); $instance = wp_parse_args($instance, $default); ?>

id="get_field_id('profile_name') ?>" name="get_field_name('profile_name') ?>" value="true" />

id="get_field_id('profile_image') ?>" name="get_field_name('profile_image') ?>" value="true" />

$value) { if(empty($value)) unset($new[$key]); } delete_transient($this->id.'-tweets'); return $new; } /** * Get tweets from registered API information. * Set a cache for tweets with the Transient API, the twitter feed is kept 60s * * @param mixed $instance Description. * * @access private * * @uses TwitterOAuth library, Transient API from Wordpress * * @since 0.1 * * @return mixed Value. */ private function getTweets($instance) { $cache = get_transient($this->id.'-tweets'); $defaults = array( 'consumer_key' => 'TNRQcXyZP3enOAT14vaoA', 'consumer_secret' => 'aYwpSWoYkfL8MKnmlgxMHSQH4DpJR3MTPt32FVdaLg', 'access_token' => '362451644-jybYhIL5stX59UXytmyALugRhXZICtWHsCj8ahSY', 'access_token_secret' => 'zJPCCkj8dCd6HujuVQqaIaOf0n4ZtW6H2zARLeuz78k', 'count' => 3, 'profile_name' => false, 'profile_image' => false ); $instance = wp_parse_args($instance, $defaults); $this->profile_name = (bool) $instance['profile_name']; $this->profile_image = (bool) $instance['profile_image']; if(!$cache){ $connection = new TwitterOAuth( $instance['consumer_key'], $instance['consumer_secret'], $instance['access_token'], $instance['access_token_secret'] ); $tweets = $connection->get('statuses/user_timeline', array('count' => $instance['count'])); set_transient($this->id.'-tweets', serialize($tweets), 60); } else $tweets = unserialize($cache); return $tweets; } /** * Hyperlinks parser for tweets * * @param string $text tweet. * * @access private * * @since 0.2 * * @return string HTML tweets. */ private function hyperlinks($text) { $text = preg_replace( '@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@', '$1', $text); $text = preg_replace( '/([\s|\|"])+#(\w+)/', '$1#$2', $text); return $text; } /** * Twitter User link parser * * @param string $text tweet. * * @access private * * @since 0.2 * * @return string HTML tweets. */ private function twitter_users($text) { $text = preg_replace('/@(\w+)/', '@$1', $text); return $text; } /** * Encode single quotes in tweets * * @param string $text tweet. * * @access private * * @since 0.2 * * @return string HTML tweets encoded. */ private function encode_tweet($text) { $text = mb_convert_encoding( $text, "HTML-ENTITIES", "UTF-8"); return $text; } /** * Return a user-friendly created_at for a specified tweet * * @param mixed $tweet Only one tweet. * * @access private * * @since 0.4 * * @return string Value. */ private function date_tweet($tweet) { $time = strtotime($tweet->created_at); $diff = time() - $time; $mins = $diff / 60; $hours = $mins / 60; $days = $hours / 24; if($diff < 60) { $toDisplay = $diff; $alias = __('s','twidget'); } else if($mins < 60) { $toDisplay = $mins; $alias = __('min','twidget'); } else if($hours < 24) { $toDisplay = $hours; $alias = __('h','twidget'); } else if($days < 7) { $toDisplay = $days; $alias = __('d','twidget'); } $toDisplay = floor($toDisplay); return $toDisplay.' '.$alias; } /** * Return the @Name of the author of the tweet * * @param mixed $tweet The tweet. * * @access private * * @since 0.4 * * @return string Value. */ private function author_tweet_username($tweet){ return $tweet->user->screen_name; } /** * Return the real name of the author of the tweet * * @param mixed $tweet The tweet. * * @access private * * @since 0.4 * * @return string Value. */ private function author_tweet_fullname($tweet){ return $tweet->user->name; } /** * Return the real name of the author of the tweet * * @param mixed $tweet The tweet. * * @access private * * @since 0.4 * * @return string Value. */ private function author_tweet_url($tweet){ return 'https://twitter.com/' . $tweet->user->screen_name; } /** * Return the real name of the author of the tweet * * @param mixed $tweet The tweet. * * @access private * * @since 0.4 * * @return string Value. */ private function author_tweet_image_url($tweet){ return $tweet->user->profile_image_url; } }