| 1 |
<?php |
| 2 |
/** |
| 3 |
* Database upgrade functions. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Updates; |
| 9 |
|
| 10 |
add_action( 'plugins_loaded', __NAMESPACE__ . '\\upgrade_database' ); |
| 11 |
/** |
| 12 |
* Migrates the plugin's stored data to the latest format. |
| 13 |
* |
| 14 |
* @return bool True if updates were carried out or false. |
| 15 |
*/ |
| 16 |
function upgrade_database(): bool { |
| 17 |
$current_version = get_option( 'faustwp_current_version', '0.0.0' ); |
| 18 |
$file_data = get_file_data( FAUSTWP_FILE, array( 'Version' => 'Version' ) ); |
| 19 |
$plugin_version = $file_data['Version']; |
| 20 |
|
| 21 |
if ( 1 === version_compare( $plugin_version, $current_version ) ) { |
| 22 |
|
| 23 |
// Array of versions requiring update and their callbacks. |
| 24 |
// Note these do not have to exactly match plugin version. |
| 25 |
$update_versions = array( |
| 26 |
'0.6.1' => 'upgrade_0_6_1', |
| 27 |
); |
| 28 |
|
| 29 |
foreach ( $update_versions as $version => $callback ) { |
| 30 |
if ( 1 === version_compare( $version, $current_version ) ) { |
| 31 |
call_user_func( __NAMESPACE__ . '\\' . $callback ); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// Save the last updated version. |
| 36 |
update_option( 'faustwp_current_version', $plugin_version ); |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Update settings option name for versions after to 0.6.1. |
| 45 |
* |
| 46 |
* @return bool True if the database was updated or false. |
| 47 |
*/ |
| 48 |
function upgrade_0_6_1(): bool { |
| 49 |
$settings = get_option( 'wpe_headless', array() ); |
| 50 |
|
| 51 |
if ( empty( $settings ) ) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
delete_option( 'wpe_headless' ); |
| 56 |
|
| 57 |
return update_option( 'faustwp_settings', $settings ); |
| 58 |
} |
| 59 |
|