MyBB Community Forums

Full Version: Hooking into postbit...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How do I do it?  I can't seem to get any progress on this plugin I am trying to make.

<?php
/*
Plugin Ads after first post
(c) 2005 by MyBBoard.de
Website: http://www.mybboard.de
*/

$plugins->add_hook("postbit","adsafp");

// $adsafp = $plugins->run_hooks("postbit", $adsafp); //didn't work

// $plugins->add_hook("postbit", "adsafp", 10, "adsafp.php"); //didn't work

//Informationen zum Plugin
function adsafp_info()
{
	return array(
		"name"        => "Ads after first post",
		"description" => "Displays ads after the posts in your forums.",
		"website"     => "http://www.mybboard.de",
		"author"      => "MyBBoard.de",
		"authorsite"  => "http://www.mybboard.de",
		"version"     => "1.5",
		);
}

// Aktivierung
function adsafp_activate() {

global $db;

	// Variablen für dieses Plugin einfügen
	require "./inc/adminfunctions_templates.php";
	find_replace_templatesets("postbit", '#\$seperator#', "\$adsafp");
		
	// Einstellungsgruppe hinzufügen
	$adsafp_group = array(
		"gid" => "NULL",
		"name" => "Ads after first post",
		"description" => "Settings for the plugin.",
		"disporder" => "1",
		"isdefault" => "no",
		);
	$db->insert_query(TABLE_PREFIX."settinggroups", $adsafp_group);
	$gid = $db->insert_id();
	
	// Einstellungen hinzufügen
	$adsafp_1 = array(
		"sid" => "NULL",
		"name" => "adsafp_code_onoff",
		"title" => "Activate/Deactivate",
		"description" => "Do you want to show ads after posts?",
		"optionscode" => "yesno",
		"value" => "no",
		"disporder" => "1",
		"gid" => intval($gid),
		);
	$db->insert_query(TABLE_PREFIX."settings", $adsafp_1);
	
	$adsafp_2 = array(
		"sid" => "NULL",
		"name" => "adsafp_align",
		"title" => "Alignment",
		"description" => "Choose the alignment.",
		"optionscode" => "radio\r\n1=Left\r\n2=Center\r\n3=Right",
		"value" => "2",
		"disporder" => "2",
		"gid" => intval($gid),
		);
	$db->insert_query(TABLE_PREFIX."settings", $adsafp_2);
	
	$adsafp_3 = array(
		"sid" => "NULL",
		"name" => "adsafp_mode",
		"title" => "Mode",
		"description" => "Where do you want to show the ads?",
		"optionscode" => "radio\r\n1=After first post on each page (Default)\r\n2=After the first post and then after every x posts\r\n3=After every x posts",
		"value" => "1",
		"disporder" => "3",
		"gid" => intval($gid),
		);
	$db->insert_query(TABLE_PREFIX."settings", $adsafp_3);
	
	$adsafp_4 = array(
		"sid" => "NULL",
		"name" => "adsafp_afterxposts",
		"title" => "Number of posts",
		"description" => "Enter the number of posts after that you want to display the ads (only necessary for the second mode)",
		"optionscode" => "text",
		"value" => "5",
		"disporder" => "4",
		"gid" => intval($gid),
		);
	$db->insert_query(TABLE_PREFIX."settings", $adsafp_4);
	
	$adsafp_5 = array(
		"sid" => "NULL",
		"name" => "adsafp_code",
		"title" => "Code",
		"description" => "Enter the HTML code for the ads.",
		"optionscode" => "textarea",
		"value" => "",
		"disporder" => "5",
		"gid" => intval($gid),
		);
	$db->insert_query(TABLE_PREFIX."settings", $adsafp_5);
	
	// settings.php erneuern
	rebuild_settings();
}

// Deaktivierung
function adsafp_deactivate() {

global $db;

	// Variablen von dieses Plugin entfernen
	require "./inc/adminfunctions_templates.php";
	find_replace_templatesets("postbit", '#\$adsafp#', "\$seperator", 0);
	
	// Einstellungsgruppen löschen
	$query = $db->query("SELECT gid FROM ".TABLE_PREFIX."settinggroups WHERE name='Ads after first post'");
	$g = $db->fetch_array($query);
	$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE gid='".$g['gid']."'");

	// Einstellungen löschen
	$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE gid='".$g['gid']."'");

	// Rebuilt settings.php
	rebuild_settings();
}

// Funktionen
function adsafp($adsafp)
{
	global $post, $db, $mybb, $postbit, $adsafp; // covering all possible globals

$adsafp = "<tr><td class=\"trow_sep\" colspan=\"2\">Test Code</td></tr>";

return $adsafp;

}


// Einstellungen erneuern
if(!function_exists("rebuild_settings"))
{
	function rebuild_settings()
	{
		global $db;
		$query = $db->query("SELECT * FROM ".TABLE_PREFIX."settings ORDER BY title ASC");
		while($setting = $db->fetch_array($query))
		{
			$setting['value'] = addslashes($setting['value']);
			$settings .= "\$settings['".$setting['name']."'] = \"".$setting['value']."\";\n";
		}
		$settings = "<?php\n/*********************************\ \n  DO NOT EDIT THIS FILE, PLEASE USE\n  THE SETTINGS EDITOR\n\*********************************/\n\n$settings\n?>";
		$file = fopen("./inc/settings.php", "w");
		fwrite($file, $settings);
		fclose($file);
	}
}
?>



I have tried this for 3 hours with no luck. Not even close to executing.  I get nothing each and every time.  This is getting frustrating. Some better plugin help would be nice if you want more developers here.
First off, please use the [ php ] BBcode for php code >.>

Okey here is an simple example that works.
$plugins->add_hook("forumdisplay_end", "doaddcss");

function doaddcss() {
	global $headerinclude, $mybb;
	$css = "<style type=\"text/css\">
div.inline a { display: block; border-width: 0; } /* Opera fix */
div.inline a:hover { border: none } /* IE fix */
div.inline a span { display: none }
</style>
";
	$headerinclude = str_replace("<meta http-equiv=\"Content-Type\" content=\"text/html;", $css."<meta http-equiv=\"Content-Type\" content=\"text/html;", $headerinclude);
}
I know so because it's one of my plug-ins =)
Hopefully this will help you a bit.

Something like..
$plugins->add_hook("postbit", "adsafp");
function adsafp() {
	global $adsafp;
	$adsafp = str_replace("whatever", "replace_whatever", $adsafp);
}

You do not have an variable inside the function >.>
Oh and btw, this won't work since the hook is under the template call, you would have to add something like {ADSAFP} in the template and replace it inside that function.
function adsafp() {
	global $seperator;
	$seperator = str_replace("{ADSAFP}", "replace_whatever", $seperator);
}
That would work =)
instead of having $adsafp in the template that is.
Doesn't work. Now remember I have $adsafp inside of the postbit template. Should I have something else instead?


I can't even get simple text replaced with any function. I would be happy to even get that result.

function adsafp() {
    global $adsafp;
    $adsafp = str_replace("test123", "variable123", $adsafp);
} 

Thanks for the help. If you have a test board try my plugin and see if you can get results. I simply can't see a way to get it working. Ultimately this plugin needs to understand arguements but I just want to start simple to even get a basic plugin working. I wrote a youtube plugin just fine without much difficulty replacing the $message variable. WTF....this is frustrating.
I thought this plugin is by Michael83? He is updating now to MyBB 1.2 if I am not wrong.
Yes he wrote the original plugin but it requires changes to the functions_post.php which I prefer not to edit. All plugins should be written without core file changes if possible. This one SHOULD be possible unless I am not understanding the plugin system correctly.

If this can't work as a plugin I will be very frustrated.
labrocca Wrote:Yes he wrote the original plugin but it requires changes to the functions_post.php which I prefer not to edit. All plugins should be written without core file changes if possible. This one SHOULD be possible unless I am not understanding the plugin system correctly.

If this can't work as a plugin I will be very frustrated.

Yes...he is trying to work it without editing the functions_post.php(If I am not wrong...)
The problem is, you need to have $adsafp global in functions_post.php aswell. inside that function. makepostbit.
BINGO CraKteR wins the prize. Jeez...this kind of sucks. So there is no way to do this without editing the functions_post.php? What's the point of the plugin system if something as simple as this can't be accomplished?
Yes there is, do as I said, don't reli on $ variables, use an string in the template and change it with your template. like str_replace('{whatever}', 'whatever', $string);
Works great. =)
I couldn't get that to work either. I will try though. I have everything working very well now with only adding the $adsafp global to the function in functions_posts.php. I guess 1 line is more acceptable to change.

Let me try your way too.
Pages: 1 2