暂无描述

post.js 39KB

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