updates.js 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  1. /**
  2. * Functions for ajaxified updates, deletions and installs inside the WordPress admin.
  3. *
  4. * @version 4.2.0
  5. *
  6. * @package WordPress
  7. * @subpackage Administration
  8. */
  9. /* global pagenow */
  10. /**
  11. * @param {jQuery} $ jQuery object.
  12. * @param {object} wp WP object.
  13. * @param {object} settings WP Updates settings.
  14. * @param {string} settings.ajax_nonce AJAX nonce.
  15. * @param {object} settings.l10n Translation strings.
  16. * @param {object=} settings.plugins Base names of plugins in their different states.
  17. * @param {Array} settings.plugins.all Base names of all plugins.
  18. * @param {Array} settings.plugins.active Base names of active plugins.
  19. * @param {Array} settings.plugins.inactive Base names of inactive plugins.
  20. * @param {Array} settings.plugins.upgrade Base names of plugins with updates available.
  21. * @param {Array} settings.plugins.recently_activated Base names of recently activated plugins.
  22. * @param {object=} settings.themes Plugin/theme status information or null.
  23. * @param {number} settings.themes.all Amount of all themes.
  24. * @param {number} settings.themes.upgrade Amount of themes with updates available.
  25. * @param {number} settings.themes.disabled Amount of disabled themes.
  26. * @param {object=} settings.totals Combined information for available update counts.
  27. * @param {number} settings.totals.count Holds the amount of available updates.
  28. */
  29. (function( $, wp, settings ) {
  30. var $document = $( document );
  31. wp = wp || {};
  32. /**
  33. * The WP Updates object.
  34. *
  35. * @since 4.2.0
  36. *
  37. * @type {object}
  38. */
  39. wp.updates = {};
  40. /**
  41. * User nonce for ajax calls.
  42. *
  43. * @since 4.2.0
  44. *
  45. * @type {string}
  46. */
  47. wp.updates.ajaxNonce = settings.ajax_nonce;
  48. /**
  49. * Localized strings.
  50. *
  51. * @since 4.2.0
  52. *
  53. * @type {object}
  54. */
  55. wp.updates.l10n = settings.l10n;
  56. /**
  57. * Current search term.
  58. *
  59. * @since 4.6.0
  60. *
  61. * @type {string}
  62. */
  63. wp.updates.searchTerm = '';
  64. /**
  65. * Whether filesystem credentials need to be requested from the user.
  66. *
  67. * @since 4.2.0
  68. *
  69. * @type {bool}
  70. */
  71. wp.updates.shouldRequestFilesystemCredentials = false;
  72. /**
  73. * Filesystem credentials to be packaged along with the request.
  74. *
  75. * @since 4.2.0
  76. * @since 4.6.0 Added `available` property to indicate whether credentials have been provided.
  77. *
  78. * @type {object} filesystemCredentials Holds filesystem credentials.
  79. * @type {object} filesystemCredentials.ftp Holds FTP credentials.
  80. * @type {string} filesystemCredentials.ftp.host FTP host. Default empty string.
  81. * @type {string} filesystemCredentials.ftp.username FTP user name. Default empty string.
  82. * @type {string} filesystemCredentials.ftp.password FTP password. Default empty string.
  83. * @type {string} filesystemCredentials.ftp.connectionType Type of FTP connection. 'ssh', 'ftp', or 'ftps'.
  84. * Default empty string.
  85. * @type {object} filesystemCredentials.ssh Holds SSH credentials.
  86. * @type {string} filesystemCredentials.ssh.publicKey The public key. Default empty string.
  87. * @type {string} filesystemCredentials.ssh.privateKey The private key. Default empty string.
  88. * @type {string} filesystemCredentials.fsNonce Filesystem credentials form nonce.
  89. * @type {bool} filesystemCredentials.available Whether filesystem credentials have been provided.
  90. * Default 'false'.
  91. */
  92. wp.updates.filesystemCredentials = {
  93. ftp: {
  94. host: '',
  95. username: '',
  96. password: '',
  97. connectionType: ''
  98. },
  99. ssh: {
  100. publicKey: '',
  101. privateKey: ''
  102. },
  103. fsNonce: '',
  104. available: false
  105. };
  106. /**
  107. * Whether we're waiting for an Ajax request to complete.
  108. *
  109. * @since 4.2.0
  110. * @since 4.6.0 More accurately named `ajaxLocked`.
  111. *
  112. * @type {bool}
  113. */
  114. wp.updates.ajaxLocked = false;
  115. /**
  116. * Admin notice template.
  117. *
  118. * @since 4.6.0
  119. *
  120. * @type {function} A function that lazily-compiles the template requested.
  121. */
  122. wp.updates.adminNotice = wp.template( 'wp-updates-admin-notice' );
  123. /**
  124. * Update queue.
  125. *
  126. * If the user tries to update a plugin while an update is
  127. * already happening, it can be placed in this queue to perform later.
  128. *
  129. * @since 4.2.0
  130. * @since 4.6.0 More accurately named `queue`.
  131. *
  132. * @type {Array.object}
  133. */
  134. wp.updates.queue = [];
  135. /**
  136. * Holds a jQuery reference to return focus to when exiting the request credentials modal.
  137. *
  138. * @since 4.2.0
  139. *
  140. * @type {jQuery}
  141. */
  142. wp.updates.$elToReturnFocusToFromCredentialsModal = undefined;
  143. /**
  144. * Adds or updates an admin notice.
  145. *
  146. * @since 4.6.0
  147. *
  148. * @param {object} data
  149. * @param {*=} data.selector Optional. Selector of an element to be replaced with the admin notice.
  150. * @param {string=} data.id Optional. Unique id that will be used as the notice's id attribute.
  151. * @param {string=} data.className Optional. Class names that will be used in the admin notice.
  152. * @param {string=} data.message Optional. The message displayed in the notice.
  153. * @param {number=} data.successes Optional. The amount of successful operations.
  154. * @param {number=} data.errors Optional. The amount of failed operations.
  155. * @param {Array=} data.errorMessages Optional. Error messages of failed operations.
  156. *
  157. */
  158. wp.updates.addAdminNotice = function( data ) {
  159. var $notice = $( data.selector ), $adminNotice;
  160. delete data.selector;
  161. $adminNotice = wp.updates.adminNotice( data );
  162. // Check if this admin notice already exists.
  163. if ( ! $notice.length ) {
  164. $notice = $( '#' + data.id );
  165. }
  166. if ( $notice.length ) {
  167. $notice.replaceWith( $adminNotice );
  168. } else {
  169. $( '.wrap' ).find( '> h1' ).after( $adminNotice );
  170. }
  171. $document.trigger( 'wp-updates-notice-added' );
  172. };
  173. /**
  174. * Handles Ajax requests to WordPress.
  175. *
  176. * @since 4.6.0
  177. *
  178. * @param {string} action The type of Ajax request ('update-plugin', 'install-theme', etc).
  179. * @param {object} data Data that needs to be passed to the ajax callback.
  180. * @return {$.promise} A jQuery promise that represents the request,
  181. * decorated with an abort() method.
  182. */
  183. wp.updates.ajax = function( action, data ) {
  184. var options = {};
  185. if ( wp.updates.ajaxLocked ) {
  186. wp.updates.queue.push( {
  187. action: action,
  188. data: data
  189. } );
  190. // Return a Deferred object so callbacks can always be registered.
  191. return $.Deferred();
  192. }
  193. wp.updates.ajaxLocked = true;
  194. if ( data.success ) {
  195. options.success = data.success;
  196. delete data.success;
  197. }
  198. if ( data.error ) {
  199. options.error = data.error;
  200. delete data.error;
  201. }
  202. options.data = _.extend( data, {
  203. action: action,
  204. _ajax_nonce: wp.updates.ajaxNonce,
  205. _fs_nonce: wp.updates.filesystemCredentials.fsNonce,
  206. username: wp.updates.filesystemCredentials.ftp.username,
  207. password: wp.updates.filesystemCredentials.ftp.password,
  208. hostname: wp.updates.filesystemCredentials.ftp.hostname,
  209. connection_type: wp.updates.filesystemCredentials.ftp.connectionType,
  210. public_key: wp.updates.filesystemCredentials.ssh.publicKey,
  211. private_key: wp.updates.filesystemCredentials.ssh.privateKey
  212. } );
  213. return wp.ajax.send( options ).always( wp.updates.ajaxAlways );
  214. };
  215. /**
  216. * Actions performed after every Ajax request.
  217. *
  218. * @since 4.6.0
  219. *
  220. * @param {object} response
  221. * @param {array=} response.debug Optional. Debug information.
  222. * @param {string=} response.errorCode Optional. Error code for an error that occurred.
  223. */
  224. wp.updates.ajaxAlways = function( response ) {
  225. if ( ! response.errorCode || 'unable_to_connect_to_filesystem' !== response.errorCode ) {
  226. wp.updates.ajaxLocked = false;
  227. wp.updates.queueChecker();
  228. }
  229. if ( 'undefined' !== typeof response.debug && window.console && window.console.log ) {
  230. _.map( response.debug, function( message ) {
  231. window.console.log( $( '<p />' ).html( message ).text() );
  232. } );
  233. }
  234. };
  235. /**
  236. * Refreshes update counts everywhere on the screen.
  237. *
  238. * @since 4.7.0
  239. */
  240. wp.updates.refreshCount = function() {
  241. var $adminBarUpdates = $( '#wp-admin-bar-updates' ),
  242. $dashboardNavMenuUpdateCount = $( 'a[href="update-core.php"] .update-plugins' ),
  243. $pluginsNavMenuUpdateCount = $( 'a[href="plugins.php"] .update-plugins' ),
  244. $appearanceNavMenuUpdateCount = $( 'a[href="themes.php"] .update-plugins' ),
  245. itemCount;
  246. $adminBarUpdates.find( '.ab-item' ).removeAttr( 'title' );
  247. $adminBarUpdates.find( '.ab-label' ).text( settings.totals.counts.total );
  248. // Remove the update count from the toolbar if it's zero.
  249. if ( 0 === settings.totals.counts.total ) {
  250. $adminBarUpdates.find( '.ab-label' ).parents( 'li' ).remove();
  251. }
  252. // Update the "Updates" menu item.
  253. $dashboardNavMenuUpdateCount.each( function( index, element ) {
  254. element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.total );
  255. } );
  256. if ( settings.totals.counts.total > 0 ) {
  257. $dashboardNavMenuUpdateCount.find( '.update-count' ).text( settings.totals.counts.total );
  258. } else {
  259. $dashboardNavMenuUpdateCount.remove();
  260. }
  261. // Update the "Plugins" menu item.
  262. $pluginsNavMenuUpdateCount.each( function( index, element ) {
  263. element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.plugins );
  264. } );
  265. if ( settings.totals.counts.total > 0 ) {
  266. $pluginsNavMenuUpdateCount.find( '.plugin-count' ).text( settings.totals.counts.plugins );
  267. } else {
  268. $pluginsNavMenuUpdateCount.remove();
  269. }
  270. // Update the "Appearance" menu item.
  271. $appearanceNavMenuUpdateCount.each( function( index, element ) {
  272. element.className = element.className.replace( /count-\d+/, 'count-' + settings.totals.counts.themes );
  273. } );
  274. if ( settings.totals.counts.total > 0 ) {
  275. $appearanceNavMenuUpdateCount.find( '.theme-count' ).text( settings.totals.counts.themes );
  276. } else {
  277. $appearanceNavMenuUpdateCount.remove();
  278. }
  279. // Update list table filter navigation.
  280. if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
  281. itemCount = settings.totals.counts.plugins;
  282. } else if ( 'themes' === pagenow || 'themes-network' === pagenow ) {
  283. itemCount = settings.totals.counts.themes;
  284. }
  285. if ( itemCount > 0 ) {
  286. $( '.subsubsub .upgrade .count' ).text( '(' + itemCount + ')' );
  287. } else {
  288. $( '.subsubsub .upgrade' ).remove();
  289. $( '.subsubsub li:last' ).html( function() { return $( this ).children(); } );
  290. }
  291. };
  292. /**
  293. * Decrements the update counts throughout the various menus.
  294. *
  295. * This includes the toolbar, the "Updates" menu item and the menu items
  296. * for plugins and themes.
  297. *
  298. * @since 3.9.0
  299. *
  300. * @param {string} type The type of item that was updated or deleted.
  301. * Can be 'plugin', 'theme'.
  302. */
  303. wp.updates.decrementCount = function( type ) {
  304. settings.totals.counts.total = Math.max( --settings.totals.counts.total, 0 );
  305. if ( 'plugin' === type ) {
  306. settings.totals.counts.plugins = Math.max( --settings.totals.counts.plugins, 0 );
  307. } else if ( 'theme' === type ) {
  308. settings.totals.counts.themes = Math.max( --settings.totals.counts.themes, 0 );
  309. }
  310. wp.updates.refreshCount( type );
  311. };
  312. /**
  313. * Sends an Ajax request to the server to update a plugin.
  314. *
  315. * @since 4.2.0
  316. * @since 4.6.0 More accurately named `updatePlugin`.
  317. *
  318. * @param {object} args Arguments.
  319. * @param {string} args.plugin Plugin basename.
  320. * @param {string} args.slug Plugin slug.
  321. * @param {updatePluginSuccess=} args.success Optional. Success callback. Default: wp.updates.updatePluginSuccess
  322. * @param {updatePluginError=} args.error Optional. Error callback. Default: wp.updates.updatePluginError
  323. * @return {$.promise} A jQuery promise that represents the request,
  324. * decorated with an abort() method.
  325. */
  326. wp.updates.updatePlugin = function( args ) {
  327. var $updateRow, $card, $message, message;
  328. args = _.extend( {
  329. success: wp.updates.updatePluginSuccess,
  330. error: wp.updates.updatePluginError
  331. }, args );
  332. if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
  333. $updateRow = $( 'tr[data-plugin="' + args.plugin + '"]' );
  334. $message = $updateRow.find( '.update-message' ).removeClass( 'notice-error' ).addClass( 'updating-message notice-warning' ).find( 'p' );
  335. message = wp.updates.l10n.pluginUpdatingLabel.replace( '%s', $updateRow.find( '.plugin-title strong' ).text() );
  336. } else if ( 'plugin-install' === pagenow || 'plugin-install-network' === pagenow ) {
  337. $card = $( '.plugin-card-' + args.slug );
  338. $message = $card.find( '.update-now' ).addClass( 'updating-message' );
  339. message = wp.updates.l10n.pluginUpdatingLabel.replace( '%s', $message.data( 'name' ) );
  340. // Remove previous error messages, if any.
  341. $card.removeClass( 'plugin-card-update-failed' ).find( '.notice.notice-error' ).remove();
  342. }
  343. if ( $message.html() !== wp.updates.l10n.updating ) {
  344. $message.data( 'originaltext', $message.html() );
  345. }
  346. $message
  347. .attr( 'aria-label', message )
  348. .text( wp.updates.l10n.updating );
  349. $document.trigger( 'wp-plugin-updating', args );
  350. return wp.updates.ajax( 'update-plugin', args );
  351. };
  352. /**
  353. * Updates the UI appropriately after a successful plugin update.
  354. *
  355. * @since 4.2.0
  356. * @since 4.6.0 More accurately named `updatePluginSuccess`.
  357. *
  358. * @typedef {object} updatePluginSuccess
  359. * @param {object} response Response from the server.
  360. * @param {string} response.slug Slug of the plugin to be updated.
  361. * @param {string} response.plugin Basename of the plugin to be updated.
  362. * @param {string} response.pluginName Name of the plugin to be updated.
  363. * @param {string} response.oldVersion Old version of the plugin.
  364. * @param {string} response.newVersion New version of the plugin.
  365. */
  366. wp.updates.updatePluginSuccess = function( response ) {
  367. var $pluginRow, $updateMessage, newText;
  368. if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
  369. $pluginRow = $( 'tr[data-plugin="' + response.plugin + '"]' )
  370. .removeClass( 'update' )
  371. .addClass( 'updated' );
  372. $updateMessage = $pluginRow.find( '.update-message' )
  373. .removeClass( 'updating-message notice-warning' )
  374. .addClass( 'updated-message notice-success' ).find( 'p' );
  375. // Update the version number in the row.
  376. newText = $pluginRow.find( '.plugin-version-author-uri' ).html().replace( response.oldVersion, response.newVersion );
  377. $pluginRow.find( '.plugin-version-author-uri' ).html( newText );
  378. } else if ( 'plugin-install' === pagenow || 'plugin-install-network' === pagenow ) {
  379. $updateMessage = $( '.plugin-card-' + response.slug ).find( '.update-now' )
  380. .removeClass( 'updating-message' )
  381. .addClass( 'button-disabled updated-message' );
  382. }
  383. $updateMessage
  384. .attr( 'aria-label', wp.updates.l10n.pluginUpdatedLabel.replace( '%s', response.pluginName ) )
  385. .text( wp.updates.l10n.pluginUpdated );
  386. wp.a11y.speak( wp.updates.l10n.updatedMsg, 'polite' );
  387. wp.updates.decrementCount( 'plugin' );
  388. $document.trigger( 'wp-plugin-update-success', response );
  389. };
  390. /**
  391. * Updates the UI appropriately after a failed plugin update.
  392. *
  393. * @since 4.2.0
  394. * @since 4.6.0 More accurately named `updatePluginError`.
  395. *
  396. * @typedef {object} updatePluginError
  397. * @param {object} response Response from the server.
  398. * @param {string} response.slug Slug of the plugin to be updated.
  399. * @param {string} response.plugin Basename of the plugin to be updated.
  400. * @param {string=} response.pluginName Optional. Name of the plugin to be updated.
  401. * @param {string} response.errorCode Error code for the error that occurred.
  402. * @param {string} response.errorMessage The error that occurred.
  403. */
  404. wp.updates.updatePluginError = function( response ) {
  405. var $card, $message, errorMessage;
  406. if ( ! wp.updates.isValidResponse( response, 'update' ) ) {
  407. return;
  408. }
  409. if ( wp.updates.maybeHandleCredentialError( response, 'update-plugin' ) ) {
  410. return;
  411. }
  412. errorMessage = wp.updates.l10n.updateFailed.replace( '%s', response.errorMessage );
  413. if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
  414. if ( response.plugin ) {
  415. $message = $( 'tr[data-plugin="' + response.plugin + '"]' ).find( '.update-message' );
  416. } else {
  417. $message = $( 'tr[data-slug="' + response.slug + '"]' ).find( '.update-message' );
  418. }
  419. $message.removeClass( 'updating-message notice-warning' ).addClass( 'notice-error' ).find( 'p' ).html( errorMessage );
  420. if ( response.pluginName ) {
  421. $message.find( 'p' )
  422. .attr( 'aria-label', wp.updates.l10n.pluginUpdateFailedLabel.replace( '%s', response.pluginName ) );
  423. } else {
  424. $message.find( 'p' ).removeAttr( 'aria-label' );
  425. }
  426. } else if ( 'plugin-install' === pagenow || 'plugin-install-network' === pagenow ) {
  427. $card = $( '.plugin-card-' + response.slug )
  428. .addClass( 'plugin-card-update-failed' )
  429. .append( wp.updates.adminNotice( {
  430. className: 'update-message notice-error notice-alt is-dismissible',
  431. message: errorMessage
  432. } ) );
  433. $card.find( '.update-now' )
  434. .text( wp.updates.l10n.updateFailedShort ).removeClass( 'updating-message' );
  435. if ( response.pluginName ) {
  436. $card.find( '.update-now' )
  437. .attr( 'aria-label', wp.updates.l10n.pluginUpdateFailedLabel.replace( '%s', response.pluginName ) );
  438. } else {
  439. $card.find( '.update-now' ).removeAttr( 'aria-label' );
  440. }
  441. $card.on( 'click', '.notice.is-dismissible .notice-dismiss', function() {
  442. // Use same delay as the total duration of the notice fadeTo + slideUp animation.
  443. setTimeout( function() {
  444. $card
  445. .removeClass( 'plugin-card-update-failed' )
  446. .find( '.column-name a' ).focus();
  447. $card.find( '.update-now' )
  448. .attr( 'aria-label', false )
  449. .text( wp.updates.l10n.updateNow );
  450. }, 200 );
  451. } );
  452. }
  453. wp.a11y.speak( errorMessage, 'assertive' );
  454. $document.trigger( 'wp-plugin-update-error', response );
  455. };
  456. /**
  457. * Sends an Ajax request to the server to install a plugin.
  458. *
  459. * @since 4.6.0
  460. *
  461. * @param {object} args Arguments.
  462. * @param {string} args.slug Plugin identifier in the WordPress.org Plugin repository.
  463. * @param {installPluginSuccess=} args.success Optional. Success callback. Default: wp.updates.installPluginSuccess
  464. * @param {installPluginError=} args.error Optional. Error callback. Default: wp.updates.installPluginError
  465. * @return {$.promise} A jQuery promise that represents the request,
  466. * decorated with an abort() method.
  467. */
  468. wp.updates.installPlugin = function( args ) {
  469. var $card = $( '.plugin-card-' + args.slug ),
  470. $message = $card.find( '.install-now' );
  471. args = _.extend( {
  472. success: wp.updates.installPluginSuccess,
  473. error: wp.updates.installPluginError
  474. }, args );
  475. if ( 'import' === pagenow ) {
  476. $message = $( '[data-slug="' + args.slug + '"]' );
  477. }
  478. if ( $message.html() !== wp.updates.l10n.installing ) {
  479. $message.data( 'originaltext', $message.html() );
  480. }
  481. $message
  482. .addClass( 'updating-message' )
  483. .attr( 'aria-label', wp.updates.l10n.pluginInstallingLabel.replace( '%s', $message.data( 'name' ) ) )
  484. .text( wp.updates.l10n.installing );
  485. wp.a11y.speak( wp.updates.l10n.installingMsg, 'polite' );
  486. // Remove previous error messages, if any.
  487. $card.removeClass( 'plugin-card-install-failed' ).find( '.notice.notice-error' ).remove();
  488. $document.trigger( 'wp-plugin-installing', args );
  489. return wp.updates.ajax( 'install-plugin', args );
  490. };
  491. /**
  492. * Updates the UI appropriately after a successful plugin install.
  493. *
  494. * @since 4.6.0
  495. *
  496. * @typedef {object} installPluginSuccess
  497. * @param {object} response Response from the server.
  498. * @param {string} response.slug Slug of the installed plugin.
  499. * @param {string} response.pluginName Name of the installed plugin.
  500. * @param {string} response.activateUrl URL to activate the just installed plugin.
  501. */
  502. wp.updates.installPluginSuccess = function( response ) {
  503. var $message = $( '.plugin-card-' + response.slug ).find( '.install-now' );
  504. $message
  505. .removeClass( 'updating-message' )
  506. .addClass( 'updated-message installed button-disabled' )
  507. .attr( 'aria-label', wp.updates.l10n.pluginInstalledLabel.replace( '%s', response.pluginName ) )
  508. .text( wp.updates.l10n.pluginInstalled );
  509. wp.a11y.speak( wp.updates.l10n.installedMsg, 'polite' );
  510. $document.trigger( 'wp-plugin-install-success', response );
  511. if ( response.activateUrl ) {
  512. setTimeout( function() {
  513. // Transform the 'Install' button into an 'Activate' button.
  514. $message.removeClass( 'install-now installed button-disabled updated-message' ).addClass( 'activate-now button-primary' )
  515. .attr( 'href', response.activateUrl )
  516. .attr( 'aria-label', wp.updates.l10n.activatePluginLabel.replace( '%s', response.pluginName ) )
  517. .text( wp.updates.l10n.activatePlugin );
  518. }, 1000 );
  519. }
  520. };
  521. /**
  522. * Updates the UI appropriately after a failed plugin install.
  523. *
  524. * @since 4.6.0
  525. *
  526. * @typedef {object} installPluginError
  527. * @param {object} response Response from the server.
  528. * @param {string} response.slug Slug of the plugin to be installed.
  529. * @param {string=} response.pluginName Optional. Name of the plugin to be installed.
  530. * @param {string} response.errorCode Error code for the error that occurred.
  531. * @param {string} response.errorMessage The error that occurred.
  532. */
  533. wp.updates.installPluginError = function( response ) {
  534. var $card = $( '.plugin-card-' + response.slug ),
  535. $button = $card.find( '.install-now' ),
  536. errorMessage;
  537. if ( ! wp.updates.isValidResponse( response, 'install' ) ) {
  538. return;
  539. }
  540. if ( wp.updates.maybeHandleCredentialError( response, 'install-plugin' ) ) {
  541. return;
  542. }
  543. errorMessage = wp.updates.l10n.installFailed.replace( '%s', response.errorMessage );
  544. $card
  545. .addClass( 'plugin-card-update-failed' )
  546. .append( '<div class="notice notice-error notice-alt is-dismissible"><p>' + errorMessage + '</p></div>' );
  547. $card.on( 'click', '.notice.is-dismissible .notice-dismiss', function() {
  548. // Use same delay as the total duration of the notice fadeTo + slideUp animation.
  549. setTimeout( function() {
  550. $card
  551. .removeClass( 'plugin-card-update-failed' )
  552. .find( '.column-name a' ).focus();
  553. }, 200 );
  554. } );
  555. $button
  556. .removeClass( 'updating-message' ).addClass( 'button-disabled' )
  557. .attr( 'aria-label', wp.updates.l10n.pluginInstallFailedLabel.replace( '%s', $button.data( 'name' ) ) )
  558. .text( wp.updates.l10n.installFailedShort );
  559. wp.a11y.speak( errorMessage, 'assertive' );
  560. $document.trigger( 'wp-plugin-install-error', response );
  561. };
  562. /**
  563. * Updates the UI appropriately after a successful importer install.
  564. *
  565. * @since 4.6.0
  566. *
  567. * @typedef {object} installImporterSuccess
  568. * @param {object} response Response from the server.
  569. * @param {string} response.slug Slug of the installed plugin.
  570. * @param {string} response.pluginName Name of the installed plugin.
  571. * @param {string} response.activateUrl URL to activate the just installed plugin.
  572. */
  573. wp.updates.installImporterSuccess = function( response ) {
  574. wp.updates.addAdminNotice( {
  575. id: 'install-success',
  576. className: 'notice-success is-dismissible',
  577. message: wp.updates.l10n.importerInstalledMsg.replace( '%s', response.activateUrl + '&from=import' )
  578. } );
  579. $( '[data-slug="' + response.slug + '"]' )
  580. .removeClass( 'install-now updating-message' )
  581. .addClass( 'activate-now' )
  582. .attr({
  583. 'href': response.activateUrl + '&from=import',
  584. 'aria-label': wp.updates.l10n.activateImporterLabel.replace( '%s', response.pluginName )
  585. })
  586. .text( wp.updates.l10n.activateImporter );
  587. wp.a11y.speak( wp.updates.l10n.installedMsg, 'polite' );
  588. $document.trigger( 'wp-importer-install-success', response );
  589. };
  590. /**
  591. * Updates the UI appropriately after a failed importer install.
  592. *
  593. * @since 4.6.0
  594. *
  595. * @typedef {object} installImporterError
  596. * @param {object} response Response from the server.
  597. * @param {string} response.slug Slug of the plugin to be installed.
  598. * @param {string=} response.pluginName Optional. Name of the plugin to be installed.
  599. * @param {string} response.errorCode Error code for the error that occurred.
  600. * @param {string} response.errorMessage The error that occurred.
  601. */
  602. wp.updates.installImporterError = function( response ) {
  603. var errorMessage = wp.updates.l10n.installFailed.replace( '%s', response.errorMessage ),
  604. $installLink = $( '[data-slug="' + response.slug + '"]' ),
  605. pluginName = $installLink.data( 'name' );
  606. if ( ! wp.updates.isValidResponse( response, 'install' ) ) {
  607. return;
  608. }
  609. if ( wp.updates.maybeHandleCredentialError( response, 'install-plugin' ) ) {
  610. return;
  611. }
  612. wp.updates.addAdminNotice( {
  613. id: response.errorCode,
  614. className: 'notice-error is-dismissible',
  615. message: errorMessage
  616. } );
  617. $installLink
  618. .removeClass( 'updating-message' )
  619. .text( wp.updates.l10n.installNow )
  620. .attr( 'aria-label', wp.updates.l10n.installNowLabel.replace( '%s', pluginName ) );
  621. wp.a11y.speak( errorMessage, 'assertive' );
  622. $document.trigger( 'wp-importer-install-error', response );
  623. };
  624. /**
  625. * Sends an Ajax request to the server to delete a plugin.
  626. *
  627. * @since 4.6.0
  628. *
  629. * @param {object} args Arguments.
  630. * @param {string} args.plugin Basename of the plugin to be deleted.
  631. * @param {string} args.slug Slug of the plugin to be deleted.
  632. * @param {deletePluginSuccess=} args.success Optional. Success callback. Default: wp.updates.deletePluginSuccess
  633. * @param {deletePluginError=} args.error Optional. Error callback. Default: wp.updates.deletePluginError
  634. * @return {$.promise} A jQuery promise that represents the request,
  635. * decorated with an abort() method.
  636. */
  637. wp.updates.deletePlugin = function( args ) {
  638. var $link = $( '[data-plugin="' + args.plugin + '"]' ).find( '.row-actions a.delete' );
  639. args = _.extend( {
  640. success: wp.updates.deletePluginSuccess,
  641. error: wp.updates.deletePluginError
  642. }, args );
  643. if ( $link.html() !== wp.updates.l10n.deleting ) {
  644. $link
  645. .data( 'originaltext', $link.html() )
  646. .text( wp.updates.l10n.deleting );
  647. }
  648. wp.a11y.speak( wp.updates.l10n.deleting, 'polite' );
  649. $document.trigger( 'wp-plugin-deleting', args );
  650. return wp.updates.ajax( 'delete-plugin', args );
  651. };
  652. /**
  653. * Updates the UI appropriately after a successful plugin deletion.
  654. *
  655. * @since 4.6.0
  656. *
  657. * @typedef {object} deletePluginSuccess
  658. * @param {object} response Response from the server.
  659. * @param {string} response.slug Slug of the plugin that was deleted.
  660. * @param {string} response.plugin Base name of the plugin that was deleted.
  661. * @param {string} response.pluginName Name of the plugin that was deleted.
  662. */
  663. wp.updates.deletePluginSuccess = function( response ) {
  664. // Removes the plugin and updates rows.
  665. $( '[data-plugin="' + response.plugin + '"]' ).css( { backgroundColor: '#faafaa' } ).fadeOut( 350, function() {
  666. var $form = $( '#bulk-action-form' ),
  667. $views = $( '.subsubsub' ),
  668. $pluginRow = $( this ),
  669. columnCount = $form.find( 'thead th:not(.hidden), thead td' ).length,
  670. pluginDeletedRow = wp.template( 'item-deleted-row' ),
  671. /** @type {object} plugins Base names of plugins in their different states. */
  672. plugins = settings.plugins;
  673. // Add a success message after deleting a plugin.
  674. if ( ! $pluginRow.hasClass( 'plugin-update-tr' ) ) {
  675. $pluginRow.after(
  676. pluginDeletedRow( {
  677. slug: response.slug,
  678. plugin: response.plugin,
  679. colspan: columnCount,
  680. name: response.pluginName
  681. } )
  682. );
  683. }
  684. $pluginRow.remove();
  685. // Remove plugin from update count.
  686. if ( -1 !== _.indexOf( plugins.upgrade, response.plugin ) ) {
  687. plugins.upgrade = _.without( plugins.upgrade, response.plugin );
  688. wp.updates.decrementCount( 'plugin' );
  689. }
  690. // Remove from views.
  691. if ( -1 !== _.indexOf( plugins.inactive, response.plugin ) ) {
  692. plugins.inactive = _.without( plugins.inactive, response.plugin );
  693. if ( plugins.inactive.length ) {
  694. $views.find( '.inactive .count' ).text( '(' + plugins.inactive.length + ')' );
  695. } else {
  696. $views.find( '.inactive' ).remove();
  697. }
  698. }
  699. if ( -1 !== _.indexOf( plugins.active, response.plugin ) ) {
  700. plugins.active = _.without( plugins.active, response.plugin );
  701. if ( plugins.active.length ) {
  702. $views.find( '.active .count' ).text( '(' + plugins.active.length + ')' );
  703. } else {
  704. $views.find( '.active' ).remove();
  705. }
  706. }
  707. if ( -1 !== _.indexOf( plugins.recently_activated, response.plugin ) ) {
  708. plugins.recently_activated = _.without( plugins.recently_activated, response.plugin );
  709. if ( plugins.recently_activated.length ) {
  710. $views.find( '.recently_activated .count' ).text( '(' + plugins.recently_activated.length + ')' );
  711. } else {
  712. $views.find( '.recently_activated' ).remove();
  713. }
  714. }
  715. plugins.all = _.without( plugins.all, response.plugin );
  716. if ( plugins.all.length ) {
  717. $views.find( '.all .count' ).text( '(' + plugins.all.length + ')' );
  718. } else {
  719. $form.find( '.tablenav' ).css( { visibility: 'hidden' } );
  720. $views.find( '.all' ).remove();
  721. if ( ! $form.find( 'tr.no-items' ).length ) {
  722. $form.find( '#the-list' ).append( '<tr class="no-items"><td class="colspanchange" colspan="' + columnCount + '">' + wp.updates.l10n.noPlugins + '</td></tr>' );
  723. }
  724. }
  725. } );
  726. wp.a11y.speak( wp.updates.l10n.pluginDeleted, 'polite' );
  727. $document.trigger( 'wp-plugin-delete-success', response );
  728. };
  729. /**
  730. * Updates the UI appropriately after a failed plugin deletion.
  731. *
  732. * @since 4.6.0
  733. *
  734. * @typedef {object} deletePluginError
  735. * @param {object} response Response from the server.
  736. * @param {string} response.slug Slug of the plugin to be deleted.
  737. * @param {string} response.plugin Base name of the plugin to be deleted
  738. * @param {string=} response.pluginName Optional. Name of the plugin to be deleted.
  739. * @param {string} response.errorCode Error code for the error that occurred.
  740. * @param {string} response.errorMessage The error that occurred.
  741. */
  742. wp.updates.deletePluginError = function( response ) {
  743. var $plugin, $pluginUpdateRow,
  744. pluginUpdateRow = wp.template( 'item-update-row' ),
  745. noticeContent = wp.updates.adminNotice( {
  746. className: 'update-message notice-error notice-alt',
  747. message: response.errorMessage
  748. } );
  749. if ( response.plugin ) {
  750. $plugin = $( 'tr.inactive[data-plugin="' + response.plugin + '"]' );
  751. $pluginUpdateRow = $plugin.siblings( '[data-plugin="' + response.plugin + '"]' );
  752. } else {
  753. $plugin = $( 'tr.inactive[data-slug="' + response.slug + '"]' );
  754. $pluginUpdateRow = $plugin.siblings( '[data-slug="' + response.slug + '"]' );
  755. }
  756. if ( ! wp.updates.isValidResponse( response, 'delete' ) ) {
  757. return;
  758. }
  759. if ( wp.updates.maybeHandleCredentialError( response, 'delete-plugin' ) ) {
  760. return;
  761. }
  762. // Add a plugin update row if it doesn't exist yet.
  763. if ( ! $pluginUpdateRow.length ) {
  764. $plugin.addClass( 'update' ).after(
  765. pluginUpdateRow( {
  766. slug: response.slug,
  767. plugin: response.plugin || response.slug,
  768. colspan: $( '#bulk-action-form' ).find( 'thead th:not(.hidden), thead td' ).length,
  769. content: noticeContent
  770. } )
  771. );
  772. } else {
  773. // Remove previous error messages, if any.
  774. $pluginUpdateRow.find( '.notice-error' ).remove();
  775. $pluginUpdateRow.find( '.plugin-update' ).append( noticeContent );
  776. }
  777. $document.trigger( 'wp-plugin-delete-error', response );
  778. };
  779. /**
  780. * Sends an Ajax request to the server to update a theme.
  781. *
  782. * @since 4.6.0
  783. *
  784. * @param {object} args Arguments.
  785. * @param {string} args.slug Theme stylesheet.
  786. * @param {updateThemeSuccess=} args.success Optional. Success callback. Default: wp.updates.updateThemeSuccess
  787. * @param {updateThemeError=} args.error Optional. Error callback. Default: wp.updates.updateThemeError
  788. * @return {$.promise} A jQuery promise that represents the request,
  789. * decorated with an abort() method.
  790. */
  791. wp.updates.updateTheme = function( args ) {
  792. var $notice;
  793. args = _.extend( {
  794. success: wp.updates.updateThemeSuccess,
  795. error: wp.updates.updateThemeError
  796. }, args );
  797. if ( 'themes-network' === pagenow ) {
  798. $notice = $( '[data-slug="' + args.slug + '"]' ).find( '.update-message' ).removeClass( 'notice-error' ).addClass( 'updating-message notice-warning' ).find( 'p' );
  799. } else {
  800. $notice = $( '#update-theme' ).closest( '.notice' ).removeClass( 'notice-large' );
  801. $notice.find( 'h3' ).remove();
  802. $notice = $notice.add( $( '[data-slug="' + args.slug + '"]' ).find( '.update-message' ) );
  803. $notice = $notice.addClass( 'updating-message' ).find( 'p' );
  804. }
  805. if ( $notice.html() !== wp.updates.l10n.updating ) {
  806. $notice.data( 'originaltext', $notice.html() );
  807. }
  808. wp.a11y.speak( wp.updates.l10n.updatingMsg, 'polite' );
  809. $notice.text( wp.updates.l10n.updating );
  810. $document.trigger( 'wp-theme-updating', args );
  811. return wp.updates.ajax( 'update-theme', args );
  812. };
  813. /**
  814. * Updates the UI appropriately after a successful theme update.
  815. *
  816. * @since 4.6.0
  817. *
  818. * @typedef {object} updateThemeSuccess
  819. * @param {object} response
  820. * @param {string} response.slug Slug of the theme to be updated.
  821. * @param {object} response.theme Updated theme.
  822. * @param {string} response.oldVersion Old version of the theme.
  823. * @param {string} response.newVersion New version of the theme.
  824. */
  825. wp.updates.updateThemeSuccess = function( response ) {
  826. var isModalOpen = $( 'body.modal-open' ).length,
  827. $theme = $( '[data-slug="' + response.slug + '"]' ),
  828. updatedMessage = {
  829. className: 'updated-message notice-success notice-alt',
  830. message: wp.updates.l10n.themeUpdated
  831. },
  832. $notice, newText;
  833. if ( 'themes-network' === pagenow ) {
  834. $notice = $theme.find( '.update-message' );
  835. // Update the version number in the row.
  836. newText = $theme.find( '.theme-version-author-uri' ).html().replace( response.oldVersion, response.newVersion );
  837. $theme.find( '.theme-version-author-uri' ).html( newText );
  838. } else {
  839. $notice = $( '.theme-info .notice' ).add( $theme.find( '.update-message' ) );
  840. // Focus on Customize button after updating.
  841. if ( isModalOpen ) {
  842. $( '.load-customize:visible' ).focus();
  843. } else {
  844. $theme.find( '.load-customize' ).focus();
  845. }
  846. }
  847. wp.updates.addAdminNotice( _.extend( { selector: $notice }, updatedMessage ) );
  848. wp.a11y.speak( wp.updates.l10n.updatedMsg, 'polite' );
  849. wp.updates.decrementCount( 'theme' );
  850. $document.trigger( 'wp-theme-update-success', response );
  851. // Show updated message after modal re-rendered.
  852. if ( isModalOpen ) {
  853. $( '.theme-info .theme-author' ).after( wp.updates.adminNotice( updatedMessage ) );
  854. }
  855. };
  856. /**
  857. * Updates the UI appropriately after a failed theme update.
  858. *
  859. * @since 4.6.0
  860. *
  861. * @typedef {object} updateThemeError
  862. * @param {object} response Response from the server.
  863. * @param {string} response.slug Slug of the theme to be updated.
  864. * @param {string} response.errorCode Error code for the error that occurred.
  865. * @param {string} response.errorMessage The error that occurred.
  866. */
  867. wp.updates.updateThemeError = function( response ) {
  868. var $theme = $( '[data-slug="' + response.slug + '"]' ),
  869. errorMessage = wp.updates.l10n.updateFailed.replace( '%s', response.errorMessage ),
  870. $notice;
  871. if ( ! wp.updates.isValidResponse( response, 'update' ) ) {
  872. return;
  873. }
  874. if ( wp.updates.maybeHandleCredentialError( response, 'update-theme' ) ) {
  875. return;
  876. }
  877. if ( 'themes-network' === pagenow ) {
  878. $notice = $theme.find( '.update-message ' );
  879. } else {
  880. $notice = $( '.theme-info .notice' ).add( $theme.find( '.notice' ) );
  881. $( 'body.modal-open' ).length ? $( '.load-customize:visible' ).focus() : $theme.find( '.load-customize' ).focus();
  882. }
  883. wp.updates.addAdminNotice( {
  884. selector: $notice,
  885. className: 'update-message notice-error notice-alt is-dismissible',
  886. message: errorMessage
  887. } );
  888. wp.a11y.speak( errorMessage, 'polite' );
  889. $document.trigger( 'wp-theme-update-error', response );
  890. };
  891. /**
  892. * Sends an Ajax request to the server to install a theme.
  893. *
  894. * @since 4.6.0
  895. *
  896. * @param {object} args
  897. * @param {string} args.slug Theme stylesheet.
  898. * @param {installThemeSuccess=} args.success Optional. Success callback. Default: wp.updates.installThemeSuccess
  899. * @param {installThemeError=} args.error Optional. Error callback. Default: wp.updates.installThemeError
  900. * @return {$.promise} A jQuery promise that represents the request,
  901. * decorated with an abort() method.
  902. */
  903. wp.updates.installTheme = function( args ) {
  904. var $message = $( '.theme-install[data-slug="' + args.slug + '"]' );
  905. args = _.extend( {
  906. success: wp.updates.installThemeSuccess,
  907. error: wp.updates.installThemeError
  908. }, args );
  909. $message.addClass( 'updating-message' );
  910. $message.parents( '.theme' ).addClass( 'focus' );
  911. if ( $message.html() !== wp.updates.l10n.installing ) {
  912. $message.data( 'originaltext', $message.html() );
  913. }
  914. $message
  915. .text( wp.updates.l10n.installing )
  916. .attr( 'aria-label', wp.updates.l10n.themeInstallingLabel.replace( '%s', $message.data( 'name' ) ) );
  917. wp.a11y.speak( wp.updates.l10n.installingMsg, 'polite' );
  918. // Remove previous error messages, if any.
  919. $( '.install-theme-info, [data-slug="' + args.slug + '"]' ).removeClass( 'theme-install-failed' ).find( '.notice.notice-error' ).remove();
  920. $document.trigger( 'wp-theme-installing', args );
  921. return wp.updates.ajax( 'install-theme', args );
  922. };
  923. /**
  924. * Updates the UI appropriately after a successful theme install.
  925. *
  926. * @since 4.6.0
  927. *
  928. * @typedef {object} installThemeSuccess
  929. * @param {object} response Response from the server.
  930. * @param {string} response.slug Slug of the theme to be installed.
  931. * @param {string} response.customizeUrl URL to the Customizer for the just installed theme.
  932. * @param {string} response.activateUrl URL to activate the just installed theme.
  933. */
  934. wp.updates.installThemeSuccess = function( response ) {
  935. var $card = $( '.wp-full-overlay-header, [data-slug=' + response.slug + ']' ),
  936. $message;
  937. $document.trigger( 'wp-theme-install-success', response );
  938. $message = $card.find( '.button-primary' )
  939. .removeClass( 'updating-message' )
  940. .addClass( 'updated-message disabled' )
  941. .attr( 'aria-label', wp.updates.l10n.themeInstalledLabel.replace( '%s', response.themeName ) )
  942. .text( wp.updates.l10n.themeInstalled );
  943. wp.a11y.speak( wp.updates.l10n.installedMsg, 'polite' );
  944. setTimeout( function() {
  945. if ( response.activateUrl ) {
  946. // Transform the 'Install' button into an 'Activate' button.
  947. $message
  948. .attr( 'href', response.activateUrl )
  949. .removeClass( 'theme-install updated-message disabled' )
  950. .addClass( 'activate' )
  951. .attr( 'aria-label', wp.updates.l10n.activateThemeLabel.replace( '%s', response.themeName ) )
  952. .text( wp.updates.l10n.activateTheme );
  953. }
  954. if ( response.customizeUrl ) {
  955. // Transform the 'Preview' button into a 'Live Preview' button.
  956. $message.siblings( '.preview' ).replaceWith( function () {
  957. return $( '<a>' )
  958. .attr( 'href', response.customizeUrl )
  959. .addClass( 'button load-customize' )
  960. .text( wp.updates.l10n.livePreview );
  961. } );
  962. }
  963. }, 1000 );
  964. };
  965. /**
  966. * Updates the UI appropriately after a failed theme install.
  967. *
  968. * @since 4.6.0
  969. *
  970. * @typedef {object} installThemeError
  971. * @param {object} response Response from the server.
  972. * @param {string} response.slug Slug of the theme to be installed.
  973. * @param {string} response.errorCode Error code for the error that occurred.
  974. * @param {string} response.errorMessage The error that occurred.
  975. */
  976. wp.updates.installThemeError = function( response ) {
  977. var $card, $button,
  978. errorMessage = wp.updates.l10n.installFailed.replace( '%s', response.errorMessage ),
  979. $message = wp.updates.adminNotice( {
  980. className: 'update-message notice-error notice-alt',
  981. message: errorMessage
  982. } );
  983. if ( ! wp.updates.isValidResponse( response, 'install' ) ) {
  984. return;
  985. }
  986. if ( wp.updates.maybeHandleCredentialError( response, 'install-theme' ) ) {
  987. return;
  988. }
  989. if ( $document.find( 'body' ).hasClass( 'full-overlay-active' ) ) {
  990. $button = $( '.theme-install[data-slug="' + response.slug + '"]' );
  991. $card = $( '.install-theme-info' ).prepend( $message );
  992. } else {
  993. $card = $( '[data-slug="' + response.slug + '"]' ).removeClass( 'focus' ).addClass( 'theme-install-failed' ).append( $message );
  994. $button = $card.find( '.theme-install' );
  995. }
  996. $button
  997. .removeClass( 'updating-message' )
  998. .attr( 'aria-label', wp.updates.l10n.themeInstallFailedLabel.replace( '%s', $button.data( 'name' ) ) )
  999. .text( wp.updates.l10n.installFailedShort );
  1000. wp.a11y.speak( errorMessage, 'assertive' );
  1001. $document.trigger( 'wp-theme-install-error', response );
  1002. };
  1003. /**
  1004. * Sends an Ajax request to the server to delete a theme.
  1005. *
  1006. * @since 4.6.0
  1007. *
  1008. * @param {object} args
  1009. * @param {string} args.slug Theme stylesheet.
  1010. * @param {deleteThemeSuccess=} args.success Optional. Success callback. Default: wp.updates.deleteThemeSuccess
  1011. * @param {deleteThemeError=} args.error Optional. Error callback. Default: wp.updates.deleteThemeError
  1012. * @return {$.promise} A jQuery promise that represents the request,
  1013. * decorated with an abort() method.
  1014. */
  1015. wp.updates.deleteTheme = function( args ) {
  1016. var $button;
  1017. if ( 'themes' === pagenow ) {
  1018. $button = $( '.theme-actions .delete-theme' );
  1019. } else if ( 'themes-network' === pagenow ) {
  1020. $button = $( '[data-slug="' + args.slug + '"]' ).find( '.row-actions a.delete' );
  1021. }
  1022. args = _.extend( {
  1023. success: wp.updates.deleteThemeSuccess,
  1024. error: wp.updates.deleteThemeError
  1025. }, args );
  1026. if ( $button && $button.html() !== wp.updates.l10n.deleting ) {
  1027. $button
  1028. .data( 'originaltext', $button.html() )
  1029. .text( wp.updates.l10n.deleting );
  1030. }
  1031. wp.a11y.speak( wp.updates.l10n.deleting, 'polite' );
  1032. // Remove previous error messages, if any.
  1033. $( '.theme-info .update-message' ).remove();
  1034. $document.trigger( 'wp-theme-deleting', args );
  1035. return wp.updates.ajax( 'delete-theme', args );
  1036. };
  1037. /**
  1038. * Updates the UI appropriately after a successful theme deletion.
  1039. *
  1040. * @since 4.6.0
  1041. *
  1042. * @typedef {object} deleteThemeSuccess
  1043. * @param {object} response Response from the server.
  1044. * @param {string} response.slug Slug of the theme that was deleted.
  1045. */
  1046. wp.updates.deleteThemeSuccess = function( response ) {
  1047. var $themeRows = $( '[data-slug="' + response.slug + '"]' );
  1048. if ( 'themes-network' === pagenow ) {
  1049. // Removes the theme and updates rows.
  1050. $themeRows.css( { backgroundColor: '#faafaa' } ).fadeOut( 350, function() {
  1051. var $views = $( '.subsubsub' ),
  1052. $themeRow = $( this ),
  1053. totals = settings.themes,
  1054. deletedRow = wp.template( 'item-deleted-row' );
  1055. if ( ! $themeRow.hasClass( 'plugin-update-tr' ) ) {
  1056. $themeRow.after(
  1057. deletedRow( {
  1058. slug: response.slug,
  1059. colspan: $( '#bulk-action-form' ).find( 'thead th:not(.hidden), thead td' ).length,
  1060. name: $themeRow.find( '.theme-title strong' ).text()
  1061. } )
  1062. );
  1063. }
  1064. $themeRow.remove();
  1065. // Remove theme from update count.
  1066. if ( $themeRow.hasClass( 'update' ) ) {
  1067. totals.upgrade--;
  1068. wp.updates.decrementCount( 'theme' );
  1069. }
  1070. // Remove from views.
  1071. if ( $themeRow.hasClass( 'inactive' ) ) {
  1072. totals.disabled--;
  1073. if ( totals.disabled ) {
  1074. $views.find( '.disabled .count' ).text( '(' + totals.disabled + ')' );
  1075. } else {
  1076. $views.find( '.disabled' ).remove();
  1077. }
  1078. }
  1079. // There is always at least one theme available.
  1080. $views.find( '.all .count' ).text( '(' + --totals.all + ')' );
  1081. } );
  1082. }
  1083. wp.a11y.speak( wp.updates.l10n.themeDeleted, 'polite' );
  1084. $document.trigger( 'wp-theme-delete-success', response );
  1085. };
  1086. /**
  1087. * Updates the UI appropriately after a failed theme deletion.
  1088. *
  1089. * @since 4.6.0
  1090. *
  1091. * @typedef {object} deleteThemeError
  1092. * @param {object} response Response from the server.
  1093. * @param {string} response.slug Slug of the theme to be deleted.
  1094. * @param {string} response.errorCode Error code for the error that occurred.
  1095. * @param {string} response.errorMessage The error that occurred.
  1096. */
  1097. wp.updates.deleteThemeError = function( response ) {
  1098. var $themeRow = $( 'tr.inactive[data-slug="' + response.slug + '"]' ),
  1099. $button = $( '.theme-actions .delete-theme' ),
  1100. updateRow = wp.template( 'item-update-row' ),
  1101. $updateRow = $themeRow.siblings( '#' + response.slug + '-update' ),
  1102. errorMessage = wp.updates.l10n.deleteFailed.replace( '%s', response.errorMessage ),
  1103. $message = wp.updates.adminNotice( {
  1104. className: 'update-message notice-error notice-alt',
  1105. message: errorMessage
  1106. } );
  1107. if ( wp.updates.maybeHandleCredentialError( response, 'delete-theme' ) ) {
  1108. return;
  1109. }
  1110. if ( 'themes-network' === pagenow ) {
  1111. if ( ! $updateRow.length ) {
  1112. $themeRow.addClass( 'update' ).after(
  1113. updateRow( {
  1114. slug: response.slug,
  1115. colspan: $( '#bulk-action-form' ).find( 'thead th:not(.hidden), thead td' ).length,
  1116. content: $message
  1117. } )
  1118. );
  1119. } else {
  1120. // Remove previous error messages, if any.
  1121. $updateRow.find( '.notice-error' ).remove();
  1122. $updateRow.find( '.plugin-update' ).append( $message );
  1123. }
  1124. } else {
  1125. $( '.theme-info .theme-description' ).before( $message );
  1126. }
  1127. $button.html( $button.data( 'originaltext' ) );
  1128. wp.a11y.speak( errorMessage, 'assertive' );
  1129. $document.trigger( 'wp-theme-delete-error', response );
  1130. };
  1131. /**
  1132. * Adds the appropriate callback based on the type of action and the current page.
  1133. *
  1134. * @since 4.6.0
  1135. * @private
  1136. *
  1137. * @param {object} data AJAX payload.
  1138. * @param {string} action The type of request to perform.
  1139. * @return {object} The AJAX payload with the appropriate callbacks.
  1140. */
  1141. wp.updates._addCallbacks = function( data, action ) {
  1142. if ( 'import' === pagenow && 'install-plugin' === action ) {
  1143. data.success = wp.updates.installImporterSuccess;
  1144. data.error = wp.updates.installImporterError;
  1145. }
  1146. return data;
  1147. };
  1148. /**
  1149. * Pulls available jobs from the queue and runs them.
  1150. *
  1151. * @since 4.2.0
  1152. * @since 4.6.0 Can handle multiple job types.
  1153. */
  1154. wp.updates.queueChecker = function() {
  1155. var job;
  1156. if ( wp.updates.ajaxLocked || ! wp.updates.queue.length ) {
  1157. return;
  1158. }
  1159. job = wp.updates.queue.shift();
  1160. // Handle a queue job.
  1161. switch ( job.action ) {
  1162. case 'install-plugin':
  1163. wp.updates.installPlugin( job.data );
  1164. break;
  1165. case 'update-plugin':
  1166. wp.updates.updatePlugin( job.data );
  1167. break;
  1168. case 'delete-plugin':
  1169. wp.updates.deletePlugin( job.data );
  1170. break;
  1171. case 'install-theme':
  1172. wp.updates.installTheme( job.data );
  1173. break;
  1174. case 'update-theme':
  1175. wp.updates.updateTheme( job.data );
  1176. break;
  1177. case 'delete-theme':
  1178. wp.updates.deleteTheme( job.data );
  1179. break;
  1180. default:
  1181. break;
  1182. }
  1183. };
  1184. /**
  1185. * Requests the users filesystem credentials if they aren't already known.
  1186. *
  1187. * @since 4.2.0
  1188. *
  1189. * @param {Event=} event Optional. Event interface.
  1190. */
  1191. wp.updates.requestFilesystemCredentials = function( event ) {
  1192. if ( false === wp.updates.filesystemCredentials.available ) {
  1193. /*
  1194. * After exiting the credentials request modal,
  1195. * return the focus to the element triggering the request.
  1196. */
  1197. if ( event && ! wp.updates.$elToReturnFocusToFromCredentialsModal ) {
  1198. wp.updates.$elToReturnFocusToFromCredentialsModal = $( event.target );
  1199. }
  1200. wp.updates.ajaxLocked = true;
  1201. wp.updates.requestForCredentialsModalOpen();
  1202. }
  1203. };
  1204. /**
  1205. * Requests the users filesystem credentials if needed and there is no lock.
  1206. *
  1207. * @since 4.6.0
  1208. *
  1209. * @param {Event=} event Optional. Event interface.
  1210. */
  1211. wp.updates.maybeRequestFilesystemCredentials = function( event ) {
  1212. if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.ajaxLocked ) {
  1213. wp.updates.requestFilesystemCredentials( event );
  1214. }
  1215. };
  1216. /**
  1217. * Keydown handler for the request for credentials modal.
  1218. *
  1219. * Closes the modal when the escape key is pressed and
  1220. * constrains keyboard navigation to inside the modal.
  1221. *
  1222. * @since 4.2.0
  1223. *
  1224. * @param {Event} event Event interface.
  1225. */
  1226. wp.updates.keydown = function( event ) {
  1227. if ( 27 === event.keyCode ) {
  1228. wp.updates.requestForCredentialsModalCancel();
  1229. } else if ( 9 === event.keyCode ) {
  1230. // #upgrade button must always be the last focus-able element in the dialog.
  1231. if ( 'upgrade' === event.target.id && ! event.shiftKey ) {
  1232. $( '#hostname' ).focus();
  1233. event.preventDefault();
  1234. } else if ( 'hostname' === event.target.id && event.shiftKey ) {
  1235. $( '#upgrade' ).focus();
  1236. event.preventDefault();
  1237. }
  1238. }
  1239. };
  1240. /**
  1241. * Opens the request for credentials modal.
  1242. *
  1243. * @since 4.2.0
  1244. */
  1245. wp.updates.requestForCredentialsModalOpen = function() {
  1246. var $modal = $( '#request-filesystem-credentials-dialog' );
  1247. $( 'body' ).addClass( 'modal-open' );
  1248. $modal.show();
  1249. $modal.find( 'input:enabled:first' ).focus();
  1250. $modal.on( 'keydown', wp.updates.keydown );
  1251. };
  1252. /**
  1253. * Closes the request for credentials modal.
  1254. *
  1255. * @since 4.2.0
  1256. */
  1257. wp.updates.requestForCredentialsModalClose = function() {
  1258. $( '#request-filesystem-credentials-dialog' ).hide();
  1259. $( 'body' ).removeClass( 'modal-open' );
  1260. if ( wp.updates.$elToReturnFocusToFromCredentialsModal ) {
  1261. wp.updates.$elToReturnFocusToFromCredentialsModal.focus();
  1262. }
  1263. };
  1264. /**
  1265. * Takes care of the steps that need to happen when the modal is canceled out.
  1266. *
  1267. * @since 4.2.0
  1268. * @since 4.6.0 Triggers an event for callbacks to listen to and add their actions.
  1269. */
  1270. wp.updates.requestForCredentialsModalCancel = function() {
  1271. // Not ajaxLocked and no queue means we already have cleared things up.
  1272. if ( ! wp.updates.ajaxLocked && ! wp.updates.queue.length ) {
  1273. return;
  1274. }
  1275. _.each( wp.updates.queue, function( job ) {
  1276. $document.trigger( 'credential-modal-cancel', job );
  1277. } );
  1278. // Remove the lock, and clear the queue.
  1279. wp.updates.ajaxLocked = false;
  1280. wp.updates.queue = [];
  1281. wp.updates.requestForCredentialsModalClose();
  1282. };
  1283. /**
  1284. * Displays an error message in the request for credentials form.
  1285. *
  1286. * @since 4.2.0
  1287. *
  1288. * @param {string} message Error message.
  1289. */
  1290. wp.updates.showErrorInCredentialsForm = function( message ) {
  1291. var $filesystemForm = $( '#request-filesystem-credentials-form' );
  1292. // Remove any existing error.
  1293. $filesystemForm.find( '.notice' ).remove();
  1294. $filesystemForm.find( '#request-filesystem-credentials-title' ).after( '<div class="notice notice-alt notice-error"><p>' + message + '</p></div>' );
  1295. };
  1296. /**
  1297. * Handles credential errors and runs events that need to happen in that case.
  1298. *
  1299. * @since 4.2.0
  1300. *
  1301. * @param {object} response Ajax response.
  1302. * @param {string} action The type of request to perform.
  1303. */
  1304. wp.updates.credentialError = function( response, action ) {
  1305. // Restore callbacks.
  1306. response = wp.updates._addCallbacks( response, action );
  1307. wp.updates.queue.unshift( {
  1308. action: action,
  1309. /*
  1310. * Not cool that we're depending on response for this data.
  1311. * This would feel more whole in a view all tied together.
  1312. */
  1313. data: response
  1314. } );
  1315. wp.updates.filesystemCredentials.available = false;
  1316. wp.updates.showErrorInCredentialsForm( response.errorMessage );
  1317. wp.updates.requestFilesystemCredentials();
  1318. };
  1319. /**
  1320. * Handles credentials errors if it could not connect to the filesystem.
  1321. *
  1322. * @since 4.6.0
  1323. *
  1324. * @typedef {object} maybeHandleCredentialError
  1325. * @param {object} response Response from the server.
  1326. * @param {string} response.errorCode Error code for the error that occurred.
  1327. * @param {string} response.errorMessage The error that occurred.
  1328. * @param {string} action The type of request to perform.
  1329. * @returns {boolean} Whether there is an error that needs to be handled or not.
  1330. */
  1331. wp.updates.maybeHandleCredentialError = function( response, action ) {
  1332. if ( wp.updates.shouldRequestFilesystemCredentials && response.errorCode && 'unable_to_connect_to_filesystem' === response.errorCode ) {
  1333. wp.updates.credentialError( response, action );
  1334. return true;
  1335. }
  1336. return false;
  1337. };
  1338. /**
  1339. * Validates an AJAX response to ensure it's a proper object.
  1340. *
  1341. * If the response deems to be invalid, an admin notice is being displayed.
  1342. *
  1343. * @param {(object|string)} response Response from the server.
  1344. * @param {function=} response.always Optional. Callback for when the Deferred is resolved or rejected.
  1345. * @param {string=} response.statusText Optional. Status message corresponding to the status code.
  1346. * @param {string=} response.responseText Optional. Request response as text.
  1347. * @param {string} action Type of action the response is referring to. Can be 'delete',
  1348. * 'update' or 'install'.
  1349. */
  1350. wp.updates.isValidResponse = function( response, action ) {
  1351. var error = wp.updates.l10n.unknownError,
  1352. errorMessage;
  1353. // Make sure the response is a valid data object and not a Promise object.
  1354. if ( _.isObject( response ) && ! _.isFunction( response.always ) ) {
  1355. return true;
  1356. }
  1357. if ( _.isString( response ) && '-1' === response ) {
  1358. error = wp.updates.l10n.nonceError;
  1359. } else if ( _.isString( response ) ) {
  1360. error = response;
  1361. } else if ( 'undefined' !== typeof response.readyState && 0 === response.readyState ) {
  1362. error = wp.updates.l10n.connectionError;
  1363. } else if ( _.isString( response.responseText ) && '' !== response.responseText ) {
  1364. error = response.responseText;
  1365. } else if ( _.isString( response.statusText ) ) {
  1366. error = response.statusText;
  1367. }
  1368. switch ( action ) {
  1369. case 'update':
  1370. errorMessage = wp.updates.l10n.updateFailed;
  1371. break;
  1372. case 'install':
  1373. errorMessage = wp.updates.l10n.installFailed;
  1374. break;
  1375. case 'delete':
  1376. errorMessage = wp.updates.l10n.deleteFailed;
  1377. break;
  1378. }
  1379. // Messages are escaped, remove HTML tags to make them more readable.
  1380. error = error.replace( /<[\/a-z][^<>]*>/gi, '' );
  1381. errorMessage = errorMessage.replace( '%s', error );
  1382. // Add admin notice.
  1383. wp.updates.addAdminNotice( {
  1384. id: 'unknown_error',
  1385. className: 'notice-error is-dismissible',
  1386. message: _.escape( errorMessage )
  1387. } );
  1388. // Remove the lock, and clear the queue.
  1389. wp.updates.ajaxLocked = false;
  1390. wp.updates.queue = [];
  1391. // Change buttons of all running updates.
  1392. $( '.button.updating-message' )
  1393. .removeClass( 'updating-message' )
  1394. .removeAttr( 'aria-label' )
  1395. .prop( 'disabled', true )
  1396. .text( wp.updates.l10n.updateFailedShort );
  1397. $( '.updating-message:not(.button):not(.thickbox)' )
  1398. .removeClass( 'updating-message notice-warning' )
  1399. .addClass( 'notice-error' )
  1400. .find( 'p' )
  1401. .removeAttr( 'aria-label' )
  1402. .text( errorMessage );
  1403. wp.a11y.speak( errorMessage, 'assertive' );
  1404. return false;
  1405. };
  1406. /**
  1407. * Potentially adds an AYS to a user attempting to leave the page.
  1408. *
  1409. * If an update is on-going and a user attempts to leave the page,
  1410. * opens an "Are you sure?" alert.
  1411. *
  1412. * @since 4.2.0
  1413. */
  1414. wp.updates.beforeunload = function() {
  1415. if ( wp.updates.ajaxLocked ) {
  1416. return wp.updates.l10n.beforeunload;
  1417. }
  1418. };
  1419. $( function() {
  1420. var $pluginFilter = $( '#plugin-filter' ),
  1421. $bulkActionForm = $( '#bulk-action-form' ),
  1422. $filesystemForm = $( '#request-filesystem-credentials-form' ),
  1423. $filesystemModal = $( '#request-filesystem-credentials-dialog' ),
  1424. $pluginSearch = $( '.plugins-php .wp-filter-search' ),
  1425. $pluginInstallSearch = $( '.plugin-install-php .wp-filter-search' );
  1426. settings = _.extend( settings, window._wpUpdatesItemCounts || {} );
  1427. if ( settings.totals ) {
  1428. wp.updates.refreshCount();
  1429. }
  1430. /*
  1431. * Whether a user needs to submit filesystem credentials.
  1432. *
  1433. * This is based on whether the form was output on the page server-side.
  1434. *
  1435. * @see {wp_print_request_filesystem_credentials_modal() in PHP}
  1436. */
  1437. wp.updates.shouldRequestFilesystemCredentials = $filesystemModal.length > 0;
  1438. /**
  1439. * File system credentials form submit noop-er / handler.
  1440. *
  1441. * @since 4.2.0
  1442. */
  1443. $filesystemModal.on( 'submit', 'form', function( event ) {
  1444. event.preventDefault();
  1445. // Persist the credentials input by the user for the duration of the page load.
  1446. wp.updates.filesystemCredentials.ftp.hostname = $( '#hostname' ).val();
  1447. wp.updates.filesystemCredentials.ftp.username = $( '#username' ).val();
  1448. wp.updates.filesystemCredentials.ftp.password = $( '#password' ).val();
  1449. wp.updates.filesystemCredentials.ftp.connectionType = $( 'input[name="connection_type"]:checked' ).val();
  1450. wp.updates.filesystemCredentials.ssh.publicKey = $( '#public_key' ).val();
  1451. wp.updates.filesystemCredentials.ssh.privateKey = $( '#private_key' ).val();
  1452. wp.updates.filesystemCredentials.fsNonce = $( '#_fs_nonce' ).val();
  1453. wp.updates.filesystemCredentials.available = true;
  1454. // Unlock and invoke the queue.
  1455. wp.updates.ajaxLocked = false;
  1456. wp.updates.queueChecker();
  1457. wp.updates.requestForCredentialsModalClose();
  1458. } );
  1459. /**
  1460. * Closes the request credentials modal when clicking the 'Cancel' button or outside of the modal.
  1461. *
  1462. * @since 4.2.0
  1463. */
  1464. $filesystemModal.on( 'click', '[data-js-action="close"], .notification-dialog-background', wp.updates.requestForCredentialsModalCancel );
  1465. /**
  1466. * Hide SSH fields when not selected.
  1467. *
  1468. * @since 4.2.0
  1469. */
  1470. $filesystemForm.on( 'change', 'input[name="connection_type"]', function() {
  1471. $( '#ssh-keys' ).toggleClass( 'hidden', ( 'ssh' !== $( this ).val() ) );
  1472. } ).change();
  1473. /**
  1474. * Handles events after the credential modal was closed.
  1475. *
  1476. * @since 4.6.0
  1477. *
  1478. * @param {Event} event Event interface.
  1479. * @param {string} job The install/update.delete request.
  1480. */
  1481. $document.on( 'credential-modal-cancel', function( event, job ) {
  1482. var $updatingMessage = $( '.updating-message' ),
  1483. $message, originalText;
  1484. if ( 'import' === pagenow ) {
  1485. $updatingMessage.removeClass( 'updating-message' );
  1486. } else if ( 'plugins' === pagenow || 'plugins-network' === pagenow ) {
  1487. if ( 'update-plugin' === job.action ) {
  1488. $message = $( 'tr[data-plugin="' + job.data.plugin + '"]' ).find( '.update-message' );
  1489. } else if ( 'delete-plugin' === job.action ) {
  1490. $message = $( '[data-plugin="' + job.data.plugin + '"]' ).find( '.row-actions a.delete' );
  1491. }
  1492. } else if ( 'themes' === pagenow || 'themes-network' === pagenow ) {
  1493. if ( 'update-theme' === job.action ) {
  1494. $message = $( '[data-slug="' + job.data.slug + '"]' ).find( '.update-message' );
  1495. } else if ( 'delete-theme' === job.action && 'themes-network' === pagenow ) {
  1496. $message = $( '[data-slug="' + job.data.slug + '"]' ).find( '.row-actions a.delete' );
  1497. } else if ( 'delete-theme' === job.action && 'themes' === pagenow ) {
  1498. $message = $( '.theme-actions .delete-theme' );
  1499. }
  1500. } else {
  1501. $message = $updatingMessage;
  1502. }
  1503. if ( $message && $message.hasClass( 'updating-message' ) ) {
  1504. originalText = $message.data( 'originaltext' );
  1505. if ( 'undefined' === typeof originalText ) {
  1506. originalText = $( '<p>' ).html( $message.find( 'p' ).data( 'originaltext' ) );
  1507. }
  1508. $message
  1509. .removeClass( 'updating-message' )
  1510. .html( originalText );
  1511. if ( 'plugin-install' === pagenow || 'plugin-install-network' === pagenow ) {
  1512. if ( 'update-plugin' === job.action ) {
  1513. $message.attr( 'aria-label', wp.updates.l10n.pluginUpdateNowLabel.replace( '%s', $message.data( 'name' ) ) );
  1514. } else if ( 'install-plugin' === job.action ) {
  1515. $message.attr( 'aria-label', wp.updates.l10n.pluginInstallNowLabel.replace( '%s', $message.data( 'name' ) ) );
  1516. }
  1517. }
  1518. }
  1519. wp.a11y.speak( wp.updates.l10n.updateCancel, 'polite' );
  1520. } );
  1521. /**
  1522. * Click handler for plugin updates in List Table view.
  1523. *
  1524. * @since 4.2.0
  1525. *
  1526. * @param {Event} event Event interface.
  1527. */
  1528. $bulkActionForm.on( 'click', '[data-plugin] .update-link', function( event ) {
  1529. var $message = $( event.target ),
  1530. $pluginRow = $message.parents( 'tr' );
  1531. event.preventDefault();
  1532. if ( $message.hasClass( 'updating-message' ) || $message.hasClass( 'button-disabled' ) ) {
  1533. return;
  1534. }
  1535. wp.updates.maybeRequestFilesystemCredentials( event );
  1536. // Return the user to the input box of the plugin's table row after closing the modal.
  1537. wp.updates.$elToReturnFocusToFromCredentialsModal = $pluginRow.find( '.check-column input' );
  1538. wp.updates.updatePlugin( {
  1539. plugin: $pluginRow.data( 'plugin' ),
  1540. slug: $pluginRow.data( 'slug' )
  1541. } );
  1542. } );
  1543. /**
  1544. * Click handler for plugin updates in plugin install view.
  1545. *
  1546. * @since 4.2.0
  1547. *
  1548. * @param {Event} event Event interface.
  1549. */
  1550. $pluginFilter.on( 'click', '.update-now', function( event ) {
  1551. var $button = $( event.target );
  1552. event.preventDefault();
  1553. if ( $button.hasClass( 'updating-message' ) || $button.hasClass( 'button-disabled' ) ) {
  1554. return;
  1555. }
  1556. wp.updates.maybeRequestFilesystemCredentials( event );
  1557. wp.updates.updatePlugin( {
  1558. plugin: $button.data( 'plugin' ),
  1559. slug: $button.data( 'slug' )
  1560. } );
  1561. } );
  1562. /**
  1563. * Click handler for plugin installs in plugin install view.
  1564. *
  1565. * @since 4.6.0
  1566. *
  1567. * @param {Event} event Event interface.
  1568. */
  1569. $pluginFilter.on( 'click', '.install-now', function( event ) {
  1570. var $button = $( event.target );
  1571. event.preventDefault();
  1572. if ( $button.hasClass( 'updating-message' ) || $button.hasClass( 'button-disabled' ) ) {
  1573. return;
  1574. }
  1575. if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.ajaxLocked ) {
  1576. wp.updates.requestFilesystemCredentials( event );
  1577. $document.on( 'credential-modal-cancel', function() {
  1578. var $message = $( '.install-now.updating-message' );
  1579. $message
  1580. .removeClass( 'updating-message' )
  1581. .text( wp.updates.l10n.installNow );
  1582. wp.a11y.speak( wp.updates.l10n.updateCancel, 'polite' );
  1583. } );
  1584. }
  1585. wp.updates.installPlugin( {
  1586. slug: $button.data( 'slug' )
  1587. } );
  1588. } );
  1589. /**
  1590. * Click handler for importer plugins installs in the Import screen.
  1591. *
  1592. * @since 4.6.0
  1593. *
  1594. * @param {Event} event Event interface.
  1595. */
  1596. $document.on( 'click', '.importer-item .install-now', function( event ) {
  1597. var $button = $( event.target ),
  1598. pluginName = $( this ).data( 'name' );
  1599. event.preventDefault();
  1600. if ( $button.hasClass( 'updating-message' ) ) {
  1601. return;
  1602. }
  1603. if ( wp.updates.shouldRequestFilesystemCredentials && ! wp.updates.ajaxLocked ) {
  1604. wp.updates.requestFilesystemCredentials( event );
  1605. $document.on( 'credential-modal-cancel', function() {
  1606. $button
  1607. .removeClass( 'updating-message' )
  1608. .text( wp.updates.l10n.installNow )
  1609. .attr( 'aria-label', wp.updates.l10n.installNowLabel.replace( '%s', pluginName ) );
  1610. wp.a11y.speak( wp.updates.l10n.updateCancel, 'polite' );
  1611. } );
  1612. }
  1613. wp.updates.installPlugin( {
  1614. slug: $button.data( 'slug' ),
  1615. pagenow: pagenow,
  1616. success: wp.updates.installImporterSuccess,
  1617. error: wp.updates.installImporterError
  1618. } );
  1619. } );
  1620. /**
  1621. * Click handler for plugin deletions.
  1622. *
  1623. * @since 4.6.0
  1624. *
  1625. * @param {Event} event Event interface.
  1626. */
  1627. $bulkActionForm.on( 'click', '[data-plugin] a.delete', function( event ) {
  1628. var $pluginRow = $( event.target ).parents( 'tr' );
  1629. event.preventDefault();
  1630. if ( ! window.confirm( wp.updates.l10n.aysDeleteUninstall.replace( '%s', $pluginRow.find( '.plugin-title strong' ).text() ) ) ) {
  1631. return;
  1632. }
  1633. wp.updates.maybeRequestFilesystemCredentials( event );
  1634. wp.updates.deletePlugin( {
  1635. plugin: $pluginRow.data( 'plugin' ),
  1636. slug: $pluginRow.data( 'slug' )
  1637. } );
  1638. } );
  1639. /**
  1640. * Click handler for theme updates.
  1641. *
  1642. * @since 4.6.0
  1643. *
  1644. * @param {Event} event Event interface.
  1645. */
  1646. $document.on( 'click', '.themes-php.network-admin .update-link', function( event ) {
  1647. var $message = $( event.target ),
  1648. $themeRow = $message.parents( 'tr' );
  1649. event.preventDefault();
  1650. if ( $message.hasClass( 'updating-message' ) || $message.hasClass( 'button-disabled' ) ) {
  1651. return;
  1652. }
  1653. wp.updates.maybeRequestFilesystemCredentials( event );
  1654. // Return the user to the input box of the theme's table row after closing the modal.
  1655. wp.updates.$elToReturnFocusToFromCredentialsModal = $themeRow.find( '.check-column input' );
  1656. wp.updates.updateTheme( {
  1657. slug: $themeRow.data( 'slug' )
  1658. } );
  1659. } );
  1660. /**
  1661. * Click handler for theme deletions.
  1662. *
  1663. * @since 4.6.0
  1664. *
  1665. * @param {Event} event Event interface.
  1666. */
  1667. $document.on( 'click', '.themes-php.network-admin a.delete', function( event ) {
  1668. var $themeRow = $( event.target ).parents( 'tr' );
  1669. event.preventDefault();
  1670. if ( ! window.confirm( wp.updates.l10n.aysDelete.replace( '%s', $themeRow.find( '.theme-title strong' ).text() ) ) ) {
  1671. return;
  1672. }
  1673. wp.updates.maybeRequestFilesystemCredentials( event );
  1674. wp.updates.deleteTheme( {
  1675. slug: $themeRow.data( 'slug' )
  1676. } );
  1677. } );
  1678. /**
  1679. * Bulk action handler for plugins and themes.
  1680. *
  1681. * Handles both deletions and updates.
  1682. *
  1683. * @since 4.6.0
  1684. *
  1685. * @param {Event} event Event interface.
  1686. */
  1687. $bulkActionForm.on( 'click', '[type="submit"]', function( event ) {
  1688. var bulkAction = $( event.target ).siblings( 'select' ).val(),
  1689. itemsSelected = $bulkActionForm.find( 'input[name="checked[]"]:checked' ),
  1690. success = 0,
  1691. error = 0,
  1692. errorMessages = [],
  1693. type, action;
  1694. // Determine which type of item we're dealing with.
  1695. switch ( pagenow ) {
  1696. case 'plugins':
  1697. case 'plugins-network':
  1698. type = 'plugin';
  1699. break;
  1700. case 'themes-network':
  1701. type = 'theme';
  1702. break;
  1703. default:
  1704. return;
  1705. }
  1706. // Bail if there were no items selected.
  1707. if ( ! itemsSelected.length ) {
  1708. event.preventDefault();
  1709. $( 'html, body' ).animate( { scrollTop: 0 } );
  1710. return wp.updates.addAdminNotice( {
  1711. id: 'no-items-selected',
  1712. className: 'notice-error is-dismissible',
  1713. message: wp.updates.l10n.noItemsSelected
  1714. } );
  1715. }
  1716. // Determine the type of request we're dealing with.
  1717. switch ( bulkAction ) {
  1718. case 'update-selected':
  1719. action = bulkAction.replace( 'selected', type );
  1720. break;
  1721. case 'delete-selected':
  1722. if ( ! window.confirm( 'plugin' === type ? wp.updates.l10n.aysBulkDelete : wp.updates.l10n.aysBulkDeleteThemes ) ) {
  1723. event.preventDefault();
  1724. return;
  1725. }
  1726. action = bulkAction.replace( 'selected', type );
  1727. break;
  1728. default:
  1729. return;
  1730. }
  1731. wp.updates.maybeRequestFilesystemCredentials( event );
  1732. event.preventDefault();
  1733. // Un-check the bulk checkboxes.
  1734. $bulkActionForm.find( '.manage-column [type="checkbox"]' ).prop( 'checked', false );
  1735. $document.trigger( 'wp-' + type + '-bulk-' + bulkAction, itemsSelected );
  1736. // Find all the checkboxes which have been checked.
  1737. itemsSelected.each( function( index, element ) {
  1738. var $checkbox = $( element ),
  1739. $itemRow = $checkbox.parents( 'tr' );
  1740. // Only add update-able items to the update queue.
  1741. if ( 'update-selected' === bulkAction && ( ! $itemRow.hasClass( 'update' ) || $itemRow.find( 'notice-error' ).length ) ) {
  1742. // Un-check the box.
  1743. $checkbox.prop( 'checked', false );
  1744. return;
  1745. }
  1746. // Add it to the queue.
  1747. wp.updates.queue.push( {
  1748. action: action,
  1749. data: {
  1750. plugin: $itemRow.data( 'plugin' ),
  1751. slug: $itemRow.data( 'slug' )
  1752. }
  1753. } );
  1754. } );
  1755. // Display bulk notification for updates of any kind.
  1756. $document.on( 'wp-plugin-update-success wp-plugin-update-error wp-theme-update-success wp-theme-update-error', function( event, response ) {
  1757. var $itemRow = $( '[data-slug="' + response.slug + '"]' ),
  1758. $bulkActionNotice, itemName;
  1759. if ( 'wp-' + response.update + '-update-success' === event.type ) {
  1760. success++;
  1761. } else {
  1762. itemName = response.pluginName ? response.pluginName : $itemRow.find( '.column-primary strong' ).text();
  1763. error++;
  1764. errorMessages.push( itemName + ': ' + response.errorMessage );
  1765. }
  1766. $itemRow.find( 'input[name="checked[]"]:checked' ).prop( 'checked', false );
  1767. wp.updates.adminNotice = wp.template( 'wp-bulk-updates-admin-notice' );
  1768. wp.updates.addAdminNotice( {
  1769. id: 'bulk-action-notice',
  1770. className: 'bulk-action-notice',
  1771. successes: success,
  1772. errors: error,
  1773. errorMessages: errorMessages,
  1774. type: response.update
  1775. } );
  1776. $bulkActionNotice = $( '#bulk-action-notice' ).on( 'click', 'button', function() {
  1777. // $( this ) is the clicked button, no need to get it again.
  1778. $( this )
  1779. .toggleClass( 'bulk-action-errors-collapsed' )
  1780. .attr( 'aria-expanded', ! $( this ).hasClass( 'bulk-action-errors-collapsed' ) );
  1781. // Show the errors list.
  1782. $bulkActionNotice.find( '.bulk-action-errors' ).toggleClass( 'hidden' );
  1783. } );
  1784. if ( error > 0 && ! wp.updates.queue.length ) {
  1785. $( 'html, body' ).animate( { scrollTop: 0 } );
  1786. }
  1787. } );
  1788. // Reset admin notice template after #bulk-action-notice was added.
  1789. $document.on( 'wp-updates-notice-added', function() {
  1790. wp.updates.adminNotice = wp.template( 'wp-updates-admin-notice' );
  1791. } );
  1792. // Check the queue, now that the event handlers have been added.
  1793. wp.updates.queueChecker();
  1794. } );
  1795. if ( $pluginInstallSearch.length ) {
  1796. $pluginInstallSearch.attr( 'aria-describedby', 'live-search-desc' );
  1797. }
  1798. /**
  1799. * Handles changes to the plugin search box on the new-plugin page,
  1800. * searching the repository dynamically.
  1801. *
  1802. * @since 4.6.0
  1803. */
  1804. $pluginInstallSearch.on( 'keyup input', _.debounce( function( event, eventtype ) {
  1805. var $searchTab = $( '.plugin-install-search' ), data, searchLocation;
  1806. data = {
  1807. _ajax_nonce: wp.updates.ajaxNonce,
  1808. s: event.target.value,
  1809. tab: 'search',
  1810. type: $( '#typeselector' ).val(),
  1811. pagenow: pagenow
  1812. };
  1813. searchLocation = location.href.split( '?' )[ 0 ] + '?' + $.param( _.omit( data, [ '_ajax_nonce', 'pagenow' ] ) );
  1814. // Clear on escape.
  1815. if ( 'keyup' === event.type && 27 === event.which ) {
  1816. event.target.value = '';
  1817. }
  1818. if ( wp.updates.searchTerm === data.s && 'typechange' !== eventtype ) {
  1819. return;
  1820. } else {
  1821. $pluginFilter.empty();
  1822. wp.updates.searchTerm = data.s;
  1823. }
  1824. if ( window.history && window.history.replaceState ) {
  1825. window.history.replaceState( null, '', searchLocation );
  1826. }
  1827. if ( ! $searchTab.length ) {
  1828. $searchTab = $( '<li class="plugin-install-search" />' )
  1829. .append( $( '<a />', {
  1830. 'class': 'current',
  1831. 'href': searchLocation,
  1832. 'text': wp.updates.l10n.searchResultsLabel
  1833. } ) );
  1834. $( '.wp-filter .filter-links .current' )
  1835. .removeClass( 'current' )
  1836. .parents( '.filter-links' )
  1837. .prepend( $searchTab );
  1838. $pluginFilter.prev( 'p' ).remove();
  1839. $( '.plugins-popular-tags-wrapper' ).remove();
  1840. }
  1841. if ( 'undefined' !== typeof wp.updates.searchRequest ) {
  1842. wp.updates.searchRequest.abort();
  1843. }
  1844. $( 'body' ).addClass( 'loading-content' );
  1845. wp.updates.searchRequest = wp.ajax.post( 'search-install-plugins', data ).done( function( response ) {
  1846. $( 'body' ).removeClass( 'loading-content' );
  1847. $pluginFilter.append( response.items );
  1848. delete wp.updates.searchRequest;
  1849. if ( 0 === response.count ) {
  1850. wp.a11y.speak( wp.updates.l10n.noPluginsFound );
  1851. } else {
  1852. wp.a11y.speak( wp.updates.l10n.pluginsFound.replace( '%d', response.count ) );
  1853. }
  1854. } );
  1855. }, 500 ) );
  1856. if ( $pluginSearch.length ) {
  1857. $pluginSearch.attr( 'aria-describedby', 'live-search-desc' );
  1858. }
  1859. /**
  1860. * Handles changes to the plugin search box on the Installed Plugins screen,
  1861. * searching the plugin list dynamically.
  1862. *
  1863. * @since 4.6.0
  1864. */
  1865. $pluginSearch.on( 'keyup input', _.debounce( function( event ) {
  1866. var data = {
  1867. _ajax_nonce: wp.updates.ajaxNonce,
  1868. s: event.target.value,
  1869. pagenow: pagenow,
  1870. plugin_status: 'all'
  1871. },
  1872. queryArgs;
  1873. // Clear on escape.
  1874. if ( 'keyup' === event.type && 27 === event.which ) {
  1875. event.target.value = '';
  1876. }
  1877. if ( wp.updates.searchTerm === data.s ) {
  1878. return;
  1879. } else {
  1880. wp.updates.searchTerm = data.s;
  1881. }
  1882. queryArgs = _.object( _.compact( _.map( location.search.slice( 1 ).split( '&' ), function( item ) {
  1883. if ( item ) return item.split( '=' );
  1884. } ) ) );
  1885. data.plugin_status = queryArgs.plugin_status || 'all';
  1886. if ( window.history && window.history.replaceState ) {
  1887. window.history.replaceState( null, '', location.href.split( '?' )[ 0 ] + '?s=' + data.s + '&plugin_status=' + data.plugin_status );
  1888. }
  1889. if ( 'undefined' !== typeof wp.updates.searchRequest ) {
  1890. wp.updates.searchRequest.abort();
  1891. }
  1892. $bulkActionForm.empty();
  1893. $( 'body' ).addClass( 'loading-content' );
  1894. $( '.subsubsub .current' ).removeClass( 'current' );
  1895. wp.updates.searchRequest = wp.ajax.post( 'search-plugins', data ).done( function( response ) {
  1896. // Can we just ditch this whole subtitle business?
  1897. var $subTitle = $( '<span />' ).addClass( 'subtitle' ).html( wp.updates.l10n.searchResults.replace( '%s', _.escape( data.s ) ) ),
  1898. $oldSubTitle = $( '.wrap .subtitle' );
  1899. if ( ! data.s.length ) {
  1900. $oldSubTitle.remove();
  1901. $( '.subsubsub .' + data.plugin_status + ' a' ).addClass( 'current' );
  1902. } else if ( $oldSubTitle.length ) {
  1903. $oldSubTitle.replaceWith( $subTitle );
  1904. } else {
  1905. $( '.wp-header-end' ).before( $subTitle );
  1906. }
  1907. $( 'body' ).removeClass( 'loading-content' );
  1908. $bulkActionForm.append( response.items );
  1909. delete wp.updates.searchRequest;
  1910. if ( 0 === response.count ) {
  1911. wp.a11y.speak( wp.updates.l10n.noPluginsFound );
  1912. } else {
  1913. wp.a11y.speak( wp.updates.l10n.pluginsFound.replace( '%d', response.count ) );
  1914. }
  1915. } );
  1916. }, 500 ) );
  1917. /**
  1918. * Trigger a search event when the search form gets submitted.
  1919. *
  1920. * @since 4.6.0
  1921. */
  1922. $document.on( 'submit', '.search-plugins', function( event ) {
  1923. event.preventDefault();
  1924. $( 'input.wp-filter-search' ).trigger( 'input' );
  1925. } );
  1926. /**
  1927. * Trigger a search event when the search type gets changed.
  1928. *
  1929. * @since 4.6.0
  1930. */
  1931. $( '#typeselector' ).on( 'change', function() {
  1932. var $search = $( 'input[name="s"]' );
  1933. if ( $search.val().length ) {
  1934. $search.trigger( 'input', 'typechange' );
  1935. }
  1936. } );
  1937. /**
  1938. * Click handler for updating a plugin from the details modal on `plugin-install.php`.
  1939. *
  1940. * @since 4.2.0
  1941. *
  1942. * @param {Event} event Event interface.
  1943. */
  1944. $( '#plugin_update_from_iframe' ).on( 'click', function( event ) {
  1945. var target = window.parent === window ? null : window.parent,
  1946. update;
  1947. $.support.postMessage = !! window.postMessage;
  1948. if ( false === $.support.postMessage || null === target || -1 !== window.parent.location.pathname.indexOf( 'update-core.php' ) ) {
  1949. return;
  1950. }
  1951. event.preventDefault();
  1952. update = {
  1953. action: 'update-plugin',
  1954. data: {
  1955. plugin: $( this ).data( 'plugin' ),
  1956. slug: $( this ).data( 'slug' )
  1957. }
  1958. };
  1959. target.postMessage( JSON.stringify( update ), window.location.origin );
  1960. } );
  1961. /**
  1962. * Click handler for installing a plugin from the details modal on `plugin-install.php`.
  1963. *
  1964. * @since 4.6.0
  1965. *
  1966. * @param {Event} event Event interface.
  1967. */
  1968. $( '#plugin_install_from_iframe' ).on( 'click', function( event ) {
  1969. var target = window.parent === window ? null : window.parent,
  1970. install;
  1971. $.support.postMessage = !! window.postMessage;
  1972. if ( false === $.support.postMessage || null === target || -1 !== window.parent.location.pathname.indexOf( 'index.php' ) ) {
  1973. return;
  1974. }
  1975. event.preventDefault();
  1976. install = {
  1977. action: 'install-plugin',
  1978. data: {
  1979. slug: $( this ).data( 'slug' )
  1980. }
  1981. };
  1982. target.postMessage( JSON.stringify( install ), window.location.origin );
  1983. } );
  1984. /**
  1985. * Handles postMessage events.
  1986. *
  1987. * @since 4.2.0
  1988. * @since 4.6.0 Switched `update-plugin` action to use the queue.
  1989. *
  1990. * @param {Event} event Event interface.
  1991. */
  1992. $( window ).on( 'message', function( event ) {
  1993. var originalEvent = event.originalEvent,
  1994. expectedOrigin = document.location.protocol + '//' + document.location.hostname,
  1995. message;
  1996. if ( originalEvent.origin !== expectedOrigin ) {
  1997. return;
  1998. }
  1999. try {
  2000. message = $.parseJSON( originalEvent.data );
  2001. } catch ( e ) {
  2002. return;
  2003. }
  2004. if ( 'undefined' === typeof message.action ) {
  2005. return;
  2006. }
  2007. switch ( message.action ) {
  2008. // Called from `wp-admin/includes/class-wp-upgrader-skins.php`.
  2009. case 'decrementUpdateCount':
  2010. /** @property {string} message.upgradeType */
  2011. wp.updates.decrementCount( message.upgradeType );
  2012. break;
  2013. case 'install-plugin':
  2014. case 'update-plugin':
  2015. /* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
  2016. window.tb_remove();
  2017. /* jscs:enable */
  2018. message.data = wp.updates._addCallbacks( message.data, message.action );
  2019. wp.updates.queue.push( message );
  2020. wp.updates.queueChecker();
  2021. break;
  2022. }
  2023. } );
  2024. /**
  2025. * Adds a callback to display a warning before leaving the page.
  2026. *
  2027. * @since 4.2.0
  2028. */
  2029. $( window ).on( 'beforeunload', wp.updates.beforeunload );
  2030. } );
  2031. })( jQuery, window.wp, window._wpUpdatesSettings );