Edit File by line
/home/zeestwma/ajeebong.../wp-conte.../plugins/image-op.../classes/file-sys...
File: file-system.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace ImageOptimization\Classes\File_System;
[2] Fix | Delete
[3] Fix | Delete
use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error;
[4] Fix | Delete
[5] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[6] Fix | Delete
exit; // Exit if accessed directly.
[7] Fix | Delete
}
[8] Fix | Delete
[9] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/file.php';
[10] Fix | Delete
WP_Filesystem();
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* A wrapper class under WP_Filesystem that throws clear exceptions in case if something went wrong.
[14] Fix | Delete
*/
[15] Fix | Delete
class File_System {
[16] Fix | Delete
/**
[17] Fix | Delete
* Writes a string to a file.
[18] Fix | Delete
*
[19] Fix | Delete
* @param string $file Remote path to the file where to write the data.
[20] Fix | Delete
* @param string $contents The data to write.
[21] Fix | Delete
* @param int|false $mode The file permissions as octal number, usually 0644.
[22] Fix | Delete
*
[23] Fix | Delete
* @return true
[24] Fix | Delete
*
[25] Fix | Delete
* @throws File_System_Operation_Error
[26] Fix | Delete
*/
[27] Fix | Delete
public static function put_contents( string $file, string $contents, $mode = false ): bool {
[28] Fix | Delete
global $wp_filesystem;
[29] Fix | Delete
[30] Fix | Delete
$result = $wp_filesystem->put_contents( $file, $contents, $mode );
[31] Fix | Delete
[32] Fix | Delete
if ( ! $result ) {
[33] Fix | Delete
throw new File_System_Operation_Error( 'Error while writing ' . $file );
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
return true;
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Deletes a file or directory.
[41] Fix | Delete
*
[42] Fix | Delete
* @param string $file Path to the file or directory.
[43] Fix | Delete
* @param bool $recursive If set to true, deletes files and folders recursively.
[44] Fix | Delete
* @param string|false $type Type of resource. 'f' for file, 'd' for directory.
[45] Fix | Delete
*
[46] Fix | Delete
* @return true
[47] Fix | Delete
*
[48] Fix | Delete
* @throws File_System_Operation_Error
[49] Fix | Delete
*/
[50] Fix | Delete
public static function delete( string $file, bool $recursive = false, $type = false ): bool {
[51] Fix | Delete
global $wp_filesystem;
[52] Fix | Delete
[53] Fix | Delete
$result = $wp_filesystem->delete( $file, $recursive, $type );
[54] Fix | Delete
[55] Fix | Delete
if ( ! $result ) {
[56] Fix | Delete
throw new File_System_Operation_Error( 'Error while deleting ' . $file );
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
return true;
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* @param string $source Path to the source file.
[64] Fix | Delete
* @param string $destination Path to the destination file.
[65] Fix | Delete
* @param bool $overwrite Whether to overwrite the destination file if it exists.
[66] Fix | Delete
*
[67] Fix | Delete
* @return true
[68] Fix | Delete
*
[69] Fix | Delete
* @throws File_System_Operation_Error
[70] Fix | Delete
*/
[71] Fix | Delete
public static function move( string $source, string $destination, bool $overwrite = false ): bool {
[72] Fix | Delete
global $wp_filesystem;
[73] Fix | Delete
[74] Fix | Delete
$result = $wp_filesystem->move( $source, $destination, $overwrite );
[75] Fix | Delete
[76] Fix | Delete
if ( ! $result ) {
[77] Fix | Delete
throw new File_System_Operation_Error( "Error while moving {$source} to {$destination}" );
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
return true;
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
/**
[84] Fix | Delete
* Copies a file.
[85] Fix | Delete
*
[86] Fix | Delete
* @param string $source Path to the source file.
[87] Fix | Delete
* @param string $destination Path to the destination file.
[88] Fix | Delete
* @param bool $overwrite Whether to overwrite the destination file if it exists.
[89] Fix | Delete
* @param int|false $mode The permissions as octal number, usually 0644 for files, 0755 for dirs.
[90] Fix | Delete
*
[91] Fix | Delete
* @return bool
[92] Fix | Delete
*
[93] Fix | Delete
* @throws File_System_Operation_Error
[94] Fix | Delete
*/
[95] Fix | Delete
public static function copy( string $source, string $destination, bool $overwrite = false, $mode = false ): bool {
[96] Fix | Delete
global $wp_filesystem;
[97] Fix | Delete
[98] Fix | Delete
$result = $wp_filesystem->copy( $source, $destination, $overwrite, $mode );
[99] Fix | Delete
[100] Fix | Delete
if ( ! $result ) {
[101] Fix | Delete
throw new File_System_Operation_Error( "Error while copying {$source} to {$destination}" );
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
return true;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Checks if a file or directory exists.
[109] Fix | Delete
*
[110] Fix | Delete
* @param string $path Path to file or directory.
[111] Fix | Delete
*
[112] Fix | Delete
* @return bool Whether $path exists or not.
[113] Fix | Delete
*/
[114] Fix | Delete
public static function exists( string $path ): bool {
[115] Fix | Delete
global $wp_filesystem;
[116] Fix | Delete
[117] Fix | Delete
return $wp_filesystem->exists( $path );
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
/**
[121] Fix | Delete
* Gets the file size (in bytes).
[122] Fix | Delete
*
[123] Fix | Delete
* @param ?string $path Path to file.
[124] Fix | Delete
*
[125] Fix | Delete
* @return int Size of the file in bytes on success, false on failure.
[126] Fix | Delete
*
[127] Fix | Delete
* @throws File_System_Operation_Error
[128] Fix | Delete
*/
[129] Fix | Delete
public static function size( ?string $path ): int {
[130] Fix | Delete
global $wp_filesystem;
[131] Fix | Delete
[132] Fix | Delete
if ( is_null( $path ) ) {
[133] Fix | Delete
throw new File_System_Operation_Error( 'Null file path provided' );
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
$size = $wp_filesystem->size( $path );
[137] Fix | Delete
[138] Fix | Delete
if ( ! $size ) {
[139] Fix | Delete
throw new File_System_Operation_Error( "Unable to calculate file size for $path" );
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
return $size;
[143] Fix | Delete
}
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function