post-edit-terms.js
5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*globals jQuery, WPML_core, ajaxurl, icl_post_edit_messages */
(function () {
'use strict';
/** @namespace icl_post_edit_messages.switch_language_title */
/** @namespace icl_post_edit_messages.switch_language_alert */
/** @namespace icl_post_edit_messages.connection_loss_alert */
/** @namespace icl_post_edit_messages.switch_language_message */
/** @namespace icl_post_edit_messages.switch_language_confirm */
/** @namespace icl_post_edit_messages._nonce */
jQuery(function () {
var language_selector;
/* Check if we have a language switcher present. */
language_selector = jQuery('select[name="icl_post_language"]');
if (jQuery('[name="_wpml_root_page"]').length !== 0) {
jQuery('#edit-slug-box').hide();
}
if (language_selector.length !== 0) {
language_selector.on('change', iclPostLanguageAskConfirmation);
iclSwitchPostLanguage();
} else {
// just add a hidden field with the information and the same id as the language switcher to the dom
jQuery.ajax({
type: "POST",
url: ajaxurl,
dataType: 'json',
data: {
action: 'wpml_get_default_lang',
_icl_nonce: icl_post_edit_messages._get_default_lang_nonce,
},
success: function (response) {
var hidden_language_field = jQuery('<input id="icl_post_language" type="hidden"/>');
hidden_language_field.val(response.data);
jQuery(".wrap").append(hidden_language_field);
iclSwitchPostLanguage();
}
});
}
});
function iclGetSwitchLanguageConfirmation() {
var lang_switch_confirm_html;
var defer = jQuery.Deferred();
lang_switch_confirm_html = '<div id="icl_confirm_lang_switch">';
lang_switch_confirm_html += '<h2>{switch_language_title}</h2>';
lang_switch_confirm_html += '<p>{switch_language_message}</p>';
lang_switch_confirm_html += '<p>{switch_language_confirm}</p>';
lang_switch_confirm_html += '</div>';
// make sure the title is html entities encoded.
var post_name = WPML_core.htmlentities(jQuery('#title').val());
if (post_name.length == 0) {
post_name = icl_post_edit_messages.empty_post_title;
}
lang_switch_confirm_html = lang_switch_confirm_html.replace('{switch_language_title}', icl_post_edit_messages.switch_language_title);
lang_switch_confirm_html = lang_switch_confirm_html.replace('{switch_language_message}', icl_post_edit_messages.switch_language_message);
lang_switch_confirm_html = lang_switch_confirm_html.replace('{switch_language_confirm}', icl_post_edit_messages.switch_language_confirm);
lang_switch_confirm_html = lang_switch_confirm_html.replace('{post_name}', '<i>' + post_name + '</i>');
jQuery(lang_switch_confirm_html).dialog({
modal: true,
width: 'auto',
buttons: [
{
text : icl_post_edit_messages.ok_button_label,
id : "ok",
click: function () {
defer.resolve();
jQuery(this).dialog("close");
}
},
{
text : icl_post_edit_messages.cancel_button_label,
id : "cancel",
click: function () {
defer.reject();
jQuery(this).dialog("close");
}
}
]
});
return defer.promise();
}
function iclPostLanguageAskConfirmation() {
var post_language_switcher = jQuery('#icl_post_language');
var previous_post_language = post_language_switcher.data('last_lang');
jQuery('#edit-slug-buttons').find('> .cancel').click();
iclGetSwitchLanguageConfirmation().done(function () {
iclSwitchPostLanguage();
}).fail(function () {
post_language_switcher.val(previous_post_language);
});
}
function iclSwitchPostLanguage() {
var post_language_switcher = jQuery('#icl_post_language');
var parent = post_language_switcher.parent();
var nonce = parent.find('[name="nonce"]').val();
var new_post_language = post_language_switcher.val();
var previous_post_language = post_language_switcher.data('last_lang');
var post_id = jQuery('#post_ID').val();
if (!previous_post_language) {
post_language_switcher.data('last_lang', new_post_language);
} else {
jQuery.ajax({
type: "POST",
url: ajaxurl,
dataType: 'json',
data: {
wpml_from: previous_post_language,
action: 'wpml_switch_post_language',
nonce: nonce,
/** @todo: remove the following line as most likely obsolete */
_icl_nonce: icl_post_edit_messages._nonce,
wpml_to: new_post_language,
wpml_post_id: post_id
},
success: function () {
post_language_switcher.data('last_lang', new_post_language);
var url = WPML_core.sanitize(jQuery(location).attr('href'));
if (/lang=/.test(url)) {
url = url.replace(/([\?&])(lang=)[^&#]*/, '$1$2' + new_post_language);
} else {
var sep = (url.indexOf('?') > -1) ? '&' : '?';
url = url + sep + 'lang=' + new_post_language;
}
window.location.replace(url);
}
});
}
}
})();