get_wp_filesystem(); return file_exists( $file ) && $wp_filesystem->is_writable( $file ) && $wp_filesystem->delete( $file ); } /** * Get the WordPress filesystem instance. * * @return WP_Filesystem_Base The WordPress filesystem instance. */ public function get_wp_filesystem(): WP_Filesystem_Base { global $wp_filesystem; if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } return $wp_filesystem; } /** * Handle actions after deleting a file or folder. * * @param string $deleted_file The file or folder that was deleted. * @param Scan|null $related_scan The scan instance related to the deletion. * @param string $scan_type The type of scan. * * @return array An array with a message confirming the deletion. */ public function after_delete( string $deleted_file, ?Scan $related_scan, string $scan_type ): array { $this->log( sprintf( '%s is deleted', $deleted_file ), \WP_Defender\Controller\Scan::SCAN_LOG ); $related_scan->remove_issue( $this->owner->id ); $related_scan->remove_related_issue_by( $deleted_file, $scan_type ); do_action( 'wpdef_fixed_scan_issue', $scan_type, 'delete' ); return array( 'message' => esc_html__( 'This item has been permanently removed', 'defender-security' ) ); } /** * Returns a WP_Error object with an error code indicating that the file is not writeable. * * @param string $file The path to the file that is not writeable. * * @return WP_Error The WP_Error object with the error code and the basename of the file. */ public function get_permission_error( string $file ): WP_Error { $this->log( sprintf( 'ISSUE: %s is not writeable', $file ), \WP_Defender\Controller\Scan::SCAN_LOG ); return new WP_Error( Error_Code::NOT_WRITEABLE, wp_basename( $file ) ); } /** * Get file meta details: created_at, size, and deleted flag. * * @param string $file The path to the file. * * @return array An array containing the file's created_at date, size, and a deleted flag. */ private function get_file_meta( string $file ): array { if ( ! file_exists( $file ) || ! is_readable( $file ) ) { return array( null, null, true ); } $file_created_at = filemtime( $file ); $file_created_at = $file_created_at ? wp_date( 'M j, Y', $file_created_at ) : null; $file_size = filesize( $file ); $file_size = $file_size ? $this->format_bytes_into_readable( $file_size ) : null; return array( $file_created_at, $file_size, false ); } }