: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Class for testing serialized data on SQL saves.
namespace StringLocator\Extensions\SQL\Tests;
* The content that will be scanned.
* An array holding any errors returned during testing.
public $errors = array();
public function __construct() {
add_action( 'string_locator_editor_checks', array( $this, 'print_checks_option' ) );
add_filter( 'string_locator_pre_save', array( $this, 'maybe_perform_test' ), 10, 2 );
add_filter( 'string_locator_pre_save_fail_notice', array( $this, 'return_failure_notices' ) );
* Combine reported errors with any new notices.
public function return_failure_notices( $notices ) {
if ( empty( $this->errors ) ) {
* Conditionally run the test tool.
* @param bool $can_save A boolean value if the test has passed or failed.
* @param string $content The content being modified.
* @return bool|mixed|null
public function maybe_perform_test( $can_save, $content ) {
// If another addon has determined the file can not be saved, bail early.
// Do not perform a smart scan if the option for it is disabled.
if ( ! isset( $_POST['string-locator-validate-serialized-data'] ) ) {
return $this->run( $content );
* Output a checkbox to enable or disable this feature from within the editor interface.
public function print_checks_option() {
if ( ! isset( $_GET['file-type'] ) || 'sql' !== $_GET['file-type'] ) {
<input type="checkbox" name="string-locator-validate-serialized-data" checked="checked">
<?php esc_html_e( 'If the SQL data is serialized, validate it before saving.', 'string-locator' ); ?>
* A helper function to return any errors.
public function get_errors() {
* @param string $content The content to scan.
public function run( $content ) {
$this->content = $content;
// Reset the stored errors for a fresh run.
if ( \is_serialized( $this->content ) ) {
$test_data = @unserialize( $this->content );
if ( 'b:0;' !== $this->content && false === $test_data ) {
'message' => __( 'The data is meant to be serialized with PHP, but is no longer valid.', 'string-locator' ),
if ( ! empty( $this->errors ) ) {