PluginProbe
Polylang / 2.5.2
Polylang v2.5.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
← All changes | js/block-editor.js +59 -200 2.72.5.2 View file →
@@ -2,24 +2,21 @@
2 2 * Filter REST API requests to add the language in the request
3 3 *
4 4 * @since 2.5
5 5 */
6 -
7 -wp.apiFetch.use(
8 - function( options, next ) {
9 - // If options.url is defined, this is not a REST request but a direct call to post.php for legacy metaboxes.
10 - if ( 'undefined' === typeof options.url ) {
11 - if ( 'undefined' === typeof options.data ) {
12 - // GET
13 - options.path += ( ( options.path.indexOf( '?' ) >= 0 ) ? '&lang=' : '?lang=' ) + getCurrentLanguage();
14 - } else {
15 - // PUT, POST
16 - options.data.lang = getCurrentLanguage();
17 - }
6 +wp.apiFetch.use( function( options, next ) {
7 + // If options.url is defined, this is not a REST request but a direct call to post.php for legacy metaboxes.
8 + if ( 'undefined' === typeof options.url ) {
9 + if ( 'undefined' === typeof options.data ) {
10 + // GET
11 + options.path += ( ( options.path.indexOf ( '?' ) >= 0 ) ? '&lang=' : '?lang=' ) + getCurrentLanguage();
12 + } else {
13 + // PUT, POST
14 + options.data.lang = getCurrentLanguage();
18 15 }
19 - return next( options );
20 16 }
21 -);
17 + return next( options );
18 +} );
22 19
23 20 /**
24 21 * Get the language from the HTML form
25 22 *
@@ -31,203 +28,65 @@
31 28 return document.querySelector( '[name=post_lang_choice]' ).value;
32 29 }
33 30
34 31 /**
35 - * Save post after lang choice is done and redirect to the same page for refreshing all the data
32 + * save post after lang choice is done and redirect to the same page for refreshing all the data
36 33 *
37 34 * @since 2.5
38 35 */
39 -jQuery( document ).ready(
40 - function( $ ) {
41 - // savePost after changing the post's language and reload page for refreshing post translated data
42 - $( '.post_lang_choice' ).change(
43 - function() {
44 - const select = wp.data.select;
45 - const dispatch = wp.data.dispatch;
46 - const subscribe = wp.data.subscribe;
36 +jQuery( document ).ready(function( $ ) {
37 + // savePost after changing the post's language and reload page for refreshing post translated data
38 + $( '.post_lang_choice' ).change(function() {
39 + const select = wp.data.select;
40 + const dispatch = wp.data.dispatch;
41 + const subscribe = wp.data.subscribe;
47 42
48 - let unsubscribe = null;
43 + let unsubscribe = null;
49 44
50 - // Listen if the savePost is done
51 - const savePostIsDone = new Promise(
52 - function( resolve, reject ) {
53 - unsubscribe = subscribe(
54 - function() {
55 - const isSavePostSucceeded = select( 'core/editor' ).didPostSaveRequestSucceed();
56 - const isSavePostFailed = select( 'core/editor' ).didPostSaveRequestFail();
57 - if ( isSavePostSucceeded || isSavePostFailed ) {
58 - if ( isSavePostFailed ) {
59 - reject();
60 - } else {
61 - resolve();
62 - }
63 - }
64 - }
65 - );
45 + // Listen if the savePost is done
46 + const savePostIsDone = new Promise( function( resolve, reject ) {
47 + unsubscribe = subscribe( function() {
48 + const isSavePostSucceeded = select('core/editor').didPostSaveRequestSucceed();
49 + const isSavePostFailed = select('core/editor').didPostSaveRequestFail();
50 + if ( isSavePostSucceeded || isSavePostFailed ) {
51 + if ( isSavePostFailed ) {
52 + reject();
53 + } else {
54 + resolve();
66 55 }
67 - );
68 -
69 - // Specific case for empty posts
70 - if ( location.pathname.match( /post-new.php/gi ) ) {
71 - const title = select( 'core/editor' ).getEditedPostAttribute( 'title' );
72 - const content = select( 'core/editor' ).getEditedPostAttribute( 'content' );
73 - const excerpt = select( 'core/editor' ).getEditedPostAttribute( 'excerpt' );
74 - if ( '' === title && '' === content && '' === excerpt ) {
75 - // Change the new_lang parameter with the new language value for reloading the page
76 - // WPCS location.search is never written in the page, just used to relaoad page ( See line 94 ) with the right value of new_lang
77 - // new_lang input is controlled server side in PHP. The value come from the dropdown list of language returned and escaped server side
78 - if ( -1 != location.search.indexOf( 'new_lang' ) ) {
79 - // use regexp non capturing group to replace new_lang parameter no matter where it is and capture other parameters which can be behind it
80 - window.location.search = window.location.search.replace( /(?:new_lang=[^&]*)(&)?(.*)/, 'new_lang=' + this.value + '$1$2' ); // phpcs:ignore WordPressVIPMinimum.JS.Window.location, WordPressVIPMinimum.JS.Window.VarAssignment
81 - } else {
82 - window.location.search = window.location.search + ( ( -1 != window.location.search.indexOf( '?' ) ) ? '&' : '?' ) + 'new_lang=' + this.value; // phpcs:ignore WordPressVIPMinimum.JS.Window.location, WordPressVIPMinimum.JS.Window.VarAssignment
83 - }
84 - }
85 56 }
57 + } );
58 + });
86 59
87 - // For empty posts savePost does nothing
88 - dispatch( 'core/editor' ).savePost();
89 -
90 - savePostIsDone.then(
91 - function() {
92 - // If the post is well saved, we can reload the page
93 - unsubscribe();
94 - window.location.reload();
95 - },
96 - function() {
97 - // If the post save failed
98 - unsubscribe();
99 - }
100 - ).catch(
101 - function() {
102 - // If an exception is thrown
103 - unsubscribe();
104 - }
105 - );
106 - }
107 - );
108 - }
109 -);
110 -
111 -/**
112 - * Handles internals of the metabox:
113 - * Language select, autocomplete input field and buttons.
114 - *
115 - * @since 1.5
116 - */
117 -jQuery( document ).ready(
118 - function( $ ) {
119 - // Ajax for changing the post's language in the languages metabox
120 - $( '.post_lang_choice' ).change(
121 - function() {
122 - var data = {
123 - action: 'post_lang_choice',
124 - lang: $( this ).val(),
125 - post_type: $( '#post_type' ).val(),
126 - post_id: $( '#post_ID' ).val(),
127 - _pll_nonce: $( '#_pll_nonce' ).val()
60 + // Specific case for empty posts
61 + if ( location.pathname.match( /post-new.php/gi ) ) {
62 + const title = select('core/editor').getEditedPostAttribute('title');
63 + const content = select('core/editor').getEditedPostAttribute('content');
64 + const excerpt = select('core/editor').getEditedPostAttribute('excerpt');
65 + if ( '' === title && '' === content && '' === excerpt ) {
66 + // Change the new_lang parameter with the new language value for reloading the page
67 + if ( -1 != location.search.indexOf( 'new_lang' ) ) {
68 + window.location.search = window.location.search.replace( /(?:new_lang=[^&]*)(&)?(.*)/, 'new_lang=' + this.value + '$1$2' );;
69 + } else {
70 + window.location.search = window.location.search + ( ( -1 != window.location.search.indexOf( '?' ) ) ? '&' : '?' ) + 'new_lang=' + this.value;
128 71 }
129 -
130 - $.post(
131 - ajaxurl,
132 - data,
133 - function( response ) {
134 - var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
135 - $.each(
136 - res.responses,
137 - function() {
138 - switch ( this.what ) {
139 - case 'translations': // Translations fields
140 - // Data is built and come from server side and is well escaped when necessary
141 - $( '.translations' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
142 - init_translations();
143 - break;
144 - case 'flag': // Flag in front of the select dropdown
145 - // Data is built and come from server side and is well escaped when necessary
146 - $( '.pll-select-flag' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
147 - break;
148 - }
149 - }
150 - );
151 - }
152 - );
153 72 }
154 - );
155 -
156 - // Translations autocomplete input box
157 - function init_translations() {
158 - $( '.tr_lang' ).each(
159 - function(){
160 - var tr_lang = $( this ).attr( 'id' ).substring( 8 );
161 - var td = $( this ).parent().parent().siblings( '.pll-edit-column' );
162 -
163 - $( this ).autocomplete(
164 - {
165 - minLength: 0,
166 -
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 -
173 - select: function( event, ui ) {
174 - $( '#htr_lang_' + tr_lang ).val( ui.item.id );
175 - // ui.item.link is built and come from server side and is well escaped when necessary
176 - td.html( ui.item.link ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
177 - },
178 - }
179 - );
180 -
181 - // When the input box is emptied
182 - $( this ).blur(
183 - function() {
184 - if ( ! $( this ).val() ) {
185 - $( '#htr_lang_' + tr_lang ).val( 0 );
186 - // Value is retrieved from HTML already generated server side
187 - td.html( td.siblings( '.hidden' ).children().clone() ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
188 - }
189 - }
190 - );
191 - }
192 - );
193 73 }
194 74
195 - init_translations();
75 + // For empty posts savePost does nothing
76 + dispatch( 'core/editor' ).savePost();
196 77
197 - // Handle the response to a click on a Languages metabox button
198 - $( '#ml_box' ).on(
199 - 'click',
200 - '.pll-button',
201 - function(){
202 - var value = $( this ).hasClass( 'wp-ui-text-highlight' );
203 - var id = $( this ).attr( 'id' );
204 - var post_id = $( '#htr_lang_' + id.replace( 'pll_sync_post[', '' ).replace( ']', '' ) ).val();
205 -
206 - if ( 'undefined' == typeof( post_id ) || 0 == post_id || value || confirm( confirm_text ) ) {
207 - var data = {
208 - action: 'toggle_' + id,
209 - value: value,
210 - post_type: $( '#post_type' ).val(),
211 - _pll_nonce: $( '#_pll_nonce' ).val()
212 - }
213 -
214 - $.post(
215 - ajaxurl,
216 - data,
217 - function( response ){
218 - var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
219 - $.each(
220 - res.responses,
221 - function() {
222 - id = id.replace( '[', '\\[' ).replace( ']', '\\]' );
223 - $( '#' + id ).toggleClass( 'wp-ui-text-highlight' ).attr( 'title', this.data ).children( 'span' ).text( this.data );
224 - $( 'input[name="' + id + '"]' ).val( ! data['value'] );
225 - }
226 - );
227 - }
228 - );
229 - }
230 - }
231 - );
232 - }
233 -);
78 + savePostIsDone
79 + .then( function() {
80 + // If the post is well saved, we can reload the page
81 + unsubscribe();
82 + window.location.reload();
83 + }, function() {
84 + // If the post save failed
85 + unsubscribe();
86 + } )
87 + .catch( function() {
88 + // If an exception is thrown
89 + unsubscribe();
90 + } );
91 + } );
92 +} );