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