From e936bafff67a44499a09a07262aaa2eb3dd7b5be Mon Sep 17 00:00:00 2001 From: HerrVigg Date: Sun, 28 May 2023 13:24:07 +0200 Subject: [PATCH] Fix localize script with missing ACF options (#1341) --- src/modules/acf/admin.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/modules/acf/admin.php b/src/modules/acf/admin.php index 0721d967..caa6e34b 100644 --- a/src/modules/acf/admin.php +++ b/src/modules/acf/admin.php @@ -70,6 +70,19 @@ public static function qtranslate_fields_default(): array { ]; } + /** + * Return the default built-in options without querying the options in DB. + * @return array + */ + private static function options_default(): array { + return [ + 'standard_fields' => self::standard_fields_default(), + 'group_sub_fields' => self::group_sub_fields_default(), + 'qtranslate_fields' => self::qtranslate_fields_default(), + 'show_language_tabs' => false, + ]; + } + /** * Filter the field types to exclude the extended fields that have been deactivated in settings. * @@ -109,7 +122,7 @@ public function admin_enqueue_scripts(): void { 'qtranslate-admin-main' ), QTX_VERSION ); - wp_localize_script( 'qtranslate-acf', 'qTranslateModuleAcf', get_option( QTX_OPTIONS_MODULE_ACF ) ); + wp_localize_script( 'qtranslate-acf', 'qTranslateModuleAcf', get_option( QTX_OPTIONS_MODULE_ACF, self::options_default() ) ); } /** @@ -405,12 +418,7 @@ public function update_settings(): void { // 1) the settings falls back to default when all values are unchecked (not set in POST) // 2) if new options come in later, we have no way to tell it's undefined or unchecked after user update. // Ensure we set values for all keys though the default values are ignored. - $options = [ - 'standard_fields' => self::standard_fields_default(), - 'group_sub_fields' => self::group_sub_fields_default(), - 'qtranslate_fields' => self::qtranslate_fields_default(), - 'show_language_tabs' => isset( $post_acf['show_language_tabs'] ) && $post_acf['show_language_tabs'], - ]; + $options = self::options_default(); // If checked, convert '1' strings to `true`, otherwise `false` (ignore the current value = default). $set_bool = function ( &$item, $key, $post_fields ): void { $item = isset( $post_fields[ $key ] ) && $post_fields[ $key ]; @@ -418,6 +426,7 @@ public function update_settings(): void { array_walk( $options['standard_fields'], $set_bool, $post_acf['standard_fields'] ?? [] ); array_walk( $options['group_sub_fields'], $set_bool, $post_acf['group_sub_fields'] ?? [] ); array_walk( $options['qtranslate_fields'], $set_bool, $post_acf['qtranslate_fields'] ?? [] ); + $options['show_language_tabs'] = isset( $post_acf['show_language_tabs'] ) && $post_acf['show_language_tabs']; update_option( QTX_OPTIONS_MODULE_ACF, $options, false ); }