PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.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 6.3.0, at src/traits/file-operations.php

98 lines 3.0 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_Filesystem_Base;
12 use WP_Defender\Model\Scan;
13 use WP_Defender\Component\Error_Code;
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 $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 {
36 global $wp_filesystem;
37
38 if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) {
39 require_once ABSPATH . '/wp-admin/includes/file.php';
40 WP_Filesystem();
41 }
42
43 return $wp_filesystem;
44 }
45
46 /**
47 * Handle actions after deleting a file or folder.
48 *
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.
52 *
53 * @return array An array with a message confirming the deletion.
54 */
55 public function after_delete( string $deleted_file, ?Scan $related_scan, string $scan_type ): array {
56 $this->log( sprintf( '%s is deleted', $deleted_file ), \WP_Defender\Controller\Scan::SCAN_LOG );
57 $related_scan->remove_issue( $this->owner->id );
58 $related_scan->remove_related_issue_by( $deleted_file, $scan_type );
59 do_action( 'wpdef_fixed_scan_issue', $scan_type, 'delete' );
60
61 return array( 'message' => esc_html__( 'This item has been permanently removed', 'defender-security' ) );
62 }
63
64 /**
65 * Returns a WP_Error object with an error code indicating that the file is not writeable.
66 *
67 * @param string $file The path to the file that is not writeable.
68 *
69 * @return WP_Error The WP_Error object with the error code and the basename of the file.
70 */
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
74 return new WP_Error( Error_Code::NOT_WRITEABLE, wp_basename( $file ) );
75 }
76
77 /**
78 * Get file meta details: created_at, size, and deleted flag.
79 *
80 * @param string $file The path to the file.
81 *
82 * @return array An array containing the file's created_at date, size, and a deleted flag.
83 */
84 private function get_file_meta( string $file ): array {
85 if ( ! file_exists( $file ) || ! is_readable( $file ) ) {
86 return array( null, null, true );
87 }
88
89 $file_created_at = filemtime( $file );
90 $file_created_at = $file_created_at ? wp_date( 'M j, Y', $file_created_at ) : null;
91
92 $file_size = filesize( $file );
93 $file_size = $file_size ? $this->format_bytes_into_readable( $file_size ) : null;
94
95 return array( $file_created_at, $file_size, false );
96 }
97 }
98