PluginProbe
Polylang / 2.8.1
Polylang v2.8.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.8.1, at js/classic-editor.js

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