: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
<?php namespace la\core\snapshots;
if ( ! defined( 'WPINC' ) ) die;
use la\core\db\LADBManager;
* Plugin class. This class should ideally be used to work with the
* public-facing side of the WordPress site.
* If you're interested in introducing administrative or dashboard
* functionality, then refer to `FlowFlowAdmin.php`
* @author Looks Awesome <email@looks-awesome.com>
* @link http://looks-awesome.com
* @copyright Looks Awesome
class LASnapshotManager {
public function __construct($context) {
$this->context = $context;
add_action('wp_ajax_create_backup', [ $this, 'processAjaxRequest' ] );
add_action('wp_ajax_restore_backup', [ $this, 'processAjaxRequest' ] );
add_action('wp_ajax_delete_backup', [ $this, 'processAjaxRequest' ] );
public function getSnapshots(){
$dbm = LAUtils::dbm($this->context);
$rows = $dbm->conn()->getAll('SELECT * FROM ?n ORDER BY `creation_time` DESC', $dbm->snapshot_table_name);
foreach ( $rows as $row ) {
$sn->description = $row['description'];
$sn->creation_time = $row['creation_time'];
$sn->settings = $row['settings'];
$sn->version = $row['version'];
$sn->outdated = version_compare(self::VERSION, $row['version'], '>=');
public function processAjaxRequest() {
if (!current_user_can('manage_options') && !check_ajax_referer( 'flow_flow_nonce', 'security', false ) ) {
die( json_encode( [ 'error' => 'not_allowed' ] ) );
if (isset($_REQUEST['action'])){
$dbm = LAUtils::dbm($this->context);
if (false === $conn->beginTransaction()) throw new Exception('Don`t started transaction');
switch ($_REQUEST['action']){
$result = $this->createBackup($dbm);
$result = $this->restoreBackup($dbm);
$result = $this->deleteBackup($dbm);
$conn->rollbackAndClose();
error_log($e->getMessage());
error_log($e->getTraceAsString());
switch ($_REQUEST['action']){
$result = [ 'backed_up' => false ];
$result = [ 'restore' => false ];
$result = [ 'deleted' => false ];
echo json_encode($result);
* @param LADBManager $dbm
public function createBackup ($dbm) {
$description = '';//TODO add description for snapshot
$options = $conn->getAll('SELECT `id`, `value` FROM ?n', $dbm->option_table_name);
foreach ( $options as $option ) {
$all[$option['id']] = $option['value'];
$all['streams'] = $dbm->streams();
$all['sources'] = $dbm->sources();
$result = gzcompress(serialize($all), 6);
$conn->query("INSERT INTO ?n (`description`, `settings`, `dump`, `version`) VALUES(?s, ?s, ?s, ?s)", $dbm->snapshot_table_name, $description, '', $result, $this->context['version']);
return [ 'backed_up' => true, 'result' => $conn->affectedRows() ];
* @param LADBManager $dbm
public function restoreBackup ($dbm) {
if (false !== ($dump = $conn->getOne('SELECT `dump` FROM ?n WHERE id=?s', $dbm->snapshot_table_name, $_REQUEST['id']))){
$all = gzuncompress($dump);
$all = unserialize($all);
foreach ( $dbm->sources() as $id => $source ) {
foreach ( $all['sources'] as $source ) {
$dbm->modifySource($source);
foreach ( $dbm->streams() as $stream ) {
LADB::deleteStream($conn, $dbm->streams_table_name, $dbm->streams_sources_table_name, $stream['id']);
foreach ( $all['streams'] as $stream ) {
LADB::setStream($conn, $dbm->streams_table_name, $dbm->streams_sources_table_name, $obj->id, $obj);
foreach ( $all as $key => $value ) {
$key = strpos($key, 'flow_flow_') === 0 ? str_replace('flow_flow_', '', $key) : $key;
$dbm->setOption($key, $value);
return [ 'restore' => true ];
return [ 'found' => false ];
* @param LADBManager $dbm
public function deleteBackup ($dbm) {
$op = $dbm->conn()->query ('DELETE FROM ?n WHERE `id`=?s', $dbm->snapshot_table_name, $_REQUEST['id']);
return [ 'deleted' => ( false !== $op) ];