| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 /** 2 * @file Contains all dynamic functionality needed on post and term pages. 3 * 4 * @output wp-admin/js/post.js 5 */ 6 7 /* global ajaxurl, wpAjax, postboxes, pagenow, tinymce, alert, deleteUserSetting, ClipboardJS */ 8 /* global theList:true, theExtraList:true, getUserSetting, setUserSetting, commentReply, commentsBox */ 9 /* global WPSetThumbnailHTML, wptitlehint */ 10 11 // Backward compatibility: prevent fatal errors. 12 window.makeSlugeditClickable = window.editPermalink = function(){}; 13 14 // Make sure the wp object exists. 15 window.wp = window.wp || {}; 16 17 ( function( $ ) { 18 var titleHasFocus = false, 19 __ = wp.i18n.__; 20 21 /** 22 * Control loading of comments on the post and term edit pages. 23 * 24 * @type {{st: number, get: commentsBox.get, load: commentsBox.load}} 25 * 26 * @namespace commentsBox 27 */ 28 window.commentsBox = { 29 // Comment offset to use when fetching new comments. 30 st : 0, 31 32 /** 33 * Fetch comments using Ajax and display them in the box. 34 * 35 * @memberof commentsBox 36 * 37 * @param {number} total Total number of comments for this post. 38 * @param {number} num Optional. Number of comments to fetch, defaults to 20. 39 * @return {boolean} Always returns false. 40 */ 41 get : function(total, num) { 42 var st = this.st, data; 43 if ( ! num ) 44 num = 20; 45 46 this.st += num; 47 this.total = total; 48 $( '#commentsdiv .spinner' ).addClass( 'is-active' ); 49 50 data = { 51 'action' : 'get-comments', 52 'mode' : 'single', 53 '_ajax_nonce' : $('#add_comment_nonce').val(), 54 'p' : $('#post_ID').val(), 55 'start' : st, 56 'number' : num 57 }; 58 59 $.post( 60 ajaxurl, 61 data, 62 function(r) { 63 r = wpAjax.parseAjaxResponse(r); 64 $('#commentsdiv .widefat').show(); 65 $( '#commentsdiv .spinner' ).removeClass( 'is-active' ); 66 67 if ( 'object' == typeof r && r.responses[0] ) { 68 $('#the-comment-list').append( r.responses[0].data ); 69 70 theList = theExtraList = null; 71 $( 'a[className*=\':\']' ).off(); 72 73 // If the offset is over the total number of comments we cannot fetch any more, so hide the button. 74 if ( commentsBox.st > commentsBox.total ) 75 $('#show-comments').hide(); 76 else 77 $('#show-comments').show().children('a').text( __( 'Show more comments' ) ); 78 79 return; 80 } else if ( 1 == r ) { 81 $('#show-comments').text( __( 'No more comments found.' ) ); 82 return; 83 } 84 85 $('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>'); 86 } 87 ); 88 89 return false; 90 }, 91 92 /** 93 * Load the next batch of comments. 94 * 95 * @memberof commentsBox 96 * 97 * @param {number} total Total number of comments to load. 98 */ 99 load: function(total){ 100 this.st = jQuery('#the-comment-list tr.comment:visible').length; 101 this.get(total); 102 } 103 }; 104 105 /** 106 * Overwrite the content of the Featured Image postbox 107 * 108 * @param {string} html New HTML to be displayed in the content area of the postbox. 109 * 110 * @global 111 */ 112 window.WPSetThumbnailHTML = function(html){ 113 $('.inside', '#postimagediv').html(html); 114 }; 115 116 /** 117 * Set the Image ID of the Featured Image 118 * 119 * @param {number} id The post_id of the image to use as Featured Image. 120 * 121 * @global 122 */ 123 window.WPSetThumbnailID = function(id){ 124 var field = $('input[value="_thumbnail_id"]', '#list-table'); 125 if ( field.length > 0 ) { 126 $('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id); 127 } 128 }; 129 130 /** 131 * Remove the Featured Image 132 * 133 * @param {string} nonce Nonce to use in the request. 134 * 135 * @global 136 */ 137 window.WPRemoveThumbnail = function(nonce){ 138 $.post( 139 ajaxurl, { 140 action: 'set-post-thumbnail', 141 post_id: $( '#post_ID' ).val(), 142 thumbnail_id: -1, 143 _ajax_nonce: nonce, 144 cookie: encodeURIComponent( document.cookie ) 145 }, 146 /** 147 * Handle server response 148 * 149 * @param {string} str Response, will be '0' when an error occurred otherwise contains link to add Featured Image. 150 */ 151 function(str){ 152 if ( str == '0' ) { 153 alert( __( 'Could not set that as the thumbnail image. Try a different attachment.' ) ); 154 } else { 155 WPSetThumbnailHTML(str); 156 } 157 } 158 ); 159 }; 160 161 /** 162 * Heartbeat locks. 163 * 164 * Used to lock editing of an object by only one user at a time. 165 * 166 * When the user does not send a heartbeat in a heartbeat-time 167 * the user is no longer editing and another user can start editing. 168 */ 169 $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) { 170 var lock = $('#active_post_lock').val(), 171 post_id = $('#post_ID').val(), 172 send = {}; 173 174 if ( ! post_id || ! $('#post-lock-dialog').length ) 175 return; 176 177 send.post_id = post_id; 178 179 if ( lock ) 180 send.lock = lock; 181 182 data['wp-refresh-post-lock'] = send; 183 184 }).on( 'heartbeat-tick.refresh-lock', function( e, data ) { 185 // Post locks: update the lock string or show the dialog if somebody has taken over editing. 186 var received, wrap, avatar; 187 188 if ( data['wp-refresh-post-lock'] ) { 189 received = data['wp-refresh-post-lock']; 190 191 if ( received.lock_error ) { 192 // Show "editing taken over" message. 193 wrap = $('#post-lock-dialog'); 194 195 if ( wrap.length && ! wrap.is(':visible') ) { 196 if ( wp.autosave ) { 197 // Save the latest changes and disable. 198 $(document).one( 'heartbeat-tick', function() { 199 wp.autosave.server.suspend(); 200 wrap.removeClass('saving').addClass('saved'); 201 $(window).off( 'beforeunload.edit-post' ); 202 }); 203 204 wrap.addClass('saving'); 205 wp.autosave.server.triggerSave(); 206 } 207 208 if ( received.lock_error.avatar_src ) { 209 avatar = $( '<img />', { 210 'class': 'avatar avatar-64 photo', 211 width: 64, 212 height: 64, 213 alt: '', 214 src: received.lock_error.avatar_src, 215 srcset: received.lock_error.avatar_src_2x ? 216 received.lock_error.avatar_src_2x + ' 2x' : 217 undefined 218 } ); 219 wrap.find('div.post-locked-avatar').empty().append( avatar ); 220 } 221 222 wrap.show().find('.currently-editing').text( received.lock_error.text ); 223 wrap.find('.wp-tab-first').trigger( 'focus' ); 224 } 225 } else if ( received.new_lock ) { 226 $('#active_post_lock').val( received.new_lock ); 227 } 228 } 229 }).on( 'before-autosave.update-post-slug', function() { 230 titleHasFocus = document.activeElement && document.activeElement.id === 'title'; 231 }).on( 'after-autosave.update-post-slug', function() { 232 233 /* 234 * Create slug area only if not already there 235 * and the title field was not focused (user was not typing a title) when autosave ran. 236 */ 237 if ( ! $('#edit-slug-box > *').length && ! titleHasFocus ) { 238 $.post( ajaxurl, { 239 action: 'sample-permalink', 240 post_id: $('#post_ID').val(), 241 new_title: $('#title').val(), 242 samplepermalinknonce: $('#samplepermalinknonce').val() 243 }, 244 function( data ) { 245 if ( data != '-1' ) { 246 $('#edit-slug-box').html(data); 247 } 248 } 249 ); 250 } 251 }); 252 253 }(jQuery)); 254 255 /** 256 * Heartbeat refresh nonces. 257 */ 258 (function($) { 259 var check, timeout; 260 261 /** 262 * Only allow to check for nonce refresh every 30 seconds. 263 */ 264 function schedule() { 265 check = false; 266 window.clearTimeout( timeout ); 267 timeout = window.setTimeout( function(){ check = true; }, 300000 ); 268 } 269 270 $( function() { 271 schedule(); 272 }).on( 'heartbeat-send.wp-refresh-nonces', function( e, data ) { 273 var post_id, 274 $authCheck = $('#wp-auth-check-wrap'); 275 276 if ( check || ( $authCheck.length && ! $authCheck.hasClass( 'hidden' ) ) ) { 277 if ( ( post_id = $('#post_ID').val() ) && $('#_wpnonce').val() ) { 278 data['wp-refresh-post-nonces'] = { 279 post_id: post_id 280 }; 281 } 282 } 283 }).on( 'heartbeat-tick.wp-refresh-nonces', function( e, data ) { 284 var nonces = data['wp-refresh-post-nonces']; 285 286 if ( nonces ) { 287 schedule(); 288 289 if ( nonces.replace ) { 290 $.each( nonces.replace, function( selector, value ) { 291 $( '#' + selector ).val( value ); 292 }); 293 } 294 295 if ( nonces.heartbeatNonce ) 296 window.heartbeatSettings.nonce = nonces.heartbeatNonce; 297 } 298 }); 299 }(jQuery)); 300 301 /** 302 * All post and postbox controls and functionality. 303 */ 304 jQuery( function($) { 305 var stamp, visibility, $submitButtons, updateVisibility, updateText, 306 $textarea = $('#content'), 307 $document = $(document), 308 postId = $('#post_ID').val() || 0, 309 $submitpost = $('#submitpost'), 310 releaseLock = true, 311 $postVisibilitySelect = $('#post-visibility-select'), 312 $timestampdiv = $('#timestampdiv'), 313 $postStatusSelect = $('#post-status-select'), 314 isMac = window.navigator.platform ? window.navigator.platform.indexOf( 'Mac' ) !== -1 : false, 315 copyAttachmentURLClipboard = new ClipboardJS( '.copy-attachment-url.edit-media' ), 316 copyAttachmentURLSuccessTimeout, 317 __ = wp.i18n.__, _x = wp.i18n._x; 318 319 postboxes.add_postbox_toggles(pagenow); 320 321 /* 322 * Clear the window name. Otherwise if this is a former preview window where the user navigated to edit another post, 323 * and the first post is still being edited, clicking Preview there will use this window to show the preview. 324 */ 325 window.name = ''; 326 327 // Post locks: contain focus inside the dialog. If the dialog is shown, focus the first item. 328 $('#post-lock-dialog .notification-dialog').on( 'keydown', function(e) { 329 // Don't do anything when [Tab] is pressed. 330 if ( e.which != 9 ) 331 return; 332 333 var target = $(e.target); 334 335 // [Shift] + [Tab] on first tab cycles back to last tab. 336 if ( target.hasClass('wp-tab-first') && e.shiftKey ) { 337 $(this).find('.wp-tab-last').trigger( 'focus' ); 338 e.preventDefault(); 339 // [Tab] on last tab cycles back to first tab. 340 } else if ( target.hasClass('wp-tab-last') && ! e.shiftKey ) { 341 $(this).find('.wp-tab-first').trigger( 'focus' ); 342 e.preventDefault(); 343 } 344 }).filter(':visible').find('.wp-tab-first').trigger( 'focus' ); 345 346 // Set the heartbeat interval to 10 seconds if post lock dialogs are enabled. 347 if ( wp.heartbeat && $('#post-lock-dialog').length ) { 348 wp.heartbeat.interval( 10 ); 349 } 350 351 // The form is being submitted by the user. 352 $submitButtons = $submitpost.find( ':submit, a.submitdelete, #post-preview' ).on( 'click.edit-post', function( event ) { 353 var $button = $(this); 354 355 if ( $button.hasClass('disabled') ) { 356 event.preventDefault(); 357 return; 358 } 359 360 if ( $button.hasClass('submitdelete') || $button.is( '#post-preview' ) ) { 361 return; 362 } 363 364 // The form submission can be blocked from JS or by using HTML 5.0 validation on some fields. 365 // Run this only on an actual 'submit'. 366 $('form#post').off( 'submit.edit-post' ).on( 'submit.edit-post', function( event ) { 367 if ( event.isDefaultPrevented() ) { 368 return; 369 } 370 371 // Stop auto save. 372 if ( wp.autosave ) { 373 wp.autosave.server.suspend(); 374 } 375 376 if ( typeof commentReply !== 'undefined' ) { 377 /* 378 * Warn the user they have an unsaved comment before submitting 379 * the post data for update. 380 */ 381 if ( ! commentReply.discardCommentChanges() ) { 382 return false; 383 } 384 385 /* 386 * Close the comment edit/reply form if open to stop the form 387 * action from interfering with the post's form action. 388 */ 389 commentReply.close(); 390 } 391 392 releaseLock = false; 393 $(window).off( 'beforeunload.edit-post' ); 394 395 $submitButtons.addClass( 'disabled' ); 396 397 if ( $button.attr('id') === 'publish' ) { 398 $submitpost.find( '#major-publishing-actions .spinner' ).addClass( 'is-active' ); 399 } else { 400 $submitpost.find( '#minor-publishing .spinner' ).addClass( 'is-active' ); 401 } 402 }); 403 }); 404 405 // Submit the form saving a draft or an autosave, and show a preview in a new tab. 406 $('#post-preview').on( 'click.post-preview', function( event ) { 407 var $this = $(this), 408 $form = $('form#post'), 409 $previewField = $('input#wp-preview'), 410 target = $this.attr('target') || 'wp-preview', 411 ua = navigator.userAgent.toLowerCase(); 412 413 event.preventDefault(); 414 415 if ( $this.hasClass('disabled') ) { 416 return; 417 } 418 419 if ( wp.autosave ) { 420 wp.autosave.server.tempBlockSave(); 421 } 422 423 $previewField.val('dopreview'); 424 $form.attr( 'target', target ).trigger( 'submit' ).attr( 'target', '' ); 425 426 // Workaround for WebKit bug preventing a form submitting twice to the same action. 427 // https://bugs.webkit.org/show_bug.cgi?id=28633 428 if ( ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1 ) { 429 $form.attr( 'action', function( index, value ) { 430 return value + '?t=' + ( new Date() ).getTime(); 431 }); 432 } 433 434 $previewField.val(''); 435 }); 436 437 // Auto save new posts after a title is typed. 438 if ( $( '#auto_draft' ).val() ) { 439 $( '#title' ).on( 'blur', function() { 440 var cancel; 441 442 if ( ! this.value || $('#edit-slug-box > *').length ) { 443 return; 444 } 445 446 // Cancel the auto save when the blur was triggered by the user submitting the form. 447 $('form#post').one( 'submit', function() { 448 cancel = true; 449 }); 450 451 window.setTimeout( function() { 452 if ( ! cancel && wp.autosave ) { 453 wp.autosave.server.triggerSave(); 454 } 455 }, 200 ); 456 }); 457 } 458 459 $document.on( 'autosave-disable-buttons.edit-post', function() { 460 $submitButtons.addClass( 'disabled' ); 461 }).on( 'autosave-enable-buttons.edit-post', function() { 462 if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) { 463 $submitButtons.removeClass( 'disabled' ); 464 } 465 }).on( 'before-autosave.edit-post', function() { 466 $( '.autosave-message' ).text( __( 'Saving Draft…' ) ); 467 }).on( 'after-autosave.edit-post', function( event, data ) { 468 $( '.autosave-message' ).text( data.message ); 469 470 if ( $( document.body ).hasClass( 'post-new-php' ) ) { 471 $( '.submitbox .submitdelete' ).show(); 472 } 473 }); 474 475 /* 476 * When the user is trying to load another page, or reloads current page 477 * show a confirmation dialog when there are unsaved changes. 478 */ 479 $( window ).on( 'beforeunload.edit-post', function( event ) { 480 var editor = window.tinymce && window.tinymce.get( 'content' ); 481 var changed = false; 482 483 if ( wp.autosave ) { 484 changed = wp.autosave.server.postChanged(); 485 } else if ( editor ) { 486 changed = ( ! editor.isHidden() && editor.isDirty() ); 487 } 488 489 if ( changed ) { 490 event.preventDefault(); 491 // The return string is needed for browser compat. 492 // See https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event. 493 return __( 'The changes you made will be lost if you navigate away from this page.' ); 494 } 495 }).on( 'pagehide.edit-post', function( event ) { 496 if ( ! releaseLock ) { 497 return; 498 } 499 500 /* 501 * Unload is triggered (by hand) on removing the Thickbox iframe. 502 * Make sure we process only the main document unload. 503 */ 504 if ( event.target && event.target.nodeName != '#document' ) { 505 return; 506 } 507 508 var postID = $('#post_ID').val(); 509 var postLock = $('#active_post_lock').val(); 510 511 if ( ! postID || ! postLock ) { 512 return; 513 } 514 515 var data = { 516 action: 'wp-remove-post-lock', 517 _wpnonce: $('#_wpnonce').val(), 518 post_ID: postID, 519 active_post_lock: postLock 520 }; 521 522 if ( window.FormData && window.navigator.sendBeacon ) { 523 var formData = new window.FormData(); 524 525 $.each( data, function( key, value ) { 526 formData.append( key, value ); 527 }); 528 529 if ( window.navigator.sendBeacon( ajaxurl, formData ) ) { 530 return; 531 } 532 } 533 534 // Fall back to a synchronous POST request. 535 // See https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon 536 $.post({ 537 async: false, 538 data: data, 539 url: ajaxurl 540 }); 541 }); 542 543 // Multiple taxonomies. 544 if ( $('#tagsdiv-post_tag').length ) { 545 window.tagBox && window.tagBox.init(); 546 } else { 547 $('.meta-box-sortables').children('div.postbox').each(function(){ 548 if ( this.id.indexOf('tagsdiv-') === 0 ) { 549 window.tagBox && window.tagBox.init(); 550 return false; 551 } 552 }); 553 } 554 555 // Handle categories. 556 $('.categorydiv').each( function(){ 557 var this_id = $(this).attr('id'), catAddBefore, catAddAfter, taxonomyParts, taxonomy, settingName; 558 559 taxonomyParts = this_id.split('-'); 560 taxonomyParts.shift(); 561 taxonomy = taxonomyParts.join('-'); 562 settingName = taxonomy + '_tab'; 563 564 if ( taxonomy == 'category' ) { 565 settingName = 'cats'; 566 } 567 568 // @todo Move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.js. 569 $('a', '#' + taxonomy + '-tabs').on( 'click keyup keydown', function( event ) { 570 var t = $(this).attr('href'); 571 if ( event.type === 'keydown' && event.key === ' ' ) { 572 event.preventDefault(); 573 } 574 if ( ( event.type === 'keyup' && event.key === ' ' ) || ( event.type === 'keydown' && event.key === 'Enter' ) || event.type === 'click' ) { 575 event.preventDefault(); 576 $('#' + taxonomy + '-tabs a').removeAttr( 'aria-selected' ).attr( 'tabindex', '-1' ); 577 $(this).attr( 'aria-selected', 'true' ).removeAttr( 'tabindex' ); 578 $(this).parent().addClass('tabs').siblings('li').removeClass('tabs'); 579 $('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide(); 580 $(t).show(); 581 if ( '#' + taxonomy + '-all' == t ) { 582 deleteUserSetting( settingName ); 583 } else { 584 setUserSetting( settingName, 'pop' ); 585 } 586 } 587 if ( event.type === 'keyup' && ( event.key === 'ArrowRight' || event.key === 'ArrowLeft' ) ) { 588 $(this).attr( 'tabindex', '-1' ); 589 let next = $(this).parent('li').next(); 590 let prev = $(this).parent('li').prev(); 591 if ( next.length > 0 ) { 592 next.find('a').removeAttr( 'tabindex'); 593 next.find('a').trigger( 'focus' ); 594 } else { 595 prev.find('a').removeAttr( 'tabindex'); 596 prev.find('a').trigger( 'focus' ); 597 } 598 } 599 }); 600 601 if ( getUserSetting( settingName ) ) 602 $('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').trigger( 'click' ); 603 604 // Add category button controls. 605 $('#new' + taxonomy).one( 'focus', function() { 606 $( this ).val( '' ).removeClass( 'form-input-tip' ); 607 }); 608 609 // On [Enter] submit the taxonomy. 610 $('#new' + taxonomy).on( 'keypress', function(event){ 611 if( 13 === event.keyCode ) { 612 event.preventDefault(); 613 $('#' + taxonomy + '-add-submit').trigger( 'click' ); 614 } 615 }); 616 617 // After submitting a new taxonomy, re-focus the input field. 618 $('#' + taxonomy + '-add-submit').on( 'click', function() { 619 $('#new' + taxonomy).trigger( 'focus' ); 620 }); 621 622 /** 623 * Before adding a new taxonomy, disable submit button. 624 * 625 * @param {Object} s Taxonomy object which will be added. 626 * 627 * @return {Object} 628 */ 629 catAddBefore = function( s ) { 630 if ( !$('#new'+taxonomy).val() ) { 631 return false; 632 } 633 634 s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize(); 635 $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true ); 636 return s; 637 }; 638 639 /** 640 * Re-enable submit button after a taxonomy has been added. 641 * 642 * Re-enable submit button. 643 * If the taxonomy has a parent place the taxonomy underneath the parent. 644 * 645 * @param {Object} r Response. 646 * @param {Object} s Taxonomy data. 647 * 648 * @return {void} 649 */ 650 catAddAfter = function( r, s ) { 651 var sup, drop = $('#new'+taxonomy+'_parent'); 652 653 $( '#' + taxonomy + '-add-submit' ).prop( 'disabled', false ); 654 if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) { 655 drop.before(sup); 656 drop.remove(); 657 } 658 }; 659 660 $('#' + taxonomy + 'checklist').wpList({ 661 alt: '', 662 response: taxonomy + '-ajax-response', 663 addBefore: catAddBefore, 664 addAfter: catAddAfter 665 }); 666 667 // Add new taxonomy button toggles input form visibility. 668 $('#' + taxonomy + '-add-toggle').on( 'click', function( e ) { 669 e.preventDefault(); 670 $('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' ); 671 $('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').trigger( 'click' ); 672 $('#new'+taxonomy).trigger( 'focus' ); 673 }); 674 675 // Sync checked items between "All {taxonomy}" and "Most used" lists. 676 $('#' + taxonomy + 'checklist, #' + taxonomy + 'checklist-pop').on( 677 'click', 678 'li.popular-category > label input[type="checkbox"]', 679 function() { 680 var t = $(this), c = t.is(':checked'), id = t.val(); 681 if ( id && t.parents('#taxonomy-'+taxonomy).length ) { 682 $('input#in-' + taxonomy + '-' + id + ', input[id^="in-' + taxonomy + '-' + id + '-"]').prop('checked', c); 683 $('input#in-popular-' + taxonomy + '-' + id).prop('checked', c); 684 } 685 } 686 ); 687 688 }); // End cats. 689 690 // Custom Fields postbox. 691 if ( $('#postcustom').length ) { 692 $( '#the-list' ).wpList( { 693 /** 694 * Add current post_ID to request to fetch custom fields 695 * 696 * @ignore 697 * 698 * @param {Object} s Request object. 699 * 700 * @return {Object} Data modified with post_ID attached. 701 */ 702 addBefore: function( s ) { 703 s.data += '&post_id=' + $('#post_ID').val(); 704 return s; 705 }, 706 /** 707 * Show the listing of custom fields after fetching. 708 * 709 * @ignore 710 */ 711 addAfter: function() { 712 $('table#list-table').show(); 713 } 714 }); 715 } 716 717 /* 718 * Publish Post box (#submitdiv) 719 */ 720 if ( $('#submitdiv').length ) { 721 stamp = $('#timestamp').html(); 722 visibility = $('#post-visibility-display').html(); 723 724 /** 725 * When the visibility of a post changes sub-options should be shown or hidden. 726 * 727 * @ignore 728 * 729 * @return {void} 730 */ 731 updateVisibility = function() { 732 // Show sticky for public posts. 733 if ( $postVisibilitySelect.find('input:radio:checked').val() != 'public' ) { 734 $('#sticky').prop('checked', false); 735 $('#sticky-span').hide(); 736 } else { 737 $('#sticky-span').show(); 738 } 739 740 // Show password input field for password protected post. 741 if ( $postVisibilitySelect.find('input:radio:checked').val() != 'password' ) { 742 $('#password-span').hide(); 743 } else { 744 $('#password-span').show(); 745 } 746 }; 747 748 /** 749 * Make sure all labels represent the current settings. 750 * 751 * @ignore 752 * 753 * @return {boolean} False when an invalid timestamp has been selected, otherwise True. 754 */ 755 updateText = function() { 756 757 if ( ! $timestampdiv.length ) 758 return true; 759 760 var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'), 761 optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(), 762 mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val(); 763 764 attemptedDate = new Date( aa, mm - 1, jj, hh, mn ); 765 originalDate = new Date( 766 $('#hidden_aa').val(), 767 $('#hidden_mm').val() -1, 768 $('#hidden_jj').val(), 769 $('#hidden_hh').val(), 770 $('#hidden_mn').val() 771 ); 772 currentDate = new Date( 773 $('#cur_aa').val(), 774 $('#cur_mm').val() -1, 775 $('#cur_jj').val(), 776 $('#cur_hh').val(), 777 $('#cur_mn').val() 778 ); 779 780 // Catch unexpected date problems. 781 if ( 782 attemptedDate.getFullYear() != aa || 783 (1 + attemptedDate.getMonth()) != mm || 784 attemptedDate.getDate() != jj || 785 attemptedDate.getMinutes() != mn 786 ) { 787 $timestampdiv.find('.timestamp-wrap').addClass('form-invalid'); 788 return false; 789 } else { 790 $timestampdiv.find('.timestamp-wrap').removeClass('form-invalid'); 791 } 792 793 // Determine what the publish should be depending on the date and post status. 794 if ( attemptedDate > currentDate ) { 795 publishOn = __( 'Schedule for:' ); 796 $('#publish').val( _x( 'Schedule', 'post action/button label' ) ); 797 } else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) { 798 publishOn = __( 'Publish on:' ); 799 $('#publish').val( __( 'Publish' ) ); 800 } else { 801 publishOn = __( 'Published on:' ); 802 $('#publish').val( __( 'Update' ) ); 803 } 804 805 // If the date is the same, set it to trigger update events. 806 if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { 807 // Re-set to the current value. 808 $('#timestamp').html(stamp); 809 } else { 810 $('#timestamp').html( 811 '\n' + publishOn + ' <b>' + 812 // translators: 1: Month, 2: Day, 3: Year, 4: Hour, 5: Minute. 813 __( '%1$s %2$s, %3$s at %4$s:%5$s' ) 814 .replace( '%1$s', $( 'option[value="' + mm + '"]', '#mm' ).attr( 'data-text' ) ) 815 .replace( '%2$s', parseInt( jj, 10 ) ) 816 .replace( '%3$s', aa ) 817 .replace( '%4$s', ( '00' + hh ).slice( -2 ) ) 818 .replace( '%5$s', ( '00' + mn ).slice( -2 ) ) + 819 '</b> ' 820 ); 821 } 822 823 // Add "privately published" to post status when applies. 824 if ( $postVisibilitySelect.find('input:radio:checked').val() == 'private' ) { 825 $('#publish').val( __( 'Update' ) ); 826 if ( 0 === optPublish.length ) { 827 postStatus.append('<option value="publish">' + __( 'Privately Published' ) + '</option>'); 828 } else { 829 optPublish.html( __( 'Privately Published' ) ); 830 } 831 $('option[value="publish"]', postStatus).prop('selected', true); 832 $('#misc-publishing-actions .edit-post-status').hide(); 833 } else { 834 if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) { 835 if ( optPublish.length ) { 836 optPublish.remove(); 837 postStatus.val($('#hidden_post_status').val()); 838 } 839 } else { 840 optPublish.html( __( 'Published' ) ); 841 } 842 if ( postStatus.is(':hidden') ) 843 $('#misc-publishing-actions .edit-post-status').show(); 844 } 845 846 // Update "Status:" to currently selected status. 847 $('#post-status-display').text( 848 // Remove any potential tags from post status text. 849 wp.sanitize.stripTagsAndEncodeText( $('option:selected', postStatus).text() ) 850 ); 851 852 // Show or hide the "Save Draft" button. 853 if ( 854 $('option:selected', postStatus).val() == 'private' || 855 $('option:selected', postStatus).val() == 'publish' 856 ) { 857 $('#save-post').hide(); 858 } else { 859 $('#save-post').show(); 860 if ( $('option:selected', postStatus).val() == 'pending' ) { 861 $('#save-post').show().val( __( 'Save as Pending' ) ); 862 } else { 863 $('#save-post').show().val( __( 'Save Draft' ) ); 864 } 865 } 866 return true; 867 }; 868 869 // Show the visibility options and hide the toggle button when opened. 870 $( '#visibility .edit-visibility').on( 'click', function( e ) { 871 e.preventDefault(); 872 if ( $postVisibilitySelect.is(':hidden') ) { 873 updateVisibility(); 874 $postVisibilitySelect.slideDown( 'fast', function() { 875 $postVisibilitySelect.find( 'input[type="radio"]' ).first().trigger( 'focus' ); 876 } ); 877 $(this).hide(); 878 } 879 }); 880 881 // Cancel visibility selection area and hide it from view. 882 $postVisibilitySelect.find('.cancel-post-visibility').on( 'click', function( event ) { 883 $postVisibilitySelect.slideUp('fast'); 884 $('#visibility-radio-' + $('#hidden-post-visibility').val()).prop('checked', true); 885 $('#post_password').val($('#hidden-post-password').val()); 886 $('#sticky').prop('checked', $('#hidden-post-sticky').prop('checked')); 887 $('#post-visibility-display').html(visibility); 888 $('#visibility .edit-visibility').show().trigger( 'focus' ); 889 updateText(); 890 event.preventDefault(); 891 }); 892 893 // Set the selected visibility as current. 894 $postVisibilitySelect.find('.save-post-visibility').on( 'click', function( event ) { // Crazyhorse branch - multiple OK cancels. 895 var visibilityLabel = '', selectedVisibility = $postVisibilitySelect.find('input:radio:checked').val(); 896 897 $postVisibilitySelect.slideUp('fast'); 898 $('#visibility .edit-visibility').show().trigger( 'focus' ); 899 updateText(); 900 901 if ( 'public' !== selectedVisibility ) { 902 $('#sticky').prop('checked', false); 903 } 904 905 switch ( selectedVisibility ) { 906 case 'public': 907 visibilityLabel = $( '#sticky' ).prop( 'checked' ) ? __( 'Public, Sticky' ) : __( 'Public' ); 908 break; 909 case 'private': 910 visibilityLabel = __( 'Private' ); 911 break; 912 case 'password': 913 visibilityLabel = __( 'Password Protected' ); 914 break; 915 } 916 917 $('#post-visibility-display').text( visibilityLabel ); 918 event.preventDefault(); 919 }); 920 921 // When the selection changes, update labels. 922 $postVisibilitySelect.find('input:radio').on( 'change', function() { 923 updateVisibility(); 924 }); 925 926 // Edit publish time click. 927 $timestampdiv.siblings('a.edit-timestamp').on( 'click', function( event ) { 928 if ( $timestampdiv.is( ':hidden' ) ) { 929 $timestampdiv.slideDown( 'fast', function() { 930 $( 'input, select', $timestampdiv.find( '.timestamp-wrap' ) ).first().trigger( 'focus' ); 931 } ); 932 $(this).hide(); 933 } 934 event.preventDefault(); 935 }); 936 937 // Cancel editing the publish time and hide the settings. 938 $timestampdiv.find('.cancel-timestamp').on( 'click', function( event ) { 939 $timestampdiv.slideUp('fast').siblings('a.edit-timestamp').show().trigger( 'focus' ); 940 $('#mm').val($('#hidden_mm').val()); 941 $('#jj').val($('#hidden_jj').val()); 942 $('#aa').val($('#hidden_aa').val()); 943 $('#hh').val($('#hidden_hh').val()); 944 $('#mn').val($('#hidden_mn').val()); 945 updateText(); 946 event.preventDefault(); 947 }); 948 949 // Save the changed timestamp. 950 $timestampdiv.find('.save-timestamp').on( 'click', function( event ) { // Crazyhorse branch - multiple OK cancels. 951 if ( updateText() ) { 952 $timestampdiv.slideUp('fast'); 953 $timestampdiv.siblings('a.edit-timestamp').show().trigger( 'focus' ); 954 } 955 event.preventDefault(); 956 }); 957 958 // Cancel submit when an invalid timestamp has been selected. 959 $('#post').on( 'submit', function( event ) { 960 if ( ! updateText() ) { 961 event.preventDefault(); 962 $timestampdiv.show(); 963 964 if ( wp.autosave ) { 965 wp.autosave.enableButtons(); 966 } 967 968 $( '#publishing-action .spinner' ).removeClass( 'is-active' ); 969 } 970 }); 971 972 // Post Status edit click. 973 $postStatusSelect.siblings('a.edit-post-status').on( 'click', function( event ) { 974 if ( $postStatusSelect.is( ':hidden' ) ) { 975 $postStatusSelect.slideDown( 'fast', function() { 976 $postStatusSelect.find('select').trigger( 'focus' ); 977 } ); 978 $(this).hide(); 979 } 980 event.preventDefault(); 981 }); 982 983 // Save the Post Status changes and hide the options. 984 $postStatusSelect.find('.save-post-status').on( 'click', function( event ) { 985 $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().trigger( 'focus' ); 986 updateText(); 987 event.preventDefault(); 988 }); 989 990 // Cancel Post Status editing and hide the options. 991 $postStatusSelect.find('.cancel-post-status').on( 'click', function( event ) { 992 $postStatusSelect.slideUp( 'fast' ).siblings( 'a.edit-post-status' ).show().trigger( 'focus' ); 993 $('#post_status').val( $('#hidden_post_status').val() ); 994 updateText(); 995 event.preventDefault(); 996 }); 997 } 998 999 /** 1000 * Handle the editing of the post_name. Create the required HTML elements and 1001 * update the changes via Ajax. 1002 * 1003 * @global 1004 * 1005 * @return {void} 1006 */ 1007 function editPermalink() { 1008 var i, slug_value, slug_label, 1009 $el, revert_e, 1010 c = 0, 1011 real_slug = $('#post_name'), 1012 revert_slug = real_slug.val(), 1013 permalink = $( '#sample-permalink' ), 1014 permalinkOrig = permalink.html(), 1015 permalinkInner = $( '#sample-permalink a' ).html(), 1016 buttons = $('#edit-slug-buttons'), 1017 buttonsOrig = buttons.html(), 1018 full = $('#editable-post-name-full'); 1019 1020 // Deal with Twemoji in the post-name. 1021 full.find( 'img' ).replaceWith( function() { return this.alt; } ); 1022 full = full.html(); 1023 1024 permalink.html( permalinkInner ); 1025 1026 // Save current content to revert to when cancelling. 1027 $el = $( '#editable-post-name' ); 1028 revert_e = $el.html(); 1029 1030 buttons.html( 1031 '<button type="button" class="save button button-compact">' + __( 'OK' ) + '</button> ' + 1032 '<button type="button" class="cancel button-link">' + __( 'Cancel' ) + '</button>' 1033 ); 1034 1035 // Save permalink changes. 1036 buttons.children( '.save' ).on( 'click', function() { 1037 var new_slug = $el.children( 'input' ).val(); 1038 1039 if ( new_slug == $('#editable-post-name-full').text() ) { 1040 buttons.children('.cancel').trigger( 'click' ); 1041 return; 1042 } 1043 1044 $.post( 1045 ajaxurl, 1046 { 1047 action: 'sample-permalink', 1048 post_id: postId, 1049 new_slug: new_slug, 1050 new_title: $('#title').val(), 1051 samplepermalinknonce: $('#samplepermalinknonce').val() 1052 }, 1053 function(data) { 1054 var box = $('#edit-slug-box'); 1055 box.html(data); 1056 if (box.hasClass('hidden')) { 1057 box.fadeIn('fast', function () { 1058 box.removeClass('hidden'); 1059 }); 1060 } 1061 1062 buttons.html(buttonsOrig); 1063 permalink.html(permalinkOrig); 1064 real_slug.val(new_slug); 1065 $( '.edit-slug' ).trigger( 'focus' ); 1066 wp.a11y.speak( __( 'Permalink saved' ) ); 1067 } 1068 ); 1069 }); 1070 1071 // Cancel editing of permalink. 1072 buttons.children( '.cancel' ).on( 'click', function() { 1073 $('#view-post-btn').show(); 1074 $el.html(revert_e); 1075 buttons.html(buttonsOrig); 1076 permalink.html(permalinkOrig); 1077 real_slug.val(revert_slug); 1078 $( '.edit-slug' ).trigger( 'focus' ); 1079 }); 1080 1081 // If more than 1/4th of 'full' is '%', make it empty. 1082 for ( i = 0; i < full.length; ++i ) { 1083 if ( '%' == full.charAt(i) ) 1084 c++; 1085 } 1086 slug_value = ( c > full.length / 4 ) ? '' : full; 1087 slug_label = __( 'URL Slug' ); 1088 1089 $el.html( 1090 '<label for="new-post-slug" class="screen-reader-text">' + slug_label + '</label>' + 1091 '<input type="text" id="new-post-slug" value="' + slug_value + '" autocomplete="off" spellcheck="false" />' 1092 ).children( 'input' ).on( 'keydown', function( e ) { 1093 var key = e.which; 1094 // On [Enter], just save the new slug, don't save the post. 1095 if ( 13 === key ) { 1096 e.preventDefault(); 1097 buttons.children( '.save' ).trigger( 'click' ); 1098 } 1099 // On [Esc] cancel the editing. 1100 if ( 27 === key ) { 1101 buttons.children( '.cancel' ).trigger( 'click' ); 1102 } 1103 } ).on( 'keyup', function() { 1104 real_slug.val( this.value ); 1105 }).trigger( 'focus' ); 1106 } 1107 1108 $( '#titlediv' ).on( 'click', '.edit-slug', function() { 1109 editPermalink(); 1110 }); 1111 1112 /** 1113 * Adds screen reader text to the title label when needed. 1114 * 1115 * Use the 'screen-reader-text' class to emulate a placeholder attribute 1116 * and hide the label when entering a value. 1117 * 1118 * @param {string} id Optional. HTML ID to add the screen reader helper text to. 1119 * 1120 * @global 1121 * 1122 * @return {void} 1123 */ 1124 window.wptitlehint = function( id ) { 1125 id = id || 'title'; 1126 1127 var title = $( '#' + id ), titleprompt = $( '#' + id + '-prompt-text' ); 1128 1129 if ( '' === title.val() ) { 1130 titleprompt.removeClass( 'screen-reader-text' ); 1131 } 1132 1133 title.on( 'input', function() { 1134 if ( '' === this.value ) { 1135 titleprompt.removeClass( 'screen-reader-text' ); 1136 return; 1137 } 1138 1139 titleprompt.addClass( 'screen-reader-text' ); 1140 } ); 1141 }; 1142 1143 wptitlehint(); 1144 1145 // Resize the WYSIWYG and plain text editors. 1146 ( function() { 1147 var editor, offset, mce, 1148 $handle = $('#post-status-info'), 1149 $postdivrich = $('#postdivrich'); 1150 1151 // If there are no textareas or we are on a touch device, we can't do anything. 1152 if ( ! $textarea.length || 'ontouchstart' in window ) { 1153 // Hide the resize handle. 1154 $('#content-resize-handle').hide(); 1155 return; 1156 } 1157 1158 /** 1159 * Handle drag event. 1160 * 1161 * @param {Object} event Event containing details about the drag. 1162 */ 1163 function dragging( event ) { 1164 if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) { 1165 return; 1166 } 1167 1168 if ( mce ) { 1169 editor.theme.resizeTo( null, offset + event.pageY ); 1170 } else { 1171 $textarea.height( Math.max( 50, offset + event.pageY ) ); 1172 } 1173 1174 event.preventDefault(); 1175 } 1176 1177 /** 1178 * When the dragging stopped make sure we return focus and do a confidence check on the height. 1179 */ 1180 function endDrag() { 1181 var height, toolbarHeight; 1182 1183 if ( $postdivrich.hasClass( 'wp-editor-expand' ) ) { 1184 return; 1185 } 1186 1187 if ( mce ) { 1188 editor.focus(); 1189 toolbarHeight = parseInt( $( '#wp-content-editor-container .mce-toolbar-grp' ).height(), 10 ); 1190 1191 if ( toolbarHeight < 10 || toolbarHeight > 200 ) { 1192 toolbarHeight = 30; 1193 } 1194 1195 height = parseInt( $('#content_ifr').css('height'), 10 ) + toolbarHeight - 28; 1196 } else { 1197 $textarea.trigger( 'focus' ); 1198 height = parseInt( $textarea.css('height'), 10 ); 1199 } 1200 1201 $document.off( '.wp-editor-resize' ); 1202 1203 // Confidence check: normalize height to stay within acceptable ranges. 1204 if ( height && height > 50 && height < 5000 ) { 1205 setUserSetting( 'ed_size', height ); 1206 } 1207 } 1208 1209 $handle.on( 'mousedown.wp-editor-resize', function( event ) { 1210 if ( typeof tinymce !== 'undefined' ) { 1211 editor = tinymce.get('content'); 1212 } 1213 1214 if ( editor && ! editor.isHidden() ) { 1215 mce = true; 1216 offset = $('#content_ifr').height() - event.pageY; 1217 } else { 1218 mce = false; 1219 offset = $textarea.height() - event.pageY; 1220 $textarea.trigger( 'blur' ); 1221 } 1222 1223 $document.on( 'mousemove.wp-editor-resize', dragging ) 1224 .on( 'mouseup.wp-editor-resize mouseleave.wp-editor-resize', endDrag ); 1225 1226 event.preventDefault(); 1227 }).on( 'mouseup.wp-editor-resize', endDrag ); 1228 })(); 1229 1230 // TinyMCE specific handling of Post Format changes to reflect in the editor. 1231 if ( typeof tinymce !== 'undefined' ) { 1232 // When changing post formats, change the editor body class. 1233 $( '#post-formats-select input.post-format' ).on( 'change.set-editor-class', function() { 1234 var editor, body, format = this.id; 1235 1236 if ( format && $( this ).prop( 'checked' ) && ( editor = tinymce.get( 'content' ) ) ) { 1237 body = editor.getBody(); 1238 body.className = body.className.replace( /\bpost-format-[^ ]+/, '' ); 1239 editor.dom.addClass( body, format == 'post-format-0' ? 'post-format-standard' : format ); 1240 $( document ).trigger( 'editor-classchange' ); 1241 } 1242 }); 1243 1244 // When changing page template, change the editor body class. 1245 $( '#page_template' ).on( 'change.set-editor-class', function() { 1246 var editor, body, pageTemplate = $( this ).val() || ''; 1247 1248 pageTemplate = pageTemplate.substr( pageTemplate.lastIndexOf( '/' ) + 1, pageTemplate.length ) 1249 .replace( /\.php$/, '' ) 1250 .replace( /\./g, '-' ); 1251 1252 if ( pageTemplate && ( editor = tinymce.get( 'content' ) ) ) { 1253 body = editor.getBody(); 1254 body.className = body.className.replace( /\bpage-template-[^ ]+/, '' ); 1255 editor.dom.addClass( body, 'page-template-' + pageTemplate ); 1256 $( document ).trigger( 'editor-classchange' ); 1257 } 1258 }); 1259 1260 } 1261 1262 // Save on pressing [Ctrl]/[Command] + [S] in the Text editor. 1263 $textarea.on( 'keydown.wp-autosave', function( event ) { 1264 // Key [S] has code 83. 1265 if ( event.which === 83 ) { 1266 if ( 1267 event.shiftKey || 1268 event.altKey || 1269 ( isMac && ( ! event.metaKey || event.ctrlKey ) ) || 1270 ( ! isMac && ! event.ctrlKey ) 1271 ) { 1272 return; 1273 } 1274 1275 wp.autosave && wp.autosave.server.triggerSave(); 1276 event.preventDefault(); 1277 } 1278 }); 1279 1280 // If the last status was auto-draft and the save is triggered, edit the current URL. 1281 if ( $( '#original_post_status' ).val() === 'auto-draft' && window.history.replaceState ) { 1282 var location; 1283 1284 $( '#publish' ).on( 'click', function() { 1285 location = window.location.href; 1286 location += ( location.indexOf( '?' ) !== -1 ) ? '&' : '?'; 1287 location += 'wp-post-new-reload=true'; 1288 1289 window.history.replaceState( null, null, location ); 1290 }); 1291 } 1292 1293 /** 1294 * Copies the attachment URL in the Edit Media page to the clipboard. 1295 * 1296 * @since 5.5.0 1297 * 1298 * @param {MouseEvent} event A click event. 1299 * 1300 * @return {void} 1301 */ 1302 copyAttachmentURLClipboard.on( 'success', function( event ) { 1303 var triggerElement = $( event.trigger ), 1304 successElement = $( '.success', triggerElement.closest( '.copy-to-clipboard-container' ) ); 1305 1306 // Clear the selection and move focus back to the trigger. 1307 event.clearSelection(); 1308 1309 // Show success visual feedback. 1310 clearTimeout( copyAttachmentURLSuccessTimeout ); 1311 successElement.removeClass( 'hidden' ); 1312 1313 // Hide success visual feedback after 3 seconds since last success. 1314 copyAttachmentURLSuccessTimeout = setTimeout( function() { 1315 successElement.addClass( 'hidden' ); 1316 }, 3000 ); 1317 1318 // Handle success audible feedback. 1319 wp.a11y.speak( __( 'The file URL has been copied to your clipboard' ) ); 1320 } ); 1321 } ); 1322 1323 /** 1324 * TinyMCE word count display 1325 */ 1326 ( function( $, counter ) { 1327 $( function() { 1328 var $content = $( '#content' ), 1329 $count = $( '#wp-word-count' ).find( '.word-count' ), 1330 prevCount = 0, 1331 contentEditor; 1332 1333 /** 1334 * Get the word count from TinyMCE and display it 1335 */ 1336 function update() { 1337 var text, count; 1338 1339 if ( ! contentEditor || contentEditor.isHidden() ) { 1340 text = $content.val(); 1341 } else { 1342 text = contentEditor.getContent( { format: 'raw' } ); 1343 } 1344 1345 count = counter.count( text ); 1346 1347 if ( count !== prevCount ) { 1348 $count.text( count ); 1349 } 1350 1351 prevCount = count; 1352 } 1353 1354 /** 1355 * Bind the word count update triggers. 1356 * 1357 * When a node change in the main TinyMCE editor has been triggered. 1358 * When a key has been released in the plain text content editor. 1359 */ 1360 $( document ).on( 'tinymce-editor-init', function( event, editor ) { 1361 if ( editor.id !== 'content' ) { 1362 return; 1363 } 1364 1365 contentEditor = editor; 1366 1367 editor.on( 'nodechange keyup', _.debounce( update, 1000 ) ); 1368 } ); 1369 1370 $content.on( 'input keyup', _.debounce( update, 1000 ) ); 1371 1372 update(); 1373 } ); 1374 1375 } )( jQuery, new wp.utils.WordCounter() );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Mon Apr 27 08:20:11 2026 | Cross-referenced by PHPXref |