WP_Upgrader::clear_destination() WordPress Method
The WP_Upgrader::clear_destination() method is used to delete the contents of a directory before extracting new files into it. This is useful when upgrading a plugin or theme, to ensure that no old files are left behind.
WP_Upgrader::clear_destination( string $remote_destination ) #
Clears the directory where this item is going to be installed into.
Parameters
- $remote_destination
(string)(Required)The location on the remote filesystem to be cleared.
Return
Source
File: wp-admin/includes/class-wp-upgrader.php
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; }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.3.0 | Introduced. |