| @@ -7,11 +7,11 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace WP_Defender\Traits; |
| 9 | 9 | |
| 10 | 10 | use WP_Error; |
| 11 | +use WP_Filesystem_Base; | |
| 11 | 12 | use WP_Defender\Model\Scan; |
| 12 | 13 | use WP_Defender\Component\Error_Code; |
| 13 | -use WP_Filesystem_Base; | |
| 14 | 14 | |
| 15 | 15 | trait File_Operations { |
| 16 | 16 | |
| 17 | 17 | /** |
| @@ -21,15 +21,27 @@ | ||
| 21 | 21 | * |
| 22 | 22 | * @return bool Returns true if the file was successfully deleted, false otherwise. |
| 23 | 23 | */ |
| 24 | 24 | public function delete_infected_file( string $file ): bool { |
| 25 | + $wp_filesystem = $this->get_wp_filesystem(); | |
| 26 | + | |
| 27 | + return file_exists( $file ) && $wp_filesystem->is_writable( $file ) && $wp_filesystem->delete( $file ); | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Get the WordPress filesystem instance. | |
| 32 | + * | |
| 33 | + * @return WP_Filesystem_Base The WordPress filesystem instance. | |
| 34 | + */ | |
| 35 | + public function get_wp_filesystem(): WP_Filesystem_Base { | |
| 25 | 36 | global $wp_filesystem; |
| 26 | - // Initialize the WP filesystem, no more using 'file-put-contents' function. | |
| 37 | + | |
| 27 | 38 | if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) { |
| 28 | 39 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 29 | 40 | WP_Filesystem(); |
| 30 | 41 | } |
| 31 | - return file_exists( $file ) && $wp_filesystem->is_writable( $file ) && $wp_filesystem->delete( $file ); | |
| 42 | + | |
| 43 | + return $wp_filesystem; | |
| 32 | 44 | } |
| 33 | 45 | |
| 34 | 46 | /** |
| 35 | 47 | * Handle actions after deleting a file or folder. |
| @@ -57,8 +69,9 @@ | ||
| 57 | 69 | * @return WP_Error The WP_Error object with the error code and the basename of the file. |
| 58 | 70 | */ |
| 59 | 71 | public function get_permission_error( string $file ): WP_Error { |
| 60 | 72 | $this->log( sprintf( 'ISSUE: %s is not writeable', $file ), \WP_Defender\Controller\Scan::SCAN_LOG ); |
| 73 | + | |
| 61 | 74 | return new WP_Error( Error_Code::NOT_WRITEABLE, wp_basename( $file ) ); |
| 62 | 75 | } |
| 63 | 76 | |
| 64 | 77 | /** |
| @@ -64,20 +77,21 @@ | ||
| 64 | 77 | /** |
| 65 | 78 | * Get file meta details: created_at, size, and deleted flag. |
| 66 | 79 | * |
| 67 | 80 | * @param string $file The path to the file. |
| 81 | + * | |
| 68 | 82 | * @return array An array containing the file's created_at date, size, and a deleted flag. |
| 69 | 83 | */ |
| 70 | 84 | private function get_file_meta( string $file ): array { |
| 71 | 85 | if ( ! file_exists( $file ) || ! is_readable( $file ) ) { |
| 72 | - return array( 'n/a', 'n/a', true ); | |
| 86 | + return array( null, null, true ); | |
| 73 | 87 | } |
| 74 | 88 | |
| 75 | 89 | $file_created_at = filemtime( $file ); |
| 76 | - $file_created_at = $file_created_at ? $this->format_date_time( $file_created_at ) : 'n/a'; | |
| 90 | + $file_created_at = $file_created_at ? wp_date( 'M j, Y', $file_created_at ) : null; | |
| 77 | 91 | |
| 78 | 92 | $file_size = filesize( $file ); |
| 79 | - $file_size = $file_size ? $this->format_bytes_into_readable( $file_size ) : 'n/a'; | |
| 93 | + $file_size = $file_size ? $this->format_bytes_into_readable( $file_size ) : null; | |
| 80 | 94 | |
| 81 | 95 | return array( $file_created_at, $file_size, false ); |
| 82 | 96 | } |
| 83 | 97 | } |