| @@ -1,13 +1,11 @@ | ||
| 1 | 1 | // tag suggest in metabox |
| 2 | 2 | (function( $ ){ |
| 3 | - $.ajaxPrefilter( | |
| 4 | - function( options, originalOptions, jqXHR ) { | |
| 5 | - if ( 'string' === typeof options.data && -1 !== options.url.indexOf( 'action=ajax-tag-search' ) && ( lang = $( '.post_lang_choice' ).val() ) ) { | |
| 6 | - options.data = 'lang=' + lang + '&' + options.data; | |
| 7 | - } | |
| 3 | + $.ajaxPrefilter(function( options, originalOptions, jqXHR ) { | |
| 4 | + if ( 'string' === typeof options.data && -1 !== options.url.indexOf( 'action=ajax-tag-search' ) && ( lang = $( '.post_lang_choice' ).val() ) ) { | |
| 5 | + options.data = 'lang=' + lang + '&' + options.data; | |
| 8 | 6 | } |
| 9 | - ); | |
| 7 | + }); | |
| 10 | 8 | })( jQuery ); |
| 11 | 9 | |
| 12 | 10 | // overrides tagBox.get |
| 13 | 11 | (function( $ ){ |
| @@ -21,212 +19,164 @@ | ||
| 21 | 19 | lang: $( '.post_lang_choice' ).val(), |
| 22 | 20 | tax: tax |
| 23 | 21 | } |
| 24 | 22 | |
| 25 | - $.post( | |
| 26 | - ajaxurl, | |
| 27 | - data, | |
| 28 | - function( r, stat ) { | |
| 29 | - if ( 0 == r || 'success' != stat ) { | |
| 30 | - r = wpAjax.broken; | |
| 31 | - } | |
| 23 | + $.post( ajaxurl, data, function( r, stat ) { | |
| 24 | + if ( 0 == r || 'success' != stat ) { | |
| 25 | + r = wpAjax.broken; | |
| 26 | + } | |
| 32 | 27 | |
| 33 | - // @see code from WordPress core https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/js/tags-box.js#L291 | |
| 34 | - // @see wp_generate_tag_cloud function which generate the escaped HTML https://github.com/WordPress/WordPress/blob/a02b5cc2a8eecb8e076fbb7cf4de7bd2ec8a8eb1/wp-includes/category-template.php#L966-L975 | |
| 35 | - r = $( '<div />' ).addClass( 'the-tagcloud' ).attr( 'id', 'tagcloud-' + tax ).html( r ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 36 | - $( 'a', r ).click( | |
| 37 | - function(){ | |
| 38 | - tagBox.flushTags( $( this ).closest( '.inside' ).children( '.tagsdiv' ), this ); | |
| 39 | - return false; | |
| 40 | - } | |
| 41 | - ); | |
| 28 | + r = $( '<div id="tagcloud-' + tax + '" class="the-tagcloud">' + r + '</div>' ); | |
| 29 | + $( 'a', r ).click(function(){ | |
| 30 | + tagBox.flushTags( $( this ).closest( '.inside' ).children( '.tagsdiv' ), this ); | |
| 31 | + return false; | |
| 32 | + }); | |
| 42 | 33 | |
| 43 | - // add an if else condition to allow modifying the tags outputed when switching the language | |
| 44 | - if ( v = $( '.the-tagcloud' ).css( 'display' ) ) { | |
| 45 | - $( '.the-tagcloud' ).replaceWith( r ); | |
| 46 | - $( '.the-tagcloud' ).css( 'display', v ); | |
| 47 | - } | |
| 48 | - else { | |
| 49 | - $( '#' + id ).after( r ); | |
| 50 | - } | |
| 34 | + // add an if else condition to allow modifying the tags outputed when switching the language | |
| 35 | + if ( v = $( '.the-tagcloud' ).css( 'display' ) ) { | |
| 36 | + $( '.the-tagcloud' ).replaceWith( r ); | |
| 37 | + $( '.the-tagcloud' ).css( 'display', v ); | |
| 51 | 38 | } |
| 52 | - ); | |
| 39 | + else { | |
| 40 | + $( '#' + id ).after( r ); | |
| 41 | + } | |
| 42 | + }); | |
| 53 | 43 | } |
| 54 | 44 | })( jQuery ); |
| 55 | 45 | |
| 56 | -jQuery( document ).ready( | |
| 57 | - function( $ ) { | |
| 58 | - // collect taxonomies - code partly copied from WordPress | |
| 59 | - var taxonomies = new Array(); | |
| 60 | - $( '.categorydiv' ).each( | |
| 61 | - function(){ | |
| 62 | - var this_id = $( this ).attr( 'id' ), taxonomyParts, taxonomy; | |
| 46 | +jQuery( document ).ready(function( $ ) { | |
| 47 | + // collect taxonomies - code partly copied from WordPress | |
| 48 | + var taxonomies = new Array(); | |
| 49 | + $( '.categorydiv' ).each(function(){ | |
| 50 | + var this_id = $( this ).attr( 'id' ), taxonomyParts, taxonomy; | |
| 63 | 51 | |
| 64 | - taxonomyParts = this_id.split( '-' ); | |
| 65 | - taxonomyParts.shift(); | |
| 66 | - taxonomy = taxonomyParts.join( '-' ); | |
| 67 | - taxonomies.push( taxonomy ); // store the taxonomy for future use | |
| 52 | + taxonomyParts = this_id.split( '-' ); | |
| 53 | + taxonomyParts.shift(); | |
| 54 | + taxonomy = taxonomyParts.join( '-' ); | |
| 55 | + taxonomies.push( taxonomy ); // store the taxonomy for future use | |
| 68 | 56 | |
| 69 | - // add our hidden field in the new category form - for each hierarchical taxonomy | |
| 70 | - // to set the language when creating a new category | |
| 71 | - $( '#' + taxonomy + '-add-submit' ).before( | |
| 72 | - $( '<input />' ).attr( 'type', 'hidden' ) | |
| 73 | - .attr( 'id', taxonomy + '-lang' ) | |
| 74 | - .attr( 'name', 'term_lang_choice' ) | |
| 75 | - .attr( 'value', $( '.post_lang_choice' ).val() ) | |
| 76 | - ); | |
| 77 | - } | |
| 57 | + // add our hidden field in the new category form - for each hierarchical taxonomy | |
| 58 | + // to set the language when creating a new category | |
| 59 | + $( '#' + taxonomy + '-add-submit' ).before( $( '<input />' ) | |
| 60 | + .attr( 'type', 'hidden' ) | |
| 61 | + .attr( 'id', taxonomy + '-lang' ) | |
| 62 | + .attr( 'name', 'term_lang_choice' ) | |
| 63 | + .attr( 'value', $( '.post_lang_choice' ).val() ) | |
| 78 | 64 | ); |
| 65 | + }); | |
| 79 | 66 | |
| 80 | - // ajax for changing the post's language in the languages metabox | |
| 81 | - $( '.post_lang_choice' ).change( | |
| 82 | - function() { | |
| 83 | - var value = $( this ).val(); | |
| 84 | - var lang = $( this ).children( 'option[value="' + value + '"]' ).attr( 'lang' ); | |
| 85 | - var dir = $( '.pll-translation-column > span[lang="' + lang + '"]' ).attr( 'dir' ); | |
| 67 | + // ajax for changing the post's language in the languages metabox | |
| 68 | + $( '.post_lang_choice' ).change(function() { | |
| 69 | + var value = $( this ).val(); | |
| 70 | + var lang = $( this ).children( 'option[value="' + value + '"]' ).attr( 'lang' ); | |
| 71 | + var dir = $( '.pll-translation-column > span[lang="' + lang + '"]' ).attr( 'dir' ); | |
| 86 | 72 | |
| 87 | - var data = { | |
| 88 | - action: 'post_lang_choice', | |
| 89 | - lang: value, | |
| 90 | - post_type: $( '#post_type' ).val(), | |
| 91 | - taxonomies: taxonomies, | |
| 92 | - post_id: $( '#post_ID' ).val(), | |
| 93 | - _pll_nonce: $( '#_pll_nonce' ).val() | |
| 73 | + var data = { | |
| 74 | + action: 'post_lang_choice', | |
| 75 | + lang: value, | |
| 76 | + post_type: $( '#post_type' ).val(), | |
| 77 | + taxonomies: taxonomies, | |
| 78 | + post_id: $( '#post_ID' ).val(), | |
| 79 | + _pll_nonce: $( '#_pll_nonce' ).val() | |
| 80 | + } | |
| 81 | + | |
| 82 | + $.post( ajaxurl, data , function( response ) { | |
| 83 | + var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 84 | + $.each( res.responses, function() { | |
| 85 | + switch ( this.what ) { | |
| 86 | + case 'translations': // translations fields | |
| 87 | + $( '.translations' ).html( this.data ); | |
| 88 | + init_translations(); | |
| 89 | + break; | |
| 90 | + case 'taxonomy': // categories metabox for posts | |
| 91 | + var tax = this.data; | |
| 92 | + $( '#' + tax + 'checklist' ).html( this.supplemental.all ); | |
| 93 | + $( '#' + tax + 'checklist-pop' ).html( this.supplemental.populars ); | |
| 94 | + $( '#new' + tax + '_parent' ).replaceWith( this.supplemental.dropdown ); | |
| 95 | + $( '#' + tax + '-lang' ).val( $( '.post_lang_choice' ).val() ); // hidden field | |
| 96 | + break; | |
| 97 | + case 'pages': // parent dropdown list for pages | |
| 98 | + $( '#parent_id' ).html( this.data ); | |
| 99 | + break; | |
| 100 | + case 'flag': // flag in front of the select dropdown | |
| 101 | + $( '.pll-select-flag' ).html( this.data ); | |
| 102 | + break; | |
| 103 | + case 'permalink': // Sample permalink | |
| 104 | + var div = $( '#edit-slug-box' ); | |
| 105 | + if ( '-1' != this.data && div.children().length ) { | |
| 106 | + div.html( this.data ); | |
| 107 | + } | |
| 108 | + break; | |
| 94 | 109 | } |
| 110 | + }); | |
| 95 | 111 | |
| 96 | - $.post( | |
| 97 | - ajaxurl, | |
| 98 | - data, | |
| 99 | - function( response ) { | |
| 100 | - var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 101 | - $.each( | |
| 102 | - res.responses, | |
| 103 | - function() { | |
| 104 | - switch ( this.what ) { | |
| 105 | - case 'translations': // translations fields | |
| 106 | - // Data is built and come from server side and is well escaped when necessary | |
| 107 | - $( '.translations' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 108 | - init_translations(); | |
| 109 | - break; | |
| 110 | - case 'taxonomy': // categories metabox for posts | |
| 111 | - var tax = this.data; | |
| 112 | - // @see wp_terms_checklist https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/template.php#L175 | |
| 113 | - // @see https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/class-walker-category-checklist.php#L89-L111 | |
| 114 | - $( '#' + tax + 'checklist' ).html( this.supplemental.all ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 115 | - // @see wp_popular_terms_checklist https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/template.php#L236 | |
| 116 | - $( '#' + tax + 'checklist-pop' ).html( this.supplemental.populars ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 117 | - $( '#new' + tax + '_parent' ).replaceWith( this.supplemental.dropdown ); | |
| 118 | - $( '#' + tax + '-lang' ).val( $( '.post_lang_choice' ).val() ); // hidden field | |
| 119 | - break; | |
| 120 | - case 'pages': // parent dropdown list for pages | |
| 121 | - // @see wp_dropdown_pages https://github.com/WordPress/WordPress/blob/5.2.2/wp-includes/post-template.php#L1186-L1208 | |
| 122 | - // @see https://github.com/WordPress/WordPress/blob/5.2.2/wp-includes/class-walker-page-dropdown.php#L88 | |
| 123 | - $( '#parent_id' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 124 | - break; | |
| 125 | - case 'flag': // flag in front of the select dropdown | |
| 126 | - // Data is built and come from server side and is well escaped when necessary | |
| 127 | - $( '.pll-select-flag' ).html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 128 | - break; | |
| 129 | - case 'permalink': // Sample permalink | |
| 130 | - var div = $( '#edit-slug-box' ); | |
| 131 | - if ( '-1' != this.data && div.children().length ) { | |
| 132 | - // @see get_sample_permalink_html https://github.com/WordPress/WordPress/blob/5.2.2/wp-admin/includes/post.php#L1425-L1454 | |
| 133 | - div.html( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 134 | - } | |
| 135 | - break; | |
| 136 | - } | |
| 137 | - } | |
| 138 | - ); | |
| 112 | + // modifies the language in the tag cloud | |
| 113 | + $( '.tagcloud-link' ).each(function() { | |
| 114 | + var id = $( this ).attr( 'id' ); | |
| 115 | + tagBox.get( id ); | |
| 116 | + }); | |
| 139 | 117 | |
| 140 | - // modifies the language in the tag cloud | |
| 141 | - $( '.tagcloud-link' ).each( | |
| 142 | - function() { | |
| 143 | - var id = $( this ).attr( 'id' ); | |
| 144 | - tagBox.get( id ); | |
| 145 | - } | |
| 146 | - ); | |
| 118 | + // Modifies the text direction | |
| 119 | + $( 'body' ).removeClass( 'pll-dir-rtl' ).removeClass( 'pll-dir-ltr' ).addClass( 'pll-dir-' + dir ); | |
| 120 | + $( '#content_ifr' ).contents().find( 'html' ).attr( 'lang', lang ).attr( 'dir', dir ); | |
| 121 | + $( '#content_ifr' ).contents().find( 'body' ).attr( 'dir', dir ); | |
| 122 | + }); | |
| 123 | + }); | |
| 147 | 124 | |
| 148 | - // Modifies the text direction | |
| 149 | - $( 'body' ).removeClass( 'pll-dir-rtl' ).removeClass( 'pll-dir-ltr' ).addClass( 'pll-dir-' + dir ); | |
| 150 | - $( '#content_ifr' ).contents().find( 'html' ).attr( 'lang', lang ).attr( 'dir', dir ); | |
| 151 | - $( '#content_ifr' ).contents().find( 'body' ).attr( 'dir', dir ); | |
| 152 | - } | |
| 153 | - ); | |
| 154 | - } | |
| 155 | - ); | |
| 125 | + // translations autocomplete input box | |
| 126 | + function init_translations() { | |
| 127 | + $( '.tr_lang' ).each(function(){ | |
| 128 | + var tr_lang = $( this ).attr( 'id' ).substring( 8 ); | |
| 129 | + var td = $( this ).parent().parent().siblings( '.pll-edit-column' ); | |
| 156 | 130 | |
| 157 | - // translations autocomplete input box | |
| 158 | - function init_translations() { | |
| 159 | - $( '.tr_lang' ).each( | |
| 160 | - function(){ | |
| 161 | - var tr_lang = $( this ).attr( 'id' ).substring( 8 ); | |
| 162 | - var td = $( this ).parent().parent().siblings( '.pll-edit-column' ); | |
| 131 | + $( this ).autocomplete({ | |
| 132 | + minLength: 0, | |
| 163 | 133 | |
| 164 | - $( this ).autocomplete( | |
| 165 | - { | |
| 166 | - minLength: 0, | |
| 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 | - select: function( event, ui ) { | |
| 173 | - $( '#htr_lang_' + tr_lang ).val( ui.item.id ); | |
| 174 | - // ui.item.link is built and come from server side and is well escaped when necessary | |
| 175 | - td.html( ui.item.link ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 176 | - }, | |
| 177 | - } | |
| 178 | - ); | |
| 134 | + source: ajaxurl + '?action=pll_posts_not_translated' + | |
| 135 | + '&post_language=' + $( '.post_lang_choice' ).val() + | |
| 136 | + '&translation_language=' + tr_lang + | |
| 137 | + '&post_type=' + $( '#post_type' ).val() + | |
| 138 | + '&_pll_nonce=' + $( '#_pll_nonce' ).val(), | |
| 179 | 139 | |
| 180 | - // when the input box is emptied | |
| 181 | - $( this ).blur( | |
| 182 | - function() { | |
| 183 | - if ( ! $( this ).val() ) { | |
| 184 | - $( '#htr_lang_' + tr_lang ).val( 0 ); | |
| 185 | - // Value is retrieved from HTML already generated server side | |
| 186 | - td.html( td.siblings( '.hidden' ).children().clone() ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html | |
| 187 | - } | |
| 188 | - } | |
| 189 | - ); | |
| 140 | + select: function( event, ui ) { | |
| 141 | + $( '#htr_lang_' + tr_lang ).val( ui.item.id ); | |
| 142 | + td.html( ui.item.link ); | |
| 143 | + }, | |
| 144 | + }); | |
| 145 | + | |
| 146 | + // when the input box is emptied | |
| 147 | + $( this ).blur(function() { | |
| 148 | + if ( ! $( this ).val() ) { | |
| 149 | + $( '#htr_lang_' + tr_lang ).val( 0 ); | |
| 150 | + td.html( td.siblings( '.hidden' ).children().clone() ); | |
| 190 | 151 | } |
| 191 | - ); | |
| 192 | - } | |
| 152 | + }); | |
| 153 | + }); | |
| 154 | + } | |
| 193 | 155 | |
| 194 | - init_translations(); | |
| 156 | + init_translations(); | |
| 195 | 157 | |
| 196 | - // Handle the response to a click on a Languages metabox button | |
| 197 | - $( '#ml_box' ).on( | |
| 198 | - 'click', | |
| 199 | - '.pll-button', | |
| 200 | - function(){ | |
| 201 | - var value = $( this ).hasClass( 'wp-ui-text-highlight' ); | |
| 202 | - var id = $( this ).attr( 'id' ); | |
| 203 | - var post_id = $( '#htr_lang_' + id.replace( 'pll_sync_post[', '' ).replace( ']', '' ) ).val(); | |
| 158 | + // Handle the response to a click on a Languages metabox button | |
| 159 | + $( '#ml_box' ).on( 'click', '.pll-button', function(){ | |
| 160 | + var value = $( this ).hasClass( 'wp-ui-text-highlight' ); | |
| 161 | + var id = $( this ).attr( 'id' ); | |
| 162 | + var post_id = $( '#htr_lang_' + id.replace( 'pll_sync_post[', '' ).replace( ']', '' ) ).val(); | |
| 204 | 163 | |
| 205 | - if ( 'undefined' == typeof( post_id ) || 0 == post_id || value || confirm( confirm_text ) ) { | |
| 206 | - var data = { | |
| 207 | - action: 'toggle_' + id, | |
| 208 | - value: value, | |
| 209 | - post_type: $( '#post_type' ).val(), | |
| 210 | - _pll_nonce: $( '#_pll_nonce' ).val() | |
| 211 | - } | |
| 164 | + if ( 'undefined' == typeof( post_id ) || 0 == post_id || value || confirm( confirm_text ) ) { | |
| 165 | + var data = { | |
| 166 | + action: 'toggle_' + id, | |
| 167 | + value: value, | |
| 168 | + post_type: $( '#post_type' ).val(), | |
| 169 | + _pll_nonce: $( '#_pll_nonce' ).val() | |
| 170 | + } | |
| 212 | 171 | |
| 213 | - $.post( | |
| 214 | - ajaxurl, | |
| 215 | - data, | |
| 216 | - function( response ){ | |
| 217 | - var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 218 | - $.each( | |
| 219 | - res.responses, | |
| 220 | - function() { | |
| 221 | - id = id.replace( '[', '\\[' ).replace( ']', '\\]' ); | |
| 222 | - $( '#' + id ).toggleClass( 'wp-ui-text-highlight' ).attr( 'title', this.data ).children( 'span' ).text( this.data ); | |
| 223 | - $( 'input[name="' + id + '"]' ).val( ! data['value'] ); | |
| 224 | - } | |
| 225 | - ); | |
| 226 | - } | |
| 227 | - ); | |
| 228 | - } | |
| 229 | - } | |
| 230 | - ); | |
| 231 | - } | |
| 232 | -); | |
| 172 | + $.post( ajaxurl, data , function( response ){ | |
| 173 | + var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 174 | + $.each( res.responses, function() { | |
| 175 | + id = id.replace( '[', '\\[' ).replace( ']', '\\]' ); | |
| 176 | + $( '#' + id ).toggleClass( 'wp-ui-text-highlight' ).attr( 'title', this.data ).children( 'span' ).html( this.data ); | |
| 177 | + $( 'input[name="' + id + '"]' ).val( ! data['value'] ); | |
| 178 | + }); | |
| 179 | + }); | |
| 180 | + } | |
| 181 | + }); | |
| 182 | +}); | |