WordPress.org

Ready to get started?Download WordPress

Forums

Multi Twitter Stream
Some Bug Fixes and Patch Included (4 posts)

  1. msenate
    Member
    Posted 1 year ago #

    Hi!

    I needed to display the twitter stream for several accounts, as well as display an account with the results of a search query. This plugin seemed to do the trick, but there were a couple of bugs, so I tweaked it a bit, the code is below. My patch includes:

    • Improvement to the $tweet preg_replace to add proper links for account tweets
    • A "multi-twitter" css class to <ul> for customized styling
    • Renamed indeces and loop used in "// Split array and output results" to avoid confusion / naming conflict. Also improved formatting by making screenname and search result tweets consistent in display.
    • Fixed display of human_time for search results
    Index: multi-twitter-widget/widget.php
    ===================================================================
    --- multi-twitter-widget/widget.php	(revision 3527)
    +++ multi-twitter-widget/widget.php	(working copy)
    @@ -140,7 +140,7 @@
     	    $tweet = preg_replace('/(^|\s)#(\w+)/', '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', $tweet);
    
     	if( $options['links'] )
    -		$tweet = preg_replace('#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is', '\\1\\2', $tweet);
    +		$tweet = preg_replace('#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is', '\1<a href="\2">\2</a>', $tweet);
    
     	return $tweet;
     }
    @@ -197,7 +197,7 @@
     		$widget['term_limit'] = 5;
     	} 
    
    -	$output .= '<ul>';
    +	$output .= '<ul class="multi-twitter">';
    
     	// Parse the accounts and CRUD cache
     	foreach ( $accounts as $account )
    @@ -326,20 +326,27 @@
     	}
    
     	// Sort our $feeds array
    -	usort($feeds, "feed_sort");
    +	//usort($feeds, "feed_sort");
    
     	// Split array and output results
    -	$i = 1;
    +	$sn_index = 0;
    +	$term_index = 0;
    
     	foreach ( $feeds as $feed )
     	{
    -		if ( $feed->screen_name != '' AND $i <= $widget['user_limit'] )
    +		if ( $feed->screen_name != '' AND $sn_index <= $widget['user_limit'] )
     		{
     			$output .=
     				'<li class="clearfix">'.
     					'<a href="http://twitter.com/'.$feed->screen_name.'">'.
    -						'<img class="twitter-avatar" src="'.$feed->profile_image_url.'" width="40" height="40" alt="'.$feed->screen_name.'" />'.
    -						$feed->screen_name.': </a>'.format_tweet($feed->status->text, $widget).'<br />';
    +						'<img class="twitter-avatar" src="'
    +						.$feed->profile_image_url
    +						.'" width="40" height="40" alt="'
    +						.$feed->screen_name.'" />'
    +						.$feed->screen_name
    +						.':</a> '
    +						.format_tweet($feed->status->text, $widget)
    +						.'<br />';
    
     			if ( $widget['date'] )
     			{
    @@ -348,7 +355,8 @@
    
     			$output .= '</li>';
     		}
    -		else if ( preg_match('/search.twitter.com/i', $feed->id) AND $i <= $widget['term_limit'] )
    +		$sn_index++;
    +		if ( preg_match('/search.twitter.com/i', $feed->id) AND $term_index <= $widget['term_limit'] )
     		{
     			$count = count($feed->entry);
    
    @@ -363,20 +371,20 @@
     									'src="'.$feed->entry[$i]->link[1]->attributes()->href.'" '.
     									'width="40" height="40" '.
     									'alt="'.$feed->entry[$i]->author->name.'" />'.
    -								'<strong>'.$feed->entry[$i]->author->name.':</strong>'.
    -							'</a>'.
    +								$feed->entry[$i]->author->name.
    +							'</a>: '.
     							format_tweet($feed->entry[$i]->content, $widget).
     							'<br />';
    
     					if ( $widget['date'] )
     					{
    -						$output .= '<em>'.human_time(strtotime($feed->status->created_at)).'</em>';
    +						$output .= '<em>'.human_time(strtotime($feed->updated)).'</em>';
     					}
     					$output .= '</li>';
    -				}
    +				}
     			}
    -		}
    -		$i++;
    +		}
    +		$term_index++;
     	}
    
     	$output .= '</ul>';

    http://wordpress.org/extend/plugins/multi-twitter-widget/

  2. msenate
    Member
    Posted 1 year ago #

    • Includes all the features listed in the previous post ^^^
    • Adds new option for a "result limit" on the number of tweets displayed for each search term.
    Index: multi-twitter-widget/widget.php
    ===================================================================
    --- multi-twitter-widget/widget.php	(revision 3527)
    +++ multi-twitter-widget/widget.php	(working copy)
    @@ -140,7 +140,7 @@
     	    $tweet = preg_replace('/(^|\s)#(\w+)/', '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', $tweet);
    
     	if( $options['links'] )
    -		$tweet = preg_replace('#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is', '\\1\\2', $tweet);
    +		$tweet = preg_replace('#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is', '\1<a href="\2">\2</a>', $tweet);
    
     	return $tweet;
     }
    @@ -195,9 +195,13 @@
     	if ( ! $widget['term_limit'] )
     	{
     		$widget['term_limit'] = 5;
    -	}
    +	}
    +	if ( ! $widget['result_limit'] )
    +	{
    +		$widget['result_limit'] = 2;
    +	}
    
    -	$output .= '<ul>';
    +	$output .= '<ul class="multi-twitter">';
    
     	// Parse the accounts and CRUD cache
     	foreach ( $accounts as $account )
    @@ -326,20 +330,27 @@
     	}
    
     	// Sort our $feeds array
    -	usort($feeds, "feed_sort");
    +	//usort($feeds, "feed_sort");
    
     	// Split array and output results
    -	$i = 1;
    +	$sn_index = 0;
    +	$term_index = 0;
    
     	foreach ( $feeds as $feed )
     	{
    -		if ( $feed->screen_name != '' AND $i <= $widget['user_limit'] )
    +		if ( $feed->screen_name != '' AND $sn_index <= $widget['user_limit'] )
     		{
     			$output .=
     				'<li class="clearfix">'.
     					'<a href="http://twitter.com/'.$feed->screen_name.'">'.
    -						'<img class="twitter-avatar" src="'.$feed->profile_image_url.'" width="40" height="40" alt="'.$feed->screen_name.'" />'.
    -						$feed->screen_name.': </a>'.format_tweet($feed->status->text, $widget).'<br />';
    +						'<img class="twitter-avatar" src="'
    +						.$feed->profile_image_url
    +						.'" width="40" height="40" alt="'
    +						.$feed->screen_name.'" />'
    +						.$feed->screen_name
    +						.':</a> '
    +						.format_tweet($feed->status->text, $widget)
    +						.'<br />';
    
     			if ( $widget['date'] )
     			{
    @@ -348,13 +359,14 @@
    
     			$output .= '</li>';
     		}
    -		else if ( preg_match('/search.twitter.com/i', $feed->id) AND $i <= $widget['term_limit'] )
    +		$sn_index++;
    +		if ( preg_match('/search.twitter.com/i', $feed->id) AND $term_index <= $widget['term_limit'] )
     		{
     			$count = count($feed->entry);
    
     			for ( $i=0; $i<$count; $i++ )
     			{
    -				if ( $i <= $widget['term_limit'] )
    +				if ( $i < $widget['result_limit'] )
     				{
     					$output .=
     						'<li class="clearfix">'.
    @@ -363,20 +375,20 @@
     									'src="'.$feed->entry[$i]->link[1]->attributes()->href.'" '.
     									'width="40" height="40" '.
     									'alt="'.$feed->entry[$i]->author->name.'" />'.
    -								'<strong>'.$feed->entry[$i]->author->name.':</strong>'.
    -							'</a>'.
    +								$feed->entry[$i]->author->name.
    +							'</a>: '.
     							format_tweet($feed->entry[$i]->content, $widget).
     							'<br />';
    
     					if ( $widget['date'] )
     					{
    -						$output .= '<em>'.human_time(strtotime($feed->status->created_at)).'</em>';
    +						$output .= '<em>'.human_time(strtotime($feed->updated)).'</em>';
     					}
     					$output .= '</li>';
    -				}
    +				}
     			}
    -		}
    -		$i++;
    +		}
    +		$term_index++;
     	}
    
     	$output .= '</ul>';
    @@ -414,6 +426,7 @@
     			'terms' 		=> 'wordpress',
     			'user_limit' 	=> 5,
     			'term_limit' 	=> 5,
    +			'result_limit'  => 2,
     			'links' 		=> true,
     			'reply' 		=> true,
     			'hash'			=> true,
    @@ -445,6 +458,7 @@
     			'terms' 		=> 'wordpress',
     			'user_limit' 	=> 5,
     			'term_limit' 	=> 5,
    +			'result_limit'  => 2,
     			'links' 		=> true,
     			'reply'			=> true,
     			'hash'			=> true,
    @@ -461,7 +475,7 @@
     		$options['terms'] = htmlspecialchars($_POST['multi_twitter-Terms']);
     		$options['user_limit'] = $_POST['multi_twitter-UserLimit'];
     		$options['term_limit'] = $_POST['multi_twitter-TermLimit'];
    -
    +		$options['result_limit'] = $_POST['multi_twitter-ResultLimit'];
     		$options['hash']	= ($_POST['multi_twitter-Hash']) ? true : false;
     		$options['reply']	= ($_POST['multi_twitter-Reply']) ? true : false;
     		$options['links']	= ($_POST['multi_twitter-Links']) ? true : false;
    @@ -503,7 +517,7 @@
     		</select>
     	</p>
     	<p>
    -		<label for="multi_twitter-TermLimit">Limit search feed to: </label>
    +		<label for="multi_twitter-TermLimit">Limit search terms to: </label>
     		<select id="multi_twitter-TermLimit" name="multi_twitter-TermLimit">
     			<option value="<?php echo $options['term_limit']; ?>"><?php echo $options['term_limit']; ?></option>
     			<option value="1">1</option>
    @@ -519,6 +533,22 @@
     		</select>
     	</p>
     	<p>
    +		<label for="multi_twitter-ResultLimit">Limit search results to: </label>
    +		<select id="multi_twitter-ResultLimit" name="multi_twitter-ResultLimit">
    +			<option value="<?php echo $options['result_limit']; ?>"><?php echo $options['result_limit']; ?></option>
    +			<option value="1">1</option>
    +			<option value="2">2</option>
    +			<option value="3">3</option>
    +			<option value="4">4</option>
    +			<option value="5">5</option>
    +			<option value="6">6</option>
    +			<option value="7">7</option>
    +			<option value="8">8</option>
    +			<option value="9">9</option>
    +			<option value="10">10</option>
    +		</select>
    +	</p>
    +	<p>
     		<label for="multi_twitter-Links">Automatically convert links?</label>
     		<input type="checkbox" name="multi_twitter-Links" id="multi_twitter-Links" <?php if ($options['links']) echo 'checked="checked"'; ?> />
     	</p>
  3. msenate
    Member
    Posted 1 year ago #

    Needed to upgrade to WordPress 3.5, saw the plugin was using deprecated functions, so reformatted it to use the object-oriented plugin format (permits having multiple widgets now), and various other little tweaks.

    For that and any future changes (pending a note from the plugin creator), see this: https://github.com/msenateatplos/multi-twitter-widget

  4. msenate
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic