PluginProbe
Polylang / 2.9
Polylang v2.9
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 / term.js

term.js in Polylang 2.9, at js/term.js

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