diff --git a/inc/options-framework/includes/class-options-framework-admin.php b/inc/options-framework/includes/class-options-framework-admin.php index 79db8a7..88c3f90 100644 --- a/inc/options-framework/includes/class-options-framework-admin.php +++ b/inc/options-framework/includes/class-options-framework-admin.php @@ -223,6 +223,11 @@ class Options_Framework_Admin { $clean = array(); $options = & Options_Framework::_optionsframework_options(); + // 已保存的值,供"字段未提交时沿用旧值"用。放循环外,避免重复查库。 + $saved_options = get_option( 'kratos' ); + if ( ! is_array( $saved_options ) ) { + $saved_options = array(); + } foreach ( $options as $option ) { if ( ! isset( $option['id'] ) ) { @@ -249,7 +254,30 @@ class Options_Framework_Admin { // For a value to be submitted to database it must pass through a sanitization filter if ( has_filter( 'of_sanitize_' . $option['type'] ) ) { - $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option ); + /* + * ⚠️ 表单里没有这个键时,**必须保留库里已有的值**,不能写空。 + * + * 原来这里直接写 `$input[$id]`。如果该字段没被提交(比如某个 + * 分组没渲染出来、或者字段被条件隐藏、或者表单中途出错只提交了一半), + * `$input[$id]` 是未定义索引 ⇒ 取到 null ⇒ + * `sanitize_text_field(null)` 返回 `''` ⇒ **把用户存了很久的 + * 配置静默清成空串**。而"清成空串"和"用户主动清空"在库里长得一模一样, + * 事后完全无法区分,只会表现为"我明明填过,怎么没了"。 + * + * 例外:checkbox 和 multicheck 上面已经把缺省项补成了 false, + * 这两类的"没提交"在语义上确实等于"没勾",保持原样。 + */ + $is_toggle = in_array( $option['type'], array( 'checkbox', 'multicheck' ), true ); + if ( array_key_exists( $id, $input ) ) { + $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option ); + } elseif ( $is_toggle ) { + $clean[$id] = apply_filters( 'of_sanitize_' . $option['type'], $input[$id], $option ); + } else { + // 没提交 ⇒ 沿用旧值(旧值也没有就不写这个键,让运行时默认值生效) + if ( array_key_exists( $id, $saved_options ) ) { + $clean[$id] = $saved_options[$id]; + } + } } } diff --git a/style.css b/style.css index 4acdb63..93adfc4 100644 --- a/style.css +++ b/style.css @@ -1,7 +1,7 @@ /* Theme Name: Kratos_Yang Text Domain: kratos_Yang -Version: 3.2.16 +Version: 3.2.17 Requires PHP: 7.0 Description: 这是一个Kratos主题的分支版本,用来增加一些定制化功能。 Tags: 博客, 双栏, 响应式, 自定义背景, 自定义颜色, 自定义图标, 自定义菜单, 特色图像, 收录优化, 邮件中心, 主题选项, 小工具, 文章置顶, 无障碍友好