@@ -62,10 +62,13 @@ public function update_keys(): void {
6262 }
6363
6464 public function copyKeysToCentralSettings () {
65+ // Get the current state once
66+ $ keyState = $ this ->getCurrentKeyState ();
67+
6568 // First, check if we need any migration at all
66- if ($ this ->needsMigration ()) {
69+ if ($ this ->needsMigration ($ keyState )) {
6770 // Try standalone migration first (has priority)
68- $ standaloneSuccess = $ this ->migrateStandaloneKeys ();
71+ $ standaloneSuccess = $ this ->migrateStandaloneKeys ($ keyState );
6972
7073 // Only bother with settings if standalone migration didn't complete everything
7174 if (!$ standaloneSuccess ) {
@@ -74,17 +77,28 @@ public function copyKeysToCentralSettings() {
7477 }
7578 }
7679
80+ /**
81+ * Get current state of all keys
82+ *
83+ * @return array Current key state
84+ */
85+ private function getCurrentKeyState () {
86+ return array (
87+ 'test_api_key ' => get_option ('monei_test_apikey ' , '' ),
88+ 'live_api_key ' => get_option ('monei_live_apikey ' , '' ),
89+ 'test_account_id ' => get_option ('monei_test_accountid ' , '' ),
90+ 'live_account_id ' => get_option ('monei_live_accountid ' , '' ),
91+ 'current_mode ' => get_option ('monei_apikey_mode ' , '' ),
92+ );
93+ }
94+
7795 /**
7896 * Check if any migration is needed
7997 *
98+ * @param array $keyState Current key state
8099 * @return bool True if migration is needed
81100 */
82- private function needsMigration () {
83- $ newTestApiKey = get_option ('monei_test_apikey ' , '' );
84- $ newLiveApiKey = get_option ('monei_live_apikey ' , '' );
85- $ newTestAccountId = get_option ('monei_test_accountid ' , '' );
86- $ newLiveAccountId = get_option ('monei_live_accountid ' , '' );
87-
101+ private function needsMigration ($ keyState ) {
88102 // Get legacy keys
89103 $ legacyApiKey = get_option ('monei_apikey ' , '' );
90104 $ legacyAccountId = get_option ('monei_accountid ' , '' );
@@ -93,8 +107,8 @@ private function needsMigration() {
93107 $ settingsAccountId = $ existingSettings ['accountid ' ] ?? '' ;
94108
95109 // Check if both new key sets are complete
96- $ testKeysComplete = !empty ($ newTestApiKey ) && !empty ($ newTestAccountId );
97- $ liveKeysComplete = !empty ($ newLiveApiKey ) && !empty ($ newLiveAccountId );
110+ $ testKeysComplete = !empty ($ keyState [ ' test_api_key ' ] ) && !empty ($ keyState [ ' test_account_id ' ] );
111+ $ liveKeysComplete = !empty ($ keyState [ ' live_api_key ' ] ) && !empty ($ keyState [ ' live_account_id ' ] );
98112
99113 // If both are complete, no migration needed
100114 if ($ testKeysComplete && $ liveKeysComplete ) {
@@ -104,22 +118,17 @@ private function needsMigration() {
104118 // If we have any legacy keys or incomplete new keys, migration is needed
105119 return !empty ($ legacyApiKey ) || !empty ($ legacyAccountId ) ||
106120 !empty ($ settingsApiKey ) || !empty ($ settingsAccountId ) ||
107- (!empty ($ newTestApiKey ) && empty ($ newTestAccountId )) ||
108- (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId ));
121+ (!empty ($ keyState [ ' test_api_key ' ] ) && empty ($ keyState [ ' test_account_id ' ] )) ||
122+ (!empty ($ keyState [ ' live_api_key ' ] ) && empty ($ keyState [ ' live_account_id ' ] ));
109123 }
110124
111125 /**
112126 * Migrate standalone legacy keys (works regardless of settings existence)
113127 *
128+ * @param array $keyState Current key state
114129 * @return bool True if migration was successful and complete, false if settings migration is still needed
115130 */
116- private function migrateStandaloneKeys () {
117- $ newTestApiKey = get_option ('monei_test_apikey ' , '' );
118- $ newLiveApiKey = get_option ('monei_live_apikey ' , '' );
119- $ newTestAccountId = get_option ('monei_test_accountid ' , '' );
120- $ newLiveAccountId = get_option ('monei_live_accountid ' , '' );
121- $ currentMode = get_option ('monei_apikey_mode ' , '' );
122-
131+ private function migrateStandaloneKeys ($ keyState ) {
123132 // Get legacy standalone keys
124133 $ legacyApiKey = get_option ('monei_apikey ' , '' );
125134 $ legacyAccountId = get_option ('monei_accountid ' , '' );
@@ -128,48 +137,30 @@ private function migrateStandaloneKeys() {
128137 $ migratedFromStandalone = false ;
129138
130139 // Complete partial new keys using legacy standalone keys
131- if (!empty ($ newTestApiKey ) && empty ($ newTestAccountId ) && !empty ($ legacyAccountId )) {
140+ if (!empty ($ keyState [ ' test_api_key ' ] ) && empty ($ keyState [ ' test_account_id ' ] ) && !empty ($ legacyAccountId )) {
132141 update_option ('monei_test_accountid ' , $ legacyAccountId );
133142 $ needsCleanup = true ;
134143 $ migratedFromStandalone = true ;
135144 }
136145
137- if (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId ) && !empty ($ legacyAccountId )) {
146+ if (!empty ($ keyState [ ' live_api_key ' ] ) && empty ($ keyState [ ' live_account_id ' ] ) && !empty ($ legacyAccountId )) {
138147 update_option ('monei_live_accountid ' , $ legacyAccountId );
139148 $ needsCleanup = true ;
140149 $ migratedFromStandalone = true ;
141150 }
142151
143152 // Set mode based on existing new keys if mode is not set
144- if (empty ($ currentMode )) {
145- if (!empty ($ newTestApiKey )) {
153+ if (empty ($ keyState [ ' current_mode ' ] )) {
154+ if (!empty ($ keyState [ ' test_api_key ' ] )) {
146155 update_option ('monei_apikey_mode ' , 'test ' );
147- } elseif (!empty ($ newLiveApiKey )) {
156+ } elseif (!empty ($ keyState [ ' live_api_key ' ] )) {
148157 update_option ('monei_apikey_mode ' , 'live ' );
149158 }
150159 }
151160
152161 // Full migration from legacy standalone keys if no new keys exist
153- if (empty ($ newTestApiKey ) && empty ($ newLiveApiKey ) && !empty ($ legacyApiKey )) {
154- if (strpos ($ legacyApiKey , 'pk_test_ ' ) === 0 ) {
155- update_option ('monei_test_apikey ' , $ legacyApiKey );
156- if (!empty ($ legacyAccountId )) {
157- update_option ('monei_test_accountid ' , $ legacyAccountId );
158- }
159- if (empty ($ currentMode )) {
160- update_option ('monei_apikey_mode ' , 'test ' );
161- }
162- $ needsCleanup = true ;
163- $ migratedFromStandalone = true ;
164-
165- } elseif (strpos ($ legacyApiKey , 'pk_live_ ' ) === 0 ) {
166- update_option ('monei_live_apikey ' , $ legacyApiKey );
167- if (!empty ($ legacyAccountId )) {
168- update_option ('monei_live_accountid ' , $ legacyAccountId );
169- }
170- if (empty ($ currentMode )) {
171- update_option ('monei_apikey_mode ' , 'live ' );
172- }
162+ if (empty ($ keyState ['test_api_key ' ]) && empty ($ keyState ['live_api_key ' ]) && !empty ($ legacyApiKey )) {
163+ if ($ this ->migrateSingleKeySet ($ legacyApiKey , $ legacyAccountId , $ keyState ['current_mode ' ])) {
173164 $ needsCleanup = true ;
174165 $ migratedFromStandalone = true ;
175166 }
@@ -181,18 +172,12 @@ private function migrateStandaloneKeys() {
181172 delete_option ('monei_accountid ' );
182173 }
183174
184- // Check if migration is now complete (both sets of keys exist OR we successfully migrated what we had)
185- $ newTestApiKeyAfter = get_option ('monei_test_apikey ' , '' );
186- $ newLiveApiKeyAfter = get_option ('monei_live_apikey ' , '' );
187- $ newTestAccountIdAfter = get_option ('monei_test_accountid ' , '' );
188- $ newLiveAccountIdAfter = get_option ('monei_live_accountid ' , '' );
189-
190- $ testKeysComplete = !empty ($ newTestApiKeyAfter ) && !empty ($ newTestAccountIdAfter );
191- $ liveKeysComplete = !empty ($ newLiveApiKeyAfter ) && !empty ($ newLiveAccountIdAfter );
175+ // Return true if we migrated anything from standalone (has priority over settings)
176+ // or if we already had complete key sets
177+ $ initialTestKeysComplete = !empty ($ keyState ['test_api_key ' ]) && !empty ($ keyState ['test_account_id ' ]);
178+ $ initialLiveKeysComplete = !empty ($ keyState ['live_api_key ' ]) && !empty ($ keyState ['live_account_id ' ]);
192179
193- // Return true if we have at least one complete set OR if we migrated anything from standalone
194- // (meaning settings keys are irrelevant since standalone has priority)
195- return ($ testKeysComplete || $ liveKeysComplete ) || $ migratedFromStandalone ;
180+ return $ migratedFromStandalone || ($ initialTestKeysComplete || $ initialLiveKeysComplete );
196181 }
197182
198183 /**
@@ -246,24 +231,7 @@ public function processCentralSettings($default_params) {
246231
247232 // Full migration from settings keys if no new keys exist
248233 if (empty ($ newTestApiKey ) && empty ($ newLiveApiKey ) && !empty ($ settingsApiKey )) {
249- if (strpos ($ settingsApiKey , 'pk_test_ ' ) === 0 ) {
250- update_option ('monei_test_apikey ' , $ settingsApiKey );
251- if (!empty ($ settingsAccountId )) {
252- update_option ('monei_test_accountid ' , $ settingsAccountId );
253- }
254- if (empty ($ currentMode )) {
255- update_option ('monei_apikey_mode ' , 'test ' );
256- }
257- $ needsCleanup = true ;
258-
259- } elseif (strpos ($ settingsApiKey , 'pk_live_ ' ) === 0 ) {
260- update_option ('monei_live_apikey ' , $ settingsApiKey );
261- if (!empty ($ settingsAccountId )) {
262- update_option ('monei_live_accountid ' , $ settingsAccountId );
263- }
264- if (empty ($ currentMode )) {
265- update_option ('monei_apikey_mode ' , 'live ' );
266- }
234+ if ($ this ->migrateSingleKeySet ($ settingsApiKey , $ settingsAccountId , $ currentMode )) {
267235 $ needsCleanup = true ;
268236 }
269237 }
@@ -276,6 +244,37 @@ public function processCentralSettings($default_params) {
276244 return $ default_params ;
277245 }
278246
247+ /**
248+ * Migrate a single key set based on key prefix
249+ *
250+ * @param string $apiKey The API key to migrate
251+ * @param string $accountId The account ID to migrate
252+ * @param string $currentMode Current mode setting
253+ * @return bool True if migration occurred
254+ */
255+ private function migrateSingleKeySet ($ apiKey , $ accountId , $ currentMode ) {
256+ if (strpos ($ apiKey , 'pk_test_ ' ) === 0 ) {
257+ update_option ('monei_test_apikey ' , $ apiKey );
258+ if (!empty ($ accountId )) {
259+ update_option ('monei_test_accountid ' , $ accountId );
260+ }
261+ if (empty ($ currentMode )) {
262+ update_option ('monei_apikey_mode ' , 'test ' );
263+ }
264+ return true ;
265+ } elseif (strpos ($ apiKey , 'pk_live_ ' ) === 0 ) {
266+ update_option ('monei_live_apikey ' , $ apiKey );
267+ if (!empty ($ accountId )) {
268+ update_option ('monei_live_accountid ' , $ accountId );
269+ }
270+ if (empty ($ currentMode )) {
271+ update_option ('monei_apikey_mode ' , 'live ' );
272+ }
273+ return true ;
274+ }
275+ return false ;
276+ }
277+
279278 /**
280279 * Clean up legacy keys from settings array
281280 *
0 commit comments