PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-admin/js/site-health.js

https://gitlab.com/VTTE/sitios-vtte
JavaScript | 316 lines | 223 code | 59 blank | 34 comment | 41 complexity | 27504fbc4b0045d55cad6f3f2919d6b4 MD5 | raw file
  1. /**
  2. * Interactions used by the Site Health modules in WordPress.
  3. *
  4. * @output wp-admin/js/site-health.js
  5. */
  6. /* global ajaxurl, ClipboardJS, SiteHealth, wp */
  7. jQuery( document ).ready( function( $ ) {
  8. var __ = wp.i18n.__,
  9. _n = wp.i18n._n,
  10. sprintf = wp.i18n.sprintf;
  11. var data;
  12. var clipboard = new ClipboardJS( '.site-health-copy-buttons .copy-button' );
  13. var isDebugTab = $( '.health-check-body.health-check-debug-tab' ).length;
  14. var pathsSizesSection = $( '#health-check-accordion-block-wp-paths-sizes' );
  15. // Debug information copy section.
  16. clipboard.on( 'success', function( e ) {
  17. var $wrapper = $( e.trigger ).closest( 'div' );
  18. $( '.success', $wrapper ).addClass( 'visible' );
  19. wp.a11y.speak( __( 'Site information has been added to your clipboard.' ) );
  20. } );
  21. // Accordion handling in various areas.
  22. $( '.health-check-accordion' ).on( 'click', '.health-check-accordion-trigger', function() {
  23. var isExpanded = ( 'true' === $( this ).attr( 'aria-expanded' ) );
  24. if ( isExpanded ) {
  25. $( this ).attr( 'aria-expanded', 'false' );
  26. $( '#' + $( this ).attr( 'aria-controls' ) ).attr( 'hidden', true );
  27. } else {
  28. $( this ).attr( 'aria-expanded', 'true' );
  29. $( '#' + $( this ).attr( 'aria-controls' ) ).attr( 'hidden', false );
  30. }
  31. } );
  32. // Site Health test handling.
  33. $( '.site-health-view-passed' ).on( 'click', function() {
  34. var goodIssuesWrapper = $( '#health-check-issues-good' );
  35. goodIssuesWrapper.toggleClass( 'hidden' );
  36. $( this ).attr( 'aria-expanded', ! goodIssuesWrapper.hasClass( 'hidden' ) );
  37. } );
  38. /**
  39. * Append a new issue to the issue list.
  40. *
  41. * @since 5.2.0
  42. *
  43. * @param {Object} issue The issue data.
  44. */
  45. function AppendIssue( issue ) {
  46. var template = wp.template( 'health-check-issue' ),
  47. issueWrapper = $( '#health-check-issues-' + issue.status ),
  48. heading,
  49. count;
  50. SiteHealth.site_status.issues[ issue.status ]++;
  51. count = SiteHealth.site_status.issues[ issue.status ];
  52. if ( 'critical' === issue.status ) {
  53. heading = sprintf( _n( '%s critical issue', '%s critical issues', count ), '<span class="issue-count">' + count + '</span>' );
  54. } else if ( 'recommended' === issue.status ) {
  55. heading = sprintf( _n( '%s recommended improvement', '%s recommended improvements', count ), '<span class="issue-count">' + count + '</span>' );
  56. } else if ( 'good' === issue.status ) {
  57. heading = sprintf( _n( '%s item with no issues detected', '%s items with no issues detected', count ), '<span class="issue-count">' + count + '</span>' );
  58. }
  59. if ( heading ) {
  60. $( '.site-health-issue-count-title', issueWrapper ).html( heading );
  61. }
  62. $( '.issues', '#health-check-issues-' + issue.status ).append( template( issue ) );
  63. }
  64. /**
  65. * Update site health status indicator as asynchronous tests are run and returned.
  66. *
  67. * @since 5.2.0
  68. */
  69. function RecalculateProgression() {
  70. var r, c, pct;
  71. var $progress = $( '.site-health-progress' );
  72. var $wrapper = $progress.closest( '.site-health-progress-wrapper' );
  73. var $progressLabel = $( '.site-health-progress-label', $wrapper );
  74. var $circle = $( '.site-health-progress svg #bar' );
  75. var totalTests = parseInt( SiteHealth.site_status.issues.good, 0 ) + parseInt( SiteHealth.site_status.issues.recommended, 0 ) + ( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
  76. var failedTests = ( parseInt( SiteHealth.site_status.issues.recommended, 0 ) * 0.5 ) + ( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 );
  77. var val = 100 - Math.ceil( ( failedTests / totalTests ) * 100 );
  78. if ( 0 === totalTests ) {
  79. $progress.addClass( 'hidden' );
  80. return;
  81. }
  82. $wrapper.removeClass( 'loading' );
  83. r = $circle.attr( 'r' );
  84. c = Math.PI * ( r * 2 );
  85. if ( 0 > val ) {
  86. val = 0;
  87. }
  88. if ( 100 < val ) {
  89. val = 100;
  90. }
  91. pct = ( ( 100 - val ) / 100 ) * c;
  92. $circle.css( { strokeDashoffset: pct } );
  93. if ( 1 > parseInt( SiteHealth.site_status.issues.critical, 0 ) ) {
  94. $( '#health-check-issues-critical' ).addClass( 'hidden' );
  95. }
  96. if ( 1 > parseInt( SiteHealth.site_status.issues.recommended, 0 ) ) {
  97. $( '#health-check-issues-recommended' ).addClass( 'hidden' );
  98. }
  99. if ( 80 <= val && 0 === parseInt( SiteHealth.site_status.issues.critical, 0 ) ) {
  100. $wrapper.addClass( 'green' ).removeClass( 'orange' );
  101. $progressLabel.text( __( 'Good' ) );
  102. wp.a11y.speak( __( 'All site health tests have finished running. Your site is looking good, and the results are now available on the page.' ) );
  103. } else {
  104. $wrapper.addClass( 'orange' ).removeClass( 'green' );
  105. $progressLabel.text( __( 'Should be improved' ) );
  106. wp.a11y.speak( __( 'All site health tests have finished running. There are items that should be addressed, and the results are now available on the page.' ) );
  107. }
  108. if ( ! isDebugTab ) {
  109. $.post(
  110. ajaxurl,
  111. {
  112. 'action': 'health-check-site-status-result',
  113. '_wpnonce': SiteHealth.nonce.site_status_result,
  114. 'counts': SiteHealth.site_status.issues
  115. }
  116. );
  117. if ( 100 === val ) {
  118. $( '.site-status-all-clear' ).removeClass( 'hide' );
  119. $( '.site-status-has-issues' ).addClass( 'hide' );
  120. }
  121. }
  122. }
  123. /**
  124. * Queue the next asynchronous test when we're ready to run it.
  125. *
  126. * @since 5.2.0
  127. */
  128. function maybeRunNextAsyncTest() {
  129. var doCalculation = true;
  130. if ( 1 <= SiteHealth.site_status.async.length ) {
  131. $.each( SiteHealth.site_status.async, function() {
  132. var data = {
  133. 'action': 'health-check-' + this.test.replace( '_', '-' ),
  134. '_wpnonce': SiteHealth.nonce.site_status
  135. };
  136. if ( this.completed ) {
  137. return true;
  138. }
  139. doCalculation = false;
  140. this.completed = true;
  141. $.post(
  142. ajaxurl,
  143. data,
  144. function( response ) {
  145. /** This filter is documented in wp-admin/includes/class-wp-site-health.php */
  146. AppendIssue( wp.hooks.applyFilters( 'site_status_test_result', response.data ) );
  147. maybeRunNextAsyncTest();
  148. }
  149. );
  150. return false;
  151. } );
  152. }
  153. if ( doCalculation ) {
  154. RecalculateProgression();
  155. }
  156. }
  157. if ( 'undefined' !== typeof SiteHealth && ! isDebugTab ) {
  158. if ( 0 === SiteHealth.site_status.direct.length && 0 === SiteHealth.site_status.async.length ) {
  159. RecalculateProgression();
  160. } else {
  161. SiteHealth.site_status.issues = {
  162. 'good': 0,
  163. 'recommended': 0,
  164. 'critical': 0
  165. };
  166. }
  167. if ( 0 < SiteHealth.site_status.direct.length ) {
  168. $.each( SiteHealth.site_status.direct, function() {
  169. AppendIssue( this );
  170. } );
  171. }
  172. if ( 0 < SiteHealth.site_status.async.length ) {
  173. data = {
  174. 'action': 'health-check-' + SiteHealth.site_status.async[0].test.replace( '_', '-' ),
  175. '_wpnonce': SiteHealth.nonce.site_status
  176. };
  177. SiteHealth.site_status.async[0].completed = true;
  178. $.post(
  179. ajaxurl,
  180. data,
  181. function( response ) {
  182. AppendIssue( response.data );
  183. maybeRunNextAsyncTest();
  184. }
  185. );
  186. } else {
  187. RecalculateProgression();
  188. }
  189. }
  190. function getDirectorySizes() {
  191. var data = {
  192. action: 'health-check-get-sizes',
  193. _wpnonce: SiteHealth.nonce.site_status_result
  194. };
  195. var timestamp = ( new Date().getTime() );
  196. // After 3 seconds announce that we're still waiting for directory sizes.
  197. var timeout = window.setTimeout( function() {
  198. wp.a11y.speak( __( 'Please wait...' ) );
  199. }, 3000 );
  200. $.post( {
  201. type: 'POST',
  202. url: ajaxurl,
  203. data: data,
  204. dataType: 'json'
  205. } ).done( function( response ) {
  206. updateDirSizes( response.data || {} );
  207. } ).always( function() {
  208. var delay = ( new Date().getTime() ) - timestamp;
  209. $( '.health-check-wp-paths-sizes.spinner' ).css( 'visibility', 'hidden' );
  210. RecalculateProgression();
  211. if ( delay > 3000 ) {
  212. /*
  213. * We have announced that we're waiting.
  214. * Announce that we're ready after giving at least 3 seconds
  215. * for the first announcement to be read out, or the two may collide.
  216. */
  217. if ( delay > 6000 ) {
  218. delay = 0;
  219. } else {
  220. delay = 6500 - delay;
  221. }
  222. window.setTimeout( function() {
  223. wp.a11y.speak( __( 'All site health tests have finished running.' ) );
  224. }, delay );
  225. } else {
  226. // Cancel the announcement.
  227. window.clearTimeout( timeout );
  228. }
  229. $( document ).trigger( 'site-health-info-dirsizes-done' );
  230. } );
  231. }
  232. function updateDirSizes( data ) {
  233. var copyButton = $( 'button.button.copy-button' );
  234. var clipboardText = copyButton.attr( 'data-clipboard-text' );
  235. $.each( data, function( name, value ) {
  236. var text = value.debug || value.size;
  237. if ( typeof text !== 'undefined' ) {
  238. clipboardText = clipboardText.replace( name + ': loading...', name + ': ' + text );
  239. }
  240. } );
  241. copyButton.attr( 'data-clipboard-text', clipboardText );
  242. pathsSizesSection.find( 'td[class]' ).each( function( i, element ) {
  243. var td = $( element );
  244. var name = td.attr( 'class' );
  245. if ( data.hasOwnProperty( name ) && data[ name ].size ) {
  246. td.text( data[ name ].size );
  247. }
  248. } );
  249. }
  250. if ( isDebugTab ) {
  251. if ( pathsSizesSection.length ) {
  252. getDirectorySizes();
  253. } else {
  254. RecalculateProgression();
  255. }
  256. }
  257. } );