Skip to content

Commit

Permalink
tweak Settings service
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jul 28, 2023
1 parent 27b6f4c commit d71743d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
30 changes: 20 additions & 10 deletions includes/Services/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,15 @@ public function save_settings( string $id, array $settings ) {
* @return array
*/
public function get_general_settings(): array {
$general_settings = array_replace_recursive(
self::$default_settings['general'],
get_option( self::$db_prefix . 'general', array() )
);
$default_settings = self::$default_settings['general'];
$settings = get_option( self::$db_prefix . 'general', array() );

// if the key does not exist in db settings, use the default settings
foreach ( $default_settings as $key => $value ) {
if ( ! array_key_exists( $key, $settings ) ) {
$settings[ $key ] = $value;
}
}

/**
* Filters the general settings.
Expand All @@ -155,17 +160,22 @@ public function get_general_settings(): array {
* @since 1.0.0
* @hook woocommerce_pos_general_settings
*/
return apply_filters( 'woocommerce_pos_general_settings', $general_settings );
return apply_filters( 'woocommerce_pos_general_settings', $settings );
}

/**
* @return array
*/
public function get_checkout_settings(): array {
$checkout_settings = array_replace_recursive(
self::$default_settings['checkout'],
get_option( self::$db_prefix . 'checkout', array() )
);
$default_settings = self::$default_settings['checkout'];
$settings = get_option( self::$db_prefix . 'checkout', array() );

// if the key does not exist in db settings, use the default settings
foreach ( $default_settings as $key => $value ) {
if ( ! array_key_exists( $key, $settings ) ) {
$settings[ $key ] = $value;
}
}

/**
* Filters the checkout settings.
Expand All @@ -175,7 +185,7 @@ public function get_checkout_settings(): array {
* @since 1.0.0
* @hook woocommerce_pos_checkout_settings
*/
return apply_filters( 'woocommerce_pos_checkout_settings', $checkout_settings );
return apply_filters( 'woocommerce_pos_checkout_settings', $settings );
}

/**
Expand Down
23 changes: 18 additions & 5 deletions includes/Templates/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ public function remove_scripts_and_styles(): void {
}


/**
* @return void
*/
private function check_troubleshooting_form_submission() {
// Check if our form has been submitted
if ( isset( $_POST['troubleshooting_form_nonce'] ) ) {
Expand All @@ -132,6 +135,8 @@ private function check_troubleshooting_form_submission() {
// Sanitize styles array
if ( isset( $_POST['styles'] ) && is_array( $_POST['styles'] ) ) {
$sanitized_data['styles'] = array_map( 'sanitize_text_field', $_POST['styles'] );
} else {
$sanitized_data['styles'] = array(); // consider all styles unchecked if 'styles' is not submitted
}

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

// Calculate unchecked styles and scripts
$unchecked_styles = isset( $sanitized_data['all_styles'], $sanitized_data['styles'] ) ? array_diff( $sanitized_data['all_styles'], $sanitized_data['styles'] ) : array();
$unchecked_scripts = isset( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) ? array_diff( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) : array();
$unchecked_styles = isset( $sanitized_data['all_styles'] ) ? array_diff( $sanitized_data['all_styles'], $sanitized_data['styles'] ) : array();
$unchecked_scripts = isset( $sanitized_data['all_scripts'] ) ? array_diff( $sanitized_data['all_scripts'], $sanitized_data['scripts'] ) : array();

// @TODO - the save settings function should allow saving by key
$settings = new Settings();
$checkout_settings = $settings->get_checkout_settings();
$new_settings = array_replace_recursive(
$new_settings = array_merge(
$checkout_settings,
array( 'dequeue_style_handles' => $unchecked_styles ),
array( 'dequeue_script_handles' => $unchecked_scripts )
array(
'dequeue_style_handles' => $unchecked_styles,
'dequeue_script_handles' => $unchecked_scripts,
)
);
$settings->save_settings( 'checkout', $new_settings );
}
}


/**
* @return void
*/
public function get_template(): void {
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
define( 'WOOCOMMERCE_CHECKOUT', true );
Expand Down

0 comments on commit d71743d

Please sign in to comment.