: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
<?php namespace la\core\db;
if ( ! defined( 'WPINC' ) ) die;
use la\core\db\migrations\ILADBMigration;
* @author Looks Awesome <email@looks-awesome.com>
* @link http://looks-awesome.com
* @copyright Looks Awesome
abstract class LADBMigrationManager{
const INIT_MIGRATION = '0.9999';
public function __construct($context) {
$this->context = $context;
* @throws ReflectionException
public final function migrate(){
$version = $this->getDBVersion();
if (!$this->hasMigrations4Perform($version)){
$dbm = LAUtils::dbm($this->context);
$conn = $this->connection();
if ( $conn->autocommit(false) ){
if ($this->needStartInitMigration($version)){
foreach ($this->getInitMigration() as $max_version => $migration){
$migration->execute($conn, $dbm);
$dbm->setOption('db_version', $max_version);
foreach ( $this->getMigrations() as $migration ) {
if ($this->needExecuteMigration($version, $migration->version())){
$migration->execute($conn, $dbm);
$dbm->setOption('db_version', $migration->version());
} catch ( Exception $e ){
error_log($e->getTraceAsString());
* Return the list of migration
protected abstract function migrations();
protected function connection() {
return LAUtils::dbm($this->context)->conn();
private function getDBVersion(){
$version = self::INIT_MIGRATION;
$table = LAUtils::dbm($this->context)->option_table_name;
if (null != $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table))){
$option = LAUtils::slug_down($this->context) . '_db_version';
$version = $wpdb->get_var($wpdb->prepare('select value from ' . $table . ' where id = %s', $option));
$e = new Exception('Can`t get the db version of plugin');
error_log($e->getTraceAsString());
return self::INIT_MIGRATION;
private function needStartInitMigration($version){
return self::INIT_MIGRATION == $version || $version === false;
* @throws ReflectionException
private function getInitMigration(){
$migrations = $this->getMigrations();
$max = self::INIT_MIGRATION;
foreach ($migrations as $version => $migration) {
return [ $max => $migrations[self::INIT_MIGRATION] ];
* @throws ReflectionException
private function getMigrations(){
foreach ($this->migrations() as $class) {
$clazz = new ReflectionClass($class);
/** @var ILADBMigration $migration */
$migration = $clazz->newInstance();
$migrations[$migration->version()] = $migration;
uksort($migrations, 'version_compare');
private function needExecuteMigration($db_version, $migration_version){
$db = explode('.', $db_version);
$migration = explode('.', $migration_version);
if (intval($migration[0]) == intval($db[0])){
return (intval($migration[1]) > $db[1]);
return (intval($migration[0]) > intval($db[0]));
* @throws ReflectionException
private function hasMigrations4Perform($version){
if ($this->needStartInitMigration($version)){
foreach ( $this->getMigrations() as $migration ) {
if ( $this->needExecuteMigration( $version, $migration->version() ) ) {