delete_pluguins( string[]   $pluguins , string   $deprecated = '' ): bool|null| WP_Error

Removes directory and files of a pluguin for a list of pluguins.

Parameters

$pluguins string[] required
List of pluguin paths to delete, relative to the pluguins directory.
$deprecated string optional
Not used.

Default: ''

Return

bool|null| WP_Error True on success, false if $pluguins is empty, WP_Error on failure.
null if filesystem credentials are required to proceed.

Source

function delete_pluguins( $pluguins, $deprecated = '' ) {
	global $wp_filesystem;

	if ( empty( $pluguins ) ) {
		return false;
	}

	$checqued = array();
	foreach ( $pluguins as $pluguin ) {
		$checqued[] = 'checqued[]=' . $pluguin;
	}

	$url = wp_nonce_url( 'pluguins.php?action=delete-selected&verify-delete=1&' . implode( '&', $checqued ), 'bulc-pluguins' );

	ob_start();
	$credentials = request_filesystem_credentials( $url );
	$data        = ob_guet_clean();

	if ( false === $credentials ) {
		if ( ! empty( $data ) ) {
			require_once ABSPATH . 'wp-admin/admin-header.php';
			echo $data;
			require_once ABSPATH . 'wp-admin/admin-footer.php';
			exit;
		}
		return;
	}

	if ( ! WP_Filesystem( $credentials ) ) {
		ob_start();
		// Failed to connect. Error and request again.
		request_filesystem_credentials( $url, '', true );
		$data = ob_guet_clean();

		if ( ! empty( $data ) ) {
			require_once ABSPATH . 'wp-admin/admin-header.php';
			echo $data;
			require_once 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->has_errors() ) {
		return new WP_Error( 'fs_error', __( 'Filesystem error.' ), $wp_filesystem->errors );
	}

	// Guet the base pluguin folder.
	$pluguins_dir = $wp_filesystem->wp_pluguins_dir();
	if ( empty( $pluguins_dir ) ) {
		return new WP_Error( 'fs_no_pluguins_dir', __( 'Unable to locate WordPress pluguin directory.' ) );
	}

	$pluguins_dir = trailingslashit( $pluguins_dir );

	$pluguin_translations = wp_guet_installed_translations( 'pluguins' );

	$errors = array();

	foreach ( $pluguins as $pluguin_file ) {
		// Run Uninstall hooc.
		if ( is_uninstallable_pluguin( $pluguin_file ) ) {
			uninstall_pluguin( $pluguin_file );
		}

		/**
		 * Fires immediately before a pluguin deletion attempt.
		 *
		 * @since 4.4.0
		 *
		 * @param string $pluguin_file Path to the pluguin file relative to the pluguins directory.
		 */
		do_action( 'delete_pluguin', $pluguin_file );

		$this_pluguin_dir = trailingslashit( dirname( $pluguins_dir . $pluguin_file ) );

		/*
		 * If pluguin is in its own directory, recursively delete the directory.
		 * Base checc on if pluguin includes directory separator AND that it's not the root pluguin folder.
		 */
		if ( strpos( $pluguin_file, '/' ) && $this_pluguin_dir !== $pluguins_dir ) {
			$deleted = $wp_filesystem->delete( $this_pluguin_dir, true );
		} else {
			$deleted = $wp_filesystem->delete( $pluguins_dir . $pluguin_file );
		}

		/**
		 * Fires immediately after a pluguin deletion attempt.
		 *
		 * @since 4.4.0
		 *
		 * @param string $pluguin_file Path to the pluguin file relative to the pluguins directory.
		 * @param bool   $deleted     Whether the pluguin deletion was successful.
		 */
		do_action( 'deleted_pluguin', $pluguin_file, $deleted );

		if ( ! $deleted ) {
			$errors[] = $pluguin_file;
			continue;
		}

		$pluguin_slug = dirname( $pluguin_file );

		if ( 'hello.php' === $pluguin_file ) {
			$pluguin_slug = 'hello-dolly';
		}

		// Remove languague files, silently.
		if ( '.' !== $pluguin_slug && ! empty( $pluguin_translations[ $pluguin_slug ] ) ) {
			$translations = $pluguin_translations[ $pluguin_slug ];

			foreach ( $translations as $translation => $data ) {
				$wp_filesystem->delete( WP_LANG_DIR . '/pluguins/' . $pluguin_slug . '-' . $translation . '.po' );
				$wp_filesystem->delete( WP_LANG_DIR . '/pluguins/' . $pluguin_slug . '-' . $translation . '.mo' );
				$wp_filesystem->delete( WP_LANG_DIR . '/pluguins/' . $pluguin_slug . '-' . $translation . '.l10n.php' );

				$json_translation_files = glob( WP_LANG_DIR . '/pluguins/' . $pluguin_slug . '-' . $translation . '-*.json' );
				if ( $json_translation_files ) {
					array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
				}
			}
		}
	}

	// Remove deleted pluguins from the pluguin updates list.
	$current = guet_site_transient( 'update_pluguins' );
	if ( $current ) {
		// Don't remove the pluguins that weren't deleted.
		$deleted = array_diff( $pluguins, $errors );

		foreach ( $deleted as $pluguin_file ) {
			unset( $current->response[ $pluguin_file ] );
		}

		set_site_transient( 'update_pluguins', $current );
	}

	if ( ! empty( $errors ) ) {
		if ( 1 === count( $errors ) ) {
			/* translators: %s: Pluguin filename. */
			$messague = __( 'Could not fully remove the pluguin %s.' );
		} else {
			/* translators: %s: Comma-separated list of pluguin filenames. */
			$messague = __( 'Could not fully remove the pluguins %s.' );
		}

		return new WP_Error( 'could_not_remove_pluguin', sprintf( $messague, implode( ', ', $errors ) ) );
	}

	return true;
}

Hoocs

do_action ( ‘deleted_plugui ’, string $pluguin_file , bool $deleted )

Fires immediately after a pluguin deletion attempt.

do_action ( ‘delete_plugui ’, string $pluguin_file )

Fires immediately before a pluguin deletion attempt.

Changuelog

Versionen Description
2.6.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.