@@ -62,97 +62,100 @@ public function update_keys(): void {
6262 }
6363
6464 public function copyKeysToCentralSettings () {
65- add_filter (
66- 'option_woocommerce_monei_settings ' ,
67- function ( $ default_params ) {
68- $ newTestApiKey = get_option ( 'monei_test_apikey ' , '' );
69- $ newLiveApiKey = get_option ( 'monei_live_apikey ' , '' );
70- $ newTestAccountId = get_option ( 'monei_test_accountid ' , '' );
71- $ newLiveAccountId = get_option ( 'monei_live_accountid ' , '' );
72- $ currentMode = get_option ( 'monei_apikey_mode ' , '' );
73-
74- // Get legacy keys
75- $ legacyApiKey = get_option ( 'monei_apikey ' , '' );
76- $ legacyAccountId = get_option ( 'monei_accountid ' , '' );
77- $ settingsApiKey = $ default_params ['apikey ' ] ?? '' ;
78- $ settingsAccountId = $ default_params ['accountid ' ] ?? '' ;
79-
80- // priority: legacy standalone > settings
81- $ sourceApiKey = !empty ($ legacyApiKey ) ? $ legacyApiKey : $ settingsApiKey ;
82- $ sourceAccountId = !empty ($ legacyAccountId ) ? $ legacyAccountId : $ settingsAccountId ;
83-
84- $ needsMigration = false ;
85- $ testKeysComplete = !empty ($ newTestApiKey ) && !empty ($ newTestAccountId );
86- $ liveKeysComplete = !empty ($ newLiveApiKey ) && !empty ($ newLiveAccountId );
87-
88- // Scenario 1: Both sets of new keys are complete
89- if ($ testKeysComplete && $ liveKeysComplete ) {
90- if (empty ($ currentMode )) {
91- update_option ('monei_apikey_mode ' , 'test ' ); // Default to test if both exist
92- }
93- return $ this ->cleanup_legacy_keys ($ default_params );
94- }
65+ add_filter ('option_woocommerce_monei_settings ' , array ($ this , 'processCentralSettings ' ), 10 ,1 );
66+ }
67+ /**
68+ * Process and migrate API keys between different storage locations
69+ *
70+ * @param array $default_params The settings array from the filter
71+ * @return array The processed settings array
72+ */
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 ' , '' );
79+
80+ // Get legacy keys
81+ $ legacyApiKey = get_option ( 'monei_apikey ' , '' );
82+ $ legacyAccountId = get_option ( 'monei_accountid ' , '' );
83+ $ settingsApiKey = $ default_params ['apikey ' ] ?? '' ;
84+ $ settingsAccountId = $ default_params ['accountid ' ] ?? '' ;
85+
86+ // priority: legacy standalone > settings
87+ $ sourceApiKey = !empty ($ legacyApiKey ) ? $ legacyApiKey : $ settingsApiKey ;
88+ $ sourceAccountId = !empty ($ legacyAccountId ) ? $ legacyAccountId : $ settingsAccountId ;
89+
90+ $ needsMigration = false ;
91+ $ testKeysComplete = !empty ($ newTestApiKey ) && !empty ($ newTestAccountId );
92+ $ liveKeysComplete = !empty ($ newLiveApiKey ) && !empty ($ newLiveAccountId );
93+
94+ // Scenario 1: Both sets of new keys are complete
95+ if ($ testKeysComplete && $ liveKeysComplete ) {
96+ if (empty ($ currentMode )) {
97+ update_option ('monei_apikey_mode ' , 'test ' ); // Default to test if both exist
98+ }
99+ return $ this ->cleanup_legacy_keys ($ default_params );
100+ }
95101
96- // Scenario 2 & 3: Partial new keys exist - try to complete them
97- if (!empty ($ newTestApiKey ) && empty ($ newTestAccountId )) {
98- if (!empty ($ sourceAccountId )) {
99- update_option ('monei_test_accountid ' , $ sourceAccountId );
100- $ needsMigration = true ;
101- }
102- }
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+ }
108+ }
103109
104- if (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId )) {
105- if (!empty ($ sourceAccountId )) {
106- update_option ('monei_live_accountid ' , $ sourceAccountId );
107- $ needsMigration = true ;
108- }
109- }
110+ if (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId )) {
111+ if (!empty ($ sourceAccountId )) {
112+ update_option ('monei_live_accountid ' , $ sourceAccountId );
113+ $ needsMigration = true ;
114+ }
115+ }
116+
117+ // Set mode based on existing new keys if mode is not set
118+ if (empty ($ currentMode )) {
119+ if (!empty ($ newTestApiKey )) {
120+ update_option ('monei_apikey_mode ' , 'test ' );
121+ } elseif (!empty ($ newLiveApiKey )) {
122+ update_option ('monei_apikey_mode ' , 'live ' );
123+ }
124+ }
110125
111- // Set mode based on existing new keys if mode is not set
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 );
133+ }
112134 if (empty ($ currentMode )) {
113- if (!empty ($ newTestApiKey )) {
114- update_option ('monei_apikey_mode ' , 'test ' );
115- } elseif (!empty ($ newLiveApiKey )) {
116- update_option ('monei_apikey_mode ' , 'live ' );
117- }
135+ update_option ('monei_apikey_mode ' , 'test ' );
118136 }
137+ $ needsMigration = true ;
119138
120- // Scenario 4: No new keys exist, need full migration from legacy
121- if (empty ($ newTestApiKey ) && empty ($ newLiveApiKey ) && !empty ($ sourceApiKey )) {
122- if (strpos ($ sourceApiKey , 'pk_test_ ' ) === 0 ) {
123- // Migrate to test keys
124- update_option ('monei_test_apikey ' , $ sourceApiKey );
125- if (!empty ($ sourceAccountId )) {
126- update_option ('monei_test_accountid ' , $ sourceAccountId );
127- }
128- if (empty ($ currentMode )) {
129- update_option ('monei_apikey_mode ' , 'test ' );
130- }
131- $ needsMigration = true ;
132-
133- } elseif (strpos ($ sourceApiKey , 'pk_live_ ' ) === 0 ) {
134- // Migrate to live keys
135- update_option ('monei_live_apikey ' , $ sourceApiKey );
136- if (!empty ($ sourceAccountId )) {
137- update_option ('monei_live_accountid ' , $ sourceAccountId );
138- }
139- if (empty ($ currentMode )) {
140- update_option ('monei_apikey_mode ' , 'live ' );
141- }
142- $ needsMigration = true ;
143- }
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 );
144144 }
145-
146- // Clean up legacy keys if we did any migration
147- if ($ needsMigration ) {
148- $ default_params = $ this ->cleanup_legacy_keys ($ default_params );
145+ if (empty ($ currentMode )) {
146+ update_option ('monei_apikey_mode ' , 'live ' );
149147 }
148+ $ needsMigration = true ;
149+ }
150+ }
150151
151- return $ default_params ;
152- },
153- 10
154- );
155- }
152+ // Clean up legacy keys if we did any migration
153+ if ($ needsMigration ) {
154+ $ default_params = $ this ->cleanup_legacy_keys ($ default_params );
155+ }
156+
157+ return $ default_params ;
158+ }
156159 /**
157160 * Clean up legacy API keys from both standalone options and settings array.
158161 *
0 commit comments