wp/wp-admin/js/site-health.js
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    13 		sprintf = wp.i18n.sprintf,
    13 		sprintf = wp.i18n.sprintf,
    14 		clipboard = new ClipboardJS( '.site-health-copy-buttons .copy-button' ),
    14 		clipboard = new ClipboardJS( '.site-health-copy-buttons .copy-button' ),
    15 		isStatusTab = $( '.health-check-body.health-check-status-tab' ).length,
    15 		isStatusTab = $( '.health-check-body.health-check-status-tab' ).length,
    16 		isDebugTab = $( '.health-check-body.health-check-debug-tab' ).length,
    16 		isDebugTab = $( '.health-check-body.health-check-debug-tab' ).length,
    17 		pathsSizesSection = $( '#health-check-accordion-block-wp-paths-sizes' ),
    17 		pathsSizesSection = $( '#health-check-accordion-block-wp-paths-sizes' ),
       
    18 		menuCounterWrapper = $( '#adminmenu .site-health-counter' ),
       
    19 		menuCounter = $( '#adminmenu .site-health-counter .count' ),
    18 		successTimeout;
    20 		successTimeout;
    19 
    21 
    20 	// Debug information copy section.
    22 	// Debug information copy section.
    21 	clipboard.on( 'success', function( e ) {
    23 	clipboard.on( 'success', function( e ) {
    22 		var triggerElement = $( e.trigger ),
    24 		var triggerElement = $( e.trigger ),
    23 			successElement = $( '.success', triggerElement.closest( 'div' ) );
    25 			successElement = $( '.success', triggerElement.closest( 'div' ) );
    24 
    26 
    25 		// Clear the selection and move focus back to the trigger.
    27 		// Clear the selection and move focus back to the trigger.
    26 		e.clearSelection();
    28 		e.clearSelection();
    27 		// Handle ClipboardJS focus bug, see https://github.com/zenorocha/clipboard.js/issues/680
       
    28 		triggerElement.trigger( 'focus' );
       
    29 
    29 
    30 		// Show success visual feedback.
    30 		// Show success visual feedback.
    31 		clearTimeout( successTimeout );
    31 		clearTimeout( successTimeout );
    32 		successElement.removeClass( 'hidden' );
    32 		successElement.removeClass( 'hidden' );
    33 
    33 
    34 		// Hide success visual feedback after 3 seconds since last success.
    34 		// Hide success visual feedback after 3 seconds since last success.
    35 		successTimeout = setTimeout( function() {
    35 		successTimeout = setTimeout( function() {
    36 			successElement.addClass( 'hidden' );
    36 			successElement.addClass( 'hidden' );
    37 			// Remove the visually hidden textarea so that it isn't perceived by assistive technologies.
       
    38 			if ( clipboard.clipboardAction.fakeElem && clipboard.clipboardAction.removeFake ) {
       
    39 				clipboard.clipboardAction.removeFake();
       
    40 			}
       
    41 		}, 3000 );
    37 		}, 3000 );
    42 
    38 
    43 		// Handle success audible feedback.
    39 		// Handle success audible feedback.
    44 		wp.a11y.speak( __( 'Site information has been copied to your clipboard.' ) );
    40 		wp.a11y.speak( __( 'Site information has been copied to your clipboard.' ) );
    45 	} );
    41 	} );
   166 
   162 
   167 		if ( heading ) {
   163 		if ( heading ) {
   168 			$( '.site-health-issue-count-title', issueWrapper ).html( heading );
   164 			$( '.site-health-issue-count-title', issueWrapper ).html( heading );
   169 		}
   165 		}
   170 
   166 
       
   167 		menuCounter.text( SiteHealth.site_status.issues.critical );
       
   168 
       
   169 		if ( 0 < parseInt( SiteHealth.site_status.issues.critical, 0 ) ) {
       
   170 			$( '#health-check-issues-critical' ).removeClass( 'hidden' );
       
   171 
       
   172 			menuCounterWrapper.removeClass( 'count-0' );
       
   173 		} else {
       
   174 			menuCounterWrapper.addClass( 'count-0' );
       
   175 		}
       
   176 		if ( 0 < parseInt( SiteHealth.site_status.issues.recommended, 0 ) ) {
       
   177 			$( '#health-check-issues-recommended' ).removeClass( 'hidden' );
       
   178 		}
       
   179 
   171 		$( '.issues', '#health-check-issues-' + issue.status ).append( template( issue ) );
   180 		$( '.issues', '#health-check-issues-' + issue.status ).append( template( issue ) );
   172 	}
   181 	}
   173 
   182 
   174 	/**
   183 	/**
   175 	 * Updates site health status indicator as asynchronous tests are run and returned.
   184 	 * Updates site health status indicator as asynchronous tests are run and returned.
   208 
   217 
   209 		pct = ( ( 100 - val ) / 100 ) * c + 'px';
   218 		pct = ( ( 100 - val ) / 100 ) * c + 'px';
   210 
   219 
   211 		$circle.css( { strokeDashoffset: pct } );
   220 		$circle.css( { strokeDashoffset: pct } );
   212 
   221 
   213 		if ( 1 > parseInt( SiteHealth.site_status.issues.critical, 0 ) ) {
       
   214 			$( '#health-check-issues-critical' ).addClass( 'hidden' );
       
   215 		}
       
   216 
       
   217 		if ( 1 > parseInt( SiteHealth.site_status.issues.recommended, 0 ) ) {
       
   218 			$( '#health-check-issues-recommended' ).addClass( 'hidden' );
       
   219 		}
       
   220 
       
   221 		if ( 80 <= val && 0 === parseInt( SiteHealth.site_status.issues.critical, 0 ) ) {
   222 		if ( 80 <= val && 0 === parseInt( SiteHealth.site_status.issues.critical, 0 ) ) {
   222 			$wrapper.addClass( 'green' ).removeClass( 'orange' );
   223 			$wrapper.addClass( 'green' ).removeClass( 'orange' );
   223 
   224 
   224 			$progressLabel.text( __( 'Good' ) );
   225 			$progressLabel.text( __( 'Good' ) );
   225 			wp.a11y.speak( __( 'All site health tests have finished running. Your site is looking good, and the results are now available on the page.' ) );
   226 			announceTestsProgression( 'good' );
   226 		} else {
   227 		} else {
   227 			$wrapper.addClass( 'orange' ).removeClass( 'green' );
   228 			$wrapper.addClass( 'orange' ).removeClass( 'green' );
   228 
   229 
   229 			$progressLabel.text( __( 'Should be improved' ) );
   230 			$progressLabel.text( __( 'Should be improved' ) );
   230 			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.' ) );
   231 			announceTestsProgression( 'improvable' );
   231 		}
   232 		}
   232 
   233 
   233 		if ( isStatusTab ) {
   234 		if ( isStatusTab ) {
   234 			$.post(
   235 			$.post(
   235 				ajaxurl,
   236 				ajaxurl,
   374 	function getDirectorySizes() {
   375 	function getDirectorySizes() {
   375 		var timestamp = ( new Date().getTime() );
   376 		var timestamp = ( new Date().getTime() );
   376 
   377 
   377 		// After 3 seconds announce that we're still waiting for directory sizes.
   378 		// After 3 seconds announce that we're still waiting for directory sizes.
   378 		var timeout = window.setTimeout( function() {
   379 		var timeout = window.setTimeout( function() {
   379 			wp.a11y.speak( __( 'Please wait...' ) );
   380 			announceTestsProgression( 'waiting-for-directory-sizes' );
   380 		}, 3000 );
   381 		}, 3000 );
   381 
   382 
   382 		wp.apiRequest( {
   383 		wp.apiRequest( {
   383 			path: '/wp-site-health/v1/directory-sizes'
   384 			path: '/wp-site-health/v1/directory-sizes'
   384 		} ).done( function( response ) {
   385 		} ).done( function( response ) {
   385 			updateDirSizes( response || {} );
   386 			updateDirSizes( response || {} );
   386 		} ).always( function() {
   387 		} ).always( function() {
   387 			var delay = ( new Date().getTime() ) - timestamp;
   388 			var delay = ( new Date().getTime() ) - timestamp;
   388 
   389 
   389 			$( '.health-check-wp-paths-sizes.spinner' ).css( 'visibility', 'hidden' );
   390 			$( '.health-check-wp-paths-sizes.spinner' ).css( 'visibility', 'hidden' );
   390 			recalculateProgression();
       
   391 
   391 
   392 			if ( delay > 3000 ) {
   392 			if ( delay > 3000 ) {
   393 				/*
   393 				/*
   394 				 * We have announced that we're waiting.
   394 				 * We have announced that we're waiting.
   395 				 * Announce that we're ready after giving at least 3 seconds
   395 				 * Announce that we're ready after giving at least 3 seconds
   400 				} else {
   400 				} else {
   401 					delay = 6500 - delay;
   401 					delay = 6500 - delay;
   402 				}
   402 				}
   403 
   403 
   404 				window.setTimeout( function() {
   404 				window.setTimeout( function() {
   405 					wp.a11y.speak( __( 'All site health tests have finished running.' ) );
   405 					recalculateProgression();
   406 				}, delay );
   406 				}, delay );
   407 			} else {
   407 			} else {
   408 				// Cancel the announcement.
   408 				// Cancel the announcement.
   409 				window.clearTimeout( timeout );
   409 				window.clearTimeout( timeout );
   410 			}
   410 			}
   447 
   447 
   448 	// Trigger a class toggle when the extended menu button is clicked.
   448 	// Trigger a class toggle when the extended menu button is clicked.
   449 	$( '.health-check-offscreen-nav-wrapper' ).on( 'click', function() {
   449 	$( '.health-check-offscreen-nav-wrapper' ).on( 'click', function() {
   450 		$( this ).toggleClass( 'visible' );
   450 		$( this ).toggleClass( 'visible' );
   451 	} );
   451 	} );
       
   452 
       
   453 	/**
       
   454 	 * Announces to assistive technologies the tests progression status.
       
   455 	 *
       
   456 	 * @since 6.4.0
       
   457 	 *
       
   458 	 * @param {string} type The type of message to be announced.
       
   459 	 *
       
   460 	 * @return {void}
       
   461 	 */
       
   462 	function announceTestsProgression( type ) {
       
   463 		// Only announce the messages in the Site Health pages.
       
   464 		if ( 'site-health' !== SiteHealth.screen ) {
       
   465 			return;
       
   466 		}
       
   467 
       
   468 		switch ( type ) {
       
   469 			case 'good':
       
   470 				wp.a11y.speak( __( 'All site health tests have finished running. Your site is looking good.' ) );
       
   471 				break;
       
   472 			case 'improvable':
       
   473 				wp.a11y.speak( __( 'All site health tests have finished running. There are items that should be addressed.' ) );
       
   474 				break;
       
   475 			case 'waiting-for-directory-sizes':
       
   476 				wp.a11y.speak( __( 'Running additional tests... please wait.' ) );
       
   477 				break;
       
   478 			default:
       
   479 				return;
       
   480 		}
       
   481 	}
   452 } );
   482 } );