WP_Upgrader::clear_destination( string $remote_destination ): true|WP_Error

In this article

Clears the directory where this item is going to be installed into.

Parameters

$remote_destinationstringrequired
The location on the remote filesystem to be cleared.

Return

true|WP_Error True upon success, WP_Error on failure.

Source

public function clear_destination( $remote_destination ) {
	global $wp_filesystem;

	$files = $wp_filesystem->dirlist( $remote_destination, true, true );

	// False indicates that the $remote_destination doesn't exist.
	if ( false === $files ) {
		return true;
	}

	// Flatten the file list to iterate over.
	$files = $this->flatten_dirlist( $files );

	// Check all files are writable before attempting to clear the destination.
	$unwritable_files = array();

	// Check writability.
	foreach ( $files as $filename => $file_details ) {
		if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
			// Attempt to alter permissions to allow writes and try again.
			$wp_filesystem->chmod( $remote_destination . $filename, ( 'd' === $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
			if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
				$unwritable_files[] = $filename;
			}
		}
	}

	if ( ! empty( $unwritable_files ) ) {
		return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
	}

	if ( ! $wp_filesystem->delete( $remote_destination, true ) ) {
		return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
	}

	return true;
}

Changelog

VersionDescription
4.3.0Introduced.

User Contributed Notes

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