Plugin Order and Independence
by , 05-22-2010 at 04:35 PM (4824 Views)
I often say in the forums that the plugin order is important for everything to run smoothly, but this isn't entirely true anymore. A few versions ago, each plugin ran a function at the theme_index_top plugin hook, like this:
If a User page, show user posts,
If a Category page show category posts,
If a Tags page, show tag posts,
If a Search page, show search results,
Etc.
Nice and logical, right?
Wrong. The problem was that even though we knew the page name, we didn't know whether to show popular, upcoming or latest posts unless the SB Base plugin had run first. Often, you'd have to juggle the order of plugins until you got it working. The solution at the time was to change all those plugins to use a new hook: sb_base_theme_index_top which was put in SB Base itself, forcing them to run after SB Base has determined the kind of posts page.
Clever, eh?
No, not really, because that led all the plugins to require SB Base.... which is where we are at with Hotaru 1.2.
This evening, I've been going through some of the main plugins, removing all references to SB Base so that they can be used for other kinds of websites, too, not only social bookmarking. But doing this without reviving the old plugin order puzzles has again been tricky.
For my current test site, I've removed SB Base completely, but divided its functions over three plugins: Bookmarking, Post RSS and Archive. The solution I've come up with to the above problem is quite elegant, with just a touch of hackiness:
The Bookmarking plugin has to run first, but the order of other plugins shouldn't matter. First, we determine whether the page is for popular, upcoming or latest posts, and then we duplicate the theme_index_top hook, excluding the current plugin to avoid an infinite loop. Once all the other plugins have run their functions at that hook, we can then continue setting up the page in full knowledge of what the other plugins are trying to do. Finally, I've added the ability to return "skip", which will prevent the first theme_index_top calling all the plugins for a second time.PHP Code:/**
* theme_index_top
*/
public function theme_index_top($h)
{
// check if this is for popular, upcoming, latest posts or other...
$this->determinePage($h);
// run all other theme_index_top functions except this one...
$h->pluginHook('theme_index_top', '', array(), array('bookmarking'));
// we've now checked all the other plugins and know what to do, so...
$this->finalizePage($h);
// don't run anymore "theme_index_top" functions...
return "skip";
}
I need to do a lot more testing, but I think this is the key for successful communication between plugins and the "base" plugin, without tying them all up together.








Email Blog Entry