| @@ -1,177 +1,243 @@ | ||
| 1 | -/** | |
| 2 | - * @package Polylang | |
| 3 | - */ | |
| 1 | +// tag suggest | |
| 2 | +// valid for both tag metabox and quick edit | |
| 3 | +(function( $ ){ | |
| 4 | + $.ajaxPrefilter(function( options, originalOptions, jqXHR ) { | |
| 5 | + if ( 'string' === typeof options.data && ( -1 !== options.url.indexOf( 'action=ajax-tag-search' ) || -1 !== options.data.indexOf( 'action=ajax-tag-search' ) ) && ( ( lang = $( '.post_lang_choice' ).val() ) || ( lang = $( ':input[name="inline_lang_choice"]' ).val() ) ) ) { | |
| 6 | + options.data = 'lang=' + lang + '&' + options.data; | |
| 7 | + } | |
| 8 | + }); | |
| 9 | +})( jQuery ); | |
| 4 | 10 | |
| 5 | -/** | |
| 6 | - * Tag suggest in quick edit | |
| 7 | - */ | |
| 8 | -jQuery( | |
| 9 | - function( $ ) { | |
| 10 | - $.ajaxPrefilter( | |
| 11 | - function( options, originalOptions, jqXHR ) { | |
| 12 | - if ( 'string' === typeof options.data && -1 !== options.data.indexOf( 'action=ajax-tag-search' ) && ( lang = $( ':input[name="inline_lang_choice"]' ).val() ) ) { | |
| 13 | - options.data = 'lang=' + lang + '&' + options.data; | |
| 14 | - } | |
| 11 | +// overrides tagBox.get | |
| 12 | +(function( $ ){ | |
| 13 | + // overrides function to add the language | |
| 14 | + tagBox.get = function( id ) { | |
| 15 | + var tax = id.substr( id.indexOf( '-' ) + 1 ); | |
| 16 | + | |
| 17 | + // add the language in the $_POST variable | |
| 18 | + var data = { | |
| 19 | + action: 'get-tagcloud', | |
| 20 | + lang: $( '.post_lang_choice' ).val(), | |
| 21 | + tax: tax | |
| 22 | + } | |
| 23 | + | |
| 24 | + $.post( ajaxurl, data, function( r, stat ) { | |
| 25 | + if ( 0 == r || 'success' != stat ) { | |
| 26 | + r = wpAjax.broken; | |
| 15 | 27 | } |
| 16 | - ); | |
| 28 | + | |
| 29 | + r = $( '<p id="tagcloud-' + tax + '" class="the-tagcloud">' + r + '</p>' ); | |
| 30 | + $( 'a', r ).click(function(){ | |
| 31 | + tagBox.flushTags( $( this ).closest( '.inside' ).children( '.tagsdiv' ), this ); | |
| 32 | + return false; | |
| 33 | + }); | |
| 34 | + | |
| 35 | + // add an if else condition to allow modifying the tags outputed when switching the language | |
| 36 | + if ( v = $( '.the-tagcloud' ).css( 'display' ) ) { | |
| 37 | + $( '.the-tagcloud' ).replaceWith( r ); | |
| 38 | + $( '.the-tagcloud' ).css( 'display', v ); | |
| 39 | + } | |
| 40 | + else { | |
| 41 | + $( '#' + id ).after( r ); | |
| 42 | + } | |
| 43 | + }); | |
| 17 | 44 | } |
| 18 | -); | |
| 45 | +})( jQuery ); | |
| 19 | 46 | |
| 20 | -/** | |
| 21 | - * Quick edit | |
| 22 | - */ | |
| 23 | -jQuery( | |
| 24 | - function( $ ) { | |
| 25 | - $( document ).on( | |
| 26 | - 'DOMNodeInserted', | |
| 27 | - function( e ) { | |
| 28 | - var t = $( e.target ); | |
| 47 | +// quick edit | |
| 48 | +(function( $ ) { | |
| 49 | + $( document ).bind( 'DOMNodeInserted', function( e ) { | |
| 50 | + var t = $( e.target ); | |
| 29 | 51 | |
| 30 | - // WP inserts the quick edit from | |
| 31 | - if ( 'inline-edit' == t.attr( 'id' ) ) { | |
| 32 | - var post_id = t.prev().attr( 'id' ).replace( "post-", "" ); | |
| 52 | + // WP inserts the quick edit from | |
| 53 | + if ( 'inline-edit' == t.attr( 'id' ) ) { | |
| 54 | + var post_id = t.prev().attr( 'id' ).replace( "post-", "" ); | |
| 33 | 55 | |
| 34 | - if ( post_id > 0 ) { | |
| 35 | - // language dropdown | |
| 36 | - var select = t.find( ':input[name="inline_lang_choice"]' ); | |
| 37 | - var lang = $( '#lang_' + post_id ).html(); | |
| 38 | - select.val( lang ); // populates the dropdown | |
| 56 | + if ( post_id > 0 ) { | |
| 57 | + // language dropdown | |
| 58 | + var select = t.find( ':input[name="inline_lang_choice"]' ); | |
| 59 | + var lang = $( '#lang_' + post_id ).html(); | |
| 60 | + select.val( lang ); // populates the dropdown | |
| 39 | 61 | |
| 40 | - filter_terms( lang ); // initial filter for category checklist | |
| 41 | - filter_pages( lang ); // initial filter for parent dropdown | |
| 62 | + filter_terms( lang ); // initial filter for category checklist | |
| 63 | + filter_pages( lang ); // initial filter for parent dropdown | |
| 42 | 64 | |
| 43 | - // modify category checklist an parent dropdown on language change | |
| 44 | - select.on( | |
| 45 | - 'change', | |
| 46 | - function() { | |
| 47 | - filter_terms( $( this ).val() ); | |
| 48 | - filter_pages( $( this ).val() ); | |
| 49 | - } | |
| 50 | - ); | |
| 51 | - } | |
| 52 | - } | |
| 65 | + // modify category checklist an parent dropdown on language change | |
| 66 | + select.change(function() { | |
| 67 | + filter_terms( $( this ).val() ); | |
| 68 | + filter_pages( $( this ).val() ); | |
| 69 | + }); | |
| 70 | + } | |
| 71 | + } | |
| 53 | 72 | |
| 54 | - /** | |
| 55 | - * Filters the category checklist. | |
| 56 | - */ | |
| 57 | - function filter_terms( lang ) { | |
| 58 | - if ( "undefined" != typeof( pll_term_languages ) ) { | |
| 59 | - $.each( | |
| 60 | - pll_term_languages, | |
| 61 | - function( lg, term_tax ) { | |
| 62 | - $.each( | |
| 63 | - term_tax, | |
| 64 | - function( tax, terms ) { | |
| 65 | - $.each( | |
| 66 | - terms, | |
| 67 | - function( i ) { | |
| 68 | - id = '#' + tax + '-' + pll_term_languages[ lg ][ tax ][ i ]; | |
| 69 | - lang == lg ? $( id ).show() : $( id ).hide(); | |
| 70 | - } | |
| 71 | - ); | |
| 72 | - } | |
| 73 | - ); | |
| 74 | - } | |
| 75 | - ); | |
| 76 | - } | |
| 77 | - } | |
| 73 | + // filter category checklist | |
| 74 | + function filter_terms( lang ) { | |
| 75 | + if ( "undefined" != typeof( pll_term_languages ) ) { | |
| 76 | + $.each( pll_term_languages, function( lg, term_tax ) { | |
| 77 | + $.each( term_tax, function( tax, terms ) { | |
| 78 | + $.each( terms, function( i ) { | |
| 79 | + id = '#' + tax + '-' + pll_term_languages[ lg ][ tax ][ i ]; | |
| 80 | + lang == lg ? $( id ).show() : $( id ).hide(); | |
| 81 | + }); | |
| 82 | + }); | |
| 83 | + }); | |
| 84 | + } | |
| 85 | + } | |
| 78 | 86 | |
| 79 | - /** | |
| 80 | - * Filters the parent page dropdown list. | |
| 81 | - */ | |
| 82 | - function filter_pages( lang ) { | |
| 83 | - if ( "undefined" != typeof( pll_page_languages ) ) { | |
| 84 | - $.each( | |
| 85 | - pll_page_languages, | |
| 86 | - function( lg, pages ) { | |
| 87 | - $.each( | |
| 88 | - pages, | |
| 89 | - function( i ) { | |
| 90 | - v = $( '#post_parent option[value="' + pll_page_languages[ lg ][ i ] + '"]' ); | |
| 91 | - lang == lg ? v.show() : v.hide(); | |
| 92 | - } | |
| 93 | - ); | |
| 94 | - } | |
| 95 | - ); | |
| 96 | - } | |
| 97 | - } | |
| 87 | + // filter parent page dropdown list | |
| 88 | + function filter_pages( lang ) { | |
| 89 | + if ( "undefined" != typeof( pll_page_languages ) ) { | |
| 90 | + $.each( pll_page_languages, function( lg, pages ) { | |
| 91 | + $.each( pages, function( i ) { | |
| 92 | + v = $( '#post_parent option[value="' + pll_page_languages[ lg ][ i ] + '"]' ); | |
| 93 | + lang == lg ? v.show() : v.hide(); | |
| 94 | + }); | |
| 95 | + }); | |
| 98 | 96 | } |
| 99 | - ); | |
| 100 | - } | |
| 101 | -); | |
| 97 | + } | |
| 98 | + }); | |
| 99 | +})( jQuery ); | |
| 102 | 100 | |
| 103 | -/** | |
| 104 | - * Update rows of translated posts when the language is modified in quick edit | |
| 105 | - * Acts on ajaxSuccess event | |
| 106 | - */ | |
| 107 | -jQuery( | |
| 108 | - function( $ ) { | |
| 109 | - $( document ).ajaxSuccess( | |
| 110 | - function( event, xhr, settings ) { | |
| 111 | - function update_rows( post_id ) { | |
| 112 | - // collect old translations | |
| 113 | - var translations = new Array(); | |
| 114 | - $( '.translation_' + post_id ).each( | |
| 115 | - function() { | |
| 116 | - translations.push( $( this ).parent().parent().attr( 'id' ).substring( 5 ) ); | |
| 101 | +// update rows of translated posts when the language is modified in quick edit | |
| 102 | +// acts on ajaxSuccess event | |
| 103 | +(function( $ ) { | |
| 104 | + $( document ).ajaxSuccess(function( event, xhr, settings ) { | |
| 105 | + function update_rows( post_id ) { | |
| 106 | + // collect old translations | |
| 107 | + var translations = new Array; | |
| 108 | + $( '.translation_' + post_id ).each(function() { | |
| 109 | + translations.push( $( this ).parent().parent().attr( 'id' ).substring( 5 ) ); | |
| 110 | + }); | |
| 111 | + | |
| 112 | + var data = { | |
| 113 | + action: 'pll_update_post_rows', | |
| 114 | + post_id: post_id, | |
| 115 | + translations: translations.join( ',' ), | |
| 116 | + post_type: $( "input[name='post_type']" ).val(), | |
| 117 | + screen: $( "input[name='screen']" ).val(), | |
| 118 | + _pll_nonce: $( "input[name='_inline_edit']" ).val() // reuse quick edit nonce | |
| 119 | + } | |
| 120 | + | |
| 121 | + // get the modified rows in ajax and update them | |
| 122 | + $.post( ajaxurl, data, function( response ) { | |
| 123 | + if ( response ) { | |
| 124 | + var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 125 | + $.each( res.responses, function() { | |
| 126 | + if ( 'row' == this.what ) { | |
| 127 | + $( "#post-" + this.supplemental.post_id ).replaceWith( this.data ); | |
| 117 | 128 | } |
| 118 | - ); | |
| 129 | + }); | |
| 130 | + } | |
| 131 | + }); | |
| 132 | + } | |
| 119 | 133 | |
| 120 | - var data = { | |
| 121 | - action: 'pll_update_post_rows', | |
| 122 | - post_id: post_id, | |
| 123 | - translations: translations.join( ',' ), | |
| 124 | - post_type: $( "input[name='post_type']" ).val(), | |
| 125 | - screen: $( "input[name='screen']" ).val(), | |
| 126 | - _pll_nonce: $( "input[name='_inline_edit']" ).val() // reuse quick edit nonce | |
| 127 | - }; | |
| 134 | + var data = wpAjax.unserialize( settings.data ); // what were the data sent by the ajax request? | |
| 135 | + if ( 'undefined' != typeof( data['action'] ) && 'inline-save' == data['action'] ) { | |
| 136 | + update_rows( data['post_ID'] ); | |
| 137 | + } | |
| 138 | + }); | |
| 139 | +})( jQuery ); | |
| 128 | 140 | |
| 129 | - // get the modified rows in ajax and update them | |
| 130 | - $.post( | |
| 131 | - ajaxurl, | |
| 132 | - data, | |
| 133 | - function( response ) { | |
| 134 | - if ( response ) { | |
| 135 | - var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 136 | - $.each( | |
| 137 | - res.responses, | |
| 138 | - function() { | |
| 139 | - if ( 'row' == this.what ) { | |
| 140 | - // data is built with a call to WP_Posts_List_Table::single_row method | |
| 141 | - // which uses internally other WordPress methods which escape correctly values. | |
| 142 | - // For Polylang language columns the HTML code is correctly escaped in PLL_Admin_Filters_Columns::post_column method. | |
| 143 | - $( "#post-" + this.supplemental.post_id ).replaceWith( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.replaceWith | |
| 144 | - } | |
| 145 | - } | |
| 146 | - ); | |
| 147 | - } | |
| 141 | +jQuery( document ).ready(function( $ ) { | |
| 142 | + // collect taxonomies - code partly copied from WordPress | |
| 143 | + var taxonomies = new Array(); | |
| 144 | + $( '.categorydiv' ).each(function(){ | |
| 145 | + var this_id = $( this ).attr( 'id' ), taxonomyParts, taxonomy; | |
| 146 | + | |
| 147 | + taxonomyParts = this_id.split( '-' ); | |
| 148 | + taxonomyParts.shift(); | |
| 149 | + taxonomy = taxonomyParts.join( '-' ); | |
| 150 | + taxonomies.push( taxonomy ); // store the taxonomy for future use | |
| 151 | + | |
| 152 | + // add our hidden field in the new category form - for each hierarchical taxonomy | |
| 153 | + // to set the language when creating a new category | |
| 154 | + $( '#' + taxonomy + '-add-submit' ).before( $( '<input />' ) | |
| 155 | + .attr( 'type', 'hidden' ) | |
| 156 | + .attr( 'id', taxonomy + '-lang' ) | |
| 157 | + .attr( 'name', 'term_lang_choice' ) | |
| 158 | + .attr( 'value', $( '.post_lang_choice' ).val() ) | |
| 159 | + ); | |
| 160 | + }); | |
| 161 | + | |
| 162 | + // ajax for changing the post's language in the languages metabox | |
| 163 | + $( '.post_lang_choice' ).change(function() { | |
| 164 | + var data = { | |
| 165 | + action: 'post_lang_choice', | |
| 166 | + lang: $( this ).val(), | |
| 167 | + post_type: $( '#post_type' ).val(), | |
| 168 | + taxonomies: taxonomies, | |
| 169 | + post_id: $( '#post_ID' ).val(), | |
| 170 | + _pll_nonce: $( '#_pll_nonce' ).val() | |
| 171 | + } | |
| 172 | + | |
| 173 | + $.post( ajaxurl, data , function( response ) { | |
| 174 | + var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 175 | + $.each( res.responses, function() { | |
| 176 | + switch ( this.what ) { | |
| 177 | + case 'translations': // translations fields | |
| 178 | + $( '.translations' ).html( this.data ); | |
| 179 | + init_translations(); | |
| 180 | + break; | |
| 181 | + case 'taxonomy': // categories metabox for posts | |
| 182 | + var tax = this.data; | |
| 183 | + $( '#' + tax + 'checklist' ).html( this.supplemental.all ); | |
| 184 | + $( '#' + tax + 'checklist-pop' ).html( this.supplemental.populars ); | |
| 185 | + $( '#new' + tax + '_parent' ).replaceWith( this.supplemental.dropdown ); | |
| 186 | + $( '#' + tax + '-lang' ).val( $( '.post_lang_choice' ).val() ); // hidden field | |
| 187 | + break; | |
| 188 | + case 'pages': // parent dropdown list for pages | |
| 189 | + $( '#parent_id' ).html( this.data ); | |
| 190 | + break; | |
| 191 | + case 'flag': // flag in front of the select dropdown | |
| 192 | + $( '.pll-select-flag' ).html( this.data ); | |
| 193 | + break; | |
| 194 | + case 'permalink': // Sample permalink | |
| 195 | + var div = $( '#edit-slug-box' ); | |
| 196 | + if ( '-1' != this.data && div.children().length ) { | |
| 197 | + div.html( this.data ); | |
| 148 | 198 | } |
| 149 | - ); | |
| 199 | + break; | |
| 150 | 200 | } |
| 201 | + }); | |
| 151 | 202 | |
| 152 | - if ( 'string' == typeof( settings.data ) ) { // Need to check the type due to block editor sometime sending FormData objects | |
| 153 | - var data = wpAjax.unserialize( settings.data ); // what were the data sent by the ajax request? | |
| 154 | - if ( 'undefined' != typeof( data['action'] ) && 'inline-save' == data['action'] ) { | |
| 155 | - update_rows( data['post_ID'] ); | |
| 156 | - } | |
| 203 | + // modifies the language in the tag cloud | |
| 204 | + $( '.tagcloud-link' ).each(function() { | |
| 205 | + var id = $( this ).attr( 'id' ); | |
| 206 | + tagBox.get( id ); | |
| 207 | + }); | |
| 208 | + }); | |
| 209 | + }); | |
| 210 | + | |
| 211 | + // translations autocomplete input box | |
| 212 | + function init_translations() { | |
| 213 | + $( '.tr_lang' ).each(function(){ | |
| 214 | + var tr_lang = $( this ).attr( 'id' ).substring( 8 ); | |
| 215 | + var td = $( this ).parent().siblings( '.pll-edit-column' ); | |
| 216 | + | |
| 217 | + $( this ).autocomplete({ | |
| 218 | + minLength: 0, | |
| 219 | + | |
| 220 | + source: ajaxurl + '?action=pll_posts_not_translated' + | |
| 221 | + '&post_language=' + $( '.post_lang_choice' ).val() + | |
| 222 | + '&translation_language=' + tr_lang + | |
| 223 | + '&post_type=' + $( '#post_type' ).val() + | |
| 224 | + '&_pll_nonce=' + $( '#_pll_nonce' ).val(), | |
| 225 | + | |
| 226 | + select: function( event, ui ) { | |
| 227 | + $( '#htr_lang_' + tr_lang ).val( ui.item.id ); | |
| 228 | + td.html( ui.item.link ); | |
| 229 | + }, | |
| 230 | + }); | |
| 231 | + | |
| 232 | + // when the input box is emptied | |
| 233 | + $( this ).blur(function() { | |
| 234 | + if ( ! $( this ).val() ) { | |
| 235 | + $( '#htr_lang_' + tr_lang ).val( 0 ); | |
| 236 | + td.html( td.siblings( '.hidden' ).children().clone() ); | |
| 157 | 237 | } |
| 158 | - } | |
| 159 | - ); | |
| 238 | + }); | |
| 239 | + }); | |
| 160 | 240 | } |
| 161 | -); | |
| 162 | 241 | |
| 163 | -/** | |
| 164 | - * Media list table | |
| 165 | - * When clicking on attach link, filters find post list per media language | |
| 166 | - */ | |
| 167 | -jQuery( | |
| 168 | - function( $ ) { | |
| 169 | - $.ajaxPrefilter( | |
| 170 | - function ( options, originalOptions, jqXHR ) { | |
| 171 | - if ( 'string' === typeof options.data && -1 !== options.data.indexOf( 'action=find_posts' ) ) { | |
| 172 | - options.data = 'pll_post_id=' + $( '#affected' ).val() + '&' + options.data; | |
| 173 | - } | |
| 174 | - } | |
| 175 | - ); | |
| 176 | - } | |
| 177 | -); | |
| 242 | + init_translations(); | |
| 243 | +}); | |