4

Apr

Filed in Code, Lifestream, WordPress |

For a while now I’ve been wanting to rewrite Lifestream into a contained OO pattern. It seems to be the “best practice” when it comes to WordPress, but tutorials and what are not spread out so it took a bit of time.

During the process, I also took the opportunity to speed up certain areas, such as option calls. Instead of storing options on a per-key basis, I changed it over to simply use a serialized arrays. In such, I added several new commands to the codebase:

	function _populate_option_cache()
	{
		if (!$this->_optioncache)
		{
			$this->_optioncache = get_option('lifestream_options');
			if (!$this->_optioncache) $this->_optioncache = $this->_options;
		}
	}
 
	/**
	 * Fetches the value of an option. Returns `null` if the option is not set.
	 */
	function get_option($option, $default=null)
	{
		$this->_populate_option_cache();
		$value = $this->_optioncache[$option];
		if (!$value)
			return $default;
		return $value;
	}
 
	/**
	 * Removes an option.
	 */
	function delete_option($option)
	{
		$this->_populate_option-cache();
		unset($this->_optioncache[$option]);
		update_option('lifestream_options', $this->_optioncache);
	}
 
	/**
	 * Updates the value of an option.
	 */
	function update_option($option, $value)
	{
		$this->_populate_option_cache();
		$this->_optioncache[$option] = $value;
		update_option('lifestream_options', $this->_optioncache);
	}
 
	/**
	 * Sets an option if it doesn't exist.
	 */
	function add_option($option, $value)
	{
		$this->_populate_option_cache();
		if (!array_key_exists($option, $this->_optioncache))
		{
			$this->_optioncache[$option] = $value;
			add_option('lifestream_options', serialize($this->_optioncache));
		}
	}

From here I moved on to change all the translation calls __, and _e into using a built-in version of which automatically handles the sprintf calls, and the namespace.

	function __($text, $params=null)
	{
		if (!is_array($params))
		{
			$params = func_get_args();
			$params = array_slice($params, 1);
		}
		return vsprintf(__($text, 'lifestream'), $params);
	}
 
	function _e($text, $params=null)
	{
		if (!is_array($params))
		{
			$params = func_get_args();
			$params = array_slice($params, 1);
		}
		echo vsprintf(__($text, 'lifestream'), $params);
	}

These two things alone cleaned up a LOT of the plugin, and the efficiency gain from the options change is amazing.

From there I decided to go all out, beyond just refactoring it into a class, I began adding some functionality from our upcoming service. Labels were moved out of the feed classes, and instead are now their own class. Each label is reusable, and controls how it’s events should be rendered. Doing this really opened the door to the possibilities of different styles of rendering for different media types.

So after a nice days work, I’ve pushed out wp-lifestream 0.96, which contains an enormous amount of bug fixes, optimizations, and best of all new features. You can see it running live on my blog, with the new display styles and all. Hopefully tomorrow I will be able to finish up the permalinks for events, which will also give the ability to attach comments to them.

View Comments Responses to "Refactoring Lifestream"

Subscribe to this topic with RSS or get the Trackback URL
Kevin Donahue (Apr 5th):

This must have taken hours and hours and days and days to rewrite. Thanks for the slick code!

Kastam (Apr 6th):

I have tried to install this plugin, but i have problem.
This messege appear after i have activated :

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /home/icarelas/public_html/optikonline/wp-content/plugins/lifestream/lifestream.php on line 140″

Could you give me solution for this problem…
I tried to download twice and I also tried to installed directly from the dasboard of my WP.
:)

D3NE (Apr 6th):

David!,

There’s a problem with my lifestream plugin.

[ There were errors with your request:

* Error fetching feed from http://twitter.com/statuses/user_timeline/creativedn.rss?page=1&count=20 ()....

Twitter / creativedn
http://twitter.com/creativedn
]

It happen every time, i tried on different browsers and different version of the plugin, but still recieving the same bug…

Please help, i need this to start my blog x_x.

Thank you.

Mike (Apr 8th):

I’m getting the exact same error as Kastam. It’s happening on two different blogs of mine which meet the php and MySQL requirements.

Any thoughts?

Kastam (Apr 10th):

Yes Mike,
last I check my PHP version 5.2.9 and MySQL version 4.1.22-standard.

Sooo.. I can’t use this plugin from my hosting….

Any type plugin smilliar with this that matches with our hosting…?

dandyman (Apr 14th):

Still having trouble with the Twitter feature. It complains there is no RSS feed at the URL, but I can add that URL to Google Reader or any other RSS reader I’ve tried.

Tanin (Apr 16th):

In order to help sites validate XHTML, you may want to generate your $id in the render function using something like
$id = ‘lse’ . uniqid();

microtime() returns a value that does not validate as a single unique ID (in other words, it has spaces in it)

Najib Hassan (Apr 18th):

too bad it not working for my blog. reason is my hosting services still using old sql and php version. most hosting still using old php and mysql version..

David (Apr 19th):

@Najib On the contrary, most hosters have been running PHP5 and MySQL5 for quite some time. They have been the standard release for several years.

Kaitlin (May 12th):

Thanks so much for all of your hard work!

dan (May 15th):

Great plugin! I am trying to use it one one of my sites and for some reason I keep getting errors,

“exception \’LifeStream_FeedFetchError\’ with message \’Error fetching feed from / A feed could not be found at”

When I click the link it references, the feed works. It also did grab my lifestream feed from twitter originally so I do have content showing, but its stuck now and hasn’t updated in 3 days.

I have the requirements on my server to run this so im curious as to why it won’t pull a feed anymore

zlinko (Jun 24th):

Cool plugin. I wanted to ask what is going on with the http://www.ibegin.com website? I have tried to check out the forums on your plugin on ibegin.com but it seems the site is down or completely gone from the net.

Any idea what might have happened?

air jordan 10 (Jun 5th):

Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum! http://spoon8.net/

UGG boots (Jun 11th):

I am also a XX fan who really like this! I also like XX, and purchase lots of it every time,
<a href=” http://www.eeadie.com“ >eeadie
like-minded friends can have a look ,we can communicate by the way~~

I am agree with landlord, many of us are not too concerned about sports news. As the World Cup. Many of us are also don't know what it is.I believe once you go to see some sports news,you will love sports.By the way ,I know some websites content is very well.But I don’t know how to choose.Can you help me?The website are Air force 1 high

air force 1 shoes (Jun 25th):

The post of content is very interesting and exciting. I learned a lot from here.The content from simple to complex, so all of you can come in . No matter you want to see what can Nike air force be found

uggs outlet (Jul 5th):

Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
ajf 8
If its common it might be difficult to get, however, be creative and you can usually find something.

jordan shoes (Jul 14th):

Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
cheap uggs
If its common it might be difficult to get, however, be creative and you can usually find something.

Leave A Reply

 Username (*required)

 Email Address (*private)

 Website (*optional)

Note: Comments moderation may be active so there is no need to resubmit your comment.
blog comments powered by Disqus