Skip to content

Instantly share code, notes, and snippets.

@afragen
Last active December 9, 2021 03:47
Show Gist options
  • Save afragen/8ae6760330234f78ac3d1aaa168dff7d to your computer and use it in GitHub Desktop.
Save afragen/8ae6760330234f78ac3d1aaa168dff7d to your computer and use it in GitHub Desktop.
public function delete_temp_backup( $args ) {
global $wp_filesystem;
if ( empty( $args['slug'] ) || empty( $args['dir'] ) ) {
return false;
}
$temp_backup = trailingslashit( $wp_filesystem->wp_content_dir() . "upgrade/temp-backup/{$args['dir']}/{$args['slug']}" );
return $this->recursive_directory_delete( $temp_backup );
}
public function recursive_directory_delete( $source ) {
$source = untrailingslashit( $source );
if ( $dir = opendir( $source ) ) {
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( false !== ( $file = readdir( $dir ) ) ) {
if ( ( '.' !== $file ) && ( '..' !== $file ) ) {
if ( is_dir( "{$source}/{$file}" ) ) {
error_log( "recursive delete of {$source}/{$file}" );
$this->recursive_directory_delete( "{$source}/{$file}" );
} else {
error_log( "unlink file: {$source}/{$file}");
unlink( "{$source}/{$file}" );
}
}
}
$iterator = new \FilesystemIterator( $source );
if ( ! $iterator->valid() ) { // True if directory is empty.
error_log( "remove directory: {$source}" );
rmdir( $source );
}
error_log( "close directory: {$dir}" );
closedir( $dir );
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment