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