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-sql.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Handle replacements in SQL strings.
[2] Fix | Delete
*
[3] Fix | Delete
* This file borrows and adapts functions and concepts from the
[4] Fix | Delete
* interconnect.it Search-Replace script (https://github.com/interconnectit/Search-Replace-DB).
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
namespace StringLocator\Extension\SearchReplace\Replace;
[8] Fix | Delete
[9] Fix | Delete
use StringLocator\Extension\SQL\Search;
[10] Fix | Delete
use StringLocator\String_Locator;
[11] Fix | Delete
use function StringLocator\Extension\SQL\validate_sql_fields;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* SQL class.
[15] Fix | Delete
*/
[16] Fix | Delete
class SQL {
[17] Fix | Delete
[18] Fix | Delete
private $table_name;
[19] Fix | Delete
private $primary_column;
[20] Fix | Delete
private $primary_key;
[21] Fix | Delete
private $primary_type;
[22] Fix | Delete
private $column_name;
[23] Fix | Delete
private $regex;
[24] Fix | Delete
private $new_string;
[25] Fix | Delete
private $old_string;
[26] Fix | Delete
private $original_string;
[27] Fix | Delete
[28] Fix | Delete
private $search;
[29] Fix | Delete
[30] Fix | Delete
/**
[31] Fix | Delete
* Class constructor.
[32] Fix | Delete
*
[33] Fix | Delete
* @param string $primary_column The name of the primary column of the entry being edited.
[34] Fix | Delete
* @param string $primary_key The key identified of the primary column.
[35] Fix | Delete
* @param string $primary_type The type of the primary column (`int` or `string`).
[36] Fix | Delete
* @param string $table_name The name of the table to perform an edit within.
[37] Fix | Delete
* @param string $column_name The column being edited.
[38] Fix | Delete
* @param string $old_string The string to be replaced.
[39] Fix | Delete
* @param string $new_string The string to be added.
[40] Fix | Delete
* @param bool $regex Is the search string a regex string.
[41] Fix | Delete
*/
[42] Fix | Delete
public function __construct( $primary_column, $primary_key, $primary_type, $table_name, $column_name, $old_string, $new_string, $regex = false ) {
[43] Fix | Delete
$this->primary_column = $primary_column;
[44] Fix | Delete
$this->primary_key = $primary_key;
[45] Fix | Delete
$this->primary_type = $primary_type;
[46] Fix | Delete
$this->table_name = $table_name;
[47] Fix | Delete
$this->column_name = $column_name;
[48] Fix | Delete
$this->regex = $regex;
[49] Fix | Delete
$this->old_string = $old_string;
[50] Fix | Delete
$this->new_string = $new_string;
[51] Fix | Delete
[52] Fix | Delete
$this->search = new Search();
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Validate that the only non-escaped strings are alpha-numeric to avoid SQL injections.
[57] Fix | Delete
*
[58] Fix | Delete
* @return bool
[59] Fix | Delete
*/
[60] Fix | Delete
public function validate() {
[61] Fix | Delete
if ( ! validate_sql_fields( $this->primary_column ) ) {
[62] Fix | Delete
return false;
[63] Fix | Delete
}
[64] Fix | Delete
if ( ! validate_sql_fields( $this->table_name ) ) {
[65] Fix | Delete
return false;
[66] Fix | Delete
}
[67] Fix | Delete
if ( ! validate_sql_fields( $this->column_name ) ) {
[68] Fix | Delete
return false;
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
return true;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* Run the replacement function.
[76] Fix | Delete
*
[77] Fix | Delete
* @return bool|string|\WP_Error
[78] Fix | Delete
*/
[79] Fix | Delete
public function replace() {
[80] Fix | Delete
global $wpdb;
[81] Fix | Delete
[82] Fix | Delete
if ( 'int' === $this->primary_type ) {
[83] Fix | Delete
$this->original_string = $wpdb->get_var(
[84] Fix | Delete
$wpdb->prepare(
[85] Fix | Delete
'SELECT ' . $this->column_name . ' FROM ' . $this->table_name . ' WHERE ' . $this->primary_column . ' = %d LIMIT 1', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is not possible to prepare a table or column name, but these are instead validated in `/includes/Search/class-sql.php` before reaching this point.
[86] Fix | Delete
$this->primary_key
[87] Fix | Delete
)
[88] Fix | Delete
);
[89] Fix | Delete
} else {
[90] Fix | Delete
$this->original_string = $wpdb->get_var(
[91] Fix | Delete
$wpdb->prepare(
[92] Fix | Delete
'SELECT ' . $this->column_name . ' FROM ' . $this->table_name . ' WHERE ' . $this->primary_column . ' = %s LIMIT 1', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- It is not possible to prepare a table or column name, but these are instead validated in `/includes/Search/class-sql.php` before reaching this point.
[93] Fix | Delete
$this->primary_key
[94] Fix | Delete
)
[95] Fix | Delete
);
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
$replaced_line = $this->recursive_unserialize_replace( $this->old_string, $this->new_string, $this->original_string );
[99] Fix | Delete
[100] Fix | Delete
$updated = $wpdb->update(
[101] Fix | Delete
$this->table_name,
[102] Fix | Delete
array(
[103] Fix | Delete
$this->column_name => $replaced_line,
[104] Fix | Delete
),
[105] Fix | Delete
array(
[106] Fix | Delete
$this->primary_column => $this->primary_key,
[107] Fix | Delete
)
[108] Fix | Delete
);
[109] Fix | Delete
[110] Fix | Delete
if ( ! $updated ) {
[111] Fix | Delete
/*
[112] Fix | Delete
* Cause an error to be thrown if updates fail due ot the query.
[113] Fix | Delete
*
[114] Fix | Delete
* This also checks that `$wpdb->last_error` is empty before treating
[115] Fix | Delete
* the result as an error, this is because `$wpdb->update` will return
[116] Fix | Delete
* a `false` value if it did not perform an update, for example when
[117] Fix | Delete
* a string is identical. This may be the case where a class name or
[118] Fix | Delete
* object is encountered, which can not be replaced.
[119] Fix | Delete
*/
[120] Fix | Delete
if ( empty( $wpdb->last_error ) ) {
[121] Fix | Delete
return true;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
return new \WP_Error( 'search_replace_sql_error', __( 'Error updating the database.', 'search-replace' ) );
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
return String_Locator::create_preview( $replaced_line, $this->new_string, $this->regex );
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
/**
[131] Fix | Delete
* Restore the last ran modification.
[132] Fix | Delete
*
[133] Fix | Delete
* @return bool
[134] Fix | Delete
*/
[135] Fix | Delete
public function restore() {
[136] Fix | Delete
global $wpdb;
[137] Fix | Delete
[138] Fix | Delete
$wpdb->update(
[139] Fix | Delete
$this->table_name,
[140] Fix | Delete
array(
[141] Fix | Delete
$this->column_name => $this->original_string,
[142] Fix | Delete
),
[143] Fix | Delete
array(
[144] Fix | Delete
$this->primary_column => $this->primary_key,
[145] Fix | Delete
)
[146] Fix | Delete
);
[147] Fix | Delete
return true;
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
public function get_edit_url() {
[151] Fix | Delete
return $this->search->create_edit_link( $this->table_name, $this->column_name, $this->primary_column, $this->primary_type, (object) array( 'primary_column' => $this->primary_key ) );
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Take a serialised array and unserialise it replacing elements as needed and
[156] Fix | Delete
* unserialising any subordinate arrays and performing the replacement on those too.
[157] Fix | Delete
*
[158] Fix | Delete
* @param string $from String we're looking to replace.
[159] Fix | Delete
* @param string $to What we want it to be replaced with
[160] Fix | Delete
* @param array $data Used to pass any subordinate arrays back to in.
[161] Fix | Delete
* @param bool $serialised Does the array passed via $data need serialising.
[162] Fix | Delete
*
[163] Fix | Delete
* @return array|string The original array with all elements replaced as needed.
[164] Fix | Delete
*/
[165] Fix | Delete
public function recursive_unserialize_replace( $from = '', $to = '', $data = '', $serialised = false ) {
[166] Fix | Delete
// Some unserialised data cannot be re-serialised eg. SimpleXMLElements.
[167] Fix | Delete
try {
[168] Fix | Delete
$unserialized = @unserialize( $data );
[169] Fix | Delete
if ( is_string( $data ) && false !== $unserialized ) {
[170] Fix | Delete
$data = $this->recursive_unserialize_replace( $from, $to, $unserialized, true );
[171] Fix | Delete
} elseif ( is_array( $data ) ) {
[172] Fix | Delete
$_tmp = array();
[173] Fix | Delete
foreach ( $data as $key => $value ) {
[174] Fix | Delete
$_tmp[ $key ] = $this->recursive_unserialize_replace( $from, $to, $value, false );
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
$data = $_tmp;
[178] Fix | Delete
unset( $_tmp );
[179] Fix | Delete
} elseif ( is_object( $data ) && ! is_a( $data, '__PHP_Incomplete_Class' ) ) {
[180] Fix | Delete
$_tmp = $data;
[181] Fix | Delete
$props = get_object_vars( $data );
[182] Fix | Delete
foreach ( $props as $key => $value ) {
[183] Fix | Delete
$_tmp->$key = $this->recursive_unserialize_replace( $from, $to, $value, false );
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
$data = $_tmp;
[187] Fix | Delete
unset( $_tmp );
[188] Fix | Delete
} else {
[189] Fix | Delete
if ( is_string( $data ) ) {
[190] Fix | Delete
$data = $this->str_replace( $from, $to, $data );
[191] Fix | Delete
}
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
if ( $serialised ) {
[195] Fix | Delete
return serialize( $data );
[196] Fix | Delete
}
[197] Fix | Delete
} catch ( \Exception $error ) {
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
return $data;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Wrapper for regex/non regex search & replace
[205] Fix | Delete
*
[206] Fix | Delete
* @param string $search
[207] Fix | Delete
* @param string $replace
[208] Fix | Delete
* @param string $string
[209] Fix | Delete
* @param int $count
[210] Fix | Delete
*
[211] Fix | Delete
* @return string
[212] Fix | Delete
*/
[213] Fix | Delete
public function str_replace( $search, $replace, $string, &$count = 0 ) {
[214] Fix | Delete
if ( $this->regex ) {
[215] Fix | Delete
return preg_replace( $search, $replace, $string, - 1, $count );
[216] Fix | Delete
} elseif ( function_exists( 'mb_split' ) ) {
[217] Fix | Delete
return $this->mb_str_replace( $search, $replace, $string, $count );
[218] Fix | Delete
} else {
[219] Fix | Delete
return str_ireplace( $search, $replace, $string, $count );
[220] Fix | Delete
}
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
/**
[224] Fix | Delete
* Replace all occurrences of the search string with the replacement string.
[225] Fix | Delete
*
[226] Fix | Delete
* @param mixed $search
[227] Fix | Delete
* @param mixed $replace
[228] Fix | Delete
* @param mixed $subject
[229] Fix | Delete
* @param int $count
[230] Fix | Delete
*
[231] Fix | Delete
* @return mixed
[232] Fix | Delete
* @copyright Copyright 2012 Sean Murphy. All rights reserved.
[233] Fix | Delete
* @license http://creativecommons.org/publicdomain/zero/1.0/
[234] Fix | Delete
* @link http://php.net/manual/function.str-replace.php
[235] Fix | Delete
*
[236] Fix | Delete
* @author Sean Murphy <sean@iamseanmurphy.com>
[237] Fix | Delete
*/
[238] Fix | Delete
public function mb_str_replace( $search, $replace, $subject, &$count = 0 ) {
[239] Fix | Delete
if ( ! is_array( $subject ) ) {
[240] Fix | Delete
// Normalize $search and $replace so they are both arrays of the same length
[241] Fix | Delete
$searches = is_array( $search ) ? array_values( $search ) : array( $search );
[242] Fix | Delete
$replacements = is_array( $replace ) ? array_values( $replace ) : array( $replace );
[243] Fix | Delete
$replacements = array_pad( $replacements, count( $searches ), '' );
[244] Fix | Delete
[245] Fix | Delete
foreach ( $searches as $key => $search ) {
[246] Fix | Delete
$parts = mb_split( preg_quote( $search ), $subject );
[247] Fix | Delete
if ( ! is_array( $parts ) ) {
[248] Fix | Delete
continue;
[249] Fix | Delete
}
[250] Fix | Delete
$count += count( $parts ) - 1;
[251] Fix | Delete
$subject = implode( $replacements[ $key ], $parts );
[252] Fix | Delete
}
[253] Fix | Delete
} else {
[254] Fix | Delete
// Call mb_str_replace for each subject in array, recursively
[255] Fix | Delete
foreach ( $subject as $key => $value ) {
[256] Fix | Delete
$subject[ $key ] = $this->mb_str_replace( $search, $replace, $value, $count );
[257] Fix | Delete
}
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
return $subject;
[261] Fix | Delete
}
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function