| 1 |
<?php |
| 2 |
namespace YayMail\Migrations; |
| 3 |
|
| 4 |
use YayMail\SupportedPlugins; |
| 5 |
use YayMail\Utils\Logger; |
| 6 |
use YayMail\YayMailTemplate; |
| 7 |
|
| 8 |
/** |
| 9 |
* AbstractMigration |
| 10 |
*/ |
| 11 |
abstract class AbstractMigration { |
| 12 |
|
| 13 |
const BACKUP_PREFIX = '_yaymail_backup_'; |
| 14 |
const SUCCESSFUL_MIGRATIONS = '_yaymail_successful_migrations'; |
| 15 |
/** |
| 16 |
* @var string $old_version |
| 17 |
* @Example '3.9.9' |
| 18 |
*/ |
| 19 |
protected $old_version; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var string $new_version |
| 23 |
* @Example '4.0.0' |
| 24 |
*/ |
| 25 |
protected $new_version; |
| 26 |
|
| 27 |
/** @var Logger */ |
| 28 |
protected $logger; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var string $backup_option_name |
| 32 |
*/ |
| 33 |
protected $backup_option_name; |
| 34 |
|
| 35 |
/** |
| 36 |
* Addon Namespace, empty means core |
| 37 |
* |
| 38 |
* @var string $addon_namespace |
| 39 |
*/ |
| 40 |
protected $addon_namespace = ''; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
abstract protected function up(); |
| 45 |
|
| 46 |
protected function __construct( $old_version, $new_version, $addon_namespace = '' ) { |
| 47 |
$this->old_version = $old_version; |
| 48 |
$this->new_version = $new_version; |
| 49 |
|
| 50 |
$this->addon_namespace = $addon_namespace; |
| 51 |
|
| 52 |
$this->logger = new Logger(); |
| 53 |
} |
| 54 |
|
| 55 |
public function perform_migration( $skip_check_migration = false ) { |
| 56 |
$this->logger->log( "Attempt to migrate data from [$this->old_version] to [$this->new_version]." ); |
| 57 |
|
| 58 |
if ( ! $skip_check_migration && $this->has_migration_been_performed() ) { |
| 59 |
$this->logger->log( 'Migration aborted. Data had been successfully migrated before.' ); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$this->backup(); |
| 64 |
|
| 65 |
$this->up(); |
| 66 |
|
| 67 |
if ( ! $skip_check_migration ) { |
| 68 |
$this->log_succeeded_migration(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
protected function backup() { |
| 74 |
global $wpdb; |
| 75 |
|
| 76 |
$all_backups = $this->get_all_backups(); |
| 77 |
|
| 78 |
$prefix = self::BACKUP_PREFIX . ( ! empty( $this->addon_namespace ) ? $this->addon_namespace . '_' : '' ); |
| 79 |
$this->backup_option_name = $prefix . str_replace( '.', '_', $this->old_version ); |
| 80 |
|
| 81 |
foreach ( $all_backups as $backup_name => $backup ) { |
| 82 |
if ( $backup_name === $this->backup_option_name |
| 83 |
|| MigrationHelper::is_within_time_window( $backup['created_date'], 60 ) ) { |
| 84 |
|
| 85 |
// Set this backup name as the current backup option name |
| 86 |
// In order to let the log_succeeded_migration() know which backup to use |
| 87 |
$this->backup_option_name = $backup_name; |
| 88 |
|
| 89 |
// No need for new backup |
| 90 |
$this->logger->log( "Back up [$this->backup_option_name] already existed or there is new backup added recently. No more backup needed" ); |
| 91 |
return; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Backup posts and postmeta |
| 97 |
*/ |
| 98 |
$query_posts = " |
| 99 |
SELECT * |
| 100 |
FROM {$wpdb->posts} |
| 101 |
WHERE post_type = 'yaymail_template' |
| 102 |
"; |
| 103 |
$yaymail_template_posts = $wpdb->get_results( $query_posts );// phpcs:ignore |
| 104 |
|
| 105 |
$query_postmeta = " |
| 106 |
SELECT * |
| 107 |
FROM {$wpdb->postmeta} |
| 108 |
WHERE meta_key LIKE '%yaymail%' |
| 109 |
"; |
| 110 |
$yaymail_template_postmeta = $wpdb->get_results( $query_postmeta );// phpcs:ignore |
| 111 |
|
| 112 |
$backup_data = [ |
| 113 |
'posts' => $yaymail_template_posts, |
| 114 |
'postmeta' => $yaymail_template_postmeta, |
| 115 |
]; |
| 116 |
/** ****************************** */ |
| 117 |
|
| 118 |
/** |
| 119 |
* Backup options |
| 120 |
*/ |
| 121 |
$query_options = " |
| 122 |
SELECT * |
| 123 |
FROM {$wpdb->options} |
| 124 |
WHERE option_name LIKE '%yaymail%' |
| 125 |
"; |
| 126 |
$yaymail_options = $wpdb->get_results( $query_options ); // phpcs:ignore |
| 127 |
$backup_data['options'] = $yaymail_options; |
| 128 |
|
| 129 |
$backup_data['created_date'] = current_datetime()->format( 'Y-m-d H:i:s' ); |
| 130 |
|
| 131 |
$backup_data = apply_filters( 'yaymail_migration_backup_data', $backup_data ); |
| 132 |
/** ****************************** */ |
| 133 |
|
| 134 |
$backup_response = update_option( $this->backup_option_name, $backup_data ); |
| 135 |
|
| 136 |
if ( ! $backup_response ) { |
| 137 |
// When update_option failes, throw exception |
| 138 |
throw new \Exception( 'YayMail failed to save backup option' ); |
| 139 |
} |
| 140 |
$this->logger->log( $this->backup_option_name . ' saved!' ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Log succeeded migration to the db |
| 145 |
*/ |
| 146 |
protected function log_succeeded_migration() { |
| 147 |
$successful_migrations = get_option( self::SUCCESSFUL_MIGRATIONS, [] ); |
| 148 |
|
| 149 |
$current_migration = [ |
| 150 |
'created_date' => current_datetime()->format( 'Y-m-d H:i:s' ), |
| 151 |
'from_version' => $this->old_version, |
| 152 |
'to_version' => $this->new_version, |
| 153 |
'backup_name' => $this->backup_option_name, |
| 154 |
]; |
| 155 |
|
| 156 |
if ( ! empty( $this->addon_namespace ) ) { |
| 157 |
$current_migration['addon_namespace'] = $this->addon_namespace; |
| 158 |
} |
| 159 |
|
| 160 |
array_unshift( $successful_migrations, $current_migration ); |
| 161 |
|
| 162 |
update_option( self::SUCCESSFUL_MIGRATIONS, $successful_migrations ); |
| 163 |
} |
| 164 |
|
| 165 |
protected function has_migration_been_performed() { |
| 166 |
$successful_migrations = get_option( self::SUCCESSFUL_MIGRATIONS, [] ); |
| 167 |
|
| 168 |
foreach ( $successful_migrations as $migration ) { |
| 169 |
|
| 170 |
if ( version_compare( $migration['to_version'], $this->new_version, '<' ) ) { |
| 171 |
continue; |
| 172 |
} |
| 173 |
|
| 174 |
if ( empty( $this->addon_namespace ) ) { |
| 175 |
if ( empty( $migration['addon_namespace'] ) ) { |
| 176 |
return true; |
| 177 |
} |
| 178 |
} elseif ( $migration['addon_namespace'] === $this->addon_namespace ) { |
| 179 |
return true; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
protected function get_all_backups() { |
| 187 |
global $wpdb; |
| 188 |
|
| 189 |
$options = $wpdb->get_results( |
| 190 |
$wpdb->prepare( |
| 191 |
"SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 192 |
$wpdb->esc_like( self::BACKUP_PREFIX ) . '%' |
| 193 |
), |
| 194 |
ARRAY_A |
| 195 |
); |
| 196 |
$formatted_options = []; |
| 197 |
foreach ( $options as $option ) { |
| 198 |
$formatted_options[ $option['option_name'] ] = maybe_unserialize( $option['option_value'] ); |
| 199 |
} |
| 200 |
return $formatted_options; |
| 201 |
} |
| 202 |
|
| 203 |
protected function may_mark_template_as_v4_supported( string $template_post_id, $migration_manager = null ): void { |
| 204 |
if ( ! isset( $migration_manager ) ) { |
| 205 |
$supported_template_ids = SupportedPlugins::get_instance()->get_template_ids_from_core(); |
| 206 |
} else { |
| 207 |
$supported_template_ids = $migration_manager->get_supported_template_ids(); |
| 208 |
} |
| 209 |
|
| 210 |
if ( empty( $template_post_id ) || empty( $supported_template_ids ) ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
$template_name = get_post_meta( $template_post_id, YayMailTemplate::META_KEYS['name'], true ); |
| 215 |
|
| 216 |
if ( in_array( $template_name, $supported_template_ids, true ) ) { |
| 217 |
update_post_meta( $template_post_id, YayMailTemplate::META_KEYS['is_v4_supported'], true ); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|