@@ -65,42 +65,108 @@ 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- }
74- $ centralApiKey = get_option ( 'monei_apikey ' , '' );
75- $ centralAccountId = get_option ( 'monei_accountid ' , '' );
76- $ ccApiKey = $ default_params ['apikey ' ] ?? '' ;
77- $ ccAccountId = $ default_params ['accountid ' ] ?? '' ;
78-
79- if ( empty ( $ centralApiKey ) && empty ( $ ccApiKey ) ) {
80- return $ default_params ;
81- }
82- $ keyToUse = ! empty ( $ centralApiKey ) ? $ centralApiKey : $ ccApiKey ;
83- $ accountId = ! empty ( $ centralAccountId ) ? $ centralAccountId : $ ccAccountId ;
84-
85- $ settings = get_option ( 'woocommerce_monei_settings ' , array () );
86- if ( strpos ( $ keyToUse , 'pk_test_ ' ) === 0 ) {
87- update_option ( 'monei_test_apikey ' , $ keyToUse );
88- update_option ( 'monei_apikey_mode ' , 'test ' );
89- update_option ( 'monei_test_accountid ' , $ accountId );
90-
91- } elseif ( strpos ( $ keyToUse , 'pk_live_ ' ) === 0 ) {
92- update_option ( 'monei_live_apikey ' , $ keyToUse );
93- update_option ( 'monei_apikey_mode ' , 'live ' );
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 );
101- return $ 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+ $ this ->cleanup_legacy_keys ($ default_params );
94+ return $ default_params ;
95+ }
96+
97+ // Scenario 2 & 3: Partial new keys exist - try to complete them
98+ if (!empty ($ newTestApiKey ) && empty ($ newTestAccountId )) {
99+ if (!empty ($ sourceAccountId )) {
100+ update_option ('monei_test_accountid ' , $ sourceAccountId );
101+ $ needsMigration = true ;
102+ }
103+ }
104+
105+ if (!empty ($ newLiveApiKey ) && empty ($ newLiveAccountId )) {
106+ if (!empty ($ sourceAccountId )) {
107+ update_option ('monei_live_accountid ' , $ sourceAccountId );
108+ $ needsMigration = true ;
109+ }
110+ }
111+
112+ // Set mode based on existing new keys if mode is not set
113+ if (empty ($ currentMode )) {
114+ if (!empty ($ newTestApiKey )) {
115+ update_option ('monei_apikey_mode ' , 'test ' );
116+ } elseif (!empty ($ newLiveApiKey )) {
117+ update_option ('monei_apikey_mode ' , 'live ' );
118+ }
119+ }
120+
121+ // Scenario 4: No new keys exist, need full migration from legacy
122+ if (empty ($ newTestApiKey ) && empty ($ newLiveApiKey ) && !empty ($ sourceApiKey )) {
123+ if (strpos ($ sourceApiKey , 'pk_test_ ' ) === 0 ) {
124+ // Migrate to test keys
125+ update_option ('monei_test_apikey ' , $ sourceApiKey );
126+ if (!empty ($ sourceAccountId )) {
127+ update_option ('monei_test_accountid ' , $ sourceAccountId );
128+ }
129+ if (empty ($ currentMode )) {
130+ update_option ('monei_apikey_mode ' , 'test ' );
131+ }
132+ $ needsMigration = true ;
133+
134+ } elseif (strpos ($ sourceApiKey , 'pk_live_ ' ) === 0 ) {
135+ // Migrate to live keys
136+ update_option ('monei_live_apikey ' , $ sourceApiKey );
137+ if (!empty ($ sourceAccountId )) {
138+ update_option ('monei_live_accountid ' , $ sourceAccountId );
139+ }
140+ if (empty ($ currentMode )) {
141+ update_option ('monei_apikey_mode ' , 'live ' );
142+ }
143+ $ needsMigration = true ;
144+ }
145+ }
146+
147+ // Clean up legacy keys if we did any migration
148+ if ($ needsMigration ) {
149+ $ this ->cleanup_legacy_keys ($ default_params );
150+ }
151+
152+ return $ default_params ;
102153 },
103- 1
154+ 10
104155 );
105156 }
157+ function cleanup_legacy_keys ($ settings_array ) {
158+ // Remove legacy standalone options
159+ delete_option ('monei_apikey ' );
160+ delete_option ('monei_accountid ' );
161+
162+ // Remove legacy keys from settings array (which will be returned by the filter)
163+ if (isset ($ settings_array ['apikey ' ])) {
164+ unset($ settings_array ['apikey ' ]);
165+ }
166+ if (isset ($ settings_array ['accountid ' ])) {
167+ unset($ settings_array ['accountid ' ]);
168+ }
169+
170+ return $ settings_array ;
171+ }
106172}
0 commit comments