-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5900 from ampproject/fix/5772-delayed-validation-…
…error-count-display Delay showing the unreviewed counts of validated URLs and errors; remove 'At a Glance' widget
- Loading branch information
Showing
17 changed files
with
558 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import domReady from '@wordpress/dom-ready'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.css'; | ||
|
||
/** | ||
* Updates a menu item with its count. | ||
* | ||
* If the count is not a number or is `0`, the element that contains the count is instead removed (as it would be no longer relevant). | ||
* | ||
* @param {HTMLElement} itemEl Menu item element. | ||
* @param {number} count Count to set. | ||
*/ | ||
function updateMenuItem( itemEl, count ) { | ||
if ( isNaN( count ) || count === 0 ) { | ||
itemEl.parentNode.parentNode.removeChild( itemEl.parentNode ); | ||
} else { | ||
itemEl.classList.remove( 'loading' ); | ||
itemEl.textContent = count.toLocaleString(); | ||
} | ||
} | ||
|
||
/** | ||
* Updates the 'Validated URLs' and 'Error Index' menu items with their respective unreviewed count. | ||
* | ||
* @param {Object} counts Counts for menu items. | ||
* @param {number} counts.validated_urls Unreviewed validated URLs count. | ||
* @param {number} counts.errors Unreviewed validation errors count. | ||
*/ | ||
function updateMenuItemCounts( counts ) { | ||
const { validated_urls: newValidatedUrlCount, errors: newErrorCount } = counts; | ||
|
||
const errorCountEl = document.getElementById( 'new-error-index-count' ); | ||
if ( errorCountEl ) { | ||
updateMenuItem( errorCountEl, newErrorCount ); | ||
} | ||
|
||
const validatedUrlsCountEl = document.getElementById( 'new-validation-url-count' ); | ||
if ( validatedUrlsCountEl ) { | ||
updateMenuItem( validatedUrlsCountEl, newValidatedUrlCount ); | ||
} | ||
} | ||
|
||
/** | ||
* Fetches the validation counts only when the AMP submenu is open for the first time. | ||
* | ||
* @param {HTMLElement} root AMP submenu item. | ||
*/ | ||
function createObserver( root ) { | ||
// IntersectionObserver is not available in IE11, so just hide the counts entirely for that browser. | ||
if ( ! ( 'IntersectionObserver' in window ) ) { | ||
updateMenuItemCounts( { validated_urls: 0, errors: 0 } ); | ||
return; | ||
} | ||
|
||
const target = root.querySelector( 'ul' ); | ||
|
||
const observer = new IntersectionObserver( ( [ entry ] ) => { | ||
if ( ! entry || ! entry.isIntersecting ) { | ||
return; | ||
} | ||
|
||
observer.unobserve( target ); | ||
|
||
apiFetch( { path: '/amp/v1/unreviewed-validation-counts' } ).then( ( counts ) => { | ||
updateMenuItemCounts( counts ); | ||
} ).catch( ( error ) => { | ||
updateMenuItemCounts( { validated_urls: 0, errors: 0 } ); | ||
|
||
const message = error?.message || __( 'An unknown error occurred while retrieving the validation counts', 'amp' ); | ||
// eslint-disable-next-line no-console | ||
console.error( `[AMP Plugin] ${ message }` ); | ||
} ); | ||
}, { root } ); | ||
|
||
observer.observe( target ); | ||
} | ||
|
||
domReady( () => { | ||
const ampMenuItem = document.getElementById( 'toplevel_page_amp-options' ); | ||
|
||
// Bail if the AMP submenu is not in the DOM. | ||
if ( ! ampMenuItem ) { | ||
return; | ||
} | ||
|
||
createObserver( ampMenuItem ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
@keyframes rotate-forever { | ||
|
||
0% { | ||
transform: rotate(0deg); | ||
} | ||
|
||
100% { | ||
transform: rotate(360deg); | ||
} | ||
} | ||
|
||
#toplevel_page_amp-options > ul > li span.loading { | ||
animation-duration: 0.75s; | ||
animation-iteration-count: infinite; | ||
animation-name: rotate-forever; | ||
animation-timing-function: linear; | ||
height: 4px; | ||
width: 4px; | ||
border: 2px solid #fff; | ||
border-right-color: transparent; | ||
border-top-color: transparent; | ||
border-radius: 50%; | ||
display: inline-block; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* Class ValidatedUrlCounts. | ||
* | ||
* @package AmpProject\AmpWP | ||
*/ | ||
|
||
namespace AmpProject\AmpWP\Admin; | ||
|
||
use AmpProject\AmpWP\Infrastructure\Conditional; | ||
use AmpProject\AmpWP\Infrastructure\Delayed; | ||
use AmpProject\AmpWP\Infrastructure\Registerable; | ||
use AmpProject\AmpWP\Infrastructure\Service; | ||
use AmpProject\AmpWP\Services; | ||
|
||
/** | ||
* Loads assets necessary to retrieve and show the unreviewed counts for validated URLs and validation errors in | ||
* the AMP admin menu. | ||
* | ||
* @since 2.1 | ||
* @internal | ||
*/ | ||
final class ValidationCounts implements Service, Registerable, Conditional, Delayed { | ||
|
||
/** | ||
* Assets handle. | ||
* | ||
* @var string | ||
*/ | ||
const ASSETS_HANDLE = 'amp-validation-counts'; | ||
|
||
/** | ||
* Get the action to use for registering the service. | ||
* | ||
* @return string Registration action to use. | ||
*/ | ||
public static function get_registration_action() { | ||
return 'admin_enqueue_scripts'; | ||
} | ||
|
||
/** | ||
* Check whether the conditional object is currently needed. | ||
* | ||
* @return bool Whether the conditional object is needed. | ||
*/ | ||
public static function is_needed() { | ||
$dev_tools_user_access = Services::get( 'dev_tools.user_access' ); | ||
return $dev_tools_user_access->is_user_enabled(); | ||
} | ||
|
||
/** | ||
* Runs on instantiation. | ||
*/ | ||
public function register() { | ||
$this->enqueue_scripts(); | ||
} | ||
|
||
/** | ||
* Enqueue admin assets. | ||
*/ | ||
public function enqueue_scripts() { | ||
$asset_file = AMP__DIR__ . '/assets/js/' . self::ASSETS_HANDLE . '.asset.php'; | ||
$asset = require $asset_file; | ||
$dependencies = $asset['dependencies']; | ||
$version = $asset['version']; | ||
|
||
wp_enqueue_script( | ||
self::ASSETS_HANDLE, | ||
amp_get_asset_url( 'js/' . self::ASSETS_HANDLE . '.js' ), | ||
$dependencies, | ||
$version, | ||
true | ||
); | ||
|
||
wp_enqueue_style( | ||
self::ASSETS_HANDLE, | ||
amp_get_asset_url( 'css/' . self::ASSETS_HANDLE . '.css' ), | ||
false, | ||
$version | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.