| @@ -7,8 +7,9 @@ | ||
| 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 | 14 | |
| 14 | 15 | trait File_Operations { |
| @@ -15,28 +16,40 @@ | ||
| 15 | 16 | |
| 16 | 17 | /** |
| 17 | 18 | * Deletes a file if it exists and is writable. |
| 18 | 19 | * |
| 19 | - * @param string $file The path to the file to be deleted. | |
| 20 | + * @param string $file The path to the file to be deleted. | |
| 20 | 21 | * |
| 21 | 22 | * @return bool Returns true if the file was successfully deleted, false otherwise. |
| 22 | 23 | */ |
| 23 | 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 { | |
| 24 | 36 | global $wp_filesystem; |
| 25 | - // Initialize the WP filesystem, no more using 'file-put-contents' function. | |
| 26 | - if ( empty( $wp_filesystem ) ) { | |
| 37 | + | |
| 38 | + if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) { | |
| 27 | 39 | require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 28 | 40 | WP_Filesystem(); |
| 29 | 41 | } |
| 30 | - return file_exists( $file ) && $wp_filesystem->is_writable( $file ) && $wp_filesystem->delete( $file ); | |
| 42 | + | |
| 43 | + return $wp_filesystem; | |
| 31 | 44 | } |
| 32 | 45 | |
| 33 | 46 | /** |
| 34 | 47 | * Handle actions after deleting a file or folder. |
| 35 | 48 | * |
| 36 | - * @param string $deleted_file The file or folder that was deleted. | |
| 37 | - * @param Scan|null $related_scan The scan instance related to the deletion. | |
| 38 | - * @param string $scan_type The type of scan. | |
| 49 | + * @param string $deleted_file The file or folder that was deleted. | |
| 50 | + * @param Scan|null $related_scan The scan instance related to the deletion. | |
| 51 | + * @param string $scan_type The type of scan. | |
| 39 | 52 | * |
| 40 | 53 | * @return array An array with a message confirming the deletion. |
| 41 | 54 | */ |
| 42 | 55 | public function after_delete( string $deleted_file, ?Scan $related_scan, string $scan_type ): array { |
| @@ -50,13 +63,15 @@ | ||
| 50 | 63 | |
| 51 | 64 | /** |
| 52 | 65 | * Returns a WP_Error object with an error code indicating that the file is not writeable. |
| 53 | 66 | * |
| 54 | - * @param string $file The path to the file that is not writeable. | |
| 67 | + * @param string $file The path to the file that is not writeable. | |
| 55 | 68 | * |
| 56 | 69 | * @return WP_Error The WP_Error object with the error code and the basename of the file. |
| 57 | 70 | */ |
| 58 | 71 | public function get_permission_error( string $file ): WP_Error { |
| 72 | + $this->log( sprintf( 'ISSUE: %s is not writeable', $file ), \WP_Defender\Controller\Scan::SCAN_LOG ); | |
| 73 | + | |
| 59 | 74 | return new WP_Error( Error_Code::NOT_WRITEABLE, wp_basename( $file ) ); |
| 60 | 75 | } |
| 61 | 76 | |
| 62 | 77 | /** |
| @@ -62,20 +77,21 @@ | ||
| 62 | 77 | /** |
| 63 | 78 | * Get file meta details: created_at, size, and deleted flag. |
| 64 | 79 | * |
| 65 | 80 | * @param string $file The path to the file. |
| 81 | + * | |
| 66 | 82 | * @return array An array containing the file's created_at date, size, and a deleted flag. |
| 67 | 83 | */ |
| 68 | 84 | private function get_file_meta( string $file ): array { |
| 69 | 85 | if ( ! file_exists( $file ) || ! is_readable( $file ) ) { |
| 70 | - return array( 'n/a', 'n/a', true ); | |
| 86 | + return array( null, null, true ); | |
| 71 | 87 | } |
| 72 | 88 | |
| 73 | 89 | $file_created_at = filemtime( $file ); |
| 74 | - $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; | |
| 75 | 91 | |
| 76 | 92 | $file_size = filesize( $file ); |
| 77 | - $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; | |
| 78 | 94 | |
| 79 | 95 | return array( $file_created_at, $file_size, false ); |
| 80 | 96 | } |
| 81 | 97 | } |