Recent Topics

A hack for plugin priorities

Started by on Mar 15, 2008 – Contents updated: Mar 15, 2008

Mar 15, 2008 04:12    

Okay this is cool: re-prioritize ALL your plugins at the same time.

It goes like this: let's say you use the video and smilies plugin. Let's say you have a youtube video with the code "pNNNNNNN". I don't know if that's the right number of characters or not - all I care about is that your youtube code begins with "p". So the video plugin makes [video:youtube:pNNNNNNN] but the smilies plugin, with it's lower priority number, sees ":p" and makes a smilie out of it, leaving the poor video plugin sitting in the corner feeling bad about sitting in the corner. Not cool you say? You're RIGHT! Fortunately, there is a solution: my groovy hack.

First you gotta tell your skin you want to have a new disp page. That's easy: just follow the directions at http://forums.b2evolution.net/viewtopic.php?t=14046 and add

'disp_plugins' => '_plugins.disp.php',

to your skin_include bit.

Now grab this and save it in your skins/yourskin/ folder as _plugins.disp.php :

<?php /* This file is a hack */

if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );

if( ! is_logged_in() ) {
	return;
	}

if( $current_User->ID != 1 ) {
	return;
	}

global $DB, $tableprefix;

$redirect_to = param( 'redirect_to', 'string', '' );
$plugins_table = $tableprefix.'plugins';
$Form = new Form( 'plugins_order.php' );
$Form->begin_form( 'bComment' );
$Form->hidden( 'redirect_to', url_rel_to_same_host($redirect_to, $htsrv_url) );
$Form->hidden( 'tableprefix', $tableprefix );
?>
<h3>HTML Renderers</h3>
<?php 
$query = ("SELECT * FROM $plugins_table ORDER BY `plug_priority` ASC ");
$plugins = mysql_query($query);
while( $plugin = mysql_fetch_assoc($plugins) ) {
	$ID = $plugin['plug_ID'];
	$priority = $plugin['plug_priority'];
	$classname = $plugin['plug_classname'];
	$short_class = str_replace( '_plugin', '', $classname );
	$code = $plugin['plug_code'];
	$events_table = $tableprefix.'pluginevents';
	$query = ("SELECT * FROM $events_table WHERE `pevt_plug_ID` =$ID AND `pevt_event` = 'RenderItemAsHtml'");
	$result = mysql_query($query);
	$html_renderer = mysql_num_rows($result);
	if( $html_renderer ) {
		$Form->text( 'a_'.$ID, $priority, 4, $short_class, 'set priority for '.$classname.' / '.$code, 5, 'bComment' );
		}
	}
?>
<h3>Toolbar Makers</h3>
<?php 
$query = ("SELECT * FROM $plugins_table ORDER BY `plug_priority` ASC ");
$plugins = mysql_query($query);
while( $plugin = mysql_fetch_assoc($plugins) ) {
	$ID = $plugin['plug_ID'];
	$priority = $plugin['plug_priority'];
	$classname = $plugin['plug_classname'];
	$short_class = str_replace( '_plugin', '', $classname );
	$code = $plugin['plug_code'];
	$events_table = $tableprefix.'pluginevents';
	$query = ("SELECT * FROM $events_table WHERE `pevt_plug_ID` =$ID AND `pevt_event` = 'AdminDisplayToolbar'");
	$result = mysql_query($query);
	$html_renderer = mysql_num_rows($result);
	if( $html_renderer ) {
		$Form->text( 'a_'.$ID, $priority, 4, $short_class, 'set priority for '.$classname.' / '.$code, 5, 'bComment' );
		}
	}
?>
<h3>Everything Else</h3>
<?php 
$query = ("SELECT * FROM $plugins_table ORDER BY `plug_priority` ASC ");
$plugins = mysql_query($query);
while( $plugin = mysql_fetch_assoc($plugins) ) {
	$ID = $plugin['plug_ID'];
	$priority = $plugin['plug_priority'];
	$classname = $plugin['plug_classname'];
	$short_class = str_replace( '_plugin', '', $classname );
	$code = $plugin['plug_code'];
	$events_table = $tableprefix.'pluginevents';
	$query = ("SELECT * FROM $events_table WHERE `pevt_plug_ID` =$ID AND `pevt_event` = 'RenderItemAsHtml' OR `pevt_plug_ID` =$ID AND `pevt_event` = 'AdminDisplayToolbar'");
	$result = mysql_query($query);
	$html_renderer = mysql_num_rows($result);
	if( ! $html_renderer ) {
		$Form->text( 'a_'.$ID, $priority, 4, $short_class, 'set priority for '.$classname.' / '.$code, 5, 'bComment' );
		}
	}
$Form->begin_fieldset();
?>
<div class="input">
<?php
$Form->button_input( array( 'name' => 'submit_message', 'class' => 'submit', 'value' => T_('Do It!') ) );
?>
</div>
<?php
$Form->end_fieldset();
?>
<div class="clear"></div>
<?php
$Form->end_form();

?>

Finally, grab this and save it in your skins/yourskin/ folder as plugins_order.php :

<?php /* This file is a hack */

require_once dirname(__FILE__).'/../../conf/_config.php';

require_once $inc_path.'_main.inc.php';

if( ! is_logged_in() ) {
	return;
	}

if( $current_User->ID != 1 ) {
	return;
	}

header( 'Content-Type: text/html; charset='.$io_charset );

$tableprefix = param( 'tableprefix', 'string', '' );
$plugins_table = $tableprefix.'plugins';
$query = ("SELECT * FROM $plugins_table ORDER BY `plug_priority` ASC ");
$plugins = mysql_query($query);
while( $plugin = mysql_fetch_assoc( $plugins ) ) {
	$ID = $plugin['plug_ID'];
	$priority = $plugin['plug_priority'];
	$classname = $plugin['plug_classname'];
	$new_priority = param( 'a_'.$ID, 'integer', '' );
	if( $new_priority < 1 || $new_priority > 100 ) {
		$Messages->add( 'Priority numbers must be between 1 and 100!!!', 'error' );
		$Messages->add( $classname. ' priority can not be '.$new_priority, 'error' );
		continue;
		}
	if( $priority != $new_priority ) {
		$query = ("UPDATE $plugins_table SET `plug_priority` =$new_priority WHERE `plug_ID` =$ID");
		$updated_priority = mysql_query($query);
		if ( ! $updated_priority ) {
			$Messages->add( 'updating '.$classname.' failed', 'error' );
			} else {
			$Messages->add( 'updating '.$classname.' succeeded', 'success' );
			}
		}
	}

header_nocache();

header_redirect();

?>

You gotta be smarter than a 5th grader here okay? You gotta know to go to your blog's index page and add "disp=plugins" to your URL. Might be ?disp=plugins and it might be &disp=plugins, which is where all the brain power comes in. Anyway if all goes well when you do that and hit enter you will see a list of your plugins sorted by priority (lowest on top) with the priority in a form field. So change them to whatever you want (assuming you want a priority between 1 and 100 inclusive) and hit the "Do It!" button.

Be Amazed.

EDIT: Updated to organize your plugins into three groups: HTML Renderers, Toolbar Makers, and Everything Else figuring it's really only the HTML Renderers that matter for conflict but Toolbar Makers get displayed in their priority number and there is pretty good crossover across the two groups.

Mar 15, 2008 06:44

Ok, so if I go through and give each plugin it's own unique ID number it wont effect which anything else like button location in the toolbars etc etc?

Mar 15, 2008 07:11

Button locations WITHIN any given toolbar won't be affected, but the ORDER of the toolbars probably will.

Untested because I've moved my toolbars all over the back office, but I'm pretty sure that ... aw heck one sec and lemme look at something...

Okay yeah what will happen is that the toolbar-generating plugin with the lowest number will be on the top of the toolbar stack. So in the example above instead of seeing smilies ABOVE videos you would get videos on top.

BTW I do not know how it decides which is on top (for toolbars) or which renders first (for renderers) when two plugins have the same priority.

Mar 15, 2008 07:14

Cool, thanks for that. I will fiddle with ID's and see what happens.

Mar 15, 2008 07:32

One thing I couldn't do because it is a very simple hack is group them by if it is a renderer or not. Really it's only the renderers that matter wrt priority, but I didn't see a simple way to ensure having only that type show up, or to group them that way.

Mar 15, 2008 08:51

It strikes me that an adapted version of this would be a great way to publish a list of CREDITS to developers of ACTIVE plugins......
off to read your code :)

Mar 15, 2008 10:04

That'd be an adaptation of BOPIT - not this tiny little hack. All this hack does is read what's in the evo_plugins table, and information there is very sparse. For example "plug_name" and "plug_shortdesc" are actually null in all my plugins according to that table, and the idea of "author" doesn't even exist in that table.

I suppose if I was smart and not so hung up on getting it done and calling it done I'd have queried the evo_pluginevents table to find which ones were html renderers so I could at least group them together. Probably find that within that group you find the toolbar makers too. I'm not sure that there will be findable cases of priority conflict outside of the renderers yah? So really I should go back in time, not post this, correct it with another table query, then accelerate the the present and ask "what am I talking about because it's all there". But hey I'm lazy about that sort of thing.

PS: I actually started with BOPIT as source material and gave up very quickly due to my inability to grasp concepts more advanced than "shiny object".

Mar 15, 2008 10:51

John wrote:

It strikes me that an adapted version of this would be a great way to publish a list of CREDITS to developers of ACTIVE plugins......
off to read your code :)

Like this ( [url=http://waffleson.co.uk/2007/10/credits]credits[/url] ) ? :roll:

To get a list of the renderer plugins you could do something like this :

		global $AvailablePlugins;

		if( empty($AvailablePlugins) || ! is_a( $AvailablePlugins, 'Plugins_no_DB' ) )
		{
			load_class('plugins/model/_plugins_admin_no_db.class.php');
			$AvailablePlugins = & new Plugins_admin_no_DB();
			$AvailablePlugins->discover();
			$AvailablePlugins->sort('priority');
		}

		$AvailablePlugins->restart();
		while( $a_Plugin = & $AvailablePlugins->get_next() )
		{
			if( in_array( $a_plugin->apply_rendering, array( 'opt-in', 'opt-out', 'stealth', 'lazy' ) )
			{ // this is possibly a renderer plugin
				echo $a_plugin->name.' - priority '.$a_plugin->priority;
			}
		}

Completely untested of course ;)

¥

Mar 15, 2008 11:00

So, where do I put that, and be nice!!

Mar 15, 2008 11:02

The code was for EdB ;)

I can send you a copy of my credits plugin if you want that?

¥

Mar 15, 2008 11:14

Nah man I'm not too interested in running a list like that, but it's a cool plugin for sure. For most people it'd be cool is what I mean. For you it's just a way to give yourself link-love :roll:

You should publish that plugin if it doesn't rely too much on the rest of whatever you've done to your core to make it happen.

Oh wait a minute: does it talk about installed plugins or just uploaded ones? I noticed after adding whatever plugin I just made to BOPIT that BOPIT told me I was missing one of my own, so I uploaded it but didn't install it. BOPIT told me I had the latest version and was good to go - green. er... uh... er... :roll:

For the purposes of this tiny little hack all I wanted to do was be able to see all the plugin priorities at a glance, and change them if need be. Knowing nothing about $AvailablePlugins, and having a direct link to PHP, I figured I could talk to the database and get some good info from it. I was obviously wrong given that the database doesn't know jack, but I have actually managed to improve my little hack a little bit. It now sorts the plugins into "HTML Renderers", "Toolbar Makers", and "Everything else".

And now I'm desperately concerned that I totally screwed up something. Something like auto-P and it's inability to play nice under almost any condition stk can put into practical use yah?

Mar 15, 2008 11:20

I can send you a copy of my credits plugin if you want that?

Yes, I would like that. As EdB said, it should be a public plugin in fairness to all plugin developers

Mar 15, 2008 11:58

EdB wrote:

For most people it'd be cool is what I mean. For you it's just a way to give yourself link-love :roll:

I need to get out more ;)

EdB wrote:

Oh wait a minute: does it talk about installed plugins or just uploaded ones?

That snippet showed all uploaded plugins. This one from my credits plugin just shows installed ones ... ish

	function get_plugin_list()
	{
		global $Plugins;
		$credits = array();
		$download_link = $this->get_htsrv_url( 'download' );
		foreach( $Plugins->index_ID_rows as $installed_plugin )
		{
			$a_plugin = & $Plugins->get_by_ID( $installed_plugin[ 'plug_ID' ] );
			if( $a_plugin->name != $this->name )
			{
				$credits[ strtolower( $a_plugin->name ) ] = '<li>'.( $a_plugin->group == 'AstonishMe' ? 
action_icon( 'download the plugin', 'download', $download_link.'&amp;am_plug='.$a_plugin->code ).' '
 : '' ).'<a href="'.$a_plugin->get_help_url().'" title=" visit the plugins help page " class="ext">'.
( empty( $a_plugin->name ) ? 'Unknown' : $a_plugin->name ).' v'.$a_plugin->version.'</a> by '.$a_plugin->author.'</li>';
			}
		}
		ksort( $credits );
		return '<ul>'.implode( '', $credits ).'</ul>';
	}


( code broken to fit forums )

John wrote:

I can send you a copy of my credits plugin if you want that?

Yes, I would like that. As EdB said, it should be a public plugin in fairness to all plugin developers

Give me chance to delete all the auto zip stuff and I'll zip it up ;)

¥


Form is loading...

Social CMS – This forum is powered by b2evolution CMS, a complete engine for your website.