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 / block-editor.js

block-editor.js in Polylang 2.8.2, at js/block-editor.js

201 lines 6.3 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 /**
6 * Filter REST API requests to add the language in the request
7 *
8 * @since 2.5
9 */
10 wp.apiFetch.use(
11 function( options, next ) {
12 // If options.url is defined, this is not a REST request but a direct call to post.php for legacy metaboxes.
13 if ( 'undefined' === typeof options.url ) {
14 if ( 'undefined' === typeof options.data ) {
15 // GET
16 options.path += ( ( options.path.indexOf( '?' ) >= 0 ) ? '&lang=' : '?lang=' ) + getCurrentLanguage();
17 } else {
18 // PUT, POST
19 options.data.lang = getCurrentLanguage();
20 }
21 }
22 return next( options );
23 }
24 );
25
26 /**
27 * Get the language from the HTML form
28 *
29 * @since 2.5
30 *
31 * @return {Element.value}
32 */
33 function getCurrentLanguage() {
34 return document.querySelector( '[name=post_lang_choice]' ).value;
35 }
36
37 /**
38 * Save post after lang choice is done and redirect to the same page for refreshing all the data
39 *
40 * @since 2.5
41 */
42 jQuery( document ).ready(
43 function( $ ) {
44 // savePost after changing the post's language and reload page for refreshing post translated data
45 $( '.post_lang_choice' ).change(
46 function() {
47 const select = wp.data.select;
48 const dispatch = wp.data.dispatch;
49 const subscribe = wp.data.subscribe;
50
51 let unsubscribe = null;
52
53 // Listen if the savePost is done
54 const savePostIsDone = new Promise(
55 function( resolve, reject ) {
56 unsubscribe = subscribe(
57 function() {
58 const isSavePostSucceeded = select( 'core/editor' ).didPostSaveRequestSucceed();
59 const isSavePostFailed = select( 'core/editor' ).didPostSaveRequestFail();
60 if ( isSavePostSucceeded || isSavePostFailed ) {
61 if ( isSavePostFailed ) {
62 reject();
63 } else {
64 resolve();
65 }
66 }
67 }
68 );
69 }
70 );
71
72 // Specific case for empty posts
73 if ( location.pathname.match( /post-new.php/gi ) ) {
74 const title = select( 'core/editor' ).getEditedPostAttribute( 'title' );
75 const content = select( 'core/editor' ).getEditedPostAttribute( 'content' );
76 const excerpt = select( 'core/editor' ).getEditedPostAttribute( 'excerpt' );
77 if ( '' === title && '' === content && '' === excerpt ) {
78 // Change the new_lang parameter with the new language value for reloading the page
79 // WPCS location.search is never written in the page, just used to relaoad page ( See line 94 ) with the right value of new_lang
80 // new_lang input is controlled server side in PHP. The value come from the dropdown list of language returned and escaped server side
81 if ( -1 != location.search.indexOf( 'new_lang' ) ) {
82 // use regexp non capturing group to replace new_lang parameter no matter where it is and capture other parameters which can be behind it
83 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
84 } else {
85 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
86 }
87 }
88 }
89
90 // For empty posts savePost does nothing
91 dispatch( 'core/editor' ).savePost();
92
93 savePostIsDone.then(
94 function() {
95 // If the post is well saved, we can reload the page
96 unsubscribe();
97 window.location.reload();
98 },
99 function() {
100 // If the post save failed
101 unsubscribe();
102 }
103 ).catch(
104 function() {
105 // If an exception is thrown
106 unsubscribe();
107 }
108 );
109 }
110 );
111 }
112 );
113
114 /**
115 * Handles internals of the metabox:
116 * Language select, autocomplete input field.
117 *
118 * @since 1.5
119 */
120 jQuery( document ).ready(
121 function( $ ) {
122 // Ajax for changing the post's language in the languages metabox
123 $( '.post_lang_choice' ).change(
124 function() {
125 var data = {
126 action: 'post_lang_choice',
127 lang: $( this ).val(),
128 post_type: $( '#post_type' ).val(),
129 post_id: $( '#post_ID' ).val(),
130 _pll_nonce: $( '#_pll_nonce' ).val()
131 }
132
133 $.post(
134 ajaxurl,
135 data,
136 function( response ) {
137 var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
138 $.each(
139 res.responses,
140 function() {
141 switch ( this.what ) {
142 case 'translations': // Translations fields
143 // Data is built and come from server side and is well escaped when necessary
144 $( '.translations' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
145 init_translations();
146 break;
147 case 'flag': // Flag in front of the select dropdown
148 // Data is built and come from server side and is well escaped when necessary
149 $( '.pll-select-flag' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html
150 break;
151 }
152 }
153 );
154 }
155 );
156 }
157 );
158
159 // Translations autocomplete input box
160 function init_translations() {
161 $( '.tr_lang' ).each(
162 function(){
163 var tr_lang = $( this ).attr( 'id' ).substring( 8 );
164 var td = $( this ).parent().parent().siblings( '.pll-edit-column' );
165
166 $( this ).autocomplete(
167 {
168 minLength: 0,
169
170 source: ajaxurl + '?action=pll_posts_not_translated' +
171 '&post_language=' + $( '.post_lang_choice' ).val() +
172 '&translation_language=' + tr_lang +
173 '&post_type=' + $( '#post_type' ).val() +
174 '&_pll_nonce=' + $( '#_pll_nonce' ).val(),
175
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