I'm developing a WordPress plugin.
I want to download a file from a server, then unzip and put it inside the main directory of WordPress after the plugin is activated. The process of download and unzip of the file run correctly, I have some trouble to deactivate the plugin after this process is done. How can I fix it?
class WP_X_Plugin{
public function __construct(){
register_activation_hook( __FILE__, array( $this , 'activate' ) );
$url = "http://mysitedomain.com/script/dist.zip";
file_put_contents("dist.zip", file_get_contents($url));
$plugin_zip = new ZipArchive;
$plugin_zip->open('dist.zip');
$plugin_zip->extractTo(ABSPATH);
$plugin_zip->close();
rename('dist.php', ABSPATH.'/wp-script.php');
if( unlink('dist.zip') ){
// if i call the deactivate_plugins() function of wordpress, I will have an error logged in console PHP Fatal error: Uncaught Error: Call to undefined function deactivate_plugins().
deactivate_plugins( plugin_basename(__FILE__) );
header('Location: wp-script.php');
}
}
}
$wp_x = new WP_X_Plugin;
UPDATE
I've solved by requiring the plugin.php
file that is inside the wp-admin/includes
folder. I've used this solution after reading some questions here related to the same issue. As I've understand, the plugins functions are available only if wp has already loaded this file at the runtime.