MyBB Community Forums

Full Version: Help, code is doubling!?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
So I found a issue with my plugin, when add something in the text area it will make a box in the footer/header displaying what was inserted into the text area(like it should). Now when you change what you put in that text area, instead of just replacing what was recently in the box that was made, it makes a whole new one instead of replacing the current text in the box!? here is my code.

<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
    die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
$plugins->add_hook('global_start','adsense_code_insert');
$plugins->add_hook('index_start','adsense_code_insert_footer');

function adsense_info()
{
global $db;

    return array(
        'name'            => 'AdSense Code',
        'description'    => 'Add your adsense code (or any ad!) to your index.',
        'website'        => 'http://mods.mybb.com/',
        'author'        => 'Blake',
        'authorsite'    => 'http://mcmines.com',
        'version'        => '1.1',
        'compatibility'    => '16*',
        'guid'           => 'dc9443796a5dfaeb9d75fd5d0bf08be2'
    );
}

function adsense_activate()
{

global $db;
require MYBB_ROOT."/inc/adminfunctions_templates.php";
      $confirm_group = array(
        'gid'            => 'NULL',
        'name'            => 'google_adsense',
        'title'            => 'Ad Code',
        'description'    => 'Edit your Ad details in here.',
        'disporder'        => "1",
        'isdefault'        => '0',
    );
    $db->insert_query('settinggroups', $confirm_group);
    $gid = $db->insert_id();
    
$adsense_setting_1 = array(
    "sid"            => "NULL",
    "name"            => "adsense_on_off",
    "title"            => "Is the plugin active for the header?",
    "description"    => "No means off",
    "optionscode"    => "yesno",
    "value"            => "no",
    "disporder"        => "1",
    "gid"            => intval($gid),
    );
    $adsense_setting_2 = array(
    "sid"            => "NULL",
    "name"            => "adsense_code_header",
    "title"            => "Ad Code",
    "description"    => "Post your Ad Code in here",
    "optionscode"    => "textarea",
    "value"            => '',
    "disporder"        => "2",
    "gid"            => intval($gid),
    );
    $adsense_setting_3 = array(
    "sid"            => "NULL",
    "name"            => "adsense_on_off_footer",
    "title"            => "Is the plugin active for the footer?",
    "description"    => "No means off",
    "optionscode"    => "yesno",
    "value"            => "no",
    "disporder"        => "3",
    "gid"            => intval($gid),
    );
    $adsense_setting_4 = array(
    "sid"            => "NULL",
    "name"            => "adsense_code_footer",
    "title"            => "Ad Code",
    "description"    => "Post your Ad Code in here",
    "optionscode"    => "textarea",
    "value"            => '',
    "disporder"        => "4",
    "gid"            => intval($gid),
    );
    
    $db->insert_query('settings', $adsense_setting_1);
    $db->insert_query('settings', $adsense_setting_2);
    $db->insert_query('settings', $adsense_setting_3);
    $db->insert_query('settings', $adsense_setting_4);
}

function adsense_deactivate()
{
global $db;
    require MYBB_ROOT."/inc/adminfunctions_templates.php";
    $db->delete_query("settings", "name='adsense_on_off'");
    $db->delete_query("settings", "name='adsense_on_off_footer'");
    $db->delete_query("settings", "name='adsense_code_footer'");
    $db->delete_query("settings", "name='adsense_code_header'");
    $db->delete_query("settinggroups", "name='google_adsense'");
    rebuild_settings();
}

//Header insert
function adsense_code_insert(){
global $mybb, $templates, $theme, $db;
require MYBB_ROOT."/inc/adminfunctions_templates.php";
if(intval($mybb->settings['adsense_on_off']) == "1"){
find_replace_templatesets("header", "#".preg_quote("<navigation>")."#i", "<navigation> <div style=\"padding: 6px 0px 0px 0px\">
<table class=\"tborder\" cellpadding=\"6\" cellspacing=\"1\" border=\"0\" width=\"100%\" align=\"center\"> 
  
<tr>
<td class=\"thead\">
Sponsored Links
</td>
</tr>
<tr>
<td class=\"alt1\" align=\"center\">
{$mybb->settings['adsense_code_header']}
</td> 
</tr>
</table> 
<br>
</div> "
);

} 
} 
//Footer insert
function adsense_code_insert_footer(){
global $mybb, $templates, $theme, $db;
require MYBB_ROOT."/inc/adminfunctions_templates.php";
if(intval($mybb->settings['adsense_on_off_footer']) == "1"){
find_replace_templatesets("index", "#".preg_quote("</dl>")."#i", "</dl> <br /> <div style=\"padding: 6px 0px 0px 0px\">
<table class=\"tborder\" cellpadding=\"6\" cellspacing=\"1\" border=\"0\" width=\"100%\" align=\"center\"> 
  
<tr>
<td class=\"thead\">
Sponsored Links
</td>
</tr>
<tr>
<td class=\"alt1\" align=\"center\">
{$mybb->settings['adsense_code_footer']}
</td> 
</tr>
</table> 
<br>
</div> "
);

}
} 
//Header remove
global $mybb, $templates, $theme, $db;
require MYBB_ROOT."/inc/adminfunctions_templates.php";
if(intval($mybb->settings['adsense_on_off']) == "0"){
find_replace_templatesets("header", "#".preg_quote("<div style=\"padding: 6px 0px 0px 0px\">
<table class=\"tborder\" cellpadding=\"6\" cellspacing=\"1\" border=\"0\" width=\"100%\" align=\"center\"> 
  
<tr>
<td class=\"thead\">
Sponsored Links
</td>
</tr>
<tr>
<td class=\"alt1\" align=\"center\">
{$mybb->settings['adsense_code_header']}
</td> 
</tr>
</table> 
<br>
</div> ")."#i", '', 0);
}


//Footer remove
global $mybb, $templates, $theme, $db;
require MYBB_ROOT."/inc/adminfunctions_templates.php";
if(intval($mybb->settings['adsense_on_off_footer']) == "0"){
find_replace_templatesets("index", "#".preg_quote("<br /><div style=\"padding: 6px 0px 0px 0px\">
<table class=\"tborder\" cellpadding=\"6\" cellspacing=\"1\" border=\"0\" width=\"100%\" align=\"center\"> 
  
<tr>
<td class=\"thead\">
Sponsored Links
</td>
</tr>
<tr>
<td class=\"alt1\" align=\"center\">
{$mybb->settings['adsense_code_footer']}
</td> 
</tr>
</table> 
<br>
</div> ")."#i", '', 0);
}


?>
Bump. I've added a text area for each zone to prevent the mod from getting confused, any help, the footer is still giving me the double issue and they both wont replace there text.
Bump...
Well, first of all, your functions to remove the header and footer stuff aren't actually functions. Personally, I'd create a new global template in the activate function and add {$adsense} to the header and footer templates. Then, within the two hooks, I'd run the if and if true, fetch the template.

This would be much easier to maintain. Otherwise, you have to add an else condition to your ifs and add another find_replace_templatesets() to each else statement to remove the code you've hard added to the templates.


Hope that makes sense. I can give you a pseudocode example later if needed.
Example please xD I'm still new to coding so....
Ok, I'll write up a quick one tomorrow for you Smile It's too late to do it now.
Bump. Smile
Don't use find_replace_templatesets() in normal hooks. This would update your templates every time that hook is called. As you do it in the global_start hook, it would be at every page refresh. find_replace_tempaltesets() should only be used in the _activate() and _deactivate() function and use a variable like euantor suggested.

$plugins->add_hook("index_start", "myplugin_index");

function myplugin_activate()
{
	require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("index", "#".preg_quote("</dl>")."#i", "{$adsense_code}");
}

function myplugin_deactivate()
{
	require_once MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets("index", "#".preg_quote("{\$adsense_code}")."#i", "", 0);
}

function myplugin_index()
{
	global $mybb, $adsense_code;
	
	if($mybb->settings['adsense_onoff'] == 1)
	{
		$adsense_code = "here your code";
	}
}
Sorry, I'd completely forgotten about this thread. Lucky that Aries-Belgium beat me to it ;D
So it would look like this?
function myplugin_index()
{
    global $mybb, $adsense_code;
    
    if($mybb->settings['adsense_onoff'] == 1)
    {
        $adsense_code = "<div style=\"padding: 6px 0px 0px 0px\">
<table class=\"tborder\" cellpadding=\"6\" cellspacing=\"1\" border=\"0\" width=\"100%\" align=\"center\"> 
  
<tr>
<td class=\"thead\">
Sponsored Links
</td>
</tr>
<tr>
<td class=\"alt1\" align=\"center\">
{$mybb->settings['adsense_code_header']}
</td> 
</tr>
</table> 
<br>
</div>";
    }
} 
Then once thats done all I have to do is add $adsense_code each time instead of adding the whole new template code?
Pages: 1 2