Forums / Cotonti / Development / idea: Optimize plugin hook includes

code optimization idea

ez
#31899 2011-12-05 12:56

To activate the code from a hook, we always do something like this:

/* === Hook === */
$extp = sed_getextplugins('input');
if (is_array($extp))
{
	foreach($extp as $k => $pl)
	{
		include_once($cfg['plugins_dir'].'/'.$pl['pl_code'].'/'.$pl['pl_file'].'.php');
	}
}
/* ======================== */

 

The function sed_getextplugins itself gets off course the hooks

function sed_getextplugins($hook, $cond='R')
{
	global $sed_plugins, $usr;

	if (is_array($sed_plugins))
	{
		foreach($sed_plugins as $i => $k)
		{
			if($k['pl_hook']==$hook && sed_auth('plug', $k['pl_code'], $cond))
			{
				$extplugins[$i] = $k;
			}
		}
	}
	return $extplugins;
}

 

THE IDEA to speed things up (and this is a just a quick idea i got) to integrate these two parts together in a
new function in functions.php.

function sed_getextplugins_hook_includecode($hook, $cond='R')
{
	global $sed_plugins, $usr, $cfg;

	if (is_array($sed_plugins))
	{
		foreach($sed_plugins as $i => $k)
		{
			if($k['pl_hook']==$hook && sed_auth('plug', $k['pl_code'], $cond))
			{
				$extplugins[$i] = $k;
				// the above line CAN be optional if the return data is needed ????
				include_once($cfg['plugins_dir'].'/'.$k['pl_code'].'/'.$k['pl_file'].'.php');
			}
		}
	}
	return $extplugins;
}

 

Result:

And the call to activate hook includes is just:

/* === Hook === */
$extp = sed_getextplugins_hook_includecode('input');
/* ======================== */

This could save a couple off calls and possible foreach loops. Also the new code looks better :)
I am an optimizing freak... so i always look different @ the code

greetings,
EZ

==- I say: Keep it EZ -==