| 1 |
// tag suggest in metabox |
| 2 |
(function( $ ){ |
| 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; |
| 6 |
} |
| 7 |
}); |
| 8 |
})( jQuery ); |
| 9 |
|
| 10 |
// overrides tagBox.get |
| 11 |
(function( $ ){ |
| 12 |
// overrides function to add the language |
| 13 |
tagBox.get = function( id ) { |
| 14 |
var tax = id.substr( id.indexOf( '-' ) + 1 ); |
| 15 |
|
| 16 |
// add the language in the $_POST variable |
| 17 |
var data = { |
| 18 |
action: 'get-tagcloud', |
| 19 |
lang: $( '.post_lang_choice' ).val(), |
| 20 |
tax: tax |
| 21 |
} |
| 22 |
|
| 23 |
$.post( ajaxurl, data, function( r, stat ) { |
| 24 |
if ( 0 == r || 'success' != stat ) { |
| 25 |
r = wpAjax.broken; |
| 26 |
} |
| 27 |
|
| 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 |
}); |
| 33 |
|
| 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 ); |
| 38 |
} |
| 39 |
else { |
| 40 |
$( '#' + id ).after( r ); |
| 41 |
} |
| 42 |
}); |
| 43 |
} |
| 44 |
})( jQuery ); |
| 45 |
|
| 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; |
| 51 |
|
| 52 |
taxonomyParts = this_id.split( '-' ); |
| 53 |
taxonomyParts.shift(); |
| 54 |
taxonomy = taxonomyParts.join( '-' ); |
| 55 |
taxonomies.push( taxonomy ); // store the taxonomy for future use |
| 56 |
|
| 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() ) |
| 64 |
); |
| 65 |
}); |
| 66 |
|
| 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' ); |
| 72 |
|
| 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; |
| 109 |
} |
| 110 |
}); |
| 111 |
|
| 112 |
// modifies the language in the tag cloud |
| 113 |
$( '.tagcloud-link' ).each(function() { |
| 114 |
var id = $( this ).attr( 'id' ); |
| 115 |
tagBox.get( id ); |
| 116 |
}); |
| 117 |
|
| 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 |
}); |
| 124 |
|
| 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' ); |
| 130 |
|
| 131 |
$( this ).autocomplete({ |
| 132 |
minLength: 0, |
| 133 |
|
| 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(), |
| 139 |
|
| 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() ); |
| 151 |
} |
| 152 |
}); |
| 153 |
}); |
| 154 |
} |
| 155 |
|
| 156 |
init_translations(); |
| 157 |
|
| 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(); |
| 163 |
|
| 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 |
} |
| 171 |
|
| 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 |
}); |
| 183 |
|