Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors saving dropdown option translations #2513

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 34 additions & 40 deletions dt-core/admin/admin-settings-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,53 +607,37 @@ public static function edit_translations( WP_REST_Request $request ) {

switch ( $post_submission['translation_type'] ) {
case 'tile-label':
$translated_element = $tile_options[$post_type][$tile_key];
break;

case 'tile-description':
$translated_element = $tile_options[$post_type][$tile_key];
break;

case 'field-label':
if ( !isset( $post_submission['field_key'] ) ) {
return false;
}
$field_key = $post_submission['field_key'];
$translated_element = $field_customizations[$post_type][$field_key];
break;

case 'field-description':
if ( !isset( $post_submission['field_key'] ) ) {
return false;
}
$field_key = $post_submission['field_key'];
$translated_element = $field_customizations[$post_type][$field_key];
break;

case 'field-option-label':
if ( !isset( $post_submission['field_key'] ) || !isset( $post_submission['field_option_key'] ) ) {
return false;
}
$field_key = $post_submission['field_key'];
$field_option_key = $post_submission['field_option_key'];
$translated_element = $field_customizations[$post_type][$field_key]['default'][$field_option_key];
break;

case 'field-option-label':
case 'field-option-description':
if ( !isset( $post_submission['field_key'] ) || !isset( $post_submission['field_option_key'] ) ) {
return false;
}
$field_key = $post_submission['field_key'];
$field_option_key = $post_submission['field_option_key'];
$translated_element = $field_customizations[$post_type][$field_key]['default'][$field_option_key];
$translated_element = $field_customizations[$post_type][$field_key]['default'][$field_option_key] ?? [];
break;
}

// Check if translation is a description
$translations_element_key = 'translations';
if ( strpos( $post_submission['translation_type'], 'description' ) ) {
$translations_element_key = 'description_translations';
}
if ( empty( $translated_element ) ) {
$translated_element = [ $translations_element_key => [] ];
}

foreach ( $translations as $lang_key => $translation_val ) {
if ( $lang_key !== '' || !is_null( $lang_key ) ) {
Expand All @@ -663,30 +647,18 @@ public static function edit_translations( WP_REST_Request $request ) {

switch ( $post_submission['translation_type'] ) {
case 'tile-label':
$tile_options[$post_type][$tile_key] = $translated_element;
update_option( 'dt_custom_tiles', $tile_options );
break;

case 'tile-description':
$tile_options[$post_type][$tile_key] = $translated_element;
update_option( 'dt_custom_tiles', $tile_options );
break;

case 'field-label':
$field_customizations[$post_type][$field_key] = $translated_element;
update_option( 'dt_field_customizations', $field_customizations );
break;

case 'field-description':
$field_customizations[$post_type][$field_key] = $translated_element;
update_option( 'dt_field_customizations', $field_customizations );
break;

case 'field-option-label':
$field_customizations[$post_type][$field_key]['default'][$field_option_key] = $translated_element;
update_option( 'dt_field_customizations', $field_customizations );
break;

case 'field-option-description':
$field_customizations[$post_type][$field_key]['default'][$field_option_key] = $translated_element;
update_option( 'dt_field_customizations', $field_customizations );
Expand Down Expand Up @@ -911,24 +883,39 @@ public static function edit_field_option( WP_REST_Request $request ) {
$new_field_option_description = $post_submission['new_field_option_description'];
$field_option_icon = $post_submission['field_option_icon'];

$fields = DT_Posts::get_post_field_settings( $post_type, false, true );
$field_options = $fields[$field_key]['default'] ?? [];
$field_option = $field_options[$field_option_key] ?? [];

$field_customizations = dt_get_option( 'dt_field_customizations' );
$custom_field_option = [
'label' => $new_field_option_label,
'description' => $new_field_option_description,
];
$custom_field_option = [];
if ( isset( $field_customizations[$post_type][$field_key]['default'][$field_option_key] ) ){
$custom_field_option = array_merge( $field_customizations[$post_type][$field_key]['default'][$field_option_key], $custom_field_option );
}
$default_label = self::get_default_field_option_label( $post_type, $field_key, $field_option_key );
if ( $new_field_option_label !== $default_label ){
$custom_field_option['label'] = $new_field_option_label;
}
$default_description = self::get_default_field_option_description( $post_type, $field_key, $field_option_key );
if ( $new_field_option_description !== $default_description ){
$custom_field_option['description'] = $new_field_option_description;
}

if ( $field_option_icon && strpos( $field_option_icon, 'undefined' ) === false ){
$field_option_icon = strtolower( trim( $field_option_icon ) );
$icon_key = ( strpos( $field_option_icon, 'mdi' ) !== 0 ) ? 'icon' : 'font-icon';
$custom_field_option[$icon_key] = $field_option_icon;

if ( $field_option_icon !== $field_option[$icon_key] ){
$custom_field_option[$icon_key] = $field_option_icon;
}

if ( $icon_key == 'font-icon' ){
$custom_field_option['icon'] = '';
}
}

// Create default_name to store the default field option label if it changed
if ( self::default_field_option_label_changed( $post_type, $field_key, $field_option_key, $custom_field_option['label'] ) ) {
if ( self::default_field_option_label_changed( $post_type, $field_key, $field_option_key, $custom_field_option['label'] ?? '' ) ) {
$custom_field_option['default_name'] = self::get_default_field_option_label( $post_type, $field_key, $field_option_key );
}

Expand All @@ -939,7 +926,7 @@ public static function edit_field_option( WP_REST_Request $request ) {

$field_customizations[$post_type][$field_key]['default'][$field_option_key] = $custom_field_option;
update_option( 'dt_field_customizations', $field_customizations );
return $custom_field_option;
return array_merge( $field_option, $custom_field_option );
}

public function delete_field_option( WP_REST_Request $request ) {
Expand Down Expand Up @@ -1009,6 +996,13 @@ public static function get_default_field_option_label( $post_type, $field_key, $
$default_name = $all_non_custom_fields[$field_key]['default'][$field_option_key]['label'] ?? '';
return $default_name;
}
public static function get_default_field_option_description( $post_type, $field_key, $field_option_key ) {
$base_fields = Disciple_Tools_Post_Type_Template::get_base_post_type_fields();
$default_fields = apply_filters( 'dt_custom_fields_settings', [], $post_type );
$all_non_custom_fields = array_merge( $base_fields, $default_fields );
$default_name = $all_non_custom_fields[$field_key]['default'][$field_option_key]['description'] ?? '';
return $default_name;
}

public static function update_tiles_and_fields_order( WP_REST_Request $request ) {
$post_submission = $request->get_params();
Expand Down
3 changes: 2 additions & 1 deletion dt-core/admin/js/dt-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,8 @@ jQuery(document).ready(function ($) {
$(tr)
.find("input[id^='" + option_key_prefix + "']")
.each(function (okt_idx, okt_input) {
let locale = window.lodash.split($(okt_input).attr('id'), '-')[1];
// expected format: field_option_{option_value}_translation-{locale}
let locale = okt_input.id.replace(option_key_prefix, '');
let value = $(okt_input).val();
if (locale && value) {
translations['option_translations'].push({
Expand Down
3 changes: 3 additions & 0 deletions dt-core/admin/menu/tabs/admin-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ public function update_custom_field_translations( WP_REST_Request $request ){

if ( !empty( $option_key ) ){
$defaults[$option_key] = [];
if ( !empty( $custom_field['default'][$option_key]['label'] ) ) {
$defaults[$option_key]['label'] = $custom_field['default'][$option_key]['label'];
}
$defaults[$option_key]['translations'] = [];
$defaults[$option_key]['description_translations'] = [];

Expand Down
1 change: 0 additions & 1 deletion dt-core/admin/menu/tabs/tab-custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @author Disciple.Tools
*/

/**/

if ( !defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
Expand Down
Loading