PluginProbe
Polylang / 2.6.7
Polylang v2.6.7
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.6.7, at js/block-editor.js

185 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Filter REST API requests to add the language in the request
3 *
4 * @since 2.5
5 */
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();
15 }
16 }
17 return next( options );
18 } );
19
20 /**
21 * Get the language from the HTML form
22 *
23 * @since 2.5
24 *
25 * @return {Element.value}
26 */
27 function getCurrentLanguage() {
28 return document.querySelector( '[name=post_lang_choice]' ).value;
29 }
30
31 /**
32 * Save post after lang choice is done and redirect to the same page for refreshing all the data
33 *
34 * @since 2.5
35 */
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;
42
43 let unsubscribe = null;
44
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();
55 }
56 }
57 } );
58 });
59
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;
71 }
72 }
73 }
74
75 // For empty posts savePost does nothing
76 dispatch( 'core/editor' ).savePost();
77
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 } );
93
94 /**
95 * Handles internals of the metabox:
96 * Language select, autocomplete input field and buttons.
97 *
98 * @since 1.5
99 */
100 jQuery( document ).ready(function( $ ) {
101 // Ajax for changing the post's language in the languages metabox
102 $( '.post_lang_choice' ).change(function() {
103 var data = {
104 action: 'post_lang_choice',
105 lang: $( this ).val(),
106 post_type: $( '#post_type' ).val(),
107 post_id: $( '#post_ID' ).val(),
108 _pll_nonce: $( '#_pll_nonce' ).val()
109 }
110
111 $.post( ajaxurl, data , function( response ) {
112 var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
113 $.each( res.responses, function() {
114 switch ( this.what ) {
115 case 'translations': // Translations fields
116 $( '.translations' ).html( this.data );
117 init_translations();
118 break;
119 case 'flag': // Flag in front of the select dropdown
120 $( '.pll-select-flag' ).html( this.data );
121 break;
122 }
123 });
124 });
125 });
126
127 // Translations autocomplete input box
128 function init_translations() {
129 $( '.tr_lang' ).each(function(){
130 var tr_lang = $( this ).attr( 'id' ).substring( 8 );
131 var td = $( this ).parent().parent().siblings( '.pll-edit-column' );
132
133 $( this ).autocomplete({
134 minLength: 0,
135
136 source: ajaxurl + '?action=pll_posts_not_translated' +
137 '&post_language=' + $( '.post_lang_choice' ).val() +
138 '&translation_language=' + tr_lang +
139 '&post_type=' + $( '#post_type' ).val() +
140 '&_pll_nonce=' + $( '#_pll_nonce' ).val(),
141
142 select: function( event, ui ) {
143 $( '#htr_lang_' + tr_lang ).val( ui.item.id );
144 td.html( ui.item.link );
145 },
146 });
147
148 // When the input box is emptied
149 $( this ).blur(function() {
150 if ( ! $( this ).val() ) {
151 $( '#htr_lang_' + tr_lang ).val( 0 );
152 td.html( td.siblings( '.hidden' ).children().clone() );
153 }
154 });
155 });
156 }
157
158 init_translations();
159
160 // Handle the response to a click on a Languages metabox button
161 $( '#ml_box' ).on( 'click', '.pll-button', function(){
162 var value = $( this ).hasClass( 'wp-ui-text-highlight' );
163 var id = $( this ).attr( 'id' );
164 var post_id = $( '#htr_lang_' + id.replace( 'pll_sync_post[', '' ).replace( ']', '' ) ).val();
165
166 if ( 'undefined' == typeof( post_id ) || 0 == post_id || value || confirm( confirm_text ) ) {
167 var data = {
168 action: 'toggle_' + id,
169 value: value,
170 post_type: $( '#post_type' ).val(),
171 _pll_nonce: $( '#_pll_nonce' ).val()
172 }
173
174 $.post( ajaxurl, data , function( response ){
175 var res = wpAjax.parseAjaxResponse( response, 'ajax-response' );
176 $.each( res.responses, function() {
177 id = id.replace( '[', '\\[' ).replace( ']', '\\]' );
178 $( '#' + id ).toggleClass( 'wp-ui-text-highlight' ).attr( 'title', this.data ).children( 'span' ).html( this.data );
179 $( 'input[name="' + id + '"]' ).val( ! data['value'] );
180 });
181 });
182 }
183 });
184 });
185