Admin
3 years ago
Compatibility
3 years ago
Helpers
3 years ago
Providers
3 years ago
Reports
3 years ago
Tasks
3 years ago
UsageTracking
3 years ago
Conflicts.php
3 years ago
Connect.php
3 years ago
Core.php
3 years ago
Debug.php
3 years ago
Geo.php
3 years ago
MailCatcher.php
3 years ago
MailCatcherInterface.php
3 years ago
MailCatcherV6.php
3 years ago
Migration.php
3 years ago
MigrationAbstract.php
3 years ago
Options.php
3 years ago
Processor.php
3 years ago
SiteHealth.php
3 years ago
Upgrade.php
3 years ago
Uploads.php
3 years ago
WP.php
3 years ago
MigrationAbstract.php
162 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPMailSMTP; |
| 4 | |
| 5 | /** |
| 6 | * Class MigrationAbstract helps migrate plugin options, DB tables and more. |
| 7 | * |
| 8 | * @since 3.0.0 |
| 9 | */ |
| 10 | abstract class MigrationAbstract { |
| 11 | |
| 12 | /** |
| 13 | * Version of the latest migration. |
| 14 | * |
| 15 | * @since 3.0.0 |
| 16 | */ |
| 17 | const DB_VERSION = 1; |
| 18 | |
| 19 | /** |
| 20 | * Option key where we save the current migration version. |
| 21 | * |
| 22 | * @since 3.0.0 |
| 23 | */ |
| 24 | const OPTION_NAME = 'wp_mail_smtp_migration_version'; |
| 25 | |
| 26 | /** |
| 27 | * Option key where we save any errors while performing migration. |
| 28 | * |
| 29 | * @since 3.0.0 |
| 30 | */ |
| 31 | const ERROR_OPTION_NAME = 'wp_mail_smtp_migration_error'; |
| 32 | |
| 33 | /** |
| 34 | * Current migration version, received from static::OPTION_NAME WP option |
| 35 | * |
| 36 | * @since 3.0.0 |
| 37 | * |
| 38 | * @var int |
| 39 | */ |
| 40 | protected $cur_ver; |
| 41 | |
| 42 | /** |
| 43 | * Migration constructor. |
| 44 | * |
| 45 | * @since 3.0.0 |
| 46 | */ |
| 47 | public function __construct() { |
| 48 | |
| 49 | $this->cur_ver = static::get_current_version(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Initialize migration. |
| 54 | * |
| 55 | * @since 3.0.0 |
| 56 | */ |
| 57 | public function init() { |
| 58 | |
| 59 | $this->validate_db(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Whether migration is enabled. |
| 64 | * |
| 65 | * @since 3.0.0 |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | public static function is_enabled() { |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Static on purpose, to get current DB version without __construct() and validation. |
| 76 | * |
| 77 | * @since 3.0.0 |
| 78 | * |
| 79 | * @return int |
| 80 | */ |
| 81 | public static function get_current_version() { |
| 82 | |
| 83 | return (int) get_option( static::OPTION_NAME, 0 ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Check DB version and update to the latest one. |
| 88 | * |
| 89 | * @since 3.0.0 |
| 90 | */ |
| 91 | protected function validate_db() { |
| 92 | |
| 93 | if ( $this->cur_ver < static::DB_VERSION ) { |
| 94 | $this->run( static::DB_VERSION ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Update DB version in options table. |
| 100 | * |
| 101 | * @since 3.0.0 |
| 102 | * |
| 103 | * @param int $version Version number. |
| 104 | */ |
| 105 | protected function update_db_ver( $version = 0 ) { |
| 106 | |
| 107 | $version = (int) $version; |
| 108 | |
| 109 | if ( empty( $version ) ) { |
| 110 | $version = static::DB_VERSION; |
| 111 | } |
| 112 | |
| 113 | // Autoload it, because this value is checked all the time |
| 114 | // and no need to request it separately from all autoloaded options. |
| 115 | update_option( static::OPTION_NAME, $version, true ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Prevent running the same migration twice. |
| 120 | * Run migration only when required. |
| 121 | * |
| 122 | * @since 3.0.0 |
| 123 | * |
| 124 | * @param int $version The current migration version. |
| 125 | */ |
| 126 | protected function maybe_required_older_migrations( $version ) { |
| 127 | |
| 128 | $version = (int) $version; |
| 129 | |
| 130 | if ( ( $version - $this->cur_ver ) > 1 ) { |
| 131 | $this->run( $version - 1 ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Actual migration launcher. |
| 137 | * |
| 138 | * @since 3.0.0 |
| 139 | * |
| 140 | * @param int $version The specified migration version to run. |
| 141 | */ |
| 142 | protected function run( $version ) { |
| 143 | |
| 144 | $version = (int) $version; |
| 145 | |
| 146 | if ( method_exists( $this, 'migrate_to_' . $version ) ) { |
| 147 | $this->{'migrate_to_' . $version}(); |
| 148 | } else { |
| 149 | if ( WP::in_wp_admin() ) { |
| 150 | $message = sprintf( /* translators: %1$s - the DB option name, %2$s - WP Mail SMTP, %3$s - error message. */ |
| 151 | esc_html__( 'There was an error while upgrading the %1$s database. Please contact %2$s support with this information: %3$s.', 'wp-mail-smtp' ), |
| 152 | static::OPTION_NAME, |
| 153 | '<strong>WP Mail SMTP</strong>', |
| 154 | '<code>migration from v' . static::get_current_version() . ' to v' . static::DB_VERSION . ' failed. Plugin version: v' . WPMS_PLUGIN_VER . '</code>' |
| 155 | ); |
| 156 | |
| 157 | WP::add_admin_notice( $message, WP::ADMIN_NOTICE_ERROR ); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 |