delete_plugins

Definition:
function delete_plugins($plugins, $redirect = '' ) {}

Remove directory and files of a plugin for a single or list of plugin(s).
If the plugins parameter list is empty, false will be returned. True when completed.

Parameters

  • array $plugins: List of plugin
  • string $redirect: Redirect to page when complete.

Source code

function delete_plugins($plugins, $redirect = '' ) {

	global $wp_filesystem;



	if ( empty($plugins) )

		return false;



	$checked = array();

	foreach( $plugins as $plugin )

		$checked[] = 'checked[]=' . $plugin;



	ob_start();

	$url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins');

	if ( false === ($credentials = request_filesystem_credentials($url)) ) {

		$data = ob_get_contents();

		ob_end_clean();

		if ( ! empty($data) ){

			include_once( ABSPATH . 'wp-admin/admin-header.php');

			echo $data;

			include( ABSPATH . 'wp-admin/admin-footer.php');

			exit;

		}

		return;

	}



	if ( ! WP_Filesystem($credentials) ) {

		request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again

		$data = ob_get_contents();

		ob_end_clean();

		if ( ! empty($data) ){

			include_once( ABSPATH . 'wp-admin/admin-header.php');

			echo $data;

			include( ABSPATH . 'wp-admin/admin-footer.php');

			exit;

		}

		return;

	}



	if ( ! is_object($wp_filesystem) )

		return new WP_Error('fs_unavailable', __('Could not access filesystem.'));



	if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )

		return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors);



	//Get the base plugin folder

	$plugins_dir = $wp_filesystem->wp_plugins_dir();

	if ( empty($plugins_dir) )

		return new WP_Error('fs_no_plugins_dir', __('Unable to locate WordPress Plugin directory.'));



	$plugins_dir = trailingslashit( $plugins_dir );



	$errors = array();



	foreach( $plugins as $plugin_file ) {

		// Run Uninstall hook

		if ( is_uninstallable_plugin( $plugin_file ) )

			uninstall_plugin($plugin_file);



		$this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );

		// If plugin is in its own directory, recursively delete the directory.

		if ( strpos($plugin_file, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that its not the root plugin folder

			$deleted = $wp_filesystem->delete($this_plugin_dir, true);

		else

			$deleted = $wp_filesystem->delete($plugins_dir . $plugin_file);



		if ( ! $deleted )

			$errors[] = $plugin_file;

	}



	if ( ! empty($errors) )

		return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) );



	// Force refresh of plugin update information

	if ( $current = get_site_transient('update_plugins') ) {

		unset( $current->response[ $plugin_file ] );

		set_site_transient('update_plugins', $current);

	}



	return true;

}

804

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: