PluginProbe
Polylang / 3.8.9
Polylang v3.8.9
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 3.8.9, at js/build/post.js

179 lines 5.5 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 // Backward compatibility with WordPress < 6.7.
68 // Support both old (WP < 6.7) and new (WP >= 6.7) ID formats.
69 // Old format: category-123 (WordPress < 6.7).
70 // New format: in-category-123-1 (WordPress >= 6.7).
71 const termId = pll_term_languages[ lg ][ tax ][ i ];
72 const selector = `#${tax}-${termId}, [id^="in-${tax}-${termId}-"]`;
73 $( selector ).toggle( lang === lg );
74 }
75 );
76 }
77 );
78 }
79 );
80 }
81 }
82
83 /**
84 * Filters the parent page dropdown list.
85 */
86 function filter_pages( lang ) {
87 if ( "undefined" != typeof( pll_page_languages ) ) {
88 $.each(
89 pll_page_languages,
90 function ( lg, pages ) {
91 $.each(
92 pages,
93 function ( i ) {
94 v = $( '#post_parent option[value="' + pll_page_languages[ lg ][ i ] + '"]' );
95 lang == lg ? v.show() : v.hide();
96 }
97 );
98 }
99 );
100 }
101 }
102 }
103 }
104 const table = document.getElementById( 'the-list' );
105
106 if ( ! table ) {
107 return;
108 }
109
110 const config = { childList: true, subtree: true };
111 const observer = new MutationObserver( handleQuickEditInsertion );
112
113 observer.observe( table, config);
114 }
115 );
116
117 /**
118 * Update rows of translated posts when the language is modified in quick edit
119 * Acts on ajaxSuccess event
120 */
121 jQuery(
122 function ( $ ) {
123 $( document ).ajaxSuccess(
124 function ( event, xhr, settings ) {
125 function update_rows( post_id ) {
126 // collect old translations
127 var translations = new Array();
128 $( '.translation_' + post_id ).each(
129 function () {
130 translations.push( $( this ).parent().parent().attr( 'id' ).substring( 5 ) );
131 }
132 );
133
134 var data = {
135 action: 'pll_update_post_rows',
136 post_id: post_id,
137 translations: translations.join( ',' ),
138 post_type: $( "input[name='post_type']" ).val(),
139 screen: $( "input[name='screen']" ).val(),
140 _pll_nonce: $( "input[name='_inline_edit']" ).val() // reuse quick edit nonce
141 };
142
143 // get the modified rows in ajax and update them
144 $.post(
145 ajaxurl,
146 data,
147 function ( response ) {
148 if ( response ) {
149 // Since WP changeset #52710 parseAjaxResponse() return content to notice the user in a HTML tag with ajax-response id.
150 // Not to disturb this behaviour by executing another ajax request in the ajaxSuccess event, we need to target another unexisting id.
151 var res = wpAjax.parseAjaxResponse( response, 'pll-ajax-response' );
152 $.each(
153 res.responses,
154 function () {
155 if ( 'row' == this.what ) {
156 // data is built with a call to WP_Posts_List_Table::single_row method
157 // which uses internally other WordPress methods which escape correctly values.
158 // For Polylang language columns the HTML code is correctly escaped in PLL_Admin_Filters_Columns::post_column method.
159 $( "#post-" + this.supplemental.post_id ).replaceWith( this.data ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.replaceWith
160 }
161 }
162 );
163 }
164 }
165 );
166 }
167
168 if ( 'string' == typeof( settings.data ) ) { // Need to check the type due to block editor sometime sending FormData objects
169 var data = wpAjax.unserialize( settings.data ); // what were the data sent by the ajax request?
170 if ( 'undefined' != typeof( data['action'] ) && 'inline-save' == data['action'] ) {
171 update_rows( data['post_ID'] );
172 }
173 }
174 }
175 );
176 }
177 );
178
179