| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 /** 2 * Interactions used by the Site Health modules in WordPress. 3 * 4 * @output wp-admin/js/site-health.js 5 */ 6 7 /* global ajaxurl, ClipboardJS, SiteHealth, wp */ 8 9 jQuery( function( $ ) { 10 11 var __ = wp.i18n.__, 12 _n = wp.i18n._n, 13 sprintf = wp.i18n.sprintf, 14 clipboard = new ClipboardJS( '.site-health-copy-buttons .copy-button' ), 15 isStatusTab = $( '.health-check-body.health-check-status-tab' ).length, 16 isDebugTab = $( '.health-check-body.health-check-debug-tab' ).length, 17 pathsSizesSection = $( '#health-check-accordion-block-wp-paths-sizes' ), 18 menuCounterWrapper = $( '#adminmenu .site-health-counter' ), 19 menuCounter = $( '#adminmenu .site-health-counter .count' ), 20 successTimeout; 21 22 // Debug information copy section. 23 clipboard.on( 'success', function( e ) { 24 var triggerElement = $( e.trigger ), 25 successElement = $( '.success', triggerElement.closest( 'div' ) ); 26 27 // Clear the selection and move focus back to the trigger. 28 e.clearSelection(); 29 30 // Show success visual feedback. 31 clearTimeout( successTimeout ); 32 successElement.removeClass( 'hidden' ); 33 34 // Hide success visual feedback after 3 seconds since last success. 35 successTimeout = setTimeout( function() { 36 successElement.addClass( 'hidden' ); 37 }, 3000 ); 38 39 // Handle success audible feedback. 40 wp.a11y.speak( __( 'Site information has been copied to your clipboard.' ) ); 41 } ); 42 43 // Accordion handling in various areas. 44 $( '.health-check-accordion' ).on( 'click', '.health-check-accordion-trigger', function() { 45 var isExpanded = ( 'true' === $( this ).attr( 'aria-expanded' ) ); 46 47 if ( $( this ).prop( 'id' ) ) { 48 window.location.hash = $( this ).prop( 'id' ); 49 } 50 51 if ( isExpanded ) { 52 $( this ).attr( 'aria-expanded', 'false' ); 53 $( '#' + $( this ).attr( 'aria-controls' ) ).attr( 'hidden', true ); 54 } else { 55 $( this ).attr( 'aria-expanded', 'true' ); 56 $( '#' + $( this ).attr( 'aria-controls' ) ).attr( 'hidden', false ); 57 } 58 } ); 59 60 /* global setTimeout */ 61 wp.domReady( function() { 62 // Get hash from query string and open the related accordion. 63 var hash = window.location.hash; 64 65 if ( hash ) { 66 var requestedPanel = $( hash ); 67 68 if ( requestedPanel.is( '.health-check-accordion-trigger' ) ) { 69 requestedPanel.trigger( 'click' ); 70 } 71 } 72 } ); 73 74 // Site Health test handling. 75 76 $( '.site-health-view-passed' ).on( 'click', function() { 77 var goodIssuesWrapper = $( '#health-check-issues-good' ); 78 79 goodIssuesWrapper.toggleClass( 'hidden' ); 80 $( this ).attr( 'aria-expanded', ! goodIssuesWrapper.hasClass( 'hidden' ) ); 81 } ); 82 83 /** 84 * Validates the Site Health test result format. 85 * 86 * @since 5.6.0 87 * 88 * @param {Object} issue 89 * 90 * @return {boolean} 91 */ 92 function validateIssueData( issue ) { 93 // Expected minimum format of a valid SiteHealth test response. 94 var minimumExpected = { 95 test: 'string', 96 label: 'string', 97 description: 'string' 98 }, 99 passed = true, 100 key, value, subKey, subValue; 101 102 // If the issue passed is not an object, return a `false` state early. 103 if ( 'object' !== typeof( issue ) ) { 104 return false; 105 } 106 107 // Loop over expected data and match the data types. 108 for ( key in minimumExpected ) { 109 value = minimumExpected[ key ]; 110 111 if ( 'object' === typeof( value ) ) { 112 for ( subKey in value ) { 113 subValue = value[ subKey ]; 114 115 if ( 'undefined' === typeof( issue[ key ] ) || 116 'undefined' === typeof( issue[ key ][ subKey ] ) || 117 subValue !== typeof( issue[ key ][ subKey ] ) 118 ) { 119 passed = false; 120 } 121 } 122 } else { 123 if ( 'undefined' === typeof( issue[ key ] ) || 124 value !== typeof( issue[ key ] ) 125 ) { 126 passed = false; 127 } 128 } 129 } 130 131 return passed; 132 } 133 134 /** 135 * Appends a new issue to the issue list. 136 * 137 * @since 5.2.0 138 * 139 * @param {Object} issue The issue data. 140 */ 141 function appendIssue( issue ) { 142 var template = wp.template( 'health-check-issue' ), 143 issueWrapper = $( '#health-check-issues-' + issue.status ), 144 heading, 145 count; 146 147 /* 148 * Validate the issue data format before using it. 149 * If the output is invalid, discard it. 150 */ 151 if ( ! validateIssueData( issue ) ) { 152 return false; 153 } 154 155 SiteHealth.site_status.issues[ issue.status ]++; 156 157 count = SiteHealth.site_status.issues[ issue.status ]; 158 159 // If no test name is supplied, append a placeholder for markup references. 160 if ( typeof issue.test === 'undefined' ) { 161 issue.test = issue.status + count; 162 } 163 164 if ( 'critical' === issue.status ) { 165 heading = sprintf( 166 _n( '%s critical issue', '%s critical issues', count ), 167 '<span class="issue-count">' + count + '</span>' 168 ); 169 } else if ( 'recommended' === issue.status ) { 170 heading = sprintf( 171 _n( '%s recommended improvement', '%s recommended improvements', count ), 172 '<span class="issue-count">' + count + '</span>' 173 ); 174 } else if ( 'good' === issue.status ) { 175 heading = sprintf( 176 _n( '%s item with no issues detected', '%s items with no issues detected', count ), 177 '<span class="issue-count">' + count + '</span>' 178 ); 179 } 180 181 if ( heading ) { 182 $( '.site-health-issue-count-title', issueWrapper ).html( heading ); 183 } 184 185 menuCounter.text( SiteHealth.site_status.issues.critical ); 186 187 if ( 0 < parseInt( SiteHealth.site_status.issues.critical, 0 ) ) { 188 $( '#health-check-issues-critical' ).removeClass( 'hidden' ); 189 190 menuCounterWrapper.removeClass( 'count-0' ); 191 } else { 192 menuCounterWrapper.addClass( 'count-0' ); 193 } 194 if ( 0 < parseInt( SiteHealth.site_status.issues.recommended, 0 ) ) { 195 $( '#health-check-issues-recommended' ).removeClass( 'hidden' ); 196 } 197 198 $( '.issues', '#health-check-issues-' + issue.status ).append( template( issue ) ); 199 } 200 201 /** 202 * Updates site health status indicator as asynchronous tests are run and returned. 203 * 204 * @since 5.2.0 205 */ 206 function recalculateProgression() { 207 var r, c, pct; 208 var $progress = $( '.site-health-progress' ); 209 var $wrapper = $progress.closest( '.site-health-progress-wrapper' ); 210 var $progressLabel = $( '.site-health-progress-label', $wrapper ); 211 var $circle = $( '.site-health-progress svg #bar' ); 212 var totalTests = parseInt( SiteHealth.site_status.issues.good, 0 ) + 213 parseInt( SiteHealth.site_status.issues.recommended, 0 ) + 214 ( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 ); 215 var failedTests = ( parseInt( SiteHealth.site_status.issues.recommended, 0 ) * 0.5 ) + 216 ( parseInt( SiteHealth.site_status.issues.critical, 0 ) * 1.5 ); 217 var val = 100 - Math.ceil( ( failedTests / totalTests ) * 100 ); 218 219 if ( 0 === totalTests ) { 220 $progress.addClass( 'hidden' ); 221 return; 222 } 223 224 $wrapper.removeClass( 'loading' ); 225 226 r = $circle.attr( 'r' ); 227 c = Math.PI * ( r * 2 ); 228 229 if ( 0 > val ) { 230 val = 0; 231 } 232 if ( 100 < val ) { 233 val = 100; 234 } 235 236 pct = ( ( 100 - val ) / 100 ) * c + 'px'; 237 238 $circle.css( { strokeDashoffset: pct } ); 239 240 if ( 80 <= val && 0 === parseInt( SiteHealth.site_status.issues.critical, 0 ) ) { 241 $wrapper.addClass( 'green' ).removeClass( 'orange' ); 242 243 $progressLabel.text( __( 'Good' ) ); 244 announceTestsProgression( 'good' ); 245 } else { 246 $wrapper.addClass( 'orange' ).removeClass( 'green' ); 247 248 $progressLabel.text( __( 'Should be improved' ) ); 249 announceTestsProgression( 'improvable' ); 250 } 251 252 if ( isStatusTab ) { 253 $.post( 254 ajaxurl, 255 { 256 'action': 'health-check-site-status-result', 257 '_wpnonce': SiteHealth.nonce.site_status_result, 258 'counts': SiteHealth.site_status.issues 259 } 260 ); 261 262 if ( 100 === val ) { 263 $( '.site-status-all-clear' ).removeClass( 'hide' ); 264 $( '.site-status-has-issues' ).addClass( 'hide' ); 265 } 266 } 267 } 268 269 /** 270 * Queues the next asynchronous test when we're ready to run it. 271 * 272 * @since 5.2.0 273 */ 274 function maybeRunNextAsyncTest() { 275 var doCalculation = true; 276 277 if ( 1 <= SiteHealth.site_status.async.length ) { 278 $.each( SiteHealth.site_status.async, function() { 279 var data = { 280 'action': 'health-check-' + this.test.replace( '_', '-' ), 281 '_wpnonce': SiteHealth.nonce.site_status 282 }; 283 284 if ( this.completed ) { 285 return true; 286 } 287 288 doCalculation = false; 289 290 this.completed = true; 291 292 if ( 'undefined' !== typeof( this.has_rest ) && this.has_rest ) { 293 wp.apiRequest( { 294 url: wp.url.addQueryArgs( this.test, { _locale: 'user' } ), 295 headers: this.headers 296 } ) 297 .done( function( response ) { 298 /** This filter is documented in wp-admin/includes/class-wp-site-health.php */ 299 appendIssue( wp.hooks.applyFilters( 'site_status_test_result', response ) ); 300 } ) 301 .fail( function( response ) { 302 var description; 303 304 if ( 'undefined' !== typeof( response.responseJSON ) && 'undefined' !== typeof( response.responseJSON.message ) ) { 305 description = response.responseJSON.message; 306 } else { 307 description = __( 'No details available' ); 308 } 309 310 addFailedSiteHealthCheckNotice( this.url, description ); 311 } ) 312 .always( function() { 313 maybeRunNextAsyncTest(); 314 } ); 315 } else { 316 $.post( 317 ajaxurl, 318 data 319 ).done( function( response ) { 320 /** This filter is documented in wp-admin/includes/class-wp-site-health.php */ 321 appendIssue( wp.hooks.applyFilters( 'site_status_test_result', response.data ) ); 322 } ).fail( function( response ) { 323 var description; 324 325 if ( 'undefined' !== typeof( response.responseJSON ) && 'undefined' !== typeof( response.responseJSON.message ) ) { 326 description = response.responseJSON.message; 327 } else { 328 description = __( 'No details available' ); 329 } 330 331 addFailedSiteHealthCheckNotice( this.url, description ); 332 } ).always( function() { 333 maybeRunNextAsyncTest(); 334 } ); 335 } 336 337 return false; 338 } ); 339 } 340 341 if ( doCalculation ) { 342 recalculateProgression(); 343 } 344 } 345 346 /** 347 * Add the details of a failed asynchronous test to the list of test results. 348 * 349 * @since 5.6.0 350 */ 351 function addFailedSiteHealthCheckNotice( url, description ) { 352 var issue; 353 354 issue = { 355 'status': 'recommended', 356 'label': __( 'A test is unavailable' ), 357 'badge': { 358 'color': 'red', 359 'label': __( 'Unavailable' ) 360 }, 361 'description': '<p>' + url + '</p><p>' + description + '</p>', 362 'actions': '' 363 }; 364 365 /** This filter is documented in wp-admin/includes/class-wp-site-health.php */ 366 appendIssue( wp.hooks.applyFilters( 'site_status_test_result', issue ) ); 367 } 368 369 if ( 'undefined' !== typeof SiteHealth ) { 370 if ( 0 === SiteHealth.site_status.direct.length && 0 === SiteHealth.site_status.async.length ) { 371 recalculateProgression(); 372 } else { 373 SiteHealth.site_status.issues = { 374 'good': 0, 375 'recommended': 0, 376 'critical': 0 377 }; 378 } 379 380 if ( 0 < SiteHealth.site_status.direct.length ) { 381 $.each( SiteHealth.site_status.direct, function() { 382 appendIssue( this ); 383 } ); 384 } 385 386 if ( 0 < SiteHealth.site_status.async.length ) { 387 maybeRunNextAsyncTest(); 388 } else { 389 recalculateProgression(); 390 } 391 } 392 393 function getDirectorySizes() { 394 var timestamp = ( new Date().getTime() ); 395 396 // After 3 seconds announce that we're still waiting for directory sizes. 397 var timeout = window.setTimeout( function() { 398 announceTestsProgression( 'waiting-for-directory-sizes' ); 399 }, 3000 ); 400 401 wp.apiRequest( { 402 path: '/wp-site-health/v1/directory-sizes' 403 } ).done( function( response ) { 404 updateDirSizes( response || {} ); 405 } ).always( function() { 406 var delay = ( new Date().getTime() ) - timestamp; 407 408 $( '.health-check-wp-paths-sizes.spinner' ).css( 'visibility', 'hidden' ); 409 410 if ( delay > 3000 ) { 411 /* 412 * We have announced that we're waiting. 413 * Announce that we're ready after giving at least 3 seconds 414 * for the first announcement to be read out, or the two may collide. 415 */ 416 if ( delay > 6000 ) { 417 delay = 0; 418 } else { 419 delay = 6500 - delay; 420 } 421 422 window.setTimeout( function() { 423 recalculateProgression(); 424 }, delay ); 425 } else { 426 // Cancel the announcement. 427 window.clearTimeout( timeout ); 428 } 429 430 $( document ).trigger( 'site-health-info-dirsizes-done' ); 431 } ); 432 } 433 434 function updateDirSizes( data ) { 435 var copyButton = $( 'button.button.copy-button' ); 436 var clipboardText = copyButton.attr( 'data-clipboard-text' ); 437 438 $.each( data, function( name, value ) { 439 var text = value.debug || value.size; 440 441 if ( typeof text !== 'undefined' ) { 442 clipboardText = clipboardText.replace( name + ': loading...', name + ': ' + text ); 443 } 444 } ); 445 446 copyButton.attr( 'data-clipboard-text', clipboardText ); 447 448 pathsSizesSection.find( 'td[class]' ).each( function( i, element ) { 449 var td = $( element ); 450 var name = td.attr( 'class' ); 451 452 if ( data.hasOwnProperty( name ) && data[ name ].size ) { 453 td.text( data[ name ].size ); 454 } 455 } ); 456 } 457 458 if ( isDebugTab ) { 459 if ( pathsSizesSection.length ) { 460 getDirectorySizes(); 461 } else { 462 recalculateProgression(); 463 } 464 } 465 466 // Trigger a class toggle when the extended menu button is clicked. 467 $( '.health-check-offscreen-nav-wrapper' ).on( 'click', function() { 468 $( this ).toggleClass( 'visible' ); 469 } ); 470 471 /** 472 * Announces to assistive technologies the tests progression status. 473 * 474 * @since 6.4.0 475 * 476 * @param {string} type The type of message to be announced. 477 * 478 * @return {void} 479 */ 480 function announceTestsProgression( type ) { 481 // Only announce the messages in the Site Health pages. 482 if ( 'site-health' !== SiteHealth.screen ) { 483 return; 484 } 485 486 switch ( type ) { 487 case 'good': 488 wp.a11y.speak( __( 'All site health tests have finished running. Your site is looking good.' ) ); 489 break; 490 case 'improvable': 491 wp.a11y.speak( __( 'All site health tests have finished running. There are items that should be addressed.' ) ); 492 break; 493 case 'waiting-for-directory-sizes': 494 wp.a11y.speak( __( 'Running additional tests... please wait.' ) ); 495 break; 496 default: 497 return; 498 } 499 } 500 } );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Apr 15 08:20:10 2026 | Cross-referenced by PHPXref |