Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../www/wp-conte.../plugins/string-l.../includes/Extensio.../SearchRe.../Replace
File: class-file.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Class for handling replacements in files.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
namespace StringLocator\Extension\SearchReplace\Replace;
[5] Fix | Delete
[6] Fix | Delete
use StringLocator\Search;
[7] Fix | Delete
use StringLocator\String_Locator;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* File class.
[11] Fix | Delete
*/
[12] Fix | Delete
class File {
[13] Fix | Delete
[14] Fix | Delete
private $file;
[15] Fix | Delete
private $line;
[16] Fix | Delete
private $regex;
[17] Fix | Delete
private $new_string;
[18] Fix | Delete
private $old_string;
[19] Fix | Delete
private $original_string;
[20] Fix | Delete
[21] Fix | Delete
private $search;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Class constructor.
[25] Fix | Delete
*
[26] Fix | Delete
* @param string $file The relative path from the WordPress root to the file being edited.
[27] Fix | Delete
* @param int $line The line in the file containing the string to be replaced.
[28] Fix | Delete
* @param string $old_string The string to be replaced.
[29] Fix | Delete
* @param string $new_string The string to be added.
[30] Fix | Delete
* @param bool $regex Is the search string a regex string.
[31] Fix | Delete
*/
[32] Fix | Delete
public function __construct( $file, $line, $old_string, $new_string, $regex = false ) {
[33] Fix | Delete
$this->file = trailingslashit( ABSPATH ) . $file;
[34] Fix | Delete
$this->line = ( absint( $line ) - 1 );
[35] Fix | Delete
$this->regex = $regex;
[36] Fix | Delete
$this->old_string = $old_string;
[37] Fix | Delete
$this->new_string = $new_string;
[38] Fix | Delete
[39] Fix | Delete
$this->search = new Search();
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Validate that the file is in a location that does not allow directory traversals.
[44] Fix | Delete
*
[45] Fix | Delete
* @return bool
[46] Fix | Delete
*/
[47] Fix | Delete
public function validate() {
[48] Fix | Delete
return String_Locator::is_valid_location( $this->file );
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* Run the replacement function.
[53] Fix | Delete
*
[54] Fix | Delete
* @return bool|string|\WP_Error
[55] Fix | Delete
*/
[56] Fix | Delete
public function replace() {
[57] Fix | Delete
// A value of 0 or lower indicates a filename or similar matched, and these should NOT be replaced.
[58] Fix | Delete
if ( $this->line < 0 ) {
[59] Fix | Delete
return true;
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
$file_contents = file( $this->file );
[63] Fix | Delete
[64] Fix | Delete
if ( ! $file_contents ) {
[65] Fix | Delete
return new \WP_Error( 'file_inaccessible', __( 'The file could not be read.', 'string-locator' ), array( 'status' => 400 ) );
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
$this->original_string = $file_contents[ $this->line ];
[69] Fix | Delete
[70] Fix | Delete
if ( $this->regex ) {
[71] Fix | Delete
$file_contents[ $this->line ] = preg_replace( $this->old_string, $this->new_string, $file_contents[ $this->line ] );
[72] Fix | Delete
} else {
[73] Fix | Delete
$file_contents[ $this->line ] = str_ireplace( $this->old_string, $this->new_string, $file_contents[ $this->line ] );
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
$file = fopen( $this->file, 'w' );
[77] Fix | Delete
[78] Fix | Delete
if ( ! $file ) {
[79] Fix | Delete
return new \WP_Error( 'file_inaccessible', __( 'The file could not be written.', 'string-locator' ), array( 'status' => 400 ) );
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
foreach ( $file_contents as $file_line ) {
[83] Fix | Delete
fwrite( $file, $file_line );
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
fclose( $file );
[87] Fix | Delete
[88] Fix | Delete
return String_Locator::create_preview( $file_contents[ $this->line ], $this->new_string, $this->regex );
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
/**
[92] Fix | Delete
* Restore the last ran modification.
[93] Fix | Delete
*
[94] Fix | Delete
* @return bool
[95] Fix | Delete
*/
[96] Fix | Delete
public function restore() {
[97] Fix | Delete
$file_contents = file( $this->file );
[98] Fix | Delete
[99] Fix | Delete
$file_contents[ $this->line ] = $this->original_string;
[100] Fix | Delete
[101] Fix | Delete
$file = fopen( $this->file, 'w' );
[102] Fix | Delete
[103] Fix | Delete
foreach ( $file_contents as $file_line ) {
[104] Fix | Delete
fwrite( $file, $file_line );
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
fclose( $file );
[108] Fix | Delete
[109] Fix | Delete
return true;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
/**
[113] Fix | Delete
* Return the URL for the editor interface for this file.
[114] Fix | Delete
*
[115] Fix | Delete
* @return string
[116] Fix | Delete
*/
[117] Fix | Delete
public function get_edit_url() {
[118] Fix | Delete
return $this->search->create_edit_link( $this->file, $this->line, 0 );
[119] Fix | Delete
}
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function