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.../public_h.../clone/wp-conte.../plugins/string-l.../includes
File: class-save.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace StringLocator;
[2] Fix | Delete
[3] Fix | Delete
use StringLocator\Tests\Loopback;
[4] Fix | Delete
use StringLocator\Tests\Smart_Scan;
[5] Fix | Delete
[6] Fix | Delete
class Save {
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* An array of notices to send back to the user.
[10] Fix | Delete
*
[11] Fix | Delete
* @var array
[12] Fix | Delete
*/
[13] Fix | Delete
public $notice = array();
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Save constructor.
[17] Fix | Delete
*/
[18] Fix | Delete
public function __construct() {}
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Handler for storing the content of the code editor.
[22] Fix | Delete
*
[23] Fix | Delete
* Also runs over the Smart-Scan if enabled.
[24] Fix | Delete
*
[25] Fix | Delete
* @return void|array
[26] Fix | Delete
*/
[27] Fix | Delete
public function save( $save_params ) {
[28] Fix | Delete
$_POST = $save_params;
[29] Fix | Delete
[30] Fix | Delete
if ( String_Locator::is_valid_location( $_POST['string-locator-path'] ) ) {
[31] Fix | Delete
$path = urldecode( $_POST['string-locator-path'] );
[32] Fix | Delete
$content = $_POST['string-locator-editor-content'];
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Send an error notice if the file isn't writable
[36] Fix | Delete
*/
[37] Fix | Delete
if ( ! is_writeable( $path ) ) {
[38] Fix | Delete
$this->notice[] = array(
[39] Fix | Delete
'type' => 'error',
[40] Fix | Delete
'message' => __( 'The file could not be written to, please check file permissions or edit it manually.', 'string-locator' ),
[41] Fix | Delete
);
[42] Fix | Delete
[43] Fix | Delete
return array(
[44] Fix | Delete
'notices' => $this->notice,
[45] Fix | Delete
);
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* Filter if the save process should be performed or not.
[50] Fix | Delete
*
[51] Fix | Delete
* @attr bool $can_save Can the save be carried out.
[52] Fix | Delete
* @attr string $content The content to save.
[53] Fix | Delete
* @attr string $path Path to the file being edited.
[54] Fix | Delete
*/
[55] Fix | Delete
$can_save = apply_filters( 'string_locator_pre_save', true, $content, $path );
[56] Fix | Delete
[57] Fix | Delete
if ( ! $can_save ) {
[58] Fix | Delete
return array(
[59] Fix | Delete
'notices' => apply_filters( 'string_locator_pre_save_fail_notice', array() ),
[60] Fix | Delete
);
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
$original = file_get_contents( $path );
[64] Fix | Delete
[65] Fix | Delete
$this->write_file( $path, $content );
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Filter if the save process completed as it should or if warnings should be returned.
[69] Fix | Delete
*
[70] Fix | Delete
* @attr bool $save_successful Boolean indicating if the save was successful.
[71] Fix | Delete
* @attr string $content The edited content.
[72] Fix | Delete
* @attr string $original The original content.
[73] Fix | Delete
* @attr string $path The path to the file being edited.
[74] Fix | Delete
*/
[75] Fix | Delete
$save_successful = apply_filters( 'string_locator_post_save', true, $content, $original, $path );
[76] Fix | Delete
[77] Fix | Delete
/**
[78] Fix | Delete
* Check the status of the site after making our edits.
[79] Fix | Delete
* If the site fails, revert the changes to return the sites to its original state
[80] Fix | Delete
*/
[81] Fix | Delete
if ( ! $save_successful ) {
[82] Fix | Delete
$this->write_file( $path, $original );
[83] Fix | Delete
[84] Fix | Delete
return array(
[85] Fix | Delete
'notices' => apply_filters( 'string_locator_post_save_fail_notice', array() ),
[86] Fix | Delete
);
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
return array(
[90] Fix | Delete
'notices' => array(
[91] Fix | Delete
array(
[92] Fix | Delete
'type' => 'success',
[93] Fix | Delete
'message' => __( 'The file has been saved', 'string-locator' ),
[94] Fix | Delete
),
[95] Fix | Delete
),
[96] Fix | Delete
);
[97] Fix | Delete
} else {
[98] Fix | Delete
return array(
[99] Fix | Delete
'notices' => array(
[100] Fix | Delete
array(
[101] Fix | Delete
'type' => 'error',
[102] Fix | Delete
'message' => sprintf(
[103] Fix | Delete
// translators: %s: The file location that was sent.
[104] Fix | Delete
__( 'The file location provided, <strong>%s</strong>, is not valid.', 'string-locator' ),
[105] Fix | Delete
$_POST['string-locator-path']
[106] Fix | Delete
),
[107] Fix | Delete
),
[108] Fix | Delete
),
[109] Fix | Delete
);
[110] Fix | Delete
}
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
/**
[114] Fix | Delete
* When editing a file, this is where we write all the new content.
[115] Fix | Delete
* We will break early if the user isn't allowed to edit files.
[116] Fix | Delete
*
[117] Fix | Delete
* @param string $path The path to the file.
[118] Fix | Delete
* @param string $content The content to write to the file.
[119] Fix | Delete
*
[120] Fix | Delete
* @return void
[121] Fix | Delete
*/
[122] Fix | Delete
private function write_file( $path, $content ) {
[123] Fix | Delete
if ( ! current_user_can( String_Locator::$default_capability ) ) {
[124] Fix | Delete
return;
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
// Verify the location is valid before we try using it.
[128] Fix | Delete
if ( ! String_Locator::is_valid_location( $path ) ) {
[129] Fix | Delete
return;
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
$back_compat_filter = apply_filters( 'string-locator-filter-closing-php-tags', true ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
[133] Fix | Delete
[134] Fix | Delete
if ( apply_filters( 'string_locator_filter_closing_php_tags', $back_compat_filter ) ) {
[135] Fix | Delete
$content = preg_replace( '/\?>$/si', '', trim( $content ), - 1, $replaced_strings );
[136] Fix | Delete
[137] Fix | Delete
if ( $replaced_strings >= 1 ) {
[138] Fix | Delete
$this->notice[] = array(
[139] Fix | Delete
'type' => 'error',
[140] Fix | Delete
'message' => __( 'We detected a PHP code tag ending, this has been automatically stripped out to help prevent errors in your code.', 'string-locator' ),
[141] Fix | Delete
);
[142] Fix | Delete
}
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
$file = fopen( $path, 'w' );
[146] Fix | Delete
$lines = explode( "\n", str_replace( array( "\r\n", "\r" ), "\n", $content ) );
[147] Fix | Delete
$total_lines = count( $lines );
[148] Fix | Delete
[149] Fix | Delete
for ( $i = 0; $i < $total_lines; $i ++ ) {
[150] Fix | Delete
$write_line = $lines[ $i ];
[151] Fix | Delete
[152] Fix | Delete
if ( ( $i + 1 ) < $total_lines ) {
[153] Fix | Delete
$write_line .= PHP_EOL;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
fwrite( $file, $write_line );
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
fclose( $file );
[160] Fix | Delete
}
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function