PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / js / build / post.js

post.js in Polylang trunk, at js/build/post.js

174 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * @package Polylang
3 */
4
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 }
15 }
16 );
17 }
18 );
19
20 /**
21 * Quick edit
22 */
23 jQuery(
24 function ( $ ) {
25 const handleQuickEditInsertion = ( mutationsList ) => {
26 for ( const mutation of mutationsList ) {
27 const addedNodes = Array.from( mutation.addedNodes ).filter( el => el.nodeType === Node.ELEMENT_NODE )
28 const form = addedNodes[0];
29 if ( 0 < mutation.addedNodes.length && form.classList.contains( 'inline-editor' ) ) {
30 // WordPress has inserted the quick edit form.
31 const post_id = Number( form.id.substring( 5 ) );
32
33 if ( post_id > 0 ) {
34 // Get the language dropdown.
35 const select = form.querySelector( 'select[name="inline_lang_choice"]' );
36 const lang = document.querySelector( '#lang_' + String( post_id ) ).innerHTML;
37 select.value = lang; // Populates the dropdown with the post language.
38
39 filter_terms( lang ); // Initial filter for category checklist.
40 filter_pages( lang ); // Initial filter for parent dropdown.
41
42 // Modify category checklist and parent dropdown on language change.
43 select.addEventListener(
44 'change',
45 function ( event ) {
46 const newLang = event.target.value;
47 filter_terms( newLang );
48 filter_pages( newLang );
49 }
50 );
51 }
52 }
53 /**
54 * Filters the category checklist.
55 */
56 function filter_terms( lang ) {
57 if ( "undefined" != typeof( pll_term_languages ) ) {
58 $.each(
59 pll_term_languages,
60 function ( lg, term_tax ) {
61 $.each(
62 term_tax,
63 function ( tax, terms ) {
64 $.each(
65 terms,
66 function ( i ) {
67 const termId = pll_term_languages[ lg ][ tax ][ i ];
68 $( `[id^="in-${tax}-${termId}-"]` ).toggle( lang === lg );
69 }
70 );
71 }
72 );
73 }
74 );
75 }
76 }
77
78 /**
79 * Filters the parent page dropdown list.
80 */
81 function filter_pages( lang ) {
82 if ( "undefined" != typeof( pll_page_languages ) ) {
83 $.each(
84 pll_page_languages,
85 function ( lg, pages ) {
86 $.each(
87 pages,
88 function ( i ) {
89 v = $( '#post_parent option[value="' + pll_page_languages[ lg ][ i ] + '"]' );
90 lang == lg ? v.show() : v.hide();
91 }
92 );
93 }
94 );
95 }
96 }
97 }
98 }
99 const table = document.getElementById( 'the-list' );
100
101 if ( ! table ) {
102 return;
103 }
104
105 const config = { childList: true, subtree: true };
106 const observer = new MutationObserver( handleQuickEditInsertion );
107
108 observer.observe( table, config);
109 }
110 );
111
112 /**
113 * Update rows of translated posts when the language is modified in quick edit
114 * Acts on ajaxSuccess event
115 */
116 jQuery(
117 function ( $ ) {
118 $( document ).ajaxSuccess(
119 function ( event, xhr, settings ) {
120 function update_rows( post_id ) {
121 // collect old translations
122 var translations = new Array();
123 $( '.translation_' + post_id ).each(
124 function () {
125 translations.push( $( this ).parent().parent().attr( 'id' ).substring( 5 ) );
126 }
127 );
128
129 var data = {
130 action: 'pll_update_post_rows',
131 post_id: post_id,
132 translations: translations.join( ',' ),
133 post_type: $( "input[name='post_type']" ).val(),
134 screen: $( "input[name='screen']" ).val(),
135 _pll_nonce: $( "input[name='_inline_edit']" ).val() // reuse quick edit nonce
136 };
137
138 // get the modified rows in ajax and update them
139 $.post(
140 ajaxurl,
141 data,
142 function ( response ) {
143 if ( response ) {
144 // Since WP changeset #52710 parseAjaxResponse() return content to notice the user in a HTML tag with ajax-response id.
145 // Not to disturb this behaviour by executing another ajax request in the ajaxSuccess event, we need to target another unexisting id.
146 var res = wpAjax.parseAjaxResponse( response, 'pll-ajax-response' );
147 $.each(
148 res.responses,
149 function () {
150 if ( 'row' == this.what ) {
151 // data is built with a call to WP_Posts_List_Table::single_row method
152 // which uses internally other WordPress methods which escape correctly values.
153 // For Polylang language columns the HTML code is correctly escaped in PLL_Admin_Filters_Columns::post_column method.
154 $( "#post-" + this.supplemental.post_id ).replaceWith( this.data );
155 }
156 }
157 );
158 }
159 }
160 );
161 }
162
163 if ( 'string' == typeof( settings.data ) ) { // Need to check the type due to block editor sometime sending FormData objects
164 var data = wpAjax.unserialize( settings.data ); // what were the data sent by the ajax request?
165 if ( 'undefined' != typeof( data['action'] ) && 'inline-save' == data['action'] ) {
166 update_rows( data['post_ID'] );
167 }
168 }
169 }
170 );
171 }
172 );
173
174