[ Index ] |
PHP Cross Reference of WordPress |
[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 this.revert(); 182 183 $( '#bulk-edit td' ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length ); 184 185 // Insert the editor at the top of the table with an empty row above to maintain zebra striping. 186 $('table.widefat tbody').prepend( $('#bulk-edit') ).prepend('<tr class="hidden"></tr>'); 187 $('#bulk-edit').addClass('inline-editor').show(); 188 189 /** 190 * Create a HTML div with the title and a link(delete-icon) for each selected 191 * post. 192 * 193 * Get the selected posts based on the checked checkboxes in the post table. 194 */ 195 $( 'tbody th.check-column input[type="checkbox"]' ).each( function() { 196 197 // If the checkbox for a post is selected, add the post to the edit list. 198 if ( $(this).prop('checked') ) { 199 c = false; 200 var id = $( this ).val(), 201 theTitle = $( '#inline_' + id + ' .post_title' ).html() || wp.i18n.__( '(no title)' ), 202 buttonVisuallyHiddenText = wp.i18n.sprintf( 203 /* translators: %s: Post title. */ 204 wp.i18n.__( 'Remove “%s” from Bulk Edit' ), 205 theTitle 206 ); 207 208 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>'; 209 } 210 }); 211 212 // If no checkboxes where checked, just hide the quick/bulk edit rows. 213 if ( c ) { 214 return this.revert(); 215 } 216 217 // Populate the list of items to bulk edit. 218 $( '#bulk-titles' ).html( '<ul id="bulk-titles-list" role="list">' + te + '</ul>' ); 219 220 /** 221 * Binds on click events to handle the list of items to bulk edit. 222 * 223 * @listens click 224 */ 225 $( '#bulk-titles .ntdelbutton' ).click( function() { 226 var $this = $( this ), 227 id = $this.attr( 'id' ).substr( 1 ), 228 $prev = $this.parent().prev().children( '.ntdelbutton' ), 229 $next = $this.parent().next().children( '.ntdelbutton' ); 230 231 $( 'table.widefat input[value="' + id + '"]' ).prop( 'checked', false ); 232 $( '#_' + id ).parent().remove(); 233 wp.a11y.speak( wp.i18n.__( 'Item removed.' ), 'assertive' ); 234 235 // Move focus to a proper place when items are removed. 236 if ( $next.length ) { 237 $next.focus(); 238 } else if ( $prev.length ) { 239 $prev.focus(); 240 } else { 241 $( '#bulk-titles-list' ).remove(); 242 inlineEditPost.revert(); 243 wp.a11y.speak( wp.i18n.__( 'All selected items have been removed. Select new items to use Bulk Actions.' ) ); 244 } 245 }); 246 247 // Enable auto-complete for tags when editing posts. 248 if ( 'post' === type ) { 249 $( 'tr.inline-editor textarea[data-wp-taxonomy]' ).each( function ( i, element ) { 250 /* 251 * While Quick Edit clones the form each time, Bulk Edit always re-uses 252 * the same form. Let's check if an autocomplete instance already exists. 253 */ 254 if ( $( element ).autocomplete( 'instance' ) ) { 255 // jQuery equivalent of `continue` within an `each()` loop. 256 return; 257 } 258 259 $( element ).wpTagsSuggest(); 260 } ); 261 } 262 263 // Set initial focus on the Bulk Edit region. 264 $( '#bulk-edit .inline-edit-wrapper' ).attr( 'tabindex', '-1' ).focus(); 265 // Scrolls to the top of the table where the editor is rendered. 266 $('html, body').animate( { scrollTop: 0 }, 'fast' ); 267 }, 268 269 /** 270 * Creates a quick edit window for the post that has been clicked. 271 * 272 * @since 2.7.0 273 * 274 * @memberof inlineEditPost 275 * 276 * @param {number|Object} id The ID of the clicked post or an element within a post 277 * table row. 278 * @return {boolean} Always returns false at the end of execution. 279 */ 280 edit : function(id) { 281 var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, f, val, pw; 282 t.revert(); 283 284 if ( typeof(id) === 'object' ) { 285 id = t.getId(id); 286 } 287 288 fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order', 'page_template']; 289 if ( t.type === 'page' ) { 290 fields.push('post_parent'); 291 } 292 293 // Add the new edit row with an extra blank row underneath to maintain zebra striping. 294 editRow = $('#inline-edit').clone(true); 295 $( 'td', editRow ).attr( 'colspan', $( 'th:visible, td:visible', '.widefat:first thead' ).length ); 296 297 // Remove the ID from the copied row and let the `for` attribute reference the hidden ID. 298 $( 'td', editRow ).find('#quick-edit-legend').removeAttr('id'); 299 $( 'td', editRow ).find('p[id^="quick-edit-"]').removeAttr('id'); 300 301 $(t.what+id).removeClass('is-expanded').hide().after(editRow).after('<tr class="hidden"></tr>'); 302 303 // Populate fields in the quick edit window. 304 rowData = $('#inline_'+id); 305 if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) { 306 307 // The post author no longer has edit capabilities, so we need to add them to the list of authors. 308 $(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>'); 309 } 310 if ( $( ':input[name="post_author"] option', editRow ).length === 1 ) { 311 $('label.inline-edit-author', editRow).hide(); 312 } 313 314 for ( f = 0; f < fields.length; f++ ) { 315 val = $('.'+fields[f], rowData); 316 317 /** 318 * Replaces the image for a Twemoji(Twitter emoji) with it's alternate text. 319 * 320 * @return {string} Alternate text from the image. 321 */ 322 val.find( 'img' ).replaceWith( function() { return this.alt; } ); 323 val = val.text(); 324 $(':input[name="' + fields[f] + '"]', editRow).val( val ); 325 } 326 327 if ( $( '.comment_status', rowData ).text() === 'open' ) { 328 $( 'input[name="comment_status"]', editRow ).prop( 'checked', true ); 329 } 330 if ( $( '.ping_status', rowData ).text() === 'open' ) { 331 $( 'input[name="ping_status"]', editRow ).prop( 'checked', true ); 332 } 333 if ( $( '.sticky', rowData ).text() === 'sticky' ) { 334 $( 'input[name="sticky"]', editRow ).prop( 'checked', true ); 335 } 336 337 /** 338 * Creates the select boxes for the categories. 339 */ 340 $('.post_category', rowData).each(function(){ 341 var taxname, 342 term_ids = $(this).text(); 343 344 if ( term_ids ) { 345 taxname = $(this).attr('id').replace('_'+id, ''); 346 $('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(',')); 347 } 348 }); 349 350 /** 351 * Gets all the taxonomies for live auto-fill suggestions when typing the name 352 * of a tag. 353 */ 354 $('.tags_input', rowData).each(function(){ 355 var terms = $(this), 356 taxname = $(this).attr('id').replace('_' + id, ''), 357 textarea = $('textarea.tax_input_' + taxname, editRow), 358 comma = wp.i18n._x( ',', 'tag delimiter' ).trim(); 359 360 // Ensure the textarea exists. 361 if ( ! textarea.length ) { 362 return; 363 } 364 365 terms.find( 'img' ).replaceWith( function() { return this.alt; } ); 366 terms = terms.text(); 367 368 if ( terms ) { 369 if ( ',' !== comma ) { 370 terms = terms.replace(/,/g, comma); 371 } 372 textarea.val(terms); 373 } 374 375 textarea.wpTagsSuggest(); 376 }); 377 378 // Handle the post status. 379 status = $('._status', rowData).text(); 380 if ( 'future' !== status ) { 381 $('select[name="_status"] option[value="future"]', editRow).remove(); 382 } 383 384 pw = $( '.inline-edit-password-input' ).prop( 'disabled', false ); 385 if ( 'private' === status ) { 386 $('input[name="keep_private"]', editRow).prop('checked', true); 387 pw.val( '' ).prop( 'disabled', true ); 388 } 389 390 // Remove the current page and children from the parent dropdown. 391 pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow); 392 if ( pageOpt.length > 0 ) { 393 pageLevel = pageOpt[0].className.split('-')[1]; 394 nextPage = pageOpt; 395 while ( pageLoop ) { 396 nextPage = nextPage.next('option'); 397 if ( nextPage.length === 0 ) { 398 break; 399 } 400 401 nextLevel = nextPage[0].className.split('-')[1]; 402 403 if ( nextLevel <= pageLevel ) { 404 pageLoop = false; 405 } else { 406 nextPage.remove(); 407 nextPage = pageOpt; 408 } 409 } 410 pageOpt.remove(); 411 } 412 413 $(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show(); 414 $('.ptitle', editRow).trigger( 'focus' ); 415 416 return false; 417 }, 418 419 /** 420 * Saves the changes made in the quick edit window to the post. 421 * Ajax saving is only for Quick Edit and not for bulk edit. 422 * 423 * @since 2.7.0 424 * 425 * @param {number} id The ID for the post that has been changed. 426 * @return {boolean} False, so the form does not submit when pressing 427 * Enter on a focused field. 428 */ 429 save : function(id) { 430 var params, fields, page = $('.post_status_page').val() || ''; 431 432 if ( typeof(id) === 'object' ) { 433 id = this.getId(id); 434 } 435 436 $( 'table.widefat .spinner' ).addClass( 'is-active' ); 437 438 params = { 439 action: 'inline-save', 440 post_type: typenow, 441 post_ID: id, 442 edit_date: 'true', 443 post_status: page 444 }; 445 446 fields = $('#edit-'+id).find(':input').serialize(); 447 params = fields + '&' + $.param(params); 448 449 // Make Ajax request. 450 $.post( ajaxurl, params, 451 function(r) { 452 var $errorNotice = $( '#edit-' + id + ' .inline-edit-save .notice-error' ), 453 $error = $errorNotice.find( '.error' ); 454 455 $( 'table.widefat .spinner' ).removeClass( 'is-active' ); 456 457 if (r) { 458 if ( -1 !== r.indexOf( '<tr' ) ) { 459 $(inlineEditPost.what+id).siblings('tr.hidden').addBack().remove(); 460 $('#edit-'+id).before(r).remove(); 461 $( inlineEditPost.what + id ).hide().fadeIn( 400, function() { 462 // Move focus back to the Quick Edit button. $( this ) is the row being animated. 463 $( this ).find( '.editinline' ) 464 .attr( 'aria-expanded', 'false' ) 465 .trigger( 'focus' ); 466 wp.a11y.speak( wp.i18n.__( 'Changes saved.' ) ); 467 }); 468 } else { 469 r = r.replace( /<.[^<>]*?>/g, '' ); 470 $errorNotice.removeClass( 'hidden' ); 471 $error.html( r ); 472 wp.a11y.speak( $error.text() ); 473 } 474 } else { 475 $errorNotice.removeClass( 'hidden' ); 476 $error.text( wp.i18n.__( 'Error while saving the changes.' ) ); 477 wp.a11y.speak( wp.i18n.__( 'Error while saving the changes.' ) ); 478 } 479 }, 480 'html'); 481 482 // Prevent submitting the form when pressing Enter on a focused field. 483 return false; 484 }, 485 486 /** 487 * Hides and empties the Quick Edit and/or Bulk Edit windows. 488 * 489 * @since 2.7.0 490 * 491 * @memberof inlineEditPost 492 * 493 * @return {boolean} Always returns false. 494 */ 495 revert : function(){ 496 var $tableWideFat = $( '.widefat' ), 497 id = $( '.inline-editor', $tableWideFat ).attr( 'id' ); 498 499 if ( id ) { 500 $( '.spinner', $tableWideFat ).removeClass( 'is-active' ); 501 502 if ( 'bulk-edit' === id ) { 503 504 // Hide the bulk editor. 505 $( '#bulk-edit', $tableWideFat ).removeClass( 'inline-editor' ).hide().siblings( '.hidden' ).remove(); 506 $('#bulk-titles').empty(); 507 508 // Store the empty bulk editor in a hidden element. 509 $('#inlineedit').append( $('#bulk-edit') ); 510 511 // Move focus back to the Bulk Action button that was activated. 512 $( '#' + inlineEditPost.whichBulkButtonId ).trigger( 'focus' ); 513 } else { 514 515 // Remove both the inline-editor and its hidden tr siblings. 516 $('#'+id).siblings('tr.hidden').addBack().remove(); 517 id = id.substr( id.lastIndexOf('-') + 1 ); 518 519 // Show the post row and move focus back to the Quick Edit button. 520 $( this.what + id ).show().find( '.editinline' ) 521 .attr( 'aria-expanded', 'false' ) 522 .trigger( 'focus' ); 523 } 524 } 525 526 return false; 527 }, 528 529 /** 530 * Gets the ID for a the post that you want to quick edit from the row in the quick 531 * edit table. 532 * 533 * @since 2.7.0 534 * 535 * @memberof inlineEditPost 536 * 537 * @param {Object} o DOM row object to get the ID for. 538 * @return {string} The post ID extracted from the table row in the object. 539 */ 540 getId : function(o) { 541 var id = $(o).closest('tr').attr('id'), 542 parts = id.split('-'); 543 return parts[parts.length - 1]; 544 } 545 }; 546 547 $( function() { inlineEditPost.init(); } ); 548 549 // Show/hide locks on posts. 550 $( function() { 551 552 // Set the heartbeat interval to 15 seconds. 553 if ( typeof wp !== 'undefined' && wp.heartbeat ) { 554 wp.heartbeat.interval( 15 ); 555 } 556 }).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) { 557 var locked = data['wp-check-locked-posts'] || {}; 558 559 $('#the-list tr').each( function(i, el) { 560 var key = el.id, row = $(el), lock_data, avatar; 561 562 if ( locked.hasOwnProperty( key ) ) { 563 if ( ! row.hasClass('wp-locked') ) { 564 lock_data = locked[key]; 565 row.find('.column-title .locked-text').text( lock_data.text ); 566 row.find('.check-column checkbox').prop('checked', false); 567 568 if ( lock_data.avatar_src ) { 569 avatar = $( '<img />', { 570 'class': 'avatar avatar-18 photo', 571 width: 18, 572 height: 18, 573 alt: '', 574 src: lock_data.avatar_src, 575 srcset: lock_data.avatar_src_2x ? lock_data.avatar_src_2x + ' 2x' : undefined 576 } ); 577 row.find('.column-title .locked-avatar').empty().append( avatar ); 578 } 579 row.addClass('wp-locked'); 580 } 581 } else if ( row.hasClass('wp-locked') ) { 582 row.removeClass( 'wp-locked' ).find( '.locked-info span' ).empty(); 583 } 584 }); 585 }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) { 586 var check = []; 587 588 $('#the-list tr').each( function(i, el) { 589 if ( el.id ) { 590 check.push( el.id ); 591 } 592 }); 593 594 if ( check.length ) { 595 data['wp-check-locked-posts'] = check; 596 } 597 }); 598 599 })( jQuery, window.wp );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Dec 25 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |