Skip to content

Commit 131f7f8

Browse files
committed
Delete old key options
1 parent 0432ba0 commit 131f7f8

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

src/Services/ApiKeyService.php

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ class ApiKeyService {
77
private string $live_api_key;
88
private string $api_key_mode;
99
private string $test_account_id;
10-
private string $live_account_id;
10+
private string $live_account_id;
1111

1212

13-
public function __construct() {
13+
public function __construct() {
1414
// Load the API keys and mode from the database
15-
$this->test_api_key = get_option( 'monei_test_apikey', '' );
16-
$this->live_api_key = get_option( 'monei_live_apikey', '' );
17-
$this->api_key_mode = get_option( 'monei_apikey_mode', 'test' );
18-
$this->test_account_id = get_option( 'monei_test_accountid', '' );
19-
$this->live_account_id = get_option( 'monei_live_accountid', '' );
15+
$this->test_api_key = get_option( 'monei_test_apikey', '' );
16+
$this->live_api_key = get_option( 'monei_live_apikey', '' );
17+
$this->api_key_mode = get_option( 'monei_apikey_mode', 'test' );
18+
$this->test_account_id = get_option( 'monei_test_accountid', '' );
19+
$this->live_account_id = get_option( 'monei_live_accountid', '' );
2020

21-
// Copy the API keys to the central settings when the plugin is activated or updated
21+
// Copy the API keys to the central settings when the plugin is activated or updated
2222
add_action( 'init', array( $this, 'copyKeysToCentralSettings' ), 0 );
2323
}
2424

@@ -54,23 +54,23 @@ public function get_account_id(): string {
5454
* This can be called whenever the database is updated.
5555
*/
5656
public function update_keys(): void {
57-
$this->test_api_key = get_option( 'monei_test_apikey', '' );
58-
$this->live_api_key = get_option( 'monei_live_apikey', '' );
59-
$this->test_account_id = get_option( 'monei_test_accountid', '' );
60-
$this->live_account_id = get_option( 'monei_live_accountid', '' );
61-
$this->api_key_mode = get_option( 'monei_apikey_mode', 'test' );
57+
$this->test_api_key = get_option( 'monei_test_apikey', '' );
58+
$this->live_api_key = get_option( 'monei_live_apikey', '' );
59+
$this->test_account_id = get_option( 'monei_test_accountid', '' );
60+
$this->live_account_id = get_option( 'monei_live_accountid', '' );
61+
$this->api_key_mode = get_option( 'monei_apikey_mode', 'test' );
6262
}
6363

6464
public function copyKeysToCentralSettings() {
6565
add_filter(
6666
'option_woocommerce_monei_settings',
6767
function ( $default_params ) {
68-
$newCentralTestApiKey = get_option( 'monei_test_apikey', '' );
69-
$newCentralLiveApiKey = get_option( 'monei_live_apikey', '' );
70-
//we already saved the new keys, so we don't need to do anything more here.'
71-
if(! empty( $newCentralTestApiKey ) ||! empty( $newCentralLiveApiKey ) ) {
72-
return $default_params;
73-
}
68+
$newCentralTestApiKey = get_option( 'monei_test_apikey', '' );
69+
$newCentralLiveApiKey = get_option( 'monei_live_apikey', '' );
70+
//we already saved the new keys, so we don't need to do anything more here.'
71+
if ( ! empty( $newCentralTestApiKey ) || ! empty( $newCentralLiveApiKey ) ) {
72+
return $default_params;
73+
}
7474
$centralApiKey = get_option( 'monei_apikey', '' );
7575
$centralAccountId = get_option( 'monei_accountid', '' );
7676
$ccApiKey = $default_params['apikey'] ?? '';
@@ -79,19 +79,25 @@ function ( $default_params ) {
7979
if ( empty( $centralApiKey ) && empty( $ccApiKey ) ) {
8080
return $default_params;
8181
}
82-
$keyToUse = ! empty( $centralApiKey ) ? $centralApiKey : $ccApiKey;
83-
$accountId =! empty( $centralAccountId )? $centralAccountId : $ccAccountId;
82+
$keyToUse = ! empty( $centralApiKey ) ? $centralApiKey : $ccApiKey;
83+
$accountId = ! empty( $centralAccountId ) ? $centralAccountId : $ccAccountId;
8484

85+
$settings = get_option( 'woocommerce_monei_settings', array() );
8586
if ( strpos( $keyToUse, 'pk_test_' ) === 0 ) {
8687
update_option( 'monei_test_apikey', $keyToUse );
8788
update_option( 'monei_apikey_mode', 'test' );
88-
update_option( 'monei_test_accountid', $accountId );
89-
} else if(strpos( $keyToUse, 'pk_live_' ) === 0) {
89+
update_option( 'monei_test_accountid', $accountId );
90+
91+
} elseif ( strpos( $keyToUse, 'pk_live_' ) === 0 ) {
9092
update_option( 'monei_live_apikey', $keyToUse );
9193
update_option( 'monei_apikey_mode', 'live' );
92-
update_option( 'monei_live_accountid', $accountId );
93-
}
94-
94+
update_option( 'monei_live_accountid', $accountId );
95+
}
96+
delete_option( 'monei_apikey' );
97+
delete_option( 'monei_accountid' );
98+
unset( $settings['accountid'] );
99+
unset( $settings['apikey'] );
100+
update_option( 'woocommerce_monei_settings', $settings );
95101
return $default_params;
96102
},
97103
1

0 commit comments

Comments
 (0)