PluginProbe
Polylang / 2.8.2
Polylang v2.8.2
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.8.2, at js/term.js

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