Skip to content

Commit d71743d

Browse files
committed
tweak Settings service
1 parent 27b6f4c commit d71743d

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

includes/Services/Settings.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,15 @@ public function save_settings( string $id, array $settings ) {
142142
* @return array
143143
*/
144144
public function get_general_settings(): array {
145-
$general_settings = array_replace_recursive(
146-
self::$default_settings['general'],
147-
get_option( self::$db_prefix . 'general', array() )
148-
);
145+
$default_settings = self::$default_settings['general'];
146+
$settings = get_option( self::$db_prefix . 'general', array() );
147+
148+
// if the key does not exist in db settings, use the default settings
149+
foreach ( $default_settings as $key => $value ) {
150+
if ( ! array_key_exists( $key, $settings ) ) {
151+
$settings[ $key ] = $value;
152+
}
153+
}
149154

150155
/**
151156
* Filters the general settings.
@@ -155,17 +160,22 @@ public function get_general_settings(): array {
155160
* @since 1.0.0
156161
* @hook woocommerce_pos_general_settings
157162
*/
158-
return apply_filters( 'woocommerce_pos_general_settings', $general_settings );
163+
return apply_filters( 'woocommerce_pos_general_settings', $settings );
159164
}
160165

161166
/**
162167
* @return array
163168
*/
164169
public function get_checkout_settings(): array {
165-
$checkout_settings = array_replace_recursive(
166-
self::$default_settings['checkout'],
167-
get_option( self::$db_prefix . 'checkout', array() )
168-
);
170+
$default_settings = self::$default_settings['checkout'];
171+
$settings = get_option( self::$db_prefix . 'checkout', array() );
172+
173+
// if the key does not exist in db settings, use the default settings
174+
foreach ( $default_settings as $key => $value ) {
175+
if ( ! array_key_exists( $key, $settings ) ) {
176+
$settings[ $key ] = $value;
177+
}
178+
}
169179

170180
/**
171181
* Filters the checkout settings.
@@ -175,7 +185,7 @@ public function get_checkout_settings(): array {
175185
* @since 1.0.0
176186
* @hook woocommerce_pos_checkout_settings
177187
*/
178-
return apply_filters( 'woocommerce_pos_checkout_settings', $checkout_settings );
188+
return apply_filters( 'woocommerce_pos_checkout_settings', $settings );
179189
}
180190

181191
/**

includes/Templates/Payment.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public function remove_scripts_and_styles(): void {
112112
}
113113

114114

115+
/**
116+
* @return void
117+
*/
115118
private function check_troubleshooting_form_submission() {
116119
// Check if our form has been submitted
117120
if ( isset( $_POST['troubleshooting_form_nonce'] ) ) {
@@ -132,6 +135,8 @@ private function check_troubleshooting_form_submission() {
132135
// Sanitize styles array
133136
if ( isset( $_POST['styles'] ) && is_array( $_POST['styles'] ) ) {
134137
$sanitized_data['styles'] = array_map( 'sanitize_text_field', $_POST['styles'] );
138+
} else {
139+
$sanitized_data['styles'] = array(); // consider all styles unchecked if 'styles' is not submitted
135140
}
136141

137142
// Sanitize all_scripts array
@@ -142,24 +147,32 @@ private function check_troubleshooting_form_submission() {
142147
// Sanitize scripts array
143148
if ( isset( $_POST['scripts'] ) && is_array( $_POST['scripts'] ) ) {
144149
$sanitized_data['scripts'] = array_map( 'sanitize_text_field', $_POST['scripts'] );
150+
} else {
151+
$sanitized_data['scripts'] = array(); // consider all scripts unchecked if 'scripts' is not submitted
145152
}
146153

147154
// Calculate unchecked styles and scripts
148-
$unchecked_styles = isset( $sanitized_data['all_styles'], $sanitized_data['styles'] ) ? array_diff( $sanitized_data['all_styles'], $sanitized_data['styles'] ) : array();
149-
$unchecked_scripts = isset( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) ? array_diff( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) : array();
155+
$unchecked_styles = isset( $sanitized_data['all_styles'] ) ? array_diff( $sanitized_data['all_styles'], $sanitized_data['styles'] ) : array();
156+
$unchecked_scripts = isset( $sanitized_data['all_scripts'] ) ? array_diff( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) : array();
150157

151158
// @TODO - the save settings function should allow saving by key
152159
$settings = new Settings();
153160
$checkout_settings = $settings->get_checkout_settings();
154-
$new_settings = array_replace_recursive(
161+
$new_settings = array_merge(
155162
$checkout_settings,
156-
array( 'dequeue_style_handles' => $unchecked_styles ),
157-
array( 'dequeue_script_handles' => $unchecked_scripts )
163+
array(
164+
'dequeue_style_handles' => $unchecked_styles,
165+
'dequeue_script_handles' => $unchecked_scripts,
166+
)
158167
);
159168
$settings->save_settings( 'checkout', $new_settings );
160169
}
161170
}
162171

172+
173+
/**
174+
* @return void
175+
*/
163176
public function get_template(): void {
164177
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
165178
define( 'WOOCOMMERCE_CHECKOUT', true );

0 commit comments

Comments
 (0)