Thread Rating:
  • 4 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Easy way to remove settings
#1
I'm currently rewriting my Advanced Forum Signatures plugin like I said I would ages ago (I got a job and haven't had much free time) and while doing so realized I could have done much better when deleting my plugin's settings when deactivating. Before it looked like this:

Code:
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_enabled'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_cache'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_default_userid'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_custom_title'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_gradient'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_gradient_start'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_gradient_end'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_custom_image'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_default_font'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_default_fontsize'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_default_fontcolor'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_show_url'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_short_copyright'");
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name`='afs_allow_url_params'");
$db->query("DELETE FROM `".TABLE_PREFIX."settinggroups` WHERE `name`='afsignatures'");

Horrifying, right? Using SQL's LIKE and wildcards, I'm doing this in the new version:

Code:
$db->query("DELETE FROM `".TABLE_PREFIX."settings` WHERE `name` LIKE 'afs\_%'");
$db->query("DELETE FROM `".TABLE_PREFIX."settinggroups` WHERE `name`='afsignatures'");

Niiiiice.

So next time you update your plugin/write a new one, remember this simple trick (if you're not already doing it).
[Image: 422.png]
Reply
#2
That's pretty neat; thanks for the share. Big Grin
Reply
#3
Thanks for the share mate.

Really helpfull Smile
Reply
#4
I prefer this way:

PHP Code:
function sample_info()
{
    return array
    (
        
'name' => 'Sample Plugin',
        
'description' => 'This is just an example.',
        
'author' => 'Sebastian Wunderlich',
        
'version' => '1.0',
        
'compatibility' => '*',
        
'codename' => 'sample'
    
);


PHP Code:
function sample_activate()
{
    global 
$db;
    
$info sample_info();
    
$setting_group_array = array
    (
        
'name' => $info['codename'],
        
'title' => $info['name'],
        
'description'=>'Here you can edit ' $info['name'] . ' settings.',
        
'disporder' => 1,
        
'isdefault' => 0
    
);
    
$db->insert_query('settinggroups'$setting_group_array);
    
$group=$db->insert_id();
    
$settings = array
    (
        
'sample_setting_1' => array
        (
            
'Sample Setting 1',
            
'Description text 1',
            
'text',
            
'sample text'
        
),
        
'sample_setting_2' => array
        (
            
'Sample Setting 2',
            
'Description text 2',
            
'yesno',
            
0
        
),
        
'sample_setting_3' => array
        (
            
'Sample Setting 3',
            
'Description text 3',
            
'onoff',
            
0
        
)
    );
    
$i 1;
    foreach(
$settings as $name => $sinfo)
    {
        
$insert_array = array
        (
            
'name' => $name,
            
'title' => $db->escape_string($sinfo[0]),
            
'description' => $db->escape_string($sinfo[1]),
            
'optionscode' => $db->escape_string($sinfo[2]),
            
'value' => $db->escape_string($sinfo[3]),
            
'gid' => $group,
            
'disporder' => $i,
            
'isdefault' => 0
        
);
        
$db->insert_query('settings'$insert_array);
        
$i++;
    }
    
rebuild_settings();


PHP Code:
function sample_deactivate()
{
    global 
$db;
    
$info sample_info();
    
$result $db->simple_select('settinggroups''gid''name="' $info['codename'] . '"', array('limit' => 1));
    
$group $db->fetch_array($result);
    if(!empty(
$group['gid']))
    {
        
$db->delete_query('settinggroups''gid="' $group['gid'] . '"');
        
$db->delete_query('settings''gid="' $group['gid'] . '"');
        
rebuild_settings();
    }


You can use the same functions on every plugins. You just need to replace:
PHP Code:
$info sample_info(); 
with the info function of the plugin that's all!
Reply
#5
Wow, I've been learning to make plugins a complete different way from you. Toungue
Reply
#6
Very handy, querschlaeger! I also used an array for the settings, but I think I'll stick to SQL for deactivation.
[Image: 422.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)