PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.0
Defender Security – Malware Scanner, Login Security & Firewall v5.7.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / traits / file-operations.php

file-operations.php in Defender Security – Malware Scanner, Login Security & Firewall 5.7.0, at src/traits/file-operations.php

84 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper functions for file related tasks.
4 *
5 * @package WP_Defender\Traits
6 */
7
8 namespace WP_Defender\Traits;
9
10 use WP_Error;
11 use WP_Defender\Model\Scan;
12 use WP_Defender\Component\Error_Code;
13 use WP_Filesystem_Base;
14
15 trait File_Operations {
16
17 /**
18 * Deletes a file if it exists and is writable.
19 *
20 * @param string $file The path to the file to be deleted.
21 *
22 * @return bool Returns true if the file was successfully deleted, false otherwise.
23 */
24 public function delete_infected_file( string $file ): bool {
25 global $wp_filesystem;
26 // Initialize the WP filesystem, no more using 'file-put-contents' function.
27 if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) {
28 require_once ABSPATH . '/wp-admin/includes/file.php';
29 WP_Filesystem();
30 }
31 return file_exists( $file ) && $wp_filesystem->is_writable( $file ) && $wp_filesystem->delete( $file );
32 }
33
34 /**
35 * Handle actions after deleting a file or folder.
36 *
37 * @param string $deleted_file The file or folder that was deleted.
38 * @param Scan|null $related_scan The scan instance related to the deletion.
39 * @param string $scan_type The type of scan.
40 *
41 * @return array An array with a message confirming the deletion.
42 */
43 public function after_delete( string $deleted_file, ?Scan $related_scan, string $scan_type ): array {
44 $this->log( sprintf( '%s is deleted', $deleted_file ), \WP_Defender\Controller\Scan::SCAN_LOG );
45 $related_scan->remove_issue( $this->owner->id );
46 $related_scan->remove_related_issue_by( $deleted_file, $scan_type );
47 do_action( 'wpdef_fixed_scan_issue', $scan_type, 'delete' );
48
49 return array( 'message' => esc_html__( 'This item has been permanently removed', 'defender-security' ) );
50 }
51
52 /**
53 * Returns a WP_Error object with an error code indicating that the file is not writeable.
54 *
55 * @param string $file The path to the file that is not writeable.
56 *
57 * @return WP_Error The WP_Error object with the error code and the basename of the file.
58 */
59 public function get_permission_error( string $file ): WP_Error {
60 $this->log( sprintf( 'ISSUE: %s is not writeable', $file ), \WP_Defender\Controller\Scan::SCAN_LOG );
61 return new WP_Error( Error_Code::NOT_WRITEABLE, wp_basename( $file ) );
62 }
63
64 /**
65 * Get file meta details: created_at, size, and deleted flag.
66 *
67 * @param string $file The path to the file.
68 * @return array An array containing the file's created_at date, size, and a deleted flag.
69 */
70 private function get_file_meta( string $file ): array {
71 if ( ! file_exists( $file ) || ! is_readable( $file ) ) {
72 return array( 'n/a', 'n/a', true );
73 }
74
75 $file_created_at = filemtime( $file );
76 $file_created_at = $file_created_at ? $this->format_date_time( $file_created_at ) : 'n/a';
77
78 $file_size = filesize( $file );
79 $file_size = $file_size ? $this->format_bytes_into_readable( $file_size ) : 'n/a';
80
81 return array( $file_created_at, $file_size, false );
82 }
83 }
84