Skip to content

Commit

Permalink
Fix localize script with missing ACF options (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
herrvigg committed May 28, 2023
1 parent 30c90b4 commit e936baf
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/modules/acf/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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() ) );
}

/**
Expand Down Expand Up @@ -405,19 +418,15 @@ 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 ];
};
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 );
}

Expand Down

0 comments on commit e936baf

Please sign in to comment.