2016-08-15, 01:09 PM
I guys,
In my last plugins I added the ability to delete the plugin from the server. It removes all the files and folders inserted by the plugin. I think that it's a good thing to add to MyBB and that developers should start to use it.
This is the code:
In my last plugins I added the ability to delete the plugin from the server. It removes all the files and folders inserted by the plugin. I think that it's a good thing to add to MyBB and that developers should start to use it.
This is the code:
/**
*
* Plugin info
*
* @return array Info
*/
function MyWhiteboard_info()
{
global $mybb;
$destroy = <<<EOT
<p>
<a style="color: red; font-weight: bold" href="index.php?module=config-plugins&action=deactivate&uninstall=1&destroy=1&plugin=MyWhiteboard&my_post_key={$mybb->post_code}">
Delete Plugin
</a>
</p>
EOT;
return [
'name' => "Plugin Title",
'description' => "Plugin Desc".$destroy,
'website' => "website_url",
'author' => 'chack1172',
'authorsite' => "author_url",
'version' => 'version',
'compatibility' => '18*',
'codename' => 'MyWhiteboard'
];
}
/**
*
* Delete the plugin
*
*/
function MyWhiteboard_destroy()
{
global $mybb, $message;
if($mybb->input['destroy'] == 1)
{
// extra files and dirs to remove
$extra_files = [
"inc/languages/english/admin/MyWhiteboard.lang.php",
"inc/languages/english/MyWhiteboard.lang.php",
"inc/languages/italiano/admin/MyWhiteboard.lang.php",
"inc/languages/italiano/MyWhiteboard.lang.php",
"jscripts/spectrum/",
"jscripts/excanvas.min.js",
"jscripts/mywhiteboard.min.js",
"jscripts/mywhiteboard.js",
];
if(!empty($extra_files))
{
// remove extra files and dirs
foreach($extra_files as $file)
{
if(!file_exists(MYBB_ROOT.$file))
{
continue;
}
if(is_dir(MYBB_ROOT.$file))
{
MyWhiteboard_rmdir(MYBB_ROOT.$file);
} else {
unlink(MYBB_ROOT.$file);
}
}
}
// remove plugin file
unlink(__FILE__);
$message = "The selected plugin has been uninstalled and deleted successfully.";
}
}
function MyWhiteboard_rmdir($dir)
{
if(file_exists($dir) && is_dir($dir))
{
$files = array_diff(scandir($dir), ['.','..']);
foreach($files as $file)
{
(is_dir($dir."/".$file)) ? MyWhiteboard_rmdir($dir."/".$file) : unlink($dir."/".$file);
}
return rmdir($dir);
} else {
return true;
}
}