| 1 |
<?php |
| 2 |
|
| 3 |
namespace Enable\Cors; |
| 4 |
|
| 5 |
/* |
| 6 |
|-------------------------------------------------------------------------- |
| 7 |
| If this file is called directly, abort. |
| 8 |
|-------------------------------------------------------------------------- |
| 9 |
*/ |
| 10 |
if ( ! defined( 'Enable\Cors\SLUG' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
use Enable\Cors\Helpers\Option; |
| 15 |
use const Enable\Cors\VERSION; |
| 16 |
|
| 17 |
class Upgrade { |
| 18 |
|
| 19 |
/** |
| 20 |
* Handles plugin upgrade routines based on version comparisons. |
| 21 |
* |
| 22 |
* @since 1.2.4 |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public static function run() { |
| 26 |
$option = new Option(); |
| 27 |
$current_version = $option->get_version(); |
| 28 |
if ( ! version_compare( $current_version, self::get_version(), '<' ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
// Run upgrade routines based on version comparison. |
| 32 |
self::upgrade( $current_version ); |
| 33 |
// Update current version. |
| 34 |
$option->update_version(); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Upgrade routines for latest version. |
| 40 |
* |
| 41 |
* @since 1.2.4 |
| 42 |
* |
| 43 |
* @param string $current_version The current version of the plugin. |
| 44 |
*/ |
| 45 |
private static function upgrade( string $current_version ): void { |
| 46 |
if ( version_compare( $current_version, '1.2.4', '<' ) ) { |
| 47 |
$option = new Option(); |
| 48 |
$data = $option->get(); |
| 49 |
// Rename keys. |
| 50 |
$data['allow_font'] = $data['allowFont'] ?? false; |
| 51 |
unset( $data['allowFont'] ); |
| 52 |
$data['allow_image'] = $data['allowImage'] ?? false; |
| 53 |
unset( $data['allowImage'] ); |
| 54 |
$data['allow_credentials'] = $data['allowCredentials'] ?? false; |
| 55 |
unset( $data['allowCredentials'] ); |
| 56 |
$data['allowed_for'] = $data['allowedFor'] ?? array(); |
| 57 |
unset( $data['allowedFor'] ); |
| 58 |
$data['allowed_methods'] = $data['allowedMethods'] ?? array(); |
| 59 |
unset( $data['allowedMethods'] ); |
| 60 |
$data['allowed_header'] = $data['allowedHeader'] ?? array(); |
| 61 |
unset( $data['allowedHeader'] ); |
| 62 |
$option->save( $data ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Retrieve the current plugin version. |
| 68 |
* |
| 69 |
* @return string The current plugin version. |
| 70 |
* @since 1.2.4 |
| 71 |
*/ |
| 72 |
private static function get_version(): string { |
| 73 |
// Define or retrieve the plugin version dynamically. |
| 74 |
return defined( 'Enable\Cors\VERSION' ) ? VERSION : '1.2.3'; // Fallback to last version. |
| 75 |
} |
| 76 |
} |
| 77 |
|