Skip to content

Second issue #2476

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

Closed
wants to merge 12 commits into from
33 changes: 33 additions & 0 deletions dt-core/admin/admin-settings-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,39 @@ public function add_api_routes() {
'permission_callback' => [ $this, 'default_permission_check' ],
]
);

register_rest_route(
$this->namespace, '/languages', [
'methods' => 'POST',
'callback' => [ $this, 'update_languages' ],
'permission_callback' => [ $this, 'default_permission_check' ],
]
);
}

public function update_languages( WP_REST_REQUEST $request ) {
$params = $request->get_params();
$saved_language_options = dt_get_option( 'dt_working_languages' );

function recursive_array_update( &$target, $source ) {
foreach ( $source as $key => $value ) {
if ( is_array( $value ) && isset( $target[$key] ) && is_array( $target[$key] ) ) {
recursive_array_update( $target[$key], $value );
} elseif ( !isset( $target[$key] ) || $target[$key] !== $value ) {
$target[$key] = $value;
}
}
}
/**
* @todo:
* only save user provided customizations to the dt_working_languages options
* this includes labels, the iso code, the enabled status and translations
* does not include: the key, default labels etc
*/

recursive_array_update( $saved_language_options, $params );
update_option( 'dt_working_languages', $saved_language_options, false );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CptHappyHands
Here we only want to save the custom values the admin has entered. Not any that are default.

So if the admin sets english to enabled:
We'd do saved_language_options[en_US]['enabled'] = true

I expect a lot of the code from process_languages_box() would be copied here.

return true;
}

public static function get_post_fields() {
Expand Down
119 changes: 112 additions & 7 deletions dt-core/admin/js/dt-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,110 @@ jQuery(document).ready(function ($) {
$(this).siblings(),
$(this).data('form_name'),
$(this).data('source'),
$(this).data('value'),
$(this).data('callback'),
);
});

// Handle label language translations on a language
window.update_language_translations = function (source, value) {
console.log(source, 'line 17');
let translations_list = {};
$(
`#language_table .language_label_translations[data-field="${value}"]`,
).each(function (index, element) {
const language = $(element).data('field');
if (!translations_list[language]) {
translations_list[language] = {};
}
if (!translations_list[language].translations) {
translations_list[language].translations = {};
}
const translation_key = $(element).data('value');
translations_list[language].translations[translation_key] =
$(element).val();
});
console.log(translations_list, 'line 26');

$.ajax({
type: 'POST',
dataType: 'json',
data: JSON.stringify(translations_list),
contentType: 'application/json; charset=utf-8',
url: `${window.dt_admin_scripts.rest_root}dt-admin-settings/languages/`,
beforeSend: (xhr) => {
xhr.setRequestHeader('X-WP-Nonce', window.dt_admin_scripts.nonce);
},
success: function (response) {
var languages = JSON.parse(response);
console.log(languages, 'success line 40');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @CptHappyHands !

You can do window.location.reload() on the success for now.
Some in the other ajax

},
error: function (xhr, status, error) {
console.log(error, 'error line 43');
console.log(status, 'status line 44');
console.error(xhr.responseText, 'responsetext line 45');
},
});
};

// Handle languages tables
$('#save_lang_button').click(function (e) {
e.preventDefault();
let tableLangs = {};

//option two, got through each row
$('#language_table .language-row').each(function (index, element) {
// console.log(translations_list, "line 100");
// console.log(tableLangs, "line 74");
const lang = $(element).data('lang');
// console.log(lang, "line 55");
const label = $(element).find('.custom_label input').val();
// console.log(label, "line 57");
const iso_code = $(element).find('.iso_code input').val();
// console.log(iso_code, "line 59");
const enabled = $(element).find('.enabled input').prop('checked');
// console.log(enabled, "line 82");

if (!tableLangs[lang]) {
tableLangs[lang] = {
label: '',
native_name: '',
flag: '',
rtl: false,
enabled: '',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we only want to save custom_label, iso_639-3 and enabled.
The rest should be removed

// translations: "",
};
}

tableLangs[lang]['label'] = label;
tableLangs[lang]['flag'] = iso_code;
tableLangs[lang]['enabled'] = enabled;
// tableLangs[lang]["translations"] = extractedTranslations;
});

console.log(tableLangs, 'line 134');

$.ajax({
type: 'POST',
dataType: 'json',
data: JSON.stringify(tableLangs),
contentType: 'application/json; charset=utf-8',
url: `${window.dt_admin_scripts.rest_root}dt-admin-settings/languages/`,
beforeSend: (xhr) => {
xhr.setRequestHeader('X-WP-Nonce', window.dt_admin_scripts.nonce);
},
success: function (response) {
var languages = JSON.parse(response);
console.log(languages, 'success');
},
error: function (xhr, status, error) {
console.log(error, 'error line 82');
console.log(status, 'status line 83');
console.error(xhr.responseText, 'responsetext line 84');
},
});
});

$('.change-icon-button').click(function (e) {
e.preventDefault();

Expand Down Expand Up @@ -194,9 +295,15 @@ jQuery(document).ready(function ($) {
* Translation modal dialog
*/

function display_translation_dialog(container, form_name, source = '') {
function display_translation_dialog(
container,
form_name,
source = '',
value = '',
callback = '',
) {
let dialog = $('#dt_translation_dialog');
if (container && form_name && dialog) {
if (container && dialog) {
// Update dialog div
$(dialog)
.empty()
Expand Down Expand Up @@ -227,7 +334,9 @@ jQuery(document).ready(function ($) {
$('.dt-custom-fields-save-button')[0],
true,
);
} else {
} else if (callback) {
window[callback](source, value);
} else if (form_name) {
$('form[name="' + form_name + '"]').submit();
}
},
Expand All @@ -236,10 +345,6 @@ jQuery(document).ready(function ($) {

// Display updated dialog
dialog.dialog('open');
} else {
console.log(
'Unable to reference a valid: [container, form-name, dialog]',
);
}
}

Expand Down
54 changes: 32 additions & 22 deletions dt-core/admin/menu/tabs/tab-custom-lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,16 +617,15 @@ public static function admin_notice( string $notice, string $type ) {
* UI for picking languages
*/
private function languages_box(){

$languages = dt_get_option( 'dt_working_languages' ) ?: [];
$dt_global_languages_list = dt_get_global_languages_list();
uasort($dt_global_languages_list, function( $a, $b ) {
return strcmp( $a['label'], $b['label'] );
});
$form_name = 'languages_box';
?>
<form method="post" name="<?php echo esc_html( $form_name ) ?>" id="languages">
<input type="hidden" name="languages_box_nonce" value="<?php echo esc_attr( wp_create_nonce( 'languages_box' ) ) ?>" />
<table class="widefat">
<table class="widefat" id='language_table'>
<thead>
<tr>
<td><?php esc_html_e( 'Key', 'disciple_tools' ) ?></td>
Expand All @@ -640,23 +639,25 @@ private function languages_box(){
<tbody>
<?php foreach ( $languages as $language_key => $language_option ) :

$enabled = !isset( $language_option['deleted'] ) || $language_option['deleted'] == false; ?>

<tr>
<td><?php echo esc_html( $language_key ) ?></td>
<td><?php echo esc_html( isset( $dt_global_languages_list[$language_key] ) ? $dt_global_languages_list[$language_key]['label'] : '' ) ?></td>
<td><input type="text" placeholder="Custom Label" name="language_label[<?php echo esc_html( $language_key ) ?>][default]" value="<?php echo esc_html( ( !isset( $dt_global_languages_list[$language_key] ) || ( isset( $dt_global_languages_list[$language_key] ) && $dt_global_languages_list[$language_key]['label'] != $language_option['label'] ) ) ? $language_option['label'] : '' ) ?>"></td>
<td><input type="text" placeholder="ISO 639-3 code" maxlength="3" name="language_code[<?php echo esc_html( $language_key ) ?>]" value="<?php echo esc_html( $language_option['iso_639-3'] ?? '' ) ?>"></td>
<td>
$enabled = !isset( $language_option['enabled'] ) || $language_option['enabled'] == true; ?>
<tr class="language-row" data-lang="<?php echo esc_html( $language_key ) ?>">
<td class='lang_key'><?php echo esc_html( $language_key ) ?></td>
<td class='default_label'><?php echo esc_html( isset( $dt_global_languages_list[$language_key] ) ? $dt_global_languages_list[$language_key]['label'] : '' ) ?></td>
<td class='custom_label'><input type="text" placeholder="Custom Label" name="language_label[<?php echo esc_html( $language_key ) ?>][default]" value="<?php echo esc_html( $language_option['label'] ?? '' )?>"></td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CptHappyHands keep the "custom" label blank if the admin has not saved a custom label yet. This should not be filled with the default label.

<td class='iso_code'><input type="text" placeholder="ISO 639-3 code" maxlength="3" name="language_code[<?php echo esc_html( $language_key ) ?>]" value="<?php echo esc_html( $language_option['flag'] ?? '' ) ?>"></td>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was iso_639-3 replaced with 'flag' here on accident?

<td class='enabled'>
<input name="language_enabled[<?php echo esc_html( $language_key ) ?>]"
type="checkbox" <?php echo esc_html( $enabled ? 'checked' : '' ) ?> />
</td>

<td>
<td class='translation_key'>
<?php $langs = dt_get_available_languages(); ?>
<button class="button small expand_translations"
data-form_name="<?php echo esc_html( $form_name ) ?>"
data-source="lists">
data-source="dt_languages"
data-value="<?php echo esc_html( $language_key ); ?>"
data-callback="update_language_translations"
>
<?php
$number_of_translations = 0;
foreach ( $langs as $lang => $val ){
Expand All @@ -673,7 +674,13 @@ private function languages_box(){
<?php foreach ( $langs as $lang => $val ) : ?>
<tr>
<td><label for="language_label[<?php echo esc_html( $language_key ) ?>][<?php echo esc_html( $val['language'] )?>]"><?php echo esc_html( $val['native_name'] )?></label></td>
<td><input name="language_label[<?php echo esc_html( $language_key ) ?>][<?php echo esc_html( $val['language'] )?>]" type="text" value="<?php echo esc_html( $language_option['translations'][$val['language']] ?? '' );?>"/></td>
<td><input
class="language_label_translations"
data-field="<?php echo esc_html( $language_key ); ?>"
data-value="<?php echo esc_html( $val['language'] ) ?>"
name="language_label[<?php echo esc_html( $language_key ) ?>][<?php echo esc_html( $val['language'] )?>]"
type="text"
value="<?php echo esc_html( $language_option['translations'][$val['language']] ?? '' );?>"/></td>
</tr>
<?php endforeach; ?>
</table>
Expand All @@ -683,21 +690,24 @@ private function languages_box(){
<?php endforeach; ?>
</tbody>
</table>
<br><button type="button" onclick="jQuery('#add_language').toggle();" class="button">
<?php echo esc_html__( 'Add new language', 'disciple_tools' ) ?></button>
<button type="submit" class="button" style="float:right;">
<br>
<button id="save_lang_button" type="submit" class="button" style="float:right;">
<?php esc_html_e( 'Save', 'disciple_tools' ) ?>
</button>
<form method="post" name="<?php echo esc_html( $form_name ) ?>" id="languages">
<input type="hidden" name="languages_box_nonce" value="<?php echo esc_attr( wp_create_nonce( 'languages_box' ) ) ?>" />
<br><button type="button" onclick="jQuery('#add_language').toggle();" class="button">
<?php echo esc_html__( 'Add new language', 'disciple_tools' ) ?></button>
<div id="add_language" style="display:none;">
<hr>
<p><?php esc_html_e( 'Select from the language list', 'disciple_tools' ); ?></p>
<select name="new_lang_select">
<option></option>
<select name="new_lang_select" id="new_lang_select">
<option id></option>
<?php foreach ( $dt_global_languages_list as $lang_key => $lang_option ) : ?>
<option value="<?php echo esc_html( $lang_key ); ?>"><?php echo esc_html( $lang_option['label'] ?? '' ); ?></option>
<option name="<?php echo esc_html( $lang_key ); ?>" value="<?php echo esc_html( $lang_key ); ?>"><?php echo esc_html( $lang_option['label'] ?? '' ); ?></option>
<?php endforeach; ?>
</select>
<button type="submit" class="button"><?php esc_html_e( 'Add', 'disciple_tools' ) ?></button>
<button class="button" id="add_lang_button"><?php esc_html_e( 'Add', 'disciple_tools' ) ?></button>
<br>
<br>
<p><?php esc_html_e( 'If your language is not in the list, you can create it manually', 'disciple_tools' ); ?></p>
Expand Down
Loading