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