| 1 |
<?php |
| 2 |
/** |
| 3 |
* Version Checker for Easy Invoice |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @subpackage Migration |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace EasyInvoice\Migration; |
| 11 |
|
| 12 |
/** |
| 13 |
* Version Checker Class |
| 14 |
* |
| 15 |
* Handles version checking and comparison. |
| 16 |
* |
| 17 |
* @since 2.0.0 |
| 18 |
*/ |
| 19 |
class VersionChecker { |
| 20 |
|
| 21 |
/** |
| 22 |
* Target version. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $target_version = '2.0.0'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Singleton instance. |
| 31 |
* |
| 32 |
* @since 2.0.0 |
| 33 |
* @var VersionChecker |
| 34 |
*/ |
| 35 |
private static $instance = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Get singleton instance. |
| 39 |
* |
| 40 |
* @since 2.0.0 |
| 41 |
* @return VersionChecker |
| 42 |
*/ |
| 43 |
public static function get_instance(): VersionChecker { |
| 44 |
if (self::$instance === null) { |
| 45 |
self::$instance = new self(); |
| 46 |
} |
| 47 |
return self::$instance; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get stored version. |
| 52 |
* |
| 53 |
* @since 2.0.0 |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public function get_stored_version(): string { |
| 57 |
// First check the new option name |
| 58 |
$version = get_option('easy_invoice_version'); |
| 59 |
if ($version !== false) { |
| 60 |
return $this->sanitize_version($version); |
| 61 |
} |
| 62 |
|
| 63 |
// Check old option name |
| 64 |
$version = get_option('easy_invoice_db_version'); |
| 65 |
if ($version !== false) { |
| 66 |
return $this->sanitize_version($version); |
| 67 |
} |
| 68 |
|
| 69 |
// Check if old plugin exists |
| 70 |
if ($this->old_plugin_exists()) { |
| 71 |
return '1.0.0'; |
| 72 |
} |
| 73 |
|
| 74 |
// Default to current version if no old version found |
| 75 |
return $this->target_version; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get target version. |
| 80 |
* |
| 81 |
* @since 2.0.0 |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
public function get_target_version(): string { |
| 85 |
return $this->target_version; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Check if migration is needed. |
| 90 |
* |
| 91 |
* @since 2.0.0 |
| 92 |
* @return bool |
| 93 |
*/ |
| 94 |
public function is_migration_needed(): bool { |
| 95 |
// If migration was skipped, don't show it again |
| 96 |
if (get_option('easy_invoice_migration_skipped', false)) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
// If migration is completed, no need to migrate |
| 101 |
if ($this->is_migration_completed()) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
// Check if old plugin exists |
| 106 |
if ($this->old_plugin_exists()) { |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
// Check version comparison |
| 111 |
$current_version = $this->get_stored_version(); |
| 112 |
|
| 113 |
// If current version is already 2.0.0 or higher and no old data exists, |
| 114 |
// this is likely a fresh installation, so no migration needed |
| 115 |
if (version_compare($current_version, $this->target_version, '>=')) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
return version_compare($current_version, $this->target_version, '<'); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Check if migration is completed. |
| 124 |
* |
| 125 |
* @since 2.0.0 |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
public function is_migration_completed(): bool { |
| 129 |
// Check if migration completed flag is set |
| 130 |
$is_completed = get_option('easy_invoice_migration_completed', false); |
| 131 |
|
| 132 |
// Also verify version is up to date |
| 133 |
if ($is_completed) { |
| 134 |
$current_version = $this->get_stored_version(); |
| 135 |
return version_compare($current_version, $this->target_version, '>='); |
| 136 |
} |
| 137 |
|
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check if migration is running. |
| 143 |
* |
| 144 |
* @since 2.0.0 |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
public function is_migration_running(): bool { |
| 148 |
return get_option('easy_invoice_migration_running', false); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get migration status. |
| 153 |
* |
| 154 |
* @since 2.0.0 |
| 155 |
* @return string Status: 'needed', 'running', 'completed', or 'not_needed' |
| 156 |
*/ |
| 157 |
public function get_migration_status(): string { |
| 158 |
if ($this->is_migration_running()) { |
| 159 |
return 'running'; |
| 160 |
} |
| 161 |
|
| 162 |
if ($this->is_migration_completed()) { |
| 163 |
return 'completed'; |
| 164 |
} |
| 165 |
|
| 166 |
if ($this->is_migration_needed()) { |
| 167 |
return 'needed'; |
| 168 |
} |
| 169 |
|
| 170 |
return 'not_needed'; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Check if old plugin exists. |
| 175 |
* |
| 176 |
* @since 2.0.0 |
| 177 |
* @return bool |
| 178 |
*/ |
| 179 |
private function old_plugin_exists(): bool { |
| 180 |
global $wpdb; |
| 181 |
|
| 182 |
// Check if old post types exist |
| 183 |
$old_post_types = ['easy-invoice', 'easy-invoice-quotes', 'easy-invoice-payment']; |
| 184 |
foreach ($old_post_types as $post_type) { |
| 185 |
$count = $wpdb->get_var( |
| 186 |
$wpdb->prepare( |
| 187 |
"SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s LIMIT 1", |
| 188 |
$post_type |
| 189 |
) |
| 190 |
); |
| 191 |
|
| 192 |
if ($count > 0) { |
| 193 |
return true; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
// Check if old options exist |
| 198 |
$old_options = [ |
| 199 |
'easy_invoice_business_name', |
| 200 |
'easy_invoice_business_address', |
| 201 |
'easy_invoice_currency', |
| 202 |
'easy_invoice_tax_rate', |
| 203 |
'easy_invoice_invoice_number', |
| 204 |
'easy_invoice_quote_number' |
| 205 |
]; |
| 206 |
|
| 207 |
foreach ($old_options as $option) { |
| 208 |
if (get_option($option) !== false) { |
| 209 |
return true; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Mark migration as completed for fresh installations. |
| 218 |
* |
| 219 |
* @since 2.0.0 |
| 220 |
* @return void |
| 221 |
*/ |
| 222 |
public function mark_fresh_installation_completed(): void { |
| 223 |
update_option('easy_invoice_version', $this->target_version); |
| 224 |
update_option('easy_invoice_migration_completed', true); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Check if this is a fresh installation (no old data and version is current). |
| 229 |
* |
| 230 |
* @since 2.0.0 |
| 231 |
* @return bool |
| 232 |
*/ |
| 233 |
public function is_fresh_installation(): bool { |
| 234 |
// If migration is already completed, it's not a fresh installation |
| 235 |
if ($this->is_migration_completed()) { |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
// If old plugin data exists, it's not a fresh installation |
| 240 |
if ($this->old_plugin_exists()) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
// Check if current version is already up to date |
| 245 |
$current_version = $this->get_stored_version(); |
| 246 |
return version_compare($current_version, $this->target_version, '>='); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Sanitize version string. |
| 253 |
* |
| 254 |
* @since 2.0.0 |
| 255 |
* @param string $version Version string |
| 256 |
* @return string |
| 257 |
*/ |
| 258 |
private function sanitize_version(string $version): string { |
| 259 |
// Remove any non-version characters |
| 260 |
$version = preg_replace('/[^0-9.]/', '', $version); |
| 261 |
|
| 262 |
// Ensure at least major.minor.patch format |
| 263 |
$parts = explode('.', $version); |
| 264 |
while (count($parts) < 3) { |
| 265 |
$parts[] = '0'; |
| 266 |
} |
| 267 |
|
| 268 |
return implode('.', array_slice($parts, 0, 3)); |
| 269 |
} |
| 270 |
} |