@@ -61,57 +61,181 @@ public function update_keys(): void {
6161 $ this ->api_key_mode = get_option ( 'monei_apikey_mode ' , 'test ' );
6262 }
6363
64- public function copyKeysToCentralSettings () {
65- add_filter ('option_woocommerce_monei_settings ' , array ($ this , 'processCentralSettings ' ), 10 ,1 );
66- }
64+ public function copyKeysToCentralSettings () {
65+ // First, check if we need any migration at all
66+ if ($ this ->needsMigration ()) {
67+ // Try standalone migration first (has priority)
68+ $ standaloneSuccess = $ this ->migrateStandaloneKeys ();
69+
70+ // Only bother with settings if standalone migration didn't complete everything
71+ if (!$ standaloneSuccess ) {
72+ $ existingSettings = get_option ('woocommerce_monei_settings ' , null );
73+ if ($ existingSettings !== null ) {
74+ add_filter ('option_woocommerce_monei_settings ' , array ($ this , 'processCentralSettings ' ), 10 , 1 );
75+ }
76+ }
77+ }
78+ }
79+
80+ /**
81+ * Check if any migration is needed
82+ *
83+ * @return bool True if migration is needed
84+ */
85+ private function needsMigration () {
86+ $ newTestApiKey = get_option ('monei_test_apikey ' , '' );
87+ $ newLiveApiKey = get_option ('monei_live_apikey ' , '' );
88+ $ newTestAccountId = get_option ('monei_test_accountid ' , '' );
89+ $ newLiveAccountId = get_option ('monei_live_accountid ' , '' );
90+
91+ // Get legacy keys
92+ $ legacyApiKey = get_option ('monei_apikey ' , '' );
93+ $ legacyAccountId = get_option ('monei_accountid ' , '' );
94+ $ existingSettings = get_option ('woocommerce_monei_settings ' , array ());
95+ $ settingsApiKey = $ existingSettings ['apikey ' ] ?? '' ;
96+ $ settingsAccountId = $ existingSettings ['accountid ' ] ?? '' ;
97+
98+ // Check if both new key sets are complete
99+ $ testKeysComplete = !empty ($ newTestApiKey ) && !empty ($ newTestAccountId );
100+ $ liveKeysComplete = !empty ($ newLiveApiKey ) && !empty ($ newLiveAccountId );
101+
102+ // If both are complete, no migration needed
103+ if ($ testKeysComplete && $ liveKeysComplete ) {
104+ return false ;
105+ }
106+
107+ // If we have any legacy keys or incomplete new keys, migration is needed
108+ return !empty ($ legacyApiKey ) || !empty ($ legacyAccountId ) ||
109+ !empty ($ settingsApiKey ) || !empty ($ settingsAccountId ) ||
110+ (!empty ($ newTestApiKey ) && empty ($ newTestAccountId )) ||
111+ (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId ));
112+ }
113+
67114 /**
68- * Process and migrate API keys between different storage locations
115+ * Migrate standalone legacy keys (works regardless of settings existence)
116+ *
117+ * @return bool True if migration was successful and complete, false if settings migration is still needed
118+ */
119+ private function migrateStandaloneKeys () {
120+ $ newTestApiKey = get_option ('monei_test_apikey ' , '' );
121+ $ newLiveApiKey = get_option ('monei_live_apikey ' , '' );
122+ $ newTestAccountId = get_option ('monei_test_accountid ' , '' );
123+ $ newLiveAccountId = get_option ('monei_live_accountid ' , '' );
124+ $ currentMode = get_option ('monei_apikey_mode ' , '' );
125+
126+ // Get legacy standalone keys
127+ $ legacyApiKey = get_option ('monei_apikey ' , '' );
128+ $ legacyAccountId = get_option ('monei_accountid ' , '' );
129+
130+ $ needsCleanup = false ;
131+ $ migratedFromStandalone = false ;
132+
133+ // Complete partial new keys using legacy standalone keys
134+ if (!empty ($ newTestApiKey ) && empty ($ newTestAccountId ) && !empty ($ legacyAccountId )) {
135+ update_option ('monei_test_accountid ' , $ legacyAccountId );
136+ $ needsCleanup = true ;
137+ $ migratedFromStandalone = true ;
138+ }
139+
140+ if (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId ) && !empty ($ legacyAccountId )) {
141+ update_option ('monei_live_accountid ' , $ legacyAccountId );
142+ $ needsCleanup = true ;
143+ $ migratedFromStandalone = true ;
144+ }
145+
146+ // Set mode based on existing new keys if mode is not set
147+ if (empty ($ currentMode )) {
148+ if (!empty ($ newTestApiKey )) {
149+ update_option ('monei_apikey_mode ' , 'test ' );
150+ } elseif (!empty ($ newLiveApiKey )) {
151+ update_option ('monei_apikey_mode ' , 'live ' );
152+ }
153+ }
154+
155+ // Full migration from legacy standalone keys if no new keys exist
156+ if (empty ($ newTestApiKey ) && empty ($ newLiveApiKey ) && !empty ($ legacyApiKey )) {
157+ if (strpos ($ legacyApiKey , 'pk_test_ ' ) === 0 ) {
158+ update_option ('monei_test_apikey ' , $ legacyApiKey );
159+ if (!empty ($ legacyAccountId )) {
160+ update_option ('monei_test_accountid ' , $ legacyAccountId );
161+ }
162+ if (empty ($ currentMode )) {
163+ update_option ('monei_apikey_mode ' , 'test ' );
164+ }
165+ $ needsCleanup = true ;
166+ $ migratedFromStandalone = true ;
167+
168+ } elseif (strpos ($ legacyApiKey , 'pk_live_ ' ) === 0 ) {
169+ update_option ('monei_live_apikey ' , $ legacyApiKey );
170+ if (!empty ($ legacyAccountId )) {
171+ update_option ('monei_live_accountid ' , $ legacyAccountId );
172+ }
173+ if (empty ($ currentMode )) {
174+ update_option ('monei_apikey_mode ' , 'live ' );
175+ }
176+ $ needsCleanup = true ;
177+ $ migratedFromStandalone = true ;
178+ }
179+ }
180+
181+ // Clean up legacy standalone keys if we migrated
182+ if ($ needsCleanup ) {
183+ delete_option ('monei_apikey ' );
184+ delete_option ('monei_accountid ' );
185+ }
186+
187+ // Check if migration is now complete (both sets of keys exist OR we successfully migrated what we had)
188+ $ newTestApiKeyAfter = get_option ('monei_test_apikey ' , '' );
189+ $ newLiveApiKeyAfter = get_option ('monei_live_apikey ' , '' );
190+ $ newTestAccountIdAfter = get_option ('monei_test_accountid ' , '' );
191+ $ newLiveAccountIdAfter = get_option ('monei_live_accountid ' , '' );
192+
193+ $ testKeysComplete = !empty ($ newTestApiKeyAfter ) && !empty ($ newTestAccountIdAfter );
194+ $ liveKeysComplete = !empty ($ newLiveApiKeyAfter ) && !empty ($ newLiveAccountIdAfter );
195+
196+ // Return true if we have at least one complete set OR if we migrated anything from standalone
197+ // (meaning settings keys are irrelevant since standalone has priority)
198+ return ($ testKeysComplete || $ liveKeysComplete ) || $ migratedFromStandalone ;
199+ }
200+
201+ /**
202+ * Process and migrate API keys from settings (only called via filter)
69203 *
70204 * @param array $default_params The settings array from the filter
71205 * @return array The processed settings array
72206 */
73- public function processCentralSettings ( $ default_params ) {
74- $ newTestApiKey = get_option ( 'monei_test_apikey ' , '' );
75- $ newLiveApiKey = get_option ( 'monei_live_apikey ' , '' );
76- $ newTestAccountId = get_option ( 'monei_test_accountid ' , '' );
77- $ newLiveAccountId = get_option ( 'monei_live_accountid ' , '' );
78- $ currentMode = get_option ( 'monei_apikey_mode ' , '' );
207+ public function processCentralSettings ( $ default_params) {
208+ $ newTestApiKey = get_option ('monei_test_apikey ' , '' );
209+ $ newLiveApiKey = get_option ('monei_live_apikey ' , '' );
210+ $ newTestAccountId = get_option ('monei_test_accountid ' , '' );
211+ $ newLiveAccountId = get_option ('monei_live_accountid ' , '' );
212+ $ currentMode = get_option ('monei_apikey_mode ' , '' );
79213
80- // Get legacy keys
81- $ legacyApiKey = get_option ( 'monei_apikey ' , '' );
82- $ legacyAccountId = get_option ( 'monei_accountid ' , '' );
214+ // Get keys from settings
83215 $ settingsApiKey = $ default_params ['apikey ' ] ?? '' ;
84216 $ settingsAccountId = $ default_params ['accountid ' ] ?? '' ;
85217
86- // priority: legacy standalone > settings
87- $ sourceApiKey = !empty ($ legacyApiKey ) ? $ legacyApiKey : $ settingsApiKey ;
88- $ sourceAccountId = !empty ($ legacyAccountId ) ? $ legacyAccountId : $ settingsAccountId ;
89-
90- $ needsMigration = false ;
218+ $ needsCleanup = false ;
91219 $ testKeysComplete = !empty ($ newTestApiKey ) && !empty ($ newTestAccountId );
92220 $ liveKeysComplete = !empty ($ newLiveApiKey ) && !empty ($ newLiveAccountId );
93221
94- // Scenario 1: Both sets of new keys are complete
222+ // If both sets are complete, just clean up settings and return
95223 if ($ testKeysComplete && $ liveKeysComplete ) {
96224 if (empty ($ currentMode )) {
97- update_option ('monei_apikey_mode ' , 'test ' ); // Default to test if both exist
225+ update_option ('monei_apikey_mode ' , 'test ' );
98226 }
99227 return $ this ->cleanup_legacy_keys ($ default_params );
100228 }
101229
102- // Scenario 2 & 3: Partial new keys exist - try to complete them
103- if (!empty ($ newTestApiKey ) && empty ($ newTestAccountId )) {
104- if (!empty ($ sourceAccountId )) {
105- update_option ('monei_test_accountid ' , $ sourceAccountId );
106- $ needsMigration = true ;
107- }
230+ // Complete partial new keys using settings keys
231+ if (!empty ($ newTestApiKey ) && empty ($ newTestAccountId ) && !empty ($ settingsAccountId )) {
232+ update_option ('monei_test_accountid ' , $ settingsAccountId );
233+ $ needsCleanup = true ;
108234 }
109235
110- if (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId )) {
111- if (!empty ($ sourceAccountId )) {
112- update_option ('monei_live_accountid ' , $ sourceAccountId );
113- $ needsMigration = true ;
114- }
236+ if (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId ) && !empty ($ settingsAccountId )) {
237+ update_option ('monei_live_accountid ' , $ settingsAccountId );
238+ $ needsCleanup = true ;
115239 }
116240
117241 // Set mode based on existing new keys if mode is not set
@@ -123,51 +247,45 @@ public function processCentralSettings ( $default_params ) {
123247 }
124248 }
125249
126- // Scenario 4: No new keys exist, need full migration from legacy
127- if (empty ($ newTestApiKey ) && empty ($ newLiveApiKey ) && !empty ($ sourceApiKey )) {
128- if (strpos ($ sourceApiKey , 'pk_test_ ' ) === 0 ) {
129- // Migrate to test keys
130- update_option ('monei_test_apikey ' , $ sourceApiKey );
131- if (!empty ($ sourceAccountId )) {
132- update_option ('monei_test_accountid ' , $ sourceAccountId );
250+ // Full migration from settings keys if no new keys exist
251+ if (empty ($ newTestApiKey ) && empty ($ newLiveApiKey ) && !empty ($ settingsApiKey )) {
252+ if (strpos ($ settingsApiKey , 'pk_test_ ' ) === 0 ) {
253+ update_option ('monei_test_apikey ' , $ settingsApiKey );
254+ if (!empty ($ settingsAccountId )) {
255+ update_option ('monei_test_accountid ' , $ settingsAccountId );
133256 }
134257 if (empty ($ currentMode )) {
135258 update_option ('monei_apikey_mode ' , 'test ' );
136259 }
137- $ needsMigration = true ;
260+ $ needsCleanup = true ;
138261
139- } elseif (strpos ($ sourceApiKey , 'pk_live_ ' ) === 0 ) {
140- // Migrate to live keys
141- update_option ('monei_live_apikey ' , $ sourceApiKey );
142- if (!empty ($ sourceAccountId )) {
143- update_option ('monei_live_accountid ' , $ sourceAccountId );
262+ } elseif (strpos ($ settingsApiKey , 'pk_live_ ' ) === 0 ) {
263+ update_option ('monei_live_apikey ' , $ settingsApiKey );
264+ if (!empty ($ settingsAccountId )) {
265+ update_option ('monei_live_accountid ' , $ settingsAccountId );
144266 }
145267 if (empty ($ currentMode )) {
146268 update_option ('monei_apikey_mode ' , 'live ' );
147269 }
148- $ needsMigration = true ;
270+ $ needsCleanup = true ;
149271 }
150272 }
151273
152- // Clean up legacy keys if we did any migration
153- if ($ needsMigration ) {
274+ // Clean up legacy keys from settings if we did any migration
275+ if ($ needsCleanup ) {
154276 $ default_params = $ this ->cleanup_legacy_keys ($ default_params );
155277 }
156278
157279 return $ default_params ;
158280 }
281+
159282 /**
160- * Clean up legacy API keys from both standalone options and settings array.
161- *
162- * @param array $settings_array The settings array to clean
163- * @return array The cleaned settings array
164- */
283+ * Clean up legacy keys from settings array
284+ *
285+ * @param array $settings_array The settings array
286+ * @return array The cleaned settings array
287+ */
165288 private function cleanup_legacy_keys ($ settings_array ) {
166- // Remove legacy standalone options
167- delete_option ('monei_apikey ' );
168- delete_option ('monei_accountid ' );
169-
170- // Remove legacy keys from settings array (which will be returned by the filter)
171289 if (isset ($ settings_array ['apikey ' ])) {
172290 unset($ settings_array ['apikey ' ]);
173291 }
0 commit comments