From 2c65a0dce95862cada74eb3422e72dd80183f52c Mon Sep 17 00:00:00 2001 From: HerrVigg Date: Mon, 18 Apr 2022 20:39:34 +0200 Subject: [PATCH] Refactor bool-array setting to `QTX_BOOLEAN_SET` The `bool_elements_array` setting was introduced in #1135 and #1137. Re-using the QTX_ARRAY code with an extra argument makes it hard to maintain. The QTX_ARRAY type is not suited for set of checkboxes. Separate the logic by handling this type all separately. The `QTX_BOOLEAN_SET` was legacy, defined but not used in the code. Make the new type generic though only used by `ma_module_enabled`. Remove the extra `bool_elements_array` argument. --- admin/qtx_admin_options_update.php | 37 ++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/admin/qtx_admin_options_update.php b/admin/qtx_admin_options_update.php index 053a6b83..c2a08b39 100644 --- a/admin/qtx_admin_options_update.php +++ b/admin/qtx_admin_options_update.php @@ -528,12 +528,13 @@ function qtranxf_updateSetting( $var, $type = QTX_STRING, $def = null ) { qtranxf_update_setting( $var, $type, $def ); } -function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null, $bool_elements_array = false ) { +function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null ) { global $q_config, $qtranslate_options; if ( ! isset( $_POST['submit'] ) ) { return false; } - if ( ! isset( $_POST[ $var ] ) && $type != QTX_BOOLEAN && ! $bool_elements_array ) { + // Require POST data except for booleans, as unchecked boxes are not sent with the form. + if ( ! isset( $_POST[ $var ] ) && $type != QTX_BOOLEAN && $type != QTX_BOOLEAN_SET ) { return false; } @@ -570,6 +571,7 @@ function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null, $bool_el qtranxf_update_option( $var, $def ); return true; + case QTX_TEXT: $val = $_POST[ $var ]; // standardize multi-line string @@ -595,25 +597,39 @@ function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null, $bool_el qtranxf_update_option( $var, $def ); return true; + case QTX_ARRAY: $val = isset( $_POST[ $var ] ) ? $_POST[ $var ] : array(); if ( ! is_array( $val ) ) { $val = sanitize_text_field( $val ); $val = preg_split( '/[\s,]+/', $val, -1, PREG_SPLIT_NO_EMPTY ); } - if ( ! $bool_elements_array && empty( $val ) ) { + if ( empty( $val ) ) { if ( is_string( $def ) ) { $val = preg_split( '/[\s,]+/', $def, -1, PREG_SPLIT_NO_EMPTY ); } else if ( is_array( $def ) ) { $val = $def; // TODO: why replace all the array? Check if shouldn't it be merged with default. } } - if ( $bool_elements_array && is_array( $def ) ) { - // TODO: refactor ma_enabled vs state. Normally we should use array_merge($def, $val) but we can't. - // TODO: Unchecked checkboxes input are not included in $_POST so default values are ignored and forced to false. - foreach ( $def as $key => $value ) { + if ( isset( $q_config[ $var ] ) && qtranxf_array_compare( $q_config[ $var ], $val ) ) { + return false; + } + $q_config[ $var ] = $val; + qtranxf_update_option( $var, $def ); + + return true; + + case QTX_BOOLEAN_SET: + $val = isset( $_POST[ $var ] ) ? $_POST[ $var ] : array(); + // Convert all input values to boolean types + foreach ( $val as &$value ) { + $value = (bool) $value; + } + // Input checkboxes that are unchecked are not in $_POST so default values are used to detect missing keys. + if ( isset ( $def ) ) { + foreach ( array_keys( $def ) as $key ) { if ( ! array_key_exists( $key, $val ) ) { - $val[ $key ] = false; + $val[ $key ] = false; // Ignore the default value, enforce `false` for that key. } } } @@ -624,6 +640,7 @@ function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null, $bool_el qtranxf_update_option( $var, $def ); return true; + case QTX_BOOLEAN: if ( isset( $_POST[ $var ] ) && $_POST[ $var ] == 1 ) { if ( $q_config[ $var ] ) { @@ -639,6 +656,7 @@ function qtranxf_update_setting( $var, $type = QTX_STRING, $def = null, $bool_el qtranxf_update_option_bool( $var, $def ); return true; + case QTX_INTEGER: $val = sanitize_text_field( $_POST[ $var ] ); $val = intval( $val ); @@ -815,8 +833,9 @@ function qtranxf_update_settings() { foreach ( $qtranslate_options['front']['array'] as $name => $default ) { qtranxf_update_setting( $name, QTX_ARRAY, $default ); } + qtranxf_update_setting( 'filter_options', QTX_ARRAY ); - qtranxf_update_setting( 'ma_module_enabled', QTX_ARRAY, null, true ); + qtranxf_update_setting( 'ma_module_enabled', QTX_BOOLEAN_SET ); switch ( $q_config['url_mode'] ) { case QTX_URL_DOMAIN: