PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / js / build / term.js

term.js in Polylang 3.1.4, at js/build/term.js

227 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var __webpack_exports__ = {};
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Quick edit
8 */
9 jQuery(
10 function( $ ) {
11 $( document ).on(
12 'DOMNodeInserted',
13 function( e ) {
14 var t = $( e.target );
15
16 // WP inserts the quick edit from
17 if ( 'inline-edit' == t.attr( 'id' ) ) {
18 var term_id = t.prev().attr( 'id' ).replace( "tag-", "" );
19
20 if ( term_id > 0 ) {
21 // language dropdown
22 var select = t.find( ':input[name="inline_lang_choice"]' );
23 var lang = $( '#lang_' + term_id ).html();
24 select.val( lang ); // populates the dropdown
25
26 // disable the language dropdown for default categories
27 var default_cat = $( '#default_cat_' + term_id ).html();
28 if ( term_id == default_cat ) {
29 select.prop( 'disabled', true );
30 }
31 }
32 }
33 }
34 );
35 }
36 );
37
38 /**
39 * Update rows of translated terms when adding / deleting a translation or when the language is modified in quick edit.
40 * Acts on ajaxSuccess event.
41 */
42 jQuery(
43 function( $ ) {
44 $( document ).ajaxSuccess(
45 function( event, xhr, settings ) {
46 function update_rows( term_id ) {
47 // collect old translations
48 var translations = new Array();
49 $( '.translation_' + term_id ).each(
50 function() {
51 translations.push( $( this ).parent().parent().attr( 'id' ).substring( 4 ) );
52 }
53 );
54
55 var data = {
56 action: 'pll_update_term_rows',
57 term_id: term_id,
58 translations: translations.join( ',' ),
59 taxonomy: $( "input[name='taxonomy']" ).val(),
60 post_type: $( "input[name='post_type']" ).val(),
61 screen: $( "input[name='screen']" ).val(),
62 _pll_nonce: $( '#_pll_nonce' ).val()
63 };
64
65 // get the modified rows in ajax and update them
66 $.post(
67 ajaxurl,
68 data,
69 function( response ) {
70 if ( response ) {
71 // Target a non existing WP HTML id to avoid a conflict with WP ajax requests.
72 var res = wpAjax.parseAjaxResponse( response, 'pll-ajax-response' );
73 $.each(
74 res.responses,
75 function() {
76 if ( 'row' == this.what ) {
77 // data is built with a call to WP_Terms_List_Table::single_row method
78 // which uses internally other WordPress methods which escape correctly values.
79 // For Polylang language columns the HTML code is correctly escaped in PLL_Admin_Filters_Columns::term_column method.
80 $( "#tag-" + this.supplemental.term_id ).replaceWith( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.replaceWith
81 }
82 }
83 );
84 }
85 }
86 );
87 }
88
89 var data = wpAjax.unserialize( settings.data ); // what were the data sent by the ajax request?
90 if ( 'undefined' != typeof( data['action'] ) ) {
91 switch ( data['action'] ) {
92 // when adding a term, the new term_id is in the ajax response
93 case 'add-tag':
94 // Target a non existing WP HTML id to avoid a conflict with WP ajax requests.
95 res = wpAjax.parseAjaxResponse( xhr.responseXML, 'pll-ajax-response' );
96 $.each(
97 res.responses,
98 function() {
99 if ( 'term' == this.what ) {
100 update_rows( this.supplemental.term_id );
101 }
102 }
103 );
104
105 // and also reset translations hidden input fields
106 $( '.htr_lang' ).val( 0 );
107 break;
108
109 // when deleting a term
110 case 'delete-tag':
111 update_rows( data['tag_ID'] );
112 break;
113
114 // in case the language is modified in quick edit and breaks translations
115 case 'inline-save-tax':
116 update_rows( data['tax_ID'] );
117 break;
118 }
119 }
120 }
121 );
122 }
123 );
124
125 jQuery(
126 function( $ ) {
127 // translations autocomplete input box
128 function init_translations() {
129 $( '.tr_lang' ).each(
130 function(){
131 var tr_lang = $( this ).attr( 'id' ).substring( 8 );
132 var td = $( this ).parent().parent().siblings( '.pll-edit-column' );
133
134 $( this ).autocomplete(
135 {
136 minLength: 0,
137 source: ajaxurl + '?action=pll_terms_not_translated' +
138 '&term_language=' + $( '#term_lang_choice' ).val() +
139 '&term_id=' + $( "input[name='tag_ID']" ).val() +
140 '&taxonomy=' + $( "input[name='taxonomy']" ).val() +
141 '&translation_language=' + tr_lang +
142 '&post_type=' + typenow +
143 '&_pll_nonce=' + $( '#_pll_nonce' ).val(),
144 select: function( event, ui ) {
145 $( '#htr_lang_' + tr_lang ).val( ui.item.id );
146 // ui.item.link is built and come from server side and is well escaped when necessary
147 td.html( ui.item.link ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
148 },
149 }
150 );
151
152 // when the input box is emptied
153 $( this ).on(
154 'blur',
155 function() {
156 if ( ! $( this ).val() ) {
157 $( '#htr_lang_' + tr_lang ).val( 0 );
158 // Value is retrieved from HTML already generated server side
159 td.html( td.siblings( '.hidden' ).children().clone() ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
160 }
161 }
162 );
163 }
164 );
165 }
166
167 init_translations();
168
169 // ajax for changing the term's language
170 $( '#term_lang_choice' ).on(
171 'change',
172 function() {
173 var value = $( this ).val();
174 var lang = $( this ).children( 'option[value="' + value + '"]' ).attr( 'lang' );
175 var dir = $( '.pll-translation-column > span[lang="' + lang + '"]' ).attr( 'dir' );
176
177 var data = {
178 action: 'term_lang_choice',
179 lang: value,
180 from_tag: $( "input[name='from_tag']" ).val(),
181 term_id: $( "input[name='tag_ID']" ).val(),
182 taxonomy: $( "input[name='taxonomy']" ).val(),
183 post_type: typenow,
184 _pll_nonce: $( '#_pll_nonce' ).val()
185 };
186
187 $.post(
188 ajaxurl,
189 data,
190 function( response ) {
191 // Target a non existing WP HTML id to avoid a conflict with WP ajax requests.
192 var res = wpAjax.parseAjaxResponse( response, 'pll-ajax-response' );
193 $.each(
194 res.responses,
195 function() {
196 switch ( this.what ) {
197 case 'translations': // translations fields
198 // Data is built and come from server side and is well escaped when necessary
199 $( "#term-translations" ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
200 init_translations();
201 break;
202 case 'parent': // parent dropdown list for hierarchical taxonomies
203 // data correctly escaped in PLL_Admin_Filters_Term::term_lang_choice method which uses wp_dropdown_categories function.
204 $( '#parent' ).replaceWith( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.replaceWith
205 break;
206 case 'tag_cloud': // popular items
207 // data correctly escaped in PLL_Admin_Filters_Term::term_lang_choice method which uses wp_tag_cloud and wp_generate_tag_cloud functions.
208 $( '.tagcloud' ).replaceWith( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.replaceWith
209 break;
210 case 'flag': // flag in front of the select dropdown
211 // Data is built and come from server side and is well escaped when necessary
212 $( '.pll-select-flag' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
213 break;
214 }
215 }
216 );
217
218 // Modifies the text direction
219 $( 'body' ).removeClass( 'pll-dir-rtl' ).removeClass( 'pll-dir-ltr' ).addClass( 'pll-dir-' + dir );
220 }
221 );
222 }
223 );
224 }
225 );
226
227