post.js 39 KB

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