diff --git a/assets/css/plugin-check-admin.css b/assets/css/plugin-check-admin.css index 6d3d033be..a84968b11 100644 --- a/assets/css/plugin-check-admin.css +++ b/assets/css/plugin-check-admin.css @@ -1,75 +1,9 @@ -/* Responsive table */ -@media screen and (max-width: 782px) { - #plugin-check__submit { - margin-top: 1em; - } - - table#plugin-check__categories td { - padding-bottom: .5em; - } - - table.plugin-check__results-table { - border: 0; - } - - table.plugin-check__results-table caption { - font-size: 1.3em; - } - - table.plugin-check__results-table thead { - border: none; - clip: rect(0 0 0 0); - clip-path: inset(50%); - height: 1px; - margin: -1px; - overflow: hidden; - padding: 0; - position: absolute; - width: 1px; - white-space: nowrap; - } - - table.plugin-check__results-table tr { - border-bottom: 3px solid #ddd; - display: block; - } - - table.plugin-check__results-table td, - table.plugin-check__results-table code { - font-size: .9em; - } - - table.plugin-check__results-table td { - border-bottom: 1px solid #ddd; - display: block; - text-align: right; - } - - table.plugin-check__results-table td::before { - content: attr(data-label); - float: left; - font-weight: bold; - } - - .rtl table.plugin-check__results-table td { - text-align: left; - } - - .rtl table.plugin-check__results-table td::before { - float: right; - } - - table.plugin-check__results-table td:last-child { - border-bottom: 0; - } -} - /* AI Analysis Styles */ .plugin-check__ai-analysis { display: inline-flex; align-items: center; gap: 5px; - margin-top: 8px; + margin-top: 6px; padding: 6px 10px; border-radius: 3px; font-size: 0.9em; @@ -116,42 +50,6 @@ line-height: 1.5; } -.plugin-check__false-positives { - margin: 1.5em 0; - border: 1px solid #dcdcde; - background: #fff; -} - -.plugin-check__false-positives summary { - padding: 12px 14px; - font-weight: 600; - cursor: pointer; -} - -.plugin-check__false-positive-results { - padding: 0 14px 14px; -} - -#plugin-check__results .plugin-check__false-positive-results h4:first-child { - margin-top: 12px; -} - -.plugin-check__false-positive-results table.plugin-check__results-table { - margin-bottom: 1em; -} - -.plugin-check__options { - display: flex; -} - -.plugin-check__options #plugin-check__types-container { - margin-left: 40px; -} - -.plugin-check__options #plugin-check__ai-container { - margin-left: 40px; -} - /* JSON output formatting */ #plugin-check-namer-raw { font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace; @@ -224,16 +122,823 @@ font-size: 0.9em; } -/** Plugin Check Report Error/Warning Badge **/ -.plugin-check__results-row-type--error span, -.plugin-check__results-row-type--warning span { - background: rgb(235, 163, 163); - color: #570000; - padding: 4px 8px; +/* Run form */ +.plugin-check__run, +.plugin-check__summary, +.plugin-check__stat, +.plugin-check__panel, +.plugin-check__false-positives { + background: #fff; + border: 1px solid #c3c4c7; +} + +.plugin-check__run { + margin: 16px 0; +} + +.plugin-check__run-main { + display: flex; + flex-wrap: wrap; + gap: 8px; + align-items: center; + padding: 14px 16px; +} + +.plugin-check__run-main h2 { + flex-basis: 100%; + margin: 0; + font-size: 14px; +} + +.plugin-check__run-main p { + flex-basis: 100%; + margin: 0 0 4px; + color: #646970; +} + +#plugin-check__plugins-dropdown { + flex: 0 1 520px; + min-width: 0; +} + +#plugin-check__spinner { + float: none; + margin: 0; +} + +#plugin-check__client-error { + margin: 0 16px 14px; +} + +#plugin-check__client-error.is-hidden { + display: none; +} + +.plugin-check__options { + border-top: 1px solid #dcdcde; +} + +.plugin-check__options > summary { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 6px; + padding: 9px 16px; + list-style: none; + cursor: pointer; + color: #646970; +} + +.plugin-check__options > summary::-webkit-details-marker { + display: none; +} + +.plugin-check__options > summary:hover { + background: #f6f7f7; +} + +.plugin-check__options-label { + display: inline-flex; + align-items: center; + gap: 6px; + margin-inline-end: 4px; + font-weight: 600; + color: #1d2327; +} + +.plugin-check__options-edit { + margin-inline-start: auto; + color: #2271b1; +} + +.plugin-check__options-sep { + color: #c3c4c7; +} + +.plugin-check__chip { + display: inline-block; + padding: 1px 8px; + border-radius: 10px; + background: #f0f0f1; + color: #1d2327; + font-size: 12px; +} + +.plugin-check__chip--off { + background: none; + border: 1px dashed #c3c4c7; + color: #646970; +} + +.plugin-check__options-grid { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 16px; + padding: 4px 16px 16px; +} + +.plugin-check__options-grid fieldset { + margin: 0; + padding: 0; + border: 0; +} + +.plugin-check__options-grid legend { + margin-bottom: 6px; + padding: 0; + font-weight: 600; +} + +.plugin-check__options-grid label { + display: flex; + align-items: center; + gap: 6px; + padding: 3px 0; +} + +.plugin-check__options-grid label[hidden] { + display: none; +} + +.plugin-check__options-grid input[type="checkbox"] { + margin: 0; +} + +.plugin-check__chevron { + display: inline-block; + width: 0; + height: 0; + border-top: 4px solid transparent; + border-bottom: 4px solid transparent; + border-inline-start: 5px solid #646970; + transition: transform 0.15s; +} + +details[open] > summary .plugin-check__chevron { + transform: rotate(90deg); +} + +/* Report */ +.plugin-check__report[hidden] { + display: none; +} + +.plugin-check__report.is-running .plugin-check__summary { + border-inline-start-color: #2271b1; +} + +.plugin-check__summary { + display: grid; + grid-template-columns: minmax(0, 1fr) auto; + gap: 16px; + align-items: center; + padding: 16px 20px; + border-inline-start: 4px solid #00a32a; +} + +.plugin-check__summary.is-error { + border-inline-start-color: #d63638; +} + +.plugin-check__summary.is-warning { + border-inline-start-color: #dba617; +} + +.plugin-check__summary-title { + margin: 0; + font-size: 16px; +} + +.plugin-check__summary-meta { + margin: 4px 0 0; + color: #646970; +} + +.plugin-check__export-controls { + display: flex; + flex-wrap: wrap; + gap: 6px; +} + +.plugin-check__export-controls.is-hidden { + display: none; +} + +.plugin-check__stats { + display: grid; + grid-template-columns: repeat(4, minmax(0, 1fr)); + gap: 12px; + margin: 12px 0; +} + +.plugin-check__stat { + padding: 12px 16px; + font: inherit; + text-align: start; + color: inherit; +} + +button.plugin-check__stat { + cursor: pointer; +} + +button.plugin-check__stat:hover { + border-color: #8c8f94; +} + +.plugin-check__stat[aria-pressed="true"] { + outline: 2px solid #2271b1; + outline-offset: -2px; +} + +.plugin-check__stat-label { + display: flex; + align-items: center; + gap: 6px; + color: #646970; + font-size: 12px; + text-transform: uppercase; + letter-spacing: 0.04em; +} + +.plugin-check__stat-value { + display: block; + margin-top: 4px; + font-size: 28px; + font-weight: 600; + line-height: 1.2; + font-variant-numeric: tabular-nums; +} + +.plugin-check__stat--error .plugin-check__stat-value { + color: #d63638; +} + +.plugin-check__stat--warning .plugin-check__stat-value { + color: #996800; +} + +.plugin-check__dot { + display: inline-block; + flex-shrink: 0; + width: 8px; + height: 8px; + border-radius: 50%; +} + +.plugin-check__dot--error { + background: #d63638; +} + +.plugin-check__dot--warning { + background: #dba617; +} + +.plugin-check__layout { + display: grid; + grid-template-columns: 280px minmax(0, 1fr); + gap: 12px; + align-items: start; +} + +.plugin-check__panel { + min-width: 0; +} + +.plugin-check__panel-head { + display: flex; + justify-content: space-between; + align-items: center; + padding: 10px 14px; + border-bottom: 1px solid #dcdcde; + font-weight: 600; +} + +.plugin-check__panel-head small { + font-weight: 400; + color: #646970; +} + +.plugin-check__checks { + max-height: 520px; + margin: 0; + padding: 6px 0; + overflow: auto; + list-style: none; +} + +.plugin-check__checks li { + margin: 0; +} + +.plugin-check__check { + display: block; + width: 100%; + padding: 7px 14px; + border: 0; + background: none; + font: inherit; + text-align: start; + color: inherit; + cursor: pointer; +} + +.plugin-check__check:hover { + background: #f6f7f7; +} + +.plugin-check__check[aria-pressed="true"] { + background: #f0f6fc; + box-shadow: inset 3px 0 #2271b1; +} + +.plugin-check__check-row { + display: flex; + justify-content: space-between; + gap: 8px; +} + +.plugin-check__check-name { + display: flex; + align-items: center; + gap: 6px; + min-width: 0; + overflow: hidden; + font-size: 12px; + text-overflow: ellipsis; + white-space: nowrap; +} + +.plugin-check__count { + color: #646970; + font-variant-numeric: tabular-nums; +} + +.plugin-check__bar { + display: block; + height: 4px; + margin-top: 5px; + overflow: hidden; border-radius: 2px; + background: #f0f0f1; } -.plugin-check__results-row-type--warning span { - background: rgb(248, 221, 167); - color: rgb(87, 59, 0); -} \ No newline at end of file +.plugin-check__bar span { + display: block; + height: 100%; + background: #dba617; +} + +.plugin-check__bar--error span { + background: #d63638; +} + +.plugin-check__toolbar { + display: flex; + flex-wrap: wrap; + gap: 8px; + align-items: center; + padding: 10px 14px; + border-bottom: 1px solid #dcdcde; +} + +.plugin-check__toolbar label { + display: inline-flex; + align-items: center; + gap: 4px; + color: #646970; +} + +.plugin-check__filters { + padding-top: 8px; + padding-bottom: 8px; + background: #f6f7f7; +} + +.plugin-check__search { + flex: 1 1 220px; +} + +.plugin-check__segmented { + display: inline-flex; + overflow: hidden; + border: 1px solid #8c8f94; + border-radius: 4px; +} + +.plugin-check__segmented button { + min-height: 28px; + padding: 0 10px; + border: 0; + border-inline-end: 1px solid #8c8f94; + background: #fff; + font: inherit; + color: #1d2327; + cursor: pointer; +} + +.plugin-check__segmented button:last-child { + border-inline-end: 0; +} + +.plugin-check__segmented button[aria-pressed="true"] { + background: #2271b1; + color: #fff; +} + +.plugin-check__combo { + position: relative; + flex: 0 1 300px; + min-width: 200px; +} + +.plugin-check__combo-input { + width: 100%; + padding-inline-end: 52px !important; +} + +.plugin-check__combo-clear, +.plugin-check__combo-toggle { + position: absolute; + top: 1px; + bottom: 1px; + width: 24px; + padding: 0; + border: 0; + background: none; + color: #646970; + cursor: pointer; + font-size: 16px; + line-height: 1; +} + +.plugin-check__combo-toggle { + inset-inline-end: 2px; +} + +.plugin-check__combo-toggle .dashicons { + width: 16px; + height: 16px; + font-size: 16px; +} + +.plugin-check__combo-clear { + inset-inline-end: 24px; +} + +.plugin-check__combo-clear[hidden] { + display: none; +} + +.plugin-check__combo-list { + position: absolute; + z-index: 10; + top: calc(100% + 4px); + left: 0; + right: 0; + min-width: 300px; + max-height: 300px; + margin: 0; + padding: 4px 0; + overflow: auto; + list-style: none; + background: #fff; + border: 1px solid #c3c4c7; + border-radius: 4px; + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12); +} + +.plugin-check__combo-list[hidden] { + display: none; +} + +.plugin-check__combo-option { + display: grid; + grid-template-columns: 12px minmax(0, 1fr) auto; + gap: 6px; + align-items: center; + margin: 0; + padding: 6px 10px; + cursor: pointer; +} + +.plugin-check__combo-option[aria-selected="true"] { + font-weight: 600; +} + +.plugin-check__combo-option.is-active { + background: #2271b1; + color: #fff; +} + +.plugin-check__combo-option.is-active .plugin-check__combo-sub, +.plugin-check__combo-option.is-active .plugin-check__count { + color: #d6e6f5; +} + +.plugin-check__combo-name { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.plugin-check__combo-sub { + display: block; + overflow: hidden; + font-family: Consolas, Monaco, monospace; + font-size: 11px; + font-weight: 400; + text-overflow: ellipsis; + white-space: nowrap; + color: #646970; +} + +.plugin-check__combo-option mark { + padding: 0; + background: #fcf9e8; + color: inherit; +} + +.plugin-check__combo-option.is-active mark { + background: rgba(255, 255, 255, 0.25); +} + +.plugin-check__combo-empty { + margin: 0; + padding: 8px 10px; + color: #646970; +} + +.plugin-check__active-filter { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 6px; + padding: 6px 14px; + border-bottom: 1px solid #dcdcde; + background: #f0f6fc; + font-size: 12px; +} + +.plugin-check__active-filter[hidden] { + display: none; +} + +.plugin-check__link-button { + padding: 0; + border: 0; + background: none; + font: inherit; + color: #2271b1; + text-decoration: underline; + cursor: pointer; +} + +.plugin-check__group { + border-bottom: 1px solid #dcdcde; +} + +.plugin-check__group:last-child { + border-bottom: 0; +} + +.plugin-check__group > summary { + display: flex; + align-items: center; + gap: 10px; + padding: 10px 14px; + list-style: none; + cursor: pointer; +} + +.plugin-check__group > summary::-webkit-details-marker { + display: none; +} + +.plugin-check__group > summary:hover { + background: #f6f7f7; +} + +.plugin-check__path { + flex: 1; + min-width: 0; + overflow: hidden; + font-family: Consolas, Monaco, monospace; + text-overflow: ellipsis; + white-space: nowrap; +} + +.plugin-check__path-dir { + color: #646970; +} + +.plugin-check__path-base { + font-weight: 600; +} + +.plugin-check__badges { + display: flex; + flex-shrink: 0; + justify-content: flex-end; + gap: 4px; + min-width: 180px; +} + +.plugin-check__badge { + display: inline-flex; + padding: 1px 8px; + border-radius: 10px; + font-size: 11px; + font-weight: 600; + font-variant-numeric: tabular-nums; +} + +.plugin-check__badge--error { + background: #fcf0f1; + color: #d63638; +} + +.plugin-check__badge--warning { + background: #fcf9e8; + color: #996800; +} + +.plugin-check__tag { + padding: 1px 6px; + border-radius: 3px; + background: #f0f0f1; + font-size: 11px; + color: #646970; +} + +.plugin-check__issues { + margin: 0; + padding: 0 0 6px; + list-style: none; +} + +.plugin-check__issue { + display: grid; + grid-template-columns: 72px 96px minmax(0, 1fr) auto; + gap: 12px; + align-items: start; + margin: 0; + padding: 8px 14px; + padding-inline-start: 32px; + border-top: 1px solid #f0f0f1; +} + +.plugin-check__issue:hover { + background: #fafafa; +} + +.plugin-check__issue .plugin-check__path { + margin-bottom: 2px; + white-space: normal; + word-break: break-all; +} + +.plugin-check__type { + padding: 1px 6px; + border-radius: 3px; + font-size: 11px; + font-weight: 600; + text-align: center; +} + +.plugin-check__type--error { + background: #d63638; + color: #fff; +} + +.plugin-check__type--warning { + background: #dba617; + color: #1d2327; +} + +.plugin-check__loc { + font-family: Consolas, Monaco, monospace; + font-size: 12px; + color: #646970; +} + +.plugin-check__message { + margin: 0; + overflow-wrap: anywhere; +} + +.plugin-check__code { + max-width: 100%; + margin-top: 4px; + padding: 0 4px; + border: 1px solid #dcdcde; + border-radius: 3px; + background: #f6f7f7; + font-family: Consolas, Monaco, monospace; + font-size: 11px; + text-align: start; + color: #646970; + word-break: break-all; + cursor: pointer; +} + +.plugin-check__code:hover { + border-color: #c3c4c7; +} + +.plugin-check__links { + display: flex; + flex-direction: column; + align-items: flex-end; + gap: 2px; + min-width: 150px; + font-size: 12px; + white-space: nowrap; +} + +.plugin-check__links .dashicons { + width: 14px; + height: 14px; + font-size: 14px; + vertical-align: text-bottom; +} + +.plugin-check__more { + padding: 6px 14px 10px; + padding-inline-start: 32px; +} + +.plugin-check__empty { + margin: 0; + padding: 40px 14px; + text-align: center; + color: #646970; +} + +.plugin-check__false-positives { + margin: 12px 0; +} + +.plugin-check__false-positives > summary { + padding: 12px 14px; + font-weight: 600; + cursor: pointer; +} + +.plugin-check__false-positive-results { + border-top: 1px solid #dcdcde; +} + +@media screen and (max-width: 960px) { + .plugin-check__layout { + grid-template-columns: minmax(0, 1fr); + } + + .plugin-check__checks { + max-height: 240px; + } + + .plugin-check__stats, + .plugin-check__options-grid { + grid-template-columns: repeat(2, minmax(0, 1fr)); + } + + .plugin-check__summary { + grid-template-columns: minmax(0, 1fr); + } + + .plugin-check__issue { + grid-template-columns: auto minmax(0, 1fr); + padding-inline-start: 14px; + } + + .plugin-check__issue-body, + .plugin-check__links { + grid-column: 1 / -1; + } + + .plugin-check__links { + flex-direction: row; + gap: 12px; + min-width: 0; + } + + .plugin-check__group > summary { + flex-wrap: wrap; + } + + .plugin-check__group > summary .plugin-check__path { + flex: 1 1 calc(100% - 30px); + white-space: normal; + word-break: break-all; + } + + .plugin-check__badges { + min-width: 0; + margin-inline-start: 15px; + } + + .plugin-check__tag { + display: none; + } +} diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 261e8c98a..45751e1c1 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -13,13 +13,14 @@ 'input[name=categories]' ); const typesList = document.querySelectorAll( 'input[name=types]' ); - const templates = {}; + const report = document.getElementById( 'plugin-check__report' ); // Return early if the elements cannot be found on the page. if ( ! checkItButton || ! pluginsList || ! resultsContainer || + ! report || ! exportContainer || ! spinner || ! clientError || @@ -30,6 +31,21 @@ return; } + const summary = document.getElementById( 'plugin-check__summary' ); + const summaryTitle = document.getElementById( + 'plugin-check__summary-title' + ); + const summaryMeta = document.getElementById( 'plugin-check__summary-meta' ); + const falsePositivesContainer = document.getElementById( + 'plugin-check__false-positives-container' + ); + const optionsDetails = document.getElementById( 'plugin-check__options' ); + const optionsSummary = document.getElementById( + 'plugin-check__options-summary' + ); + const templates = {}; + const { __, _n, sprintf } = wp.i18n; + let aggregatedResults = createEmptyAggregatedResults(); let falsePositiveResults = createEmptyAggregatedResults(); let checksCompleted = false; @@ -134,6 +150,11 @@ resetResults(); announce( defaultString( 'checkingPlugin' ) ); + if ( optionsDetails ) { + optionsDetails.open = false; + } + lastRunCategories = getSelectedLabels( categoriesList ); + showRunningReport(); checkItButton.disabled = true; pluginsList.disabled = true; spinner.classList.add( 'is-active' ); @@ -220,10 +241,13 @@ function resetResults() { // Empty the results container. resultsContainer.textContent = ''; + falsePositivesContainer.textContent = ''; exportContainer.innerHTML = ''; exportContainer.classList.add( 'is-hidden' ); resetAggregatedResults(); + resetReportState(); checksCompleted = false; + report.hidden = true; } /** @@ -824,7 +848,8 @@ isSuccessMessage = false; } mergeAggregatedResults( results ); - renderResults( results ); + setRunningProgress( i + 1, checks.length ); + renderReport(); // Collect AI stats across checks. if ( results.ai_stats ) { @@ -861,7 +886,7 @@ } } - renderFalsePositiveResults(); + renderReport(); const resultsMessage = renderResultsMessage( isSuccessMessage, aiStats @@ -972,7 +997,6 @@ } if ( aiStats ) { - const { __, _n, sprintf } = wp.i18n; const aiParts = []; const modelsUsed = [ ...new Set( aiStats.models_used.filter( Boolean ) ), @@ -1048,11 +1072,7 @@ } } - resultsContainer.innerHTML = - renderTemplate( 'plugin-check-results-complete', { - type: messageType, - message: messageText, - } ) + resultsContainer.innerHTML; + renderSummary( messageType, messageText ); checksCompleted = true; renderExportButtons(); @@ -1147,353 +1167,1163 @@ } /** - * Renders results for each check on the page. + * Counts all results in a result tree. * - * @since 1.0.0 + * @since 2.0.0 * - * @param {Object} results The results object. + * @param {Object} tree Result tree. + * @return {number} Result count. */ - function renderResults( results ) { - const { ai_analysis: aiAnalysis } = results || {}; - const splitResults = splitResultsByFalsePositive( results ); - const errors = splitResults.actionable.errors; - const warnings = splitResults.actionable.warnings; - - // Render errors and warnings for files. - for ( const file in errors ) { - if ( warnings[ file ] ) { - renderFileResults( - file, - errors[ file ], - warnings[ file ], - aiAnalysis - ); - delete warnings[ file ]; - } else { - renderFileResults( file, errors[ file ], [], aiAnalysis ); + function countResultTree( tree ) { + let count = 0; + + for ( const file of Object.keys( tree || {} ) ) { + const lines = tree[ file ] || {}; + + for ( const line of Object.keys( lines ) ) { + const columns = lines[ line ] || {}; + + for ( const column of Object.keys( columns ) ) { + count += ( columns[ column ] || [] ).length; + } } } - // Render remaining files with only warnings. - for ( const file in warnings ) { - renderFileResults( file, [], warnings[ file ], aiAnalysis ); + return count; + } + + /* + * Results report. + * + * Results arrive one check at a time and are merged into the aggregated + * trees. The report flattens those trees into a list of issues and + * renders it, filtered and grouped by the viewer's choices. + */ + + // Number of issues shown per group before "Show more". + const GROUP_PREVIEW_SIZE = 10; + + // Paths that are usually development-only and not shipped. + const DEV_PATH_PATTERN = + /(^|\/)\.[^/]+|^(tests?|bin|scripts|node_modules|releases)(\/|$)/; + + const reportState = { + type: 'all', + check: null, + search: '', + hideDev: false, + groupBy: 'file', + expanded: new Set(), + showAll: new Set(), + }; + let lastRunCategories = []; + let runningProgress = ''; + + const searchInput = document.getElementById( 'plugin-check__search' ); + const hideDevInput = document.getElementById( 'plugin-check__hide-dev' ); + const checksList = document.getElementById( 'plugin-check__checks' ); + const checksCount = document.getElementById( 'plugin-check__checks-count' ); + const activeFilter = document.getElementById( + 'plugin-check__active-filter' + ); + const toggleAllButton = document.getElementById( + 'plugin-check__toggle-all' + ); + + /** + * Resets the viewer's filters for a new run. + * + * @since 2.3.0 + */ + function resetReportState() { + reportState.type = 'all'; + reportState.check = null; + reportState.search = ''; + reportState.expanded.clear(); + reportState.showAll.clear(); + runningProgress = ''; + if ( searchInput ) { + searchInput.value = ''; } } /** - * Renders the file results table. + * Gets the labels of the checked inputs from a list. * - * @since 1.0.0 + * @since 2.3.0 * - * @param {string} file The file name for the results. - * @param {Object} errors The file errors. - * @param {Object} warnings The file warnings. - * @param {Object} aiAnalysis AI analysis results. + * @param {NodeList|Array} list Inputs to read from. + * @return {Array} Labels of checked inputs. */ - function renderFileResults( file, errors, warnings, aiAnalysis ) { - if ( - ! hasEntries( { [ file ]: errors } ) && - ! hasEntries( { [ file ]: warnings } ) - ) { + function getSelectedLabels( list ) { + return Array.prototype.filter + .call( list, ( input ) => input && input.checked ) + .map( ( input ) => { + const label = input.closest( 'label' ); + return ( + input.dataset.label || + ( label ? label.textContent.trim() : '' ) || + input.value + ); + } ); + } + + /** + * Shows the options that are on as chips in the collapsed Options row. + * + * @since 2.3.0 + */ + function renderOptionsSummary() { + if ( ! optionsSummary ) { return; } - const index = - Date.now().toString( 36 ) + - Math.random().toString( 36 ).substr( 2 ); + const chips = ( labels ) => + labels + .map( + ( label ) => + '' + + escapeHtml( label ) + + '' + ) + .join( ' ' ); + const offChip = ( label ) => + '' + + escapeHtml( label ) + + ''; + + const flags = getSelectedLabels( + [ useAi, usePcpignore, includeExperimental ].filter( Boolean ) + ); - // Check if any errors or warnings have links. - const hasLinks = - hasLinksInResults( errors ) || hasLinksInResults( warnings ); + optionsSummary.innerHTML = [ + chips( getSelectedLabels( categoriesList ) ) || + offChip( __( 'No categories', 'plugin-check' ) ), + chips( getSelectedLabels( typesList ) ) || + offChip( __( 'No types', 'plugin-check' ) ), + flags.length + ? chips( flags ) + : offChip( __( 'No exclusions', 'plugin-check' ) ), + ].join( ' | ' ); + } - // Render the file table. - resultsContainer.innerHTML += renderTemplate( - 'plugin-check-results-table', - { file, index, hasLinks } - ); - const resultsTable = document.getElementById( - 'plugin-check__results-body-' + index + /** + * Escapes a string for use in HTML. + * + * @since 2.3.0 + * + * @param {string} value Value to escape. + * @return {string} Escaped value. + */ + function escapeHtml( value ) { + return String( value ).replace( + /[&<>"']/g, + ( c ) => + ( { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''', + } )[ c ] ); + } + + /** + * Returns the short name of a check code, e.g. "NonPrefixedFunctionFound". + * + * @since 2.3.0 + * + * @param {string} code Check code. + * @return {string} Short name. + */ + function shortCode( code ) { + return String( code || '' ) + .split( '.' ) + .pop(); + } + + /** + * Splits a path into its directory and base name. + * + * @since 2.3.0 + * + * @param {string} path File path. + * @return {Object} Object with dir and base properties. + */ + function splitPath( path ) { + const index = path.lastIndexOf( '/' ); + return index < 0 + ? { dir: '', base: path } + : { + dir: path.slice( 0, index + 1 ), + base: path.slice( index + 1 ), + }; + } + + /** + * Splits a check code into its namespace and short name. + * + * @since 2.3.0 + * + * @param {string} code Check code, e.g. "WordPress.WP.I18n.MissingTranslatorsComment". + * @return {Object} Object with dir and base properties. + */ + function splitCode( code ) { + const index = code.lastIndexOf( '.' ); + return index < 0 + ? { dir: '', base: code } + : { + dir: code.slice( 0, index + 1 ), + base: code.slice( index + 1 ), + }; + } + + /** + * Flattens a result tree into a list of issues. + * + * @since 2.3.0 + * + * @param {Object} tree Result tree. + * @param {string} type ERROR or WARNING. + * @param {boolean} falsePositive Whether AI flagged these as false positives. + * @param {Array} issues List to push into. + */ + function flattenResultTree( tree, type, falsePositive, issues ) { + for ( const file of Object.keys( tree || {} ) ) { + const lines = tree[ file ] || {}; + + for ( const line of Object.keys( lines ) ) { + const columns = lines[ line ] || {}; - // Render results to the table. - renderResultRows( + for ( const column of Object.keys( columns ) ) { + for ( const entry of columns[ column ] || [] ) { + issues.push( { + file, + line: parseInt( line, 10 ) || 0, + column: parseInt( column, 10 ) || 0, + type, + code: entry.code || '', + message: entry.message || '', + docs: entry.docs || '', + link: entry.link || '', + ai_analysis: entry.ai_analysis || null, + falsePositive, + isDev: DEV_PATH_PATTERN.test( file ), + } ); + } + } + } + } + } + + /** + * Gets all issues found so far. + * + * @since 2.3.0 + * + * @return {Object} Object with actionable and falsePositive issue lists. + */ + function getIssues() { + const actionable = []; + const falsePositive = []; + + flattenResultTree( + aggregatedResults.errors, 'ERROR', - errors, - resultsTable, - hasLinks, - aiAnalysis, - file + false, + actionable ); - renderResultRows( + flattenResultTree( + aggregatedResults.warnings, 'WARNING', - warnings, - resultsTable, - hasLinks, - aiAnalysis, - file + false, + actionable + ); + flattenResultTree( + falsePositiveResults.errors, + 'ERROR', + true, + falsePositive ); + flattenResultTree( + falsePositiveResults.warnings, + 'WARNING', + true, + falsePositive + ); + + return { actionable, falsePositive }; } /** - * Renders the possible false positives at the end of the results. + * Counts the issues of one type. * - * @since 2.0.0 + * @since 2.3.0 + * + * @param {Array} issues Issues. + * @param {string} type ERROR or WARNING. + * @return {number} Count. */ - function renderFalsePositiveResults() { - if ( - ! hasEntries( falsePositiveResults.errors ) && - ! hasEntries( falsePositiveResults.warnings ) - ) { - return; - } + function countType( issues, type ) { + return issues.filter( ( issue ) => issue.type === type ).length; + } - const index = - Date.now().toString( 36 ) + - Math.random().toString( 36 ).substr( 2 ); - const falsePositiveCount = - countResultTree( falsePositiveResults.errors ) + - countResultTree( falsePositiveResults.warnings ); + /** + * Sort rank of a result type, errors first. + * + * @since 2.3.0 + * + * @param {string} type ERROR or WARNING. + * @return {number} Rank. + */ + function typeRank( type ) { + return 'ERROR' === type ? 0 : 1; + } - resultsContainer.innerHTML += renderTemplate( - 'plugin-check-results-false-positives', - { - index, - count: falsePositiveCount, - } + /** + * Applies the viewer's filters to a list of issues. + * + * @since 2.3.0 + * + * @param {Array} issues Issues. + * @return {Array} Matching issues. + */ + function filterIssues( issues ) { + const search = reportState.search.toLowerCase(); + + return issues.filter( + ( issue ) => + ( 'all' === reportState.type || + issue.type === reportState.type ) && + ( ! reportState.check || issue.code === reportState.check ) && + ( ! reportState.hideDev || ! issue.isDev ) && + ( ! search || + ( issue.file + ' ' + issue.code + ' ' + issue.message ) + .toLowerCase() + .includes( search ) ) ); + } - const falsePositiveContainer = document.getElementById( - 'plugin-check__false-positive-results-' + index + /** + * Shows the report while checks are still running. + * + * @since 2.3.0 + */ + function showRunningReport() { + report.hidden = false; + report.classList.add( 'is-running' ); + summary.classList.remove( 'is-error', 'is-warning' ); + summaryTitle.textContent = defaultString( 'checkingPlugin' ); + summaryMeta.textContent = ''; + renderReport(); + } + + /** + * Updates the running progress line. + * + * @since 2.3.0 + * + * @param {number} done Checks finished. + * @param {number} total Checks to run. + */ + function setRunningProgress( done, total ) { + runningProgress = sprintf( + /* translators: 1: Number of checks finished, 2: Number of checks to run. */ + __( '%1$s of %2$s checks finished', 'plugin-check' ), + formatNumber( done ), + formatNumber( total ) ); + summaryMeta.textContent = runningProgress; + } - if ( ! falsePositiveContainer ) { - return; + /** + * Renders the summary once all checks have run. + * + * @since 2.3.0 + * + * @param {string} messageType error, warning or success. + * @param {string} messageText Summary sentence. + */ + function renderSummary( messageType, messageText ) { + const { actionable } = getIssues(); + const files = new Set( actionable.map( ( issue ) => issue.file ) ); + const parts = [ __( 'Checks complete', 'plugin-check' ) ]; + + if ( actionable.length ) { + parts.push( + sprintf( + /* translators: 1: Number of issues, 2: Number of files. */ + _n( + '%1$s issue in %2$s file', + '%1$s issues in %2$s files', + files.size, + 'plugin-check' + ), + formatNumber( actionable.length ), + formatNumber( files.size ) + ) + ); + } + if ( lastRunCategories.length ) { + parts.push( lastRunCategories.join( ', ' ) ); } - renderResultCollection( - falsePositiveResults.errors, - falsePositiveResults.warnings, - falsePositiveContainer + report.hidden = false; + report.classList.remove( 'is-running' ); + summary.classList.remove( 'is-error', 'is-warning' ); + if ( 'error' === messageType || 'warning' === messageType ) { + summary.classList.add( 'is-' + messageType ); + } + summaryTitle.textContent = messageText; + summaryMeta.textContent = parts.join( ' · ' ); + } + + /** + * Renders the counts, the checks list, the filters and the groups. + * + * @since 2.3.0 + */ + function renderReport() { + const { actionable, falsePositive } = getIssues(); + + renderCounts( actionable ); + renderChecksList( actionable ); + checkCombo.setChecks( actionable ); + renderGroups( + filterIssues( actionable ), + resultsContainer, + actionable.length ); + renderFalsePositives( falsePositive ); } /** - * Renders a result collection into a specific container. + * Renders the totals in the tiles and the type filter. * - * @since 2.0.0 + * @since 2.3.0 * - * @param {Object} containerErrors Error results. - * @param {Object} containerWarnings Warning results. - * @param {Object} container Container element. + * @param {Array} issues Actionable issues. */ - function renderResultCollection( - containerErrors, - containerWarnings, - container - ) { - const errors = cloneResultTree( containerErrors ); - const warnings = cloneResultTree( containerWarnings ); - - for ( const file in errors ) { - if ( warnings[ file ] ) { - renderFileResultsInContainer( - file, - errors[ file ], - warnings[ file ], - container - ); - delete warnings[ file ]; - } else { - renderFileResultsInContainer( - file, - errors[ file ], - [], - container - ); - } - } + function renderCounts( issues ) { + const counts = { + all: issues.length, + ERROR: countType( issues, 'ERROR' ), + WARNING: countType( issues, 'WARNING' ), + files: new Set( issues.map( ( issue ) => issue.file ) ).size, + }; - for ( const file in warnings ) { - renderFileResultsInContainer( - file, - [], - warnings[ file ], - container + report.querySelectorAll( '[data-count]' ).forEach( ( el ) => { + el.textContent = formatNumber( counts[ el.dataset.count ] || 0 ); + } ); + report.querySelectorAll( '[data-filter-type]' ).forEach( ( el ) => { + el.setAttribute( + 'aria-pressed', + String( el.dataset.filterType === reportState.type ) ); - } + } ); + hideDevInput.checked = reportState.hideDev; } /** - * Renders one file's results into a specific container. + * Groups issues by check code, errors first, then by count. * - * @since 2.0.0 + * @since 2.3.0 * - * @param {string} file File name. - * @param {Object} errors Error results. - * @param {Object} warnings Warning results. - * @param {Object} container Container element. + * @param {Array} issues Issues. + * @return {Array} List of { code, count, type } objects. */ - function renderFileResultsInContainer( file, errors, warnings, container ) { - if ( - ! hasEntries( { [ file ]: errors } ) && - ! hasEntries( { [ file ]: warnings } ) - ) { + function summarizeChecks( issues ) { + const byCode = new Map(); + + issues.forEach( ( issue ) => { + const entry = byCode.get( issue.code ) || { + code: issue.code, + count: 0, + type: 'WARNING', + }; + entry.count++; + if ( 'ERROR' === issue.type ) { + entry.type = 'ERROR'; + } + byCode.set( issue.code, entry ); + } ); + + return Array.from( byCode.values() ).sort( + ( a, b ) => + typeRank( a.type ) - typeRank( b.type ) || b.count - a.count + ); + } + + /** + * Renders the list of failed checks in the sidebar. + * + * @since 2.3.0 + * + * @param {Array} issues Actionable issues. + */ + function renderChecksList( issues ) { + const checks = summarizeChecks( issues ); + const max = Math.max( 1, ...checks.map( ( check ) => check.count ) ); + + checksCount.textContent = sprintf( + /* translators: %s: Number of checks that reported issues. */ + _n( + '%s check failed', + '%s checks failed', + checks.length, + 'plugin-check' + ), + formatNumber( checks.length ) + ); + + checksList.innerHTML = checks + .map( ( check ) => { + const type = check.type.toLowerCase(); + return ( + '
  • ' + ); + } ) + .join( '' ); + } + + /** + * Renders issues as collapsible groups into a container. + * + * @since 2.3.0 + * + * @param {Array} issues Issues to show. + * @param {Element} container Container element. + * @param {number} total Number of issues before filtering. + * @param {boolean} isFalsePositive Whether these are false positives. + */ + function renderGroups( issues, container, total, isFalsePositive ) { + if ( ! isFalsePositive ) { + renderActiveFilter( issues.length, total ); + } + + container.textContent = ''; + + if ( ! issues.length ) { + if ( total > 0 ) { + const empty = document.createElement( 'p' ); + empty.className = 'plugin-check__empty'; + empty.textContent = __( + 'No issues match these filters.', + 'plugin-check' + ); + container.appendChild( empty ); + } return; } - const index = - Date.now().toString( 36 ) + - Math.random().toString( 36 ).substr( 2 ); - const hasLinks = - hasLinksInResults( errors ) || hasLinksInResults( warnings ); + const byCheck = 'check' === reportState.groupBy && ! isFalsePositive; + const groups = new Map(); - container.innerHTML += renderTemplate( 'plugin-check-results-table', { - file, - index, - hasLinks, + issues.forEach( ( issue ) => { + const key = byCheck ? issue.code : issue.file; + if ( ! groups.has( key ) ) { + groups.set( key, [] ); + } + groups.get( key ).push( issue ); } ); - const resultsTable = document.getElementById( - 'plugin-check__results-body-' + index + const sorted = Array.from( groups.entries() ).sort( + ( a, b ) => + countType( b[ 1 ], 'ERROR' ) - countType( a[ 1 ], 'ERROR' ) || + b[ 1 ].length - a[ 1 ].length ); + const html = []; + + sorted.forEach( ( [ key, items ] ) => { + const errors = countType( items, 'ERROR' ); + const warnings = items.length - errors; + const groupKey = ( isFalsePositive ? 'fp:' : '' ) + key; + const showAll = reportState.showAll.has( groupKey ); + const shown = showAll + ? items + : items.slice( 0, GROUP_PREVIEW_SIZE ); + const path = byCheck ? splitCode( key ) : splitPath( key ); + + items.sort( + ( a, b ) => + a.line - b.line || + a.column - b.column || + typeRank( a.type ) - typeRank( b.type ) + ); - renderResultRows( 'ERROR', errors, resultsTable, hasLinks, {}, file ); - renderResultRows( - 'WARNING', - warnings, - resultsTable, - hasLinks, - {}, - file - ); + html.push( + renderTemplate( 'plugin-check-results-group', { + key: groupKey, + dir: path.dir, + base: path.base, + open: + reportState.expanded.has( groupKey ) || + sorted.length <= 3, + isDev: ! byCheck && DEV_PATH_PATTERN.test( key ), + errorLabel: errors + ? sprintf( + /* translators: %s: Number of errors. */ + _n( + '%s error', + '%s errors', + errors, + 'plugin-check' + ), + formatNumber( errors ) + ) + : '', + warningLabel: warnings + ? sprintf( + /* translators: %s: Number of warnings. */ + _n( + '%s warning', + '%s warnings', + warnings, + 'plugin-check' + ), + formatNumber( warnings ) + ) + : '', + moreLabel: + items.length > shown.length + ? sprintf( + /* translators: %s: Number of hidden issues. */ + __( 'Show %s more', 'plugin-check' ), + formatNumber( items.length - shown.length ) + ) + : '', + } ).replace( + '', + '' + ) + ); + } ); + + container.innerHTML = html.join( '' ); } /** - * Clones a result tree. + * Renders one issue row. * - * @since 2.0.0 + * @since 2.3.0 * - * @param {Object} tree Result tree. - * @return {Object} Cloned result tree. + * @param {Object} issue Issue. + * @param {boolean} byCheck Whether rows are grouped by check. + * @return {string} Row HTML. */ - function cloneResultTree( tree ) { - const clone = {}; - mergeResultTree( clone, tree || {} ); - return clone; + function renderIssue( issue, byCheck ) { + const path = splitPath( issue.file ); + + return renderTemplate( 'plugin-check-results-row', { + type: issue.type, + typeLabel: + 'ERROR' === issue.type + ? __( 'Error', 'plugin-check' ) + : __( 'Warning', 'plugin-check' ), + line: issue.line, + column: issue.column, + message: issue.message, + docs: issue.docs, + code: issue.code, + link: issue.link, + ai_analysis: issue.ai_analysis, + showFile: byCheck, + showCode: ! byCheck, + dir: path.dir, + base: path.base, + } ); } /** - * Counts all results in a result tree. + * Renders the "Showing X of Y" line for active filters. * - * @since 2.0.0 + * @since 2.3.0 * - * @param {Object} tree Result tree. - * @return {number} Result count. + * @param {number} shown Issues shown. + * @param {number} total Issues before filtering. */ - function countResultTree( tree ) { - let count = 0; + function renderActiveFilter( shown, total ) { + const parts = []; + + if ( reportState.check ) { + parts.push( + sprintf( + /* translators: %s: Check code. */ + __( 'check %s', 'plugin-check' ), + '' + escapeHtml( reportState.check ) + '' + ) + ); + } + if ( 'ERROR' === reportState.type ) { + parts.push( escapeHtml( __( 'errors only', 'plugin-check' ) ) ); + } else if ( 'WARNING' === reportState.type ) { + parts.push( escapeHtml( __( 'warnings only', 'plugin-check' ) ) ); + } + if ( reportState.hideDev ) { + parts.push( + escapeHtml( __( 'development files hidden', 'plugin-check' ) ) + ); + } + if ( reportState.search ) { + parts.push( + escapeHtml( + sprintf( + /* translators: %s: Search text. */ + __( 'matching "%s"', 'plugin-check' ), + reportState.search + ) + ) + ); + } - for ( const file of Object.keys( tree || {} ) ) { - const lines = tree[ file ] || {}; + activeFilter.hidden = ! parts.length; + if ( ! parts.length ) { + activeFilter.innerHTML = ''; + return; + } - for ( const line of Object.keys( lines ) ) { - const columns = lines[ line ] || {}; + activeFilter.innerHTML = + escapeHtml( + sprintf( + /* translators: 1: Number of issues shown, 2: Total number of issues. */ + __( 'Showing %1$s of %2$s:', 'plugin-check' ), + formatNumber( shown ), + formatNumber( total ) + ) + ) + + ' ' + + parts.join( ', ' ) + + ' '; + } - for ( const column of Object.keys( columns ) ) { - count += ( columns[ column ] || [] ).length; - } - } + /** + * Renders the AI possible false positives below the report. + * + * @since 2.0.0 + * + * @param {Array} issues False positive issues. + */ + function renderFalsePositives( issues ) { + falsePositivesContainer.textContent = ''; + if ( ! issues.length ) { + return; } - return count; + const index = 'fp'; + falsePositivesContainer.innerHTML = renderTemplate( + 'plugin-check-results-false-positives', + { index, count: formatNumber( issues.length ) } + ); + + const container = document.getElementById( + 'plugin-check__false-positive-results-' + index + ); + if ( container ) { + renderGroups( issues, container, issues.length, true ); + } } /** - * Checks if there are any links in the results object. + * Re-renders only the groups after a filter change. * - * @since 1.0.0 - * - * @param {Object} results The results object. - * @return {boolean} True if there are links, false otherwise. + * @since 2.3.0 */ - function hasLinksInResults( results ) { - for ( const line in results ) { - for ( const column in results[ line ] ) { - for ( let i = 0; i < results[ line ][ column ].length; i++ ) { - if ( results[ line ][ column ][ i ].link ) { - return true; - } - } + function renderFilteredGroups() { + const { actionable } = getIssues(); + renderCounts( actionable ); + renderChecksList( actionable ); + checkCombo.sync(); + renderGroups( + filterIssues( actionable ), + resultsContainer, + actionable.length + ); + } + + report.addEventListener( 'click', ( event ) => { + const button = event.target.closest( 'button' ); + if ( ! button || ! report.contains( button ) ) { + return; + } + + if ( button.dataset.filterType ) { + reportState.type = + reportState.type === button.dataset.filterType + ? 'all' + : button.dataset.filterType; + renderFilteredGroups(); + } else if ( button.dataset.filterCheck ) { + reportState.check = + reportState.check === button.dataset.filterCheck + ? null + : button.dataset.filterCheck; + renderFilteredGroups(); + } else if ( button.dataset.showMore ) { + reportState.showAll.add( button.dataset.showMore ); + reportState.expanded.add( button.dataset.showMore ); + renderReport(); + } else if ( button.dataset.groupBy ) { + reportState.groupBy = button.dataset.groupBy; + reportState.expanded.clear(); + reportState.showAll.clear(); + report + .querySelectorAll( '[data-group-by]' ) + .forEach( ( el ) => + el.setAttribute( 'aria-pressed', String( el === button ) ) + ); + renderFilteredGroups(); + } else if ( button.dataset.clearFilters ) { + reportState.type = 'all'; + reportState.check = null; + reportState.search = ''; + reportState.hideDev = false; + searchInput.value = ''; + renderFilteredGroups(); + } else if ( button.dataset.copy ) { + copyCode( button.dataset.copy ); + } else if ( button === toggleAllButton ) { + const groups = Array.from( + resultsContainer.querySelectorAll( '.plugin-check__group' ) + ); + const openAll = groups.some( ( group ) => ! group.open ); + groups.forEach( ( group ) => { + group.open = openAll; + } ); + toggleAllButton.textContent = openAll + ? __( 'Collapse all', 'plugin-check' ) + : __( 'Expand all', 'plugin-check' ); + } + } ); + + // Remember which groups are open across re-renders. + report.addEventListener( + 'toggle', + ( event ) => { + const group = event.target; + if ( + ! group.classList || + ! group.classList.contains( 'plugin-check__group' ) + ) { + return; } + if ( group.open ) { + reportState.expanded.add( group.dataset.key ); + } else { + reportState.expanded.delete( group.dataset.key ); + } + }, + true + ); + + searchInput.addEventListener( 'input', () => { + reportState.search = searchInput.value.trim(); + renderFilteredGroups(); + } ); + + hideDevInput.addEventListener( 'change', () => { + reportState.hideDev = hideDevInput.checked; + renderFilteredGroups(); + } ); + + /** + * Copies a check code to the clipboard. + * + * @since 2.3.0 + * + * @param {string} code Check code. + */ + function copyCode( code ) { + if ( ! window.navigator.clipboard ) { + return; } - return false; + window.navigator.clipboard.writeText( code ).then( + () => + announce( + sprintf( + /* translators: %s: Check code. */ + __( 'Copied %s', 'plugin-check' ), + code + ) + ), + () => {} + ); } /** - * Renders a result row onto the file table. - * - * @since 1.0.0 + * Searchable check picker (ARIA combobox). * - * @param {string} type The result type. Either ERROR or WARNING. - * @param {Object} results The results object. - * @param {Object} table The HTML table to append a result row to. - * @param {boolean} hasLinks Whether any result has links. - * @param {Object} aiAnalysis AI analysis results. - * @param {string} file The file path. + * @since 2.3.0 */ - function renderResultRows( - type, - results, - table, - hasLinks, - aiAnalysis, - file - ) { - // Loop over each result by the line, column and messages. - for ( const line in results ) { - for ( const column in results[ line ] ) { - for ( let i = 0; i < results[ line ][ column ].length; i++ ) { - const message = results[ line ][ column ][ i ].message; - const docs = results[ line ][ column ][ i ].docs; - const code = results[ line ][ column ][ i ].code; - const link = results[ line ][ column ][ i ].link; - const storedAiData = - results[ line ][ column ][ i ].ai_analysis || null; - - // Find AI analysis for this issue. - const aiData = - storedAiData || - findAiAnalysisForIssue( - file, - line, - column, - code, - aiAnalysis - ); + const checkCombo = ( () => { + const input = document.getElementById( 'plugin-check__check-input' ); + const list = document.getElementById( 'plugin-check__check-list' ); + const clear = document.getElementById( 'plugin-check__combo-clear' ); + const toggle = document.getElementById( 'plugin-check__combo-toggle' ); + let checks = []; + let total = 0; + let options = []; + let active = -1; + + function highlight( text, query ) { + const index = query ? text.toLowerCase().indexOf( query ) : -1; + if ( index < 0 ) { + return escapeHtml( text ); + } + return ( + escapeHtml( text.slice( 0, index ) ) + + '' + + escapeHtml( text.slice( index, index + query.length ) ) + + '' + + escapeHtml( text.slice( index + query.length ) ) + ); + } - const rowData = { - line, - column, - type, - message, - docs, - code, - link, - hasLinks, - }; - - // Add AI analysis data if available. - if ( aiData ) { - rowData.ai_analysis = aiData; - } + function draw() { + const raw = input.value.trim(); + // Show everything while the box only displays the current choice. + const query = + reportState.check && raw === shortCode( reportState.check ) + ? '' + : raw.toLowerCase(); + + options = checks.filter( + ( check ) => + ! query || check.code.toLowerCase().includes( query ) + ); + if ( ! query ) { + options.unshift( { code: null, count: total } ); + } + + if ( ! options.length ) { + list.innerHTML = + '
  • ' + + escapeHtml( + sprintf( + /* translators: %s: Search text. */ + __( 'No checks match "%s"', 'plugin-check' ), + raw + ) + ) + + '
  • '; + setActive( -1 ); + return; + } - table.innerHTML += renderTemplate( - 'plugin-check-results-row', - rowData + list.innerHTML = options + .map( ( option, i ) => { + const selected = + option.code === reportState.check || + ( ! option.code && ! reportState.check ); + const name = option.code + ? highlight( shortCode( option.code ), query ) + + ( option.code !== shortCode( option.code ) + ? '' + + highlight( option.code, query ) + + '' + : '' ) + : escapeHtml( __( 'All checks', 'plugin-check' ) ); + const dot = option.code + ? '' + : ''; + + return ( + '
  • ' + + dot + + '' + + name + + '' + + formatNumber( option.count ) + + '
  • ' ); - } + } ) + .join( '' ); + + setActive( + Math.max( + 0, + options.findIndex( + ( option ) => option.code === reportState.check + ) + ) + ); + } + + function setActive( index ) { + active = options.length + ? ( index + options.length ) % options.length + : -1; + list.querySelectorAll( '.plugin-check__combo-option' ).forEach( + ( item, i ) => + item.classList.toggle( 'is-active', i === active ) + ); + const item = list.querySelector( '.is-active' ); + input.setAttribute( 'aria-activedescendant', item ? item.id : '' ); + if ( item ) { + item.scrollIntoView( { block: 'nearest' } ); + } + } + + function open() { + if ( ! list.hidden ) { + return; + } + list.hidden = false; + input.setAttribute( 'aria-expanded', 'true' ); + draw(); + } + + function close() { + list.hidden = true; + input.setAttribute( 'aria-expanded', 'false' ); + sync(); + } + + function choose( index ) { + const option = options[ index ]; + if ( ! option ) { + return; } + reportState.check = option.code; + close(); + renderFilteredGroups(); } + + function sync() { + if ( input.ownerDocument.activeElement !== input || list.hidden ) { + input.value = reportState.check + ? shortCode( reportState.check ) + : ''; + } + clear.hidden = ! reportState.check; + } + + function setChecks( issues ) { + checks = summarizeChecks( issues ).sort( ( a, b ) => + shortCode( a.code ).localeCompare( shortCode( b.code ) ) + ); + total = issues.length; + if ( ! list.hidden ) { + draw(); + } + sync(); + } + + input.addEventListener( 'focus', () => { + input.select(); + open(); + } ); + input.addEventListener( 'click', open ); + input.addEventListener( 'input', () => { + if ( list.hidden ) { + open(); + } else { + draw(); + } + } ); + input.addEventListener( 'keydown', ( event ) => { + if ( 'ArrowDown' === event.key ) { + event.preventDefault(); + if ( list.hidden ) { + open(); + } else { + setActive( active + 1 ); + } + } else if ( 'ArrowUp' === event.key ) { + event.preventDefault(); + if ( list.hidden ) { + open(); + } else { + setActive( active - 1 ); + } + } else if ( 'Enter' === event.key ) { + event.preventDefault(); + if ( ! list.hidden ) { + choose( active ); + } + } else if ( 'Escape' === event.key && ! list.hidden ) { + event.preventDefault(); + close(); + } else if ( 'Tab' === event.key ) { + close(); + } + } ); + input.addEventListener( 'blur', () => { + setTimeout( () => { + if ( input.ownerDocument.activeElement !== input ) { + close(); + } + }, 0 ); + } ); + // Keep focus in the input while picking with the mouse. + list.addEventListener( 'mousedown', ( event ) => + event.preventDefault() + ); + list.addEventListener( 'click', ( event ) => { + const item = event.target.closest( '.plugin-check__combo-option' ); + if ( item ) { + choose( parseInt( item.dataset.index, 10 ) ); + } + } ); + list.addEventListener( 'mousemove', ( event ) => { + const item = event.target.closest( '.plugin-check__combo-option' ); + if ( item && parseInt( item.dataset.index, 10 ) !== active ) { + setActive( parseInt( item.dataset.index, 10 ) ); + } + } ); + clear.addEventListener( 'click', ( event ) => { + event.stopPropagation(); + reportState.check = null; + renderFilteredGroups(); + input.focus(); + } ); + toggle.addEventListener( 'mousedown', ( event ) => { + event.preventDefault(); + if ( list.hidden ) { + input.focus(); + open(); + } else { + close(); + } + } ); + + return { setChecks, sync }; + } )(); + + if ( optionsDetails ) { + optionsDetails.addEventListener( 'change', renderOptionsSummary ); + pluginsList.addEventListener( 'change', renderOptionsSummary ); + renderOptionsSummary(); } /** diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 1be41fc9b..43f2ac6a5 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -372,83 +372,29 @@ public function filter_plugin_action_links( $actions, $plugin_file, $plugin_data } /** - * Render the results table templates in the footer. + * Renders the results templates in the footer. * * @since 1.0.0 */ public function admin_footer() { - ob_start(); - require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-table.php'; - $results_table_template = ob_get_clean(); - wp_print_inline_script_tag( - $results_table_template, - array( - 'id' => 'tmpl-plugin-check-results-table', - 'type' => 'text/template', - ) - ); - - ob_start(); - require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-row.php'; - $results_row_template = ob_get_clean(); - wp_print_inline_script_tag( - $results_row_template, - array( - 'id' => 'tmpl-plugin-check-results-row', - 'type' => 'text/template', - ) + $templates = array( + 'plugin-check-results-group' => 'templates/results-group.php', + 'plugin-check-results-row' => 'templates/results-row.php', + 'plugin-check-results-false-positives' => 'templates/results-false-positives.php', ); - ob_start(); - require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-false-positives.php'; - $results_false_positives_template = ob_get_clean(); - wp_print_inline_script_tag( - $results_false_positives_template, - array( - 'id' => 'tmpl-plugin-check-results-false-positives', - 'type' => 'text/template', - ) - ); - - ob_start(); - require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/results-complete.php'; - $results_row_template = ob_get_clean(); - wp_print_inline_script_tag( - $results_row_template, - array( - 'id' => 'tmpl-plugin-check-results-complete', - 'type' => 'text/template', - ) - ); - ?> - - $template_file ) { + ob_start(); + require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . $template_file; + $template = ob_get_clean(); + wp_print_inline_script_tag( + $template, + array( + 'id' => 'tmpl-' . $template_id, + 'type' => 'text/template', + ) + ); + } } /** diff --git a/templates/admin-page.php b/templates/admin-page.php index a334decb2..21eb9aa55 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -15,91 +15,89 @@ -
    -

    - -

    - -

    - -

    - - - - - - -
    -
    -

    - - - $label ) : ?> - - - - -
    -
    - - -
    -
    - -
    -
    -

    - - - $label ) : ?> - - - - -
    -
    - - -
    -
    - -
    -
    -

    -

    - -

    -
    -
    -

    -

    - -

    -
    -
    - -

    - + +

    +

    + +

    + +

    +

    - + + + + +
    + + + +
    + + + + + + + + + +
    +
    + + $label ) : ?> + + +
    + +
    + + $label ) : ?> + + +
    + +
    + + +
    + +
    + + + + + +
    +
    +
    @@ -109,7 +107,77 @@ -
    -
    + diff --git a/templates/results-complete.php b/templates/results-complete.php deleted file mode 100644 index cd2871be2..000000000 --- a/templates/results-complete.php +++ /dev/null @@ -1,3 +0,0 @@ -
    -

    {{ data.message }}

    -
    diff --git a/templates/results-group.php b/templates/results-group.php new file mode 100644 index 000000000..2507936cc --- /dev/null +++ b/templates/results-group.php @@ -0,0 +1,33 @@ + +
    open<# } #>> + + + + {{ data.dir }}{{ data.base }} + + <# if ( data.isDev ) { #> + + <# } #> + + <# if ( data.errorLabel ) { #> + {{ data.errorLabel }} + <# } #> + <# if ( data.warningLabel ) { #> + {{ data.warningLabel }} + <# } #> + + + + <# if ( data.moreLabel ) { #> +
    + +
    + <# } #> +
    diff --git a/templates/results-row.php b/templates/results-row.php index c9765854d..8e7f7f637 100644 --- a/templates/results-row.php +++ b/templates/results-row.php @@ -1,66 +1,75 @@ - - - {{data.line}} - - - {{data.column}} - - - {{data.type}} - - - {{data.code}} + +
  • + {{ data.typeLabel }} + + <# if ( parseInt( data.line, 10 ) ) { #> + + <# } else { #> + + <# } #> + +
    + <# if ( data.showFile ) { #> +
    + {{ data.dir }}{{ data.base }} +
    + <# } #> +

    {{{ data.message }}}

    + <# if ( data.showCode ) { #> + + <# } #> <# if ( data.ai_analysis ) { #> -
    - <# if ( data.ai_analysis.is_false_positive ) { #> - - - - <# if ( data.ai_analysis.confidence ) { #> - (: {{Math.round(data.ai_analysis.confidence * 100)}}%) - <# } #> - - <# } else { #> - - - - <# if ( data.ai_analysis.confidence ) { #> - (: {{Math.round(data.ai_analysis.confidence * 100)}}%) - <# } #> - +
    + <# if ( data.ai_analysis.is_false_positive ) { #> + + + + <# if ( data.ai_analysis.confidence ) { #> + (: {{Math.round(data.ai_analysis.confidence * 100)}}%) + <# } #> + + <# } else { #> + + + + <# if ( data.ai_analysis.confidence ) { #> + (: {{Math.round(data.ai_analysis.confidence * 100)}}%) + <# } #> + + <# } #> +
    + <# if ( data.ai_analysis.reasoning ) { #> + {{{data.ai_analysis.reasoning}}} + <# } #> + <# if ( data.ai_analysis.recommendation ) { #> + : {{{data.ai_analysis.recommendation}}} <# } #> <# } #> - - - {{{data.message}}} +
    + <# if ( data.docs ) { #> -
    - + <# } #> - <# if ( data.ai_analysis ) { #> - <# if ( data.ai_analysis.reasoning ) { #> -
    - {{{data.ai_analysis.reasoning}}} - <# } #> - <# if ( data.ai_analysis.recommendation ) { #> -
    - : {{{data.ai_analysis.recommendation}}} - <# } #> + <# if ( data.link ) { #> + + + + + <# } #> - - <# if ( data.hasLinks ) { #> - - <# if ( data.link ) { #> - - - - - - <# } #> - - <# } #> - +
    +
  • diff --git a/templates/results-table.php b/templates/results-table.php deleted file mode 100644 index 7c1dd6b63..000000000 --- a/templates/results-table.php +++ /dev/null @@ -1,29 +0,0 @@ -

    {{ data.file }}

    - - - - - - - - - <# if ( data.hasLinks ) { #> - - <# } #> - - - -
    - - - - - - - - - - - -
    -