PluginProbe
Polylang / 2.7.1
Polylang v2.7.1
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 / classic-editor.js

classic-editor.js in Polylang 2.7.1, at js/classic-editor.js

233 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // tag suggest in metabox
2 (function( $ ){
3 $.ajaxPrefilter(
4 function( options, originalOptions, jqXHR ) {
5 if ( 'string' === typeof options.data && -1 !== options.url.indexOf( 'action=ajax-tag-search' ) && ( lang = $( '.post_lang_choice' ).val() ) ) {
6 options.data = 'lang=' + lang + '&' + options.data;
7 }
8 }
9 );
10 })( jQuery );
11
12 // overrides tagBox.get
13 (function( $ ){
14 // overrides function to add the language
15 tagBox.get = function( id ) {
16 var tax = id.substr( id.indexOf( '-' ) + 1 );
17
18 // add the language in the $_POST variable
19 var data = {
20 action: 'get-tagcloud',
21 lang: $( '.post_lang_choice' ).val(),
22 tax: tax
23 }
24
25 $.post(
26 ajaxurl,
27 data,
28 function( r, stat ) {
29 if ( 0 == r || 'success' != stat ) {
30 r = wpAjax.broken;
31 }
32
33 // @see code from WordPress core https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/js/tags-box.js#L291
34 // @see wp_generate_tag_cloud function which generate the escaped HTML https://github.com/WordPress/WordPress/blob/a02b5cc2a8eecb8e076fbb7cf4de7bd2ec8a8eb1/wp-includes/category-template.php#L966-L975
35 r = $( '<div />' ).addClass( 'the-tagcloud' ).attr( 'id', 'tagcloud-' + tax ).html( r ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
36 $( 'a', r ).click(
37 function(){
38 tagBox.flushTags( $( this ).closest( '.inside' ).children( '.tagsdiv' ), this );
39 return false;
40 }
41 );
42
43 // add an if else condition to allow modifying the tags outputed when switching the language
44 if ( v = $( '.the-tagcloud' ).css( 'display' ) ) {
45 $( '.the-tagcloud' ).replaceWith( r );
46 $( '.the-tagcloud' ).css( 'display', v );
47 }
48 else {
49 $( '#' + id ).after( r );
50 }
51 }
52 );
53 }
54 })( jQuery );
55
56 jQuery( document ).ready(
57 function( $ ) {
58 // collect taxonomies - code partly copied from WordPress
59 var taxonomies = new Array();
60 $( '.categorydiv' ).each(
61 function(){
62 var this_id = $( this ).attr( 'id' ), taxonomyParts, taxonomy;
63
64 taxonomyParts = this_id.split( '-' );
65 taxonomyParts.shift();
66 taxonomy = taxonomyParts.join( '-' );
67 taxonomies.push( taxonomy ); // store the taxonomy for future use
68
69 // add our hidden field in the new category form - for each hierarchical taxonomy
70 // to set the language when creating a new category
71 $( '#' + taxonomy + '-add-submit' ).before(
72 $( '<input />' ).attr( 'type', 'hidden' )
73 .attr( 'id', taxonomy + '-lang' )
74 .attr( 'name', 'term_lang_choice' )
75 .attr( 'value', $( '.post_lang_choice' ).val() )
76 );
77 }
78 );
79
80 // ajax for changing the post's language in the languages metabox
81 $( '.post_lang_choice' ).change(
82 function() {
83 var value = $( this ).val();
84 var lang = $( this ).children( 'option[value="' + value + '"]' ).attr( 'lang' );
85 var dir = $( '.pll-translation-column > span[lang="' + lang + '"]' ).attr( 'dir' );
86
87 var data = {
88 action: 'post_lang_choice',
89 lang: value,
90 post_type: $( '#post_type' ).val(),
91 taxonomies: taxonomies,
92 post_id: $( '#post_ID' ).val(),
93 _pll_nonce: $( '#_pll_nonce' ).val()
94 }
95
96 $.post(
97 ajaxurl,
98 data,
99 function( response ) {
100 var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
101 $.each(
102 res.responses,
103 function() {
104 switch ( this.what ) {
105 case 'translations': // translations fields
106 // Data is built and come from server side and is well escaped when necessary
107 $( '.translations' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
108 init_translations();
109 break;
110 case 'taxonomy': // categories metabox for posts
111 var tax = this.data;
112 // @see wp_terms_checklist https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/template.php#L175
113 // @see https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/class-walker-category-checklist.php#L89-L111
114 $( '#' + tax + 'checklist' ).html( this.supplemental.all ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
115 // @see wp_popular_terms_checklist https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/template.php#L236
116 $( '#' + tax + 'checklist-pop' ).html( this.supplemental.populars ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
117 $( '#new' + tax + '_parent' ).replaceWith( this.supplemental.dropdown );
118 $( '#' + tax + '-lang' ).val( $( '.post_lang_choice' ).val() ); // hidden field
119 break;
120 case 'pages': // parent dropdown list for pages
121 // @see wp_dropdown_pages https://github.com/WordPress/WordPress/blob/5.2.2/wp-includes/post-template.php#L1186-L1208
122 // @see https://github.com/WordPress/WordPress/blob/5.2.2/wp-includes/class-walker-page-dropdown.php#L88
123 $( '#parent_id' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
124 break;
125 case 'flag': // flag in front of the select dropdown
126 // Data is built and come from server side and is well escaped when necessary
127 $( '.pll-select-flag' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
128 break;
129 case 'permalink': // Sample permalink
130 var div = $( '#edit-slug-box' );
131 if ( '-1' != this.data && div.children().length ) {
132 // @see get_sample_permalink_html https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/post.php#L1425-L1454
133 div.html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
134 }
135 break;
136 }
137 }
138 );
139
140 // modifies the language in the tag cloud
141 $( '.tagcloud-link' ).each(
142 function() {
143 var id = $( this ).attr( 'id' );
144 tagBox.get( id );
145 }
146 );
147
148 // Modifies the text direction
149 $( 'body' ).removeClass( 'pll-dir-rtl' ).removeClass( 'pll-dir-ltr' ).addClass( 'pll-dir-' + dir );
150 $( '#content_ifr' ).contents().find( 'html' ).attr( 'lang', lang ).attr( 'dir', dir );
151 $( '#content_ifr' ).contents().find( 'body' ).attr( 'dir', dir );
152 }
153 );
154 }
155 );
156
157 // translations autocomplete input box
158 function init_translations() {
159 $( '.tr_lang' ).each(
160 function(){
161 var tr_lang = $( this ).attr( 'id' ).substring( 8 );
162 var td = $( this ).parent().parent().siblings( '.pll-edit-column' );
163
164 $( this ).autocomplete(
165 {
166 minLength: 0,
167 source: ajaxurl + '?action=pll_posts_not_translated' +
168 '&post_language=' + $( '.post_lang_choice' ).val() +
169 '&translation_language=' + tr_lang +
170 '&post_type=' + $( '#post_type' ).val() +
171 '&_pll_nonce=' + $( '#_pll_nonce' ).val(),
172 select: function( event, ui ) {
173 $( '#htr_lang_' + tr_lang ).val( ui.item.id );
174 // ui.item.link is built and come from server side and is well escaped when necessary
175 td.html( ui.item.link ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
176 },
177 }
178 );
179
180 // when the input box is emptied
181 $( this ).blur(
182 function() {
183 if ( ! $( this ).val() ) {
184 $( '#htr_lang_' + tr_lang ).val( 0 );
185 // Value is retrieved from HTML already generated server side
186 td.html( td.siblings( '.hidden' ).children().clone() ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
187 }
188 }
189 );
190 }
191 );
192 }
193
194 init_translations();
195
196 // Handle the response to a click on a Languages metabox button
197 $( '#ml_box' ).on(
198 'click',
199 '.pll-button',
200 function(){
201 var value = $( this ).hasClass( 'wp-ui-text-highlight' );
202 var id = $( this ).attr( 'id' );
203 var post_id = $( '#htr_lang_' + id.replace( 'pll_sync_post[', '' ).replace( ']', '' ) ).val();
204
205 if ( 'undefined' == typeof( post_id ) || 0 == post_id || value || confirm( confirm_text ) ) {
206 var data = {
207 action: 'toggle_' + id,
208 value: value,
209 post_type: $( '#post_type' ).val(),
210 _pll_nonce: $( '#_pll_nonce' ).val()
211 }
212
213 $.post(
214 ajaxurl,
215 data,
216 function( response ){
217 var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
218 $.each(
219 res.responses,
220 function() {
221 id = id.replace( '[', '\\[' ).replace( ']', '\\]' );
222 $( '#' + id ).toggleClass( 'wp-ui-text-highlight' ).attr( 'title', this.data ).children( 'span' ).text( this.data );
223 $( 'input[name="' + id + '"]' ).val( ! data['value'] );
224 }
225 );
226 }
227 );
228 }
229 }
230 );
231 }
232 );
233