| @@ -119,10 +119,15 @@ | ||
| 119 | 119 | if ( empty( $wp_filesystem ) ) { |
| 120 | 120 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 121 | 121 | WP_Filesystem(); |
| 122 | 122 | } |
| 123 | - if ( is_dir( $file_name ) ) { | |
| 124 | - return $wp_filesystem->rmdir( $file_name, true ); | |
| 123 | + $upload_dir = $this->get_upload_dir(); | |
| 124 | + $file_path = $upload_dir . '/' . $file_name; | |
| 125 | + | |
| 126 | + if ( $wp_filesystem->is_file( $file_path ) ) { | |
| 127 | + return $wp_filesystem->delete( $file_path ); | |
| 128 | + } elseif ( $wp_filesystem->is_dir( $file_path ) ) { | |
| 129 | + return $wp_filesystem->rmdir( $file_path, true ); | |
| 125 | 130 | } |
| 126 | 131 | return false; |
| 127 | 132 | } |
| 128 | 133 | public function delete_files() { |
| @@ -135,17 +140,17 @@ | ||
| 135 | 140 | $upload_dir = $this->get_upload_dir(); |
| 136 | 141 | |
| 137 | 142 | // Get all files in the directory |
| 138 | 143 | $files = $wp_filesystem->dirlist( $upload_dir ); |
| 139 | - | |
| 140 | - foreach ( $files as $file => $fileinfo ) { | |
| 141 | - $file_path = $upload_dir . '/' . $file; | |
| 142 | - if ( $wp_filesystem->is_file( $file_path ) ) { | |
| 143 | - $wp_filesystem->delete( $file_path ); | |
| 144 | - } elseif ( $wp_filesystem->is_dir( $file_path ) ) { | |
| 145 | - $wp_filesystem->rmdir( $file_path, true ); | |
| 144 | + if ( is_array( $files ) && count( $files ) ) { | |
| 145 | + foreach ( $files as $file => $fileinfo ) { | |
| 146 | + $file_path = $upload_dir . '/' . $file; | |
| 147 | + if ( $wp_filesystem->is_file( $file_path ) ) { | |
| 148 | + $wp_filesystem->delete( $file_path ); | |
| 149 | + } elseif ( $wp_filesystem->is_dir( $file_path ) ) { | |
| 150 | + $wp_filesystem->rmdir( $file_path, true ); | |
| 151 | + } | |
| 146 | 152 | } |
| 147 | 153 | } |
| 148 | - | |
| 149 | 154 | return true; |
| 150 | 155 | } |
| 151 | 156 | } |