PluginProbe
Polylang / 3.8.10
Polylang v3.8.10
3.8.10 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 All 234 releases
← All changes | js/build/classic-editor.js +164 -134 3.0 → 3.8.10 View file →
@@ -1,19 +1,19 @@
1 1 /******/ "use strict";
2 2
3 -;// CONCATENATED MODULE: ./js/lib/confirmation-modal.js
3 +;// ./js/src/lib/confirmation-modal.js
4 4 /**
5 5 * @package Polylang
6 6 */
7 7
8 -// We can't use underscore or lodash in this common code because it depends of the context classic or block editor.
9 -// Classic editor underscore is loaded, Block editor lodash is loaded.
10 -const { __ } = wp.i18n;
11 -
12 8 const languagesList = jQuery( '.post_lang_choice' );
13 9
14 10 // Dialog box for alerting the user about a risky changing.
15 -const initializeConfimationModal = () => {
11 +const initializeConfirmationModal = () => {
12 + // We can't use underscore or lodash in this common code because it depends of the context classic or block editor.
13 + // Classic editor underscore is loaded, Block editor lodash is loaded.
14 + const { __ } = wp.i18n;
15 +
16 16 // Create dialog container.
17 17 const dialogContainer = jQuery(
18 18 '<div/>',
19 19 {
@@ -31,9 +31,9 @@
31 31 const confirmDialog = ( what ) => { // phpcs:ignore PEAR.Functions.FunctionCallSignature.Indent
32 32 switch ( what ) { // phpcs:ignore PEAR.Functions.FunctionCallSignature.Indent
33 33 case 'yes':
34 34 // Confirm the new language.
35 - languagesList.data( 'old-value', languagesList.children( ':selected' )[0].value );
35 + languagesList.data( 'old-value', languagesList.children( ':selected' ).first().val() );
36 36 confirm();
37 37 break;
38 38 case 'no':
39 39 // Revert to the old language.
@@ -44,50 +44,54 @@
44 44 dialogContainer.dialog( 'close' ); // phpcs:ignore PEAR.Functions.FunctionCallSignature.Indent
45 45 } // phpcs:ignore PEAR.Functions.FunctionCallSignature.Indent
46 46
47 47 // Initialize dialog box in the case a language is selected but not added in the list.
48 - dialogContainer.dialog(
49 - {
50 - autoOpen: false,
51 - modal: true,
52 - draggable: false,
53 - resizable: false,
54 - title: __( 'Change language', 'polylang' ),
55 - minWidth: 600,
56 - maxWidth: '100%',
57 - classes: {
58 - 'ui-dialog': 'pll-confirmation-modal',
59 - },
60 - open: function( event, ui ) {
61 - // Change dialog box position for rtl language
62 - if ( jQuery( 'body' ).hasClass( 'rtl' ) ) {
63 - jQuery( this ).parent().css(
64 - {
65 - right: jQuery( this ).parent().css( 'left' ),
66 - left: 'auto'
67 - }
68 - );
69 - }
70 - },
71 - close: function( event, ui ) {
72 - // When we're closing the dialog box we need to cancel the language change as we click on Cancel button.
73 - confirmDialog( 'no' );
74 - },
75 - buttons: [
48 + const dialogOptions = {
49 + autoOpen: false,
50 + modal: true,
51 + draggable: false,
52 + resizable: false,
53 + title: __( 'Change language', 'polylang' ),
54 + minWidth: 600,
55 + maxWidth: '100%',
56 + open: function ( event, ui ) {
57 + // Change dialog box position for rtl language
58 + if ( jQuery( 'body' ).hasClass( 'rtl' ) ) {
59 + jQuery( this ).parent().css(
60 + {
61 + right: jQuery( this ).parent().css( 'left' ),
62 + left: 'auto'
63 + }
64 + );
65 + }
66 + },
67 + close: function ( event, ui ) {
68 + // When we're closing the dialog box we need to cancel the language change as we click on Cancel button.
69 + confirmDialog( 'no' );
70 + },
71 + buttons: [
76 72 {
77 73 text: __( 'OK', 'polylang' ),
78 - click: function( event ) {
74 + click: function ( event ) {
79 75 confirmDialog( 'yes' );
80 76 }
81 77 },
82 78 {
83 79 text: __( 'Cancel', 'polylang' ),
84 - click: function( event ) {
80 + click: function ( event ) {
85 81 confirmDialog( 'no' );
86 82 }
87 - } ]
88 - }
89 - );
83 + }
84 + ]
85 + };
86 +
87 + if ( jQuery.ui.version >= '1.12.0' ) {
88 + Object.assign( dialogOptions, { classes: { 'ui-dialog': 'pll-confirmation-modal' } } );
89 + } else {
90 + Object.assign( dialogOptions, { dialogClass: 'pll-confirmation-modal' } ); // jQuery UI 1.11.4 - WP < 5.6
91 + }
92 +
93 + dialogContainer.dialog( dialogOptions );
90 94 }
91 95 );
92 96 return { dialogContainer, dialogResult };
93 97 }
@@ -93,23 +97,68 @@
93 97 }
94 98
95 99 const initializeLanguageOldValue = () => {
96 100 // Keep the old language value to be able to compare to the new one and revert to it if necessary.
97 - languagesList.attr( 'data-old-value', languagesList.children( ':selected' )[0].value );
101 + languagesList.attr( 'data-old-value', languagesList.children( ':selected' ).first().val() );
98 102 };
99 103
100 -;// CONCATENATED MODULE: ./js/classic-editor.js
104 +;// ./js/src/lib/metabox-autocomplete.js
101 105 /**
102 106 * @package Polylang
103 107 */
104 108
109 +// Translations autocomplete input box.
110 +function initMetaboxAutoComplete() {
111 + jQuery('.tr_lang').each(
112 + function () {
113 + var tr_lang = jQuery(this).attr('id').substring(8);
114 + var td = jQuery(this).parent().parent().siblings('.pll-edit-column');
105 115
116 + jQuery(this).autocomplete(
117 + {
118 + minLength: 0,
119 + source: ajaxurl + '?action=pll_posts_not_translated' +
120 + '&post_language=' + jQuery('.post_lang_choice').val() +
121 + '&translation_language=' + tr_lang +
122 + '&post_type=' + jQuery('#post_type').val() +
123 + '&_pll_nonce=' + jQuery('#_pll_nonce').val(),
124 + select: function (event, ui) {
125 + jQuery('#htr_lang_' + tr_lang).val(ui.item.id);
126 + // ui.item.link is built and come from server side and is well escaped when necessary
127 + td.html(ui.item.link); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
128 + },
129 + }
130 + );
106 131
132 + // when the input box is emptied
133 + jQuery(this).on(
134 + 'blur',
135 + function () {
136 + if ( ! jQuery(this).val() ) {
137 + jQuery('#htr_lang_' + tr_lang).val(0);
138 + // Value is retrieved from HTML already generated server side
139 + td.html(td.siblings('.hidden').children().clone()); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
140 + }
141 + }
142 + );
143 + }
144 + );
145 +}
146 +
147 +;// ./js/src/classic-editor.js
148 +/**
149 + * @package Polylang
150 + */
151 +
152 +
153 +
154 +
155 +
107 156 // tag suggest in metabox
108 157 jQuery(
109 - function( $ ) {
158 + function ( $ ) {
110 159 $.ajaxPrefilter(
111 - function( options, originalOptions, jqXHR ) {
160 + function ( options, originalOptions, jqXHR ) {
112 161 var lang = $( '.post_lang_choice' ).val();
113 162 if ( 'string' === typeof options.data && -1 !== options.url.indexOf( 'action=ajax-tag-search' ) && lang ) {
114 163 options.data = 'lang=' + lang + '&' + options.data;
115 164 }
@@ -119,11 +168,11 @@
119 168 );
120 169
121 170 // overrides tagBox.get
122 171 jQuery(
123 - function( $ ) {
172 + function ( $ ) {
124 173 // overrides function to add the language
125 - tagBox.get = function( id ) {
174 + tagBox.get = function ( id ) {
126 175 var tax = id.substr( id.indexOf( '-' ) + 1 );
127 176
128 177 // add the language in the $_POST variable
129 178 var data = {
@@ -134,9 +183,9 @@
134 183
135 184 $.post(
136 185 ajaxurl,
137 186 data,
138 - function( r, stat ) {
187 + function ( r, stat ) {
139 188 if ( 0 == r || 'success' != stat ) {
140 189 r = wpAjax.broken;
141 190 }
142 191
@@ -144,9 +193,9 @@
144 193 // @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
145 194 r = $( '<div />' ).addClass( 'the-tagcloud' ).attr( 'id', 'tagcloud-' + tax ).html( r ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
146 195 $( 'a', r ).on(
147 196 'click',
148 - function(){
197 + function () {
149 198 tagBox.flushTags( $( this ).closest( '.inside' ).children( '.tagsdiv' ), this );
150 199 return false;
151 200 }
152 201 );
@@ -151,9 +200,9 @@
151 200 }
152 201 );
153 202
154 203 var tagCloud = $( '#tagcloud-' + tax );
155 - // add an if else condition to allow modifying the tags outputed when switching the language
204 + // add an if else condition to allow modifying the tags outputted when switching the language
156 205 var v = tagCloud.css( 'display' );
157 206 if ( v ) {
158 207 // See the comment above when r variable is created.
159 208 $( '#tagcloud-' + tax ).replaceWith( r ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.replaceWith
@@ -169,13 +218,13 @@
169 218 }
170 219 );
171 220
172 221 jQuery(
173 - function( $ ) {
222 + function ( $ ) {
174 223 // collect taxonomies - code partly copied from WordPress
175 224 var taxonomies = new Array();
176 225 $( '.categorydiv' ).each(
177 - function(){
226 + function () {
178 227 var this_id = $( this ).attr( 'id' ), taxonomyParts, taxonomy;
179 228
180 229 taxonomyParts = this_id.split( '-' );
181 230 taxonomyParts.shift();
@@ -199,11 +248,11 @@
199 248
200 249 // ajax for changing the post's language in the languages metabox
201 250 $( '.post_lang_choice' ).on(
202 251 'change',
203 - function( event ) {
252 + function ( event ) {
204 253 // Initialize the confirmation dialog box.
205 - const confirmationModal = initializeConfimationModal();
254 + const confirmationModal = initializeConfirmationModal();
206 255 const { dialogContainer: dialog } = confirmationModal;
207 256 let { dialogResult } = confirmationModal;
208 257 // The selected option in the dropdown list.
209 258 const selectedOption = event.target;
@@ -213,15 +262,11 @@
213 262 } else {
214 263 dialogResult = Promise.resolve();
215 264 }
216 265
217 - // phpcs:disable PEAR.Functions.FunctionCallSignature.EmptyLine
218 266 dialogResult.then(
219 267 () => {
220 - var lang = selectedOption.options[selectedOption.options.selectedIndex].lang; // phpcs:ignore PEAR.Functions.FunctionCallSignature.Indent
221 - var dir = $( '.pll-translation-column > span[lang="' + lang + '"]' ).attr( 'dir' ); // phpcs:ignore PEAR.Functions.FunctionCallSignature.Indent
222 -
223 - var data = { // phpcs:ignore PEAR.Functions.FunctionCallSignature.Indent
268 + var data = {
224 269 action: 'post_lang_choice',
225 270 lang: selectedOption.value,
226 271 post_type: $( '#post_type' ).val(),
227 272 taxonomies: taxonomies,
@@ -231,18 +276,19 @@
231 276
232 277 $.post(
233 278 ajaxurl,
234 279 data,
235 - function( response ) {
236 - var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
280 + function ( response ) {
281 + // Target a non existing WP HTML id to avoid a conflict with WP ajax requests.
282 + var res = wpAjax.parseAjaxResponse( response, 'pll-ajax-response' );
237 283 $.each(
238 284 res.responses,
239 - function() {
285 + function () {
240 286 switch ( this.what ) {
241 287 case 'translations': // translations fields
242 288 // Data is built and come from server side and is well escaped when necessary
243 289 $( '.translations' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
244 - init_translations();
290 + initMetaboxAutoComplete();
245 291 break;
246 292 case 'taxonomy': // categories metabox for posts
247 293 var tax = this.data;
248 294 // @see wp_terms_checklist https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/template.php#L175
@@ -274,30 +320,23 @@
274 320 }
275 321 }
276 322 );
277 323
278 - // Update the old language with the new one to be able to compare it in the next changing.
279 - initializeLanguageOldValue();
280 - // modifies the language in the tag cloud
281 - $( '.tagcloud-link' ).each(
282 - function() {
283 - var id = $( this ).attr( 'id' );
284 - tagBox.get( id );
324 + // Creates an event once the language has been successfully changed.
325 + const onPostLangChoice = new CustomEvent(
326 + "onPostLangChoice",
327 + {
328 + detail: {
329 + lang: JSON.parse( selectedOption.options[selectedOption.options.selectedIndex].getAttribute( 'data-lang' ) )
330 + },
285 331 }
286 332 );
287 -
288 - // Modifies the text direction
289 - $( 'body' ).removeClass( 'pll-dir-rtl' ).removeClass( 'pll-dir-ltr' ).addClass( 'pll-dir-' + dir );
290 - $( '#content_ifr' ).contents().find( 'html' ).attr( 'lang', lang ).attr( 'dir', dir );
291 - $( '#content_ifr' ).contents().find( 'body' ).attr( 'dir', dir );
292 -
293 - pll.media.resetAllAttachmentsCollections();
333 + document.dispatchEvent( onPostLangChoice );
294 334 }
295 335 )
296 336 },
297 337 () => {} // Do nothing when promise is rejected by clicking the Cancel dialog button.
298 338 );
299 - // phpcs:enable PEAR.Functions.FunctionCallSignature.EmptyLine
300 339
301 340 function isEmptyPost() {
302 341 const title = $( 'input#title' ).val();
303 342 const content = $( 'textarea#content' ).val();
@@ -307,47 +346,35 @@
307 346 }
308 347 }
309 348 );
310 349
311 - // translations autocomplete input box
312 - function init_translations() {
313 - $( '.tr_lang' ).each(
314 - function(){
315 - var tr_lang = $( this ).attr( 'id' ).substring( 8 );
316 - var td = $( this ).parent().parent().siblings( '.pll-edit-column' );
350 + // Listen to `onPostLangChoice` to perform actions after the language has been changed.
351 + document.addEventListener(
352 + 'onPostLangChoice',
353 + ( e ) => {
354 + // Update the old language with the new one to be able to compare it in the next changing.
355 + initializeLanguageOldValue();
317 356
318 - $( this ).autocomplete(
319 - {
320 - minLength: 0,
321 - source: ajaxurl + '?action=pll_posts_not_translated' +
322 - '&post_language=' + $( '.post_lang_choice' ).val() +
323 - '&translation_language=' + tr_lang +
324 - '&post_type=' + $( '#post_type' ).val() +
325 - '&_pll_nonce=' + $( '#_pll_nonce' ).val(),
326 - select: function( event, ui ) {
327 - $( '#htr_lang_' + tr_lang ).val( ui.item.id );
328 - // ui.item.link is built and come from server side and is well escaped when necessary
329 - td.html( ui.item.link ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
330 - },
331 - }
332 - );
357 + // Modifies the language in the tag cloud.
358 + $( '.tagcloud-link' ).each(
359 + function () {
360 + var id = $( this ).attr( 'id' );
361 + tagBox.get( id );
362 + }
363 + );
333 364
334 - // when the input box is emptied
335 - $( this ).on(
336 - 'blur',
337 - function() {
338 - if ( ! $( this ).val() ) {
339 - $( '#htr_lang_' + tr_lang ).val( 0 );
340 - // Value is retrieved from HTML already generated server side
341 - td.html( td.siblings( '.hidden' ).children().clone() ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
342 - }
343 - }
344 - );
345 - }
346 - );
347 - }
365 + // Modifies the text direction.
366 + let dir = e.detail.lang.is_rtl ? 'rtl' : 'ltr'
367 + $( 'body' ).removeClass( 'pll-dir-rtl' ).removeClass( 'pll-dir-ltr' ).addClass( 'pll-dir-' + dir );
368 + $( '#content_ifr' ).contents().find( 'html' ).attr( 'lang', e.detail.lang.locale ).attr( 'dir', dir );
369 + $( '#content_ifr' ).contents().find( 'body' ).attr( 'dir', dir );
348 370
349 - init_translations();
371 + // Refresh media libraries.
372 + pll.media.resetAllAttachmentsCollections();
373 + }
374 + );
375 +
376 + initMetaboxAutoComplete();
350 377 }
351 378 );
352 379
353 380 /**
@@ -386,10 +413,10 @@
386 413 *
387 414 * @param {Object} [props]
388 415 * @return {wp.media.model.Attachments}
389 416 */
390 - query: function( props ) {
391 - var attachments = pll.media.query.delegate();
417 + query: function ( props ) {
418 + var attachments = pll.media.query.delegate( props );
392 419
393 420 pll.media.attachmentsCollections.push( attachments );
394 421
395 422 return attachments;
@@ -394,16 +421,16 @@
394 421
395 422 return attachments;
396 423 },
397 424
398 - resetAllAttachmentsCollections: function() {
425 + resetAllAttachmentsCollections: function () {
399 426 this.attachmentsCollections.forEach(
400 - function( attachmentsCollection ) {
427 + function ( attachmentsCollection ) {
401 428 /**
402 429 * First reset the { @see wp.media.model.Attachments } collection.
403 430 * Then, if it is mirroring a { @see wp.media.model.Query } collection,
404 431 * refresh this one too, so it will fetch new data from the server,
405 - * and then the wp.media.model.Attachments collection will syncrhonize with the new data.
432 + * and then the wp.media.model.Attachments collection will synchronize with the new data.
406 433 */
407 434 attachmentsCollection.reset();
408 435 if (attachmentsCollection.mirroring) {
409 436 attachmentsCollection.mirroring._hasMore = true;
@@ -414,23 +441,26 @@
414 441 }
415 442 }
416 443 );
417 444
418 -/**
419 - * @since 3.0
420 - *
421 - * @memberOf pll.media
422 - */
423 -media.query = _.extend(
424 - media.query, /** @lends pll.media.query prototype */
425 - {
426 - /**
427 - * @type Function References WordPress { @see wp.media.query } constructor
428 - */
429 - delegate: wp.media.query
430 - }
431 -)
445 +if ( 'undefined' !== typeof wp && 'undefined' !== typeof wp.media ) {
432 446
433 -// Substitute WordPress media query shortcut with our decorated function.
434 -wp.media.query = media.query
447 + /**
448 + * @since 3.0
449 + *
450 + * @memberOf pll.media
451 + */
452 + media.query = _.extend(
453 + media.query, /** @lends pll.media.query prototype */
454 + {
455 + /**
456 + * @type Function References WordPress { @see wp.media.query } constructor
457 + */
458 + delegate: wp.media.query
459 + }
460 + )
435 461
462 + // Substitute WordPress media query shortcut with our decorated function.
463 + wp.media.query = media.query
464 +
465 +}
436 466