| 1 |
<?php |
| 2 |
namespace YayMail\Migrations; |
| 3 |
|
| 4 |
use YayMail\Migrations\MigrationHelper; |
| 5 |
use YayMail\Utils\SingletonTrait; |
| 6 |
use YayMail\Utils\Logger; |
| 7 |
|
| 8 |
/** |
| 9 |
* Database migration Main class |
| 10 |
*/ |
| 11 |
class MainMigration { |
| 12 |
use SingletonTrait; |
| 13 |
|
| 14 |
private $logger; |
| 15 |
|
| 16 |
private $old_version; |
| 17 |
private $new_version; |
| 18 |
|
| 19 |
const CORE_MIGRATIONS = [ |
| 20 |
'4.0.0' => '\YayMail\Migrations\Versions\Ver_4_0_0', |
| 21 |
'4.0.7' => '\YayMail\Migrations\Versions\Ver_4_0_7', |
| 22 |
'4.1.0' => '\YayMail\Migrations\Versions\Ver_4_1_0', |
| 23 |
]; |
| 24 |
|
| 25 |
private function __construct() { |
| 26 |
if ( ! defined( 'YAYMAIL_VERSION' ) ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
$this->logger = new Logger(); |
| 30 |
$this->new_version = MigrationHelper::format_version_number( YAYMAIL_VERSION ); |
| 31 |
|
| 32 |
$old_version = get_option( 'yaymail_version' ); |
| 33 |
// YayMail's version from db |
| 34 |
$this->old_version = MigrationHelper::format_version_number( $old_version ?? '3.9.9' ); |
| 35 |
} |
| 36 |
|
| 37 |
public function migrate( $skip_check_migration = false ) { |
| 38 |
$args = [ |
| 39 |
'post_type' => 'yaymail_template', |
| 40 |
'post_status' => 'any', |
| 41 |
'posts_per_page' => -1, |
| 42 |
]; |
| 43 |
|
| 44 |
$query = new \WP_Query( $args ); |
| 45 |
|
| 46 |
$has_yaymail_template = $query->have_posts(); |
| 47 |
if ( ! $skip_check_migration && ( empty( $this->old_version ) && ! $has_yaymail_template ) ) { |
| 48 |
$this->logger->log( 'YayMail is freshly installed, no migrations needed!' ); |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
$this->logger->log( '***** Start migration transaction *****' ); |
| 53 |
global $wpdb; |
| 54 |
$wpdb->query( 'START TRANSACTION' ); |
| 55 |
|
| 56 |
try { |
| 57 |
$core_migrations = self::CORE_MIGRATIONS; |
| 58 |
$this->logger->log( 'Start core migrations' ); |
| 59 |
|
| 60 |
if ( $skip_check_migration ) { |
| 61 |
$filtered_migrations = $core_migrations; |
| 62 |
} else { |
| 63 |
$filtered_migrations = MigrationHelper::filter_migrations( $core_migrations, $this->old_version, $this->new_version ); |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! empty( $filtered_migrations ) ) { |
| 67 |
MigrationHelper::perform_migrations( $filtered_migrations, $skip_check_migration ); |
| 68 |
update_option( 'yaymail_version', $this->new_version ); |
| 69 |
wp_cache_delete( 'yaymail_version', 'options' ); |
| 70 |
} |
| 71 |
|
| 72 |
$this->logger->log( 'Finish core migrations' ); |
| 73 |
|
| 74 |
do_action( 'yaymail_run_addon_migrations' ); |
| 75 |
$wpdb->query( 'COMMIT' ); |
| 76 |
|
| 77 |
return true; |
| 78 |
} catch ( \Exception $e ) { |
| 79 |
$wpdb->query( 'ROLLBACK' ); |
| 80 |
$this->logger->log( "[Migration failed] {$e->getMessage()}" ); |
| 81 |
return false; |
| 82 |
} finally { |
| 83 |
$this->logger->log( '***** End migration transaction *****' ); |
| 84 |
}//end try |
| 85 |
} |
| 86 |
} |
| 87 |
|