Changeset 3797325
- Timestamp:
- 11/12/2026 01:30:29 AM (6 days ago)
- Location:
- imzura-site-check
- Files:
-
- 34 added
- 68 edited
- 1 copied
-
tags/1.3.0 (copied) (copied from imzura-site-check/trunk )
-
tags/1.3.0/assets/admin.css (modified) (4 diffs)
-
tags/1.3.0/assets/admin.js (modified) (29 diffs)
-
tags/1.3.0/assets/probe.js (modified) (17 diffs)
-
tags/1.3.0/imzura-site-check.php (modified) (4 diffs)
-
tags/1.3.0/includes/checks/class-autoload.php (modified) (1 diff)
-
tags/1.3.0/includes/checks/class-code-time.php (added)
-
tags/1.3.0/includes/checks/class-delivery.php (modified) (4 diffs)
-
tags/1.3.0/includes/checks/class-environment.php (modified) (4 diffs)
-
tags/1.3.0/includes/checks/class-errors.php (added)
-
tags/1.3.0/includes/checks/class-fonts.php (added)
-
tags/1.3.0/includes/checks/class-front-pages.php (modified) (8 diffs)
-
tags/1.3.0/includes/checks/class-heartbeat.php (added)
-
tags/1.3.0/includes/checks/class-housekeeping.php (added)
-
tags/1.3.0/includes/checks/class-idle-styles.php (added)
-
tags/1.3.0/includes/checks/class-image-copies.php (added)
-
tags/1.3.0/includes/checks/class-images.php (added)
-
tags/1.3.0/includes/checks/class-plugin-settings.php (added)
-
tags/1.3.0/includes/checks/class-scheduled-work.php (modified) (1 diff)
-
tags/1.3.0/includes/checks/class-scripts.php (added)
-
tags/1.3.0/includes/checks/class-store.php (modified) (1 diff)
-
tags/1.3.0/includes/class-admin-page.php (modified) (6 diffs)
-
tags/1.3.0/includes/class-cache.php (modified) (10 diffs)
-
tags/1.3.0/includes/class-check.php (modified) (4 diffs)
-
tags/1.3.0/includes/class-conflicts.php (modified) (9 diffs)
-
tags/1.3.0/includes/class-fix.php (modified) (2 diffs)
-
tags/1.3.0/includes/class-fixer.php (modified) (19 diffs)
-
tags/1.3.0/includes/class-paths.php (modified) (5 diffs)
-
tags/1.3.0/includes/class-pipeline.php (added)
-
tags/1.3.0/includes/class-plugin.php (modified) (3 diffs)
-
tags/1.3.0/includes/class-probe.php (modified) (15 diffs)
-
tags/1.3.0/includes/class-profiler.php (added)
-
tags/1.3.0/includes/class-rest.php (modified) (5 diffs)
-
tags/1.3.0/includes/class-scanner.php (modified) (10 diffs)
-
tags/1.3.0/includes/class-tidy.php (added)
-
tags/1.3.0/includes/fixes/class-cart-script.php (modified) (3 diffs)
-
tags/1.3.0/includes/fixes/class-contact-form-7.php (modified) (6 diffs)
-
tags/1.3.0/includes/fixes/class-defer-scripts.php (modified) (5 diffs)
-
tags/1.3.0/includes/fixes/class-emoji.php (modified) (1 diff)
-
tags/1.3.0/includes/fixes/class-heavy-images.php (modified) (8 diffs)
-
tags/1.3.0/includes/fixes/class-image-sizes.php (added)
-
tags/1.3.0/includes/fixes/class-jquery-migrate.php (modified) (2 diffs)
-
tags/1.3.0/includes/fixes/class-lcp-image.php (added)
-
tags/1.3.0/includes/fixes/class-local-fonts.php (added)
-
tags/1.3.0/includes/fixes/class-minify-scripts.php (modified) (2 diffs)
-
tags/1.3.0/includes/fixes/class-minify-styles.php (modified) (1 diff)
-
tags/1.3.0/includes/fixes/class-minify.php (modified) (13 diffs)
-
tags/1.3.0/includes/fixes/class-video-facade.php (modified) (7 diffs)
-
tags/1.3.0/includes/fixes/class-visitor-heartbeat.php (added)
-
tags/1.3.0/includes/fixes/class-woocommerce-files.php (modified) (14 diffs)
-
tags/1.3.0/readme.txt (modified) (7 diffs)
-
tags/1.3.0/uninstall.php (modified) (3 diffs)
-
trunk/assets/admin.css (modified) (4 diffs)
-
trunk/assets/admin.js (modified) (29 diffs)
-
trunk/assets/probe.js (modified) (17 diffs)
-
trunk/imzura-site-check.php (modified) (4 diffs)
-
trunk/includes/checks/class-autoload.php (modified) (1 diff)
-
trunk/includes/checks/class-code-time.php (added)
-
trunk/includes/checks/class-delivery.php (modified) (4 diffs)
-
trunk/includes/checks/class-environment.php (modified) (4 diffs)
-
trunk/includes/checks/class-errors.php (added)
-
trunk/includes/checks/class-fonts.php (added)
-
trunk/includes/checks/class-front-pages.php (modified) (8 diffs)
-
trunk/includes/checks/class-heartbeat.php (added)
-
trunk/includes/checks/class-housekeeping.php (added)
-
trunk/includes/checks/class-idle-styles.php (added)
-
trunk/includes/checks/class-image-copies.php (added)
-
trunk/includes/checks/class-images.php (added)
-
trunk/includes/checks/class-plugin-settings.php (added)
-
trunk/includes/checks/class-scheduled-work.php (modified) (1 diff)
-
trunk/includes/checks/class-scripts.php (added)
-
trunk/includes/checks/class-store.php (modified) (1 diff)
-
trunk/includes/class-admin-page.php (modified) (6 diffs)
-
trunk/includes/class-cache.php (modified) (10 diffs)
-
trunk/includes/class-check.php (modified) (4 diffs)
-
trunk/includes/class-conflicts.php (modified) (9 diffs)
-
trunk/includes/class-fix.php (modified) (2 diffs)
-
trunk/includes/class-fixer.php (modified) (19 diffs)
-
trunk/includes/class-paths.php (modified) (5 diffs)
-
trunk/includes/class-pipeline.php (added)
-
trunk/includes/class-plugin.php (modified) (3 diffs)
-
trunk/includes/class-probe.php (modified) (15 diffs)
-
trunk/includes/class-profiler.php (added)
-
trunk/includes/class-rest.php (modified) (5 diffs)
-
trunk/includes/class-scanner.php (modified) (10 diffs)
-
trunk/includes/class-tidy.php (added)
-
trunk/includes/fixes/class-cart-script.php (modified) (3 diffs)
-
trunk/includes/fixes/class-contact-form-7.php (modified) (6 diffs)
-
trunk/includes/fixes/class-defer-scripts.php (modified) (5 diffs)
-
trunk/includes/fixes/class-emoji.php (modified) (1 diff)
-
trunk/includes/fixes/class-heavy-images.php (modified) (8 diffs)
-
trunk/includes/fixes/class-image-sizes.php (added)
-
trunk/includes/fixes/class-jquery-migrate.php (modified) (2 diffs)
-
trunk/includes/fixes/class-lcp-image.php (added)
-
trunk/includes/fixes/class-local-fonts.php (added)
-
trunk/includes/fixes/class-minify-scripts.php (modified) (2 diffs)
-
trunk/includes/fixes/class-minify-styles.php (modified) (1 diff)
-
trunk/includes/fixes/class-minify.php (modified) (13 diffs)
-
trunk/includes/fixes/class-video-facade.php (modified) (7 diffs)
-
trunk/includes/fixes/class-visitor-heartbeat.php (added)
-
trunk/includes/fixes/class-woocommerce-files.php (modified) (14 diffs)
-
trunk/readme.txt (modified) (7 diffs)
-
trunk/uninstall.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
imzura-site-check/tags/1.3.0/assets/admin.css
r3797250 r3797325 1 /* imzura Site Check by imzura.com */ 1 2 .imzura-site-check { 2 3 --sp-ink: #14213d; … … 180 181 } 181 182 183 .speed-doctor-review { 184 display: flex; 185 flex-wrap: wrap; 186 align-items: center; 187 gap: 8px 16px; 188 margin-top: 32px; 189 padding: 16px 20px; 190 border: 1px solid var(--sp-line); 191 border-radius: 6px; 192 background: #fff; 193 } 194 195 .speed-doctor-review p { 196 flex: 1 1 32ch; 197 max-width: 70ch; 198 margin: 0; 199 font-size: 14px; 200 line-height: 1.6; 201 } 202 203 .speed-doctor-review + .speed-doctor-help { 204 margin-top: 16px; 205 } 206 182 207 .speed-doctor-help h2 { 183 208 margin: 0 0 8px; … … 268 293 font-weight: 600; 269 294 color: var(--sp-good); 295 } 296 297 .speed-doctor-tidy { 298 margin: 0 0 16px; 299 padding: 16px 16px 16px 20px; 300 border: 1px solid var(--sp-line); 301 border-radius: 6px; 302 background: var(--sp-card); 303 } 304 305 .speed-doctor-tidy p { 306 max-width: 78ch; 307 margin: 0; 308 font-size: 14px; 309 line-height: 1.6; 310 } 311 312 .speed-doctor-tidy p + p { 313 margin-top: 8px; 270 314 } 271 315 … … 308 352 } 309 353 354 .speed-doctor-frame.is-scan { 355 opacity: 0.01; 356 } 357 310 358 .speed-doctor-tabs { 311 359 margin: 0 0 20px; -
imzura-site-check/tags/1.3.0/assets/admin.js
r3797250 r3797325 1 // imzura Site Check by imzura.com 1 2 ( function () { 2 3 'use strict'; … … 13 14 const cacheBox = document.getElementById( 'speed-doctor-cache' ); 14 15 const tabs = Array.from( document.querySelectorAll( '.speed-doctor-tabs [role="tab"]' ) ); 16 const tidyBox = document.getElementById( 'speed-doctor-tidy' ); 15 17 let fixes = data.fixes || []; 18 let tidy = data.tidy || null; 19 let tidyNote = ''; 16 20 let lastReport = data.report; 17 21 let busy = false; … … 98 102 const fix = finding.fix && fixes.find( ( item ) => item.id === finding.fix ); 99 103 100 if ( fix && fix.state === 'on' ) { 104 if ( finding.fix === 'tidy:revisions' && tidy ) { 105 card.append( el( 'p', '', text.tidy_card ) ); 106 } else if ( finding.fix === 'tidy:revisions' ) { 107 const actions = el( 'div', 'speed-doctor-actions' ); 108 actions.append( tidyButton( text.tidy_go, 'run', true ) ); 109 card.append( actions ); 110 } else if ( fix && fix.state === 'on' ) { 101 111 card.append( el( 'p', '', text.fixed ) ); 102 112 } else if ( fix && fix.state !== 'unavailable' ) { … … 125 135 const thead = el( 'thead' ); 126 136 const body = el( 'tbody' ); 127 const read = report.pages.filter( ( page ) => page.read ).length;128 129 [ text.owner, text. files, text.owner_kb, text.owner_pages ].forEach( ( label ) => top.append( el( 'th', '', label ) ) );137 const read = report.pages.filter( ( page ) => page.read && ! page.admin ).length; 138 139 [ text.owner, text.owner_ms, text.files, text.owner_kb, text.owner_pages ].forEach( ( label ) => top.append( el( 'th', '', label ) ) ); 130 140 thead.append( top ); 131 141 … … 134 144 row.append( 135 145 el( 'td', '', owner.kind === 'theme' ? fill( text.owner_theme, [ owner.name ] ) : owner.name ), 146 el( 'td', '', owner.ms === null || owner.ms === undefined ? '—' : fill( text.owner_code, [ String( owner.ms ), String( owner.queries ) ] ) ), 136 147 el( 'td', '', String( owner.files ) ), 137 148 el( 'td', '', owner.kb + ' ' + text.kb ), … … 184 195 const started = await call( '/scan', 'POST' ); 185 196 const targets = started.targets; 197 const pages = {}; 198 const busy = {}; 199 let reading = ''; 200 let watcher = null; 201 202 try { 203 watcher = new PerformanceObserver( ( list ) => list.getEntries().forEach( ( entry ) => ( entry.scripts || [] ).forEach( ( script ) => { 204 const url = String( script.sourceURL || '' ).split( /[?#]/ )[ 0 ]; 205 206 if ( reading && url && script.windowAttribution === 'descendant' ) { 207 busy[ reading ][ url ] = ( busy[ reading ][ url ] || 0 ) + script.duration; 208 } 209 } ) ) ); 210 watcher.observe( { type: 'long-animation-frame' } ); 211 } catch ( error ) { 212 watcher = null; 213 } 186 214 187 215 for ( let i = 0; i < targets.length; i++ ) { 188 216 status.textContent = fill( text.reading, [ String( i + 1 ), String( targets.length ), targets[ i ].label ] ); 189 try { 190 const page = await fetch( targets[ i ].url, { credentials: 'omit', cache: 'no-store', redirect: 'follow' } ); 191 await page.text(); 192 } catch ( error ) { 193 // A page that cannot be opened is reported as not read; the rest of the check goes on. 194 } 217 reading = targets[ i ].kind; 218 busy[ reading ] = {}; 219 220 const seen = await open( Object.assign( { screen: 'phone', scan: true }, targets[ i ] ) ); 221 222 if ( seen ) { 223 pages[ reading ] = { errors: seen.errors, sources: seen.sources, look: Object.assign( {}, seen.look, { busy: Object.keys( busy[ reading ] ).map( ( url ) => ( { url: url.slice( 0, 300 ), ms: Math.round( busy[ reading ][ url ] ) } ) ).sort( ( a, b ) => b.ms - a.ms ).slice( 0, 20 ) } ) }; 224 } 225 } 226 227 reading = ''; 228 229 if ( watcher ) { 230 watcher.disconnect(); 195 231 } 196 232 197 233 status.textContent = text.judging; 198 const seen = await headersOf( { page: data.cache.home, asset: data.asset } ); 234 const urls = { page: data.cache.home, asset: data.asset }; 235 236 if ( data.log ) { 237 urls.log = data.log; 238 } 239 240 const seen = await headersOf( urls ); 241 seen.pages = pages; 199 242 const report = await call( '/report', 'POST', { tokens: targets.map( ( target ) => target.token ), seen: seen } ); 200 243 show( report ); … … 206 249 } 207 250 208 // Compression and browser caching are added by the server after WordPress has finished: only the browser sees them.209 251 async function headersOf( urls ) { 210 252 const seen = {}; … … 212 254 for ( const [ kind, url ] of Object.entries( urls ) ) { 213 255 try { 214 const answer = await fetch( url, { credentials: 'omit', cache: 'no-store' } );256 const answer = await fetch( url, { credentials: 'omit', cache: 'no-store', method: kind === 'log' ? 'HEAD' : 'GET' } ); 215 257 await answer.arrayBuffer(); 216 seen[ kind ] = { };258 seen[ kind ] = { status: answer.status }; 217 259 [ 'content-encoding', 'cache-control', 'expires', 'date' ].forEach( ( name ) => { 218 260 if ( answer.headers.has( name ) ) { … … 221 263 } ); 222 264 } catch ( error ) { 223 // The check goes on without what this file would have shown.224 265 } 225 266 } … … 271 312 } ); 272 313 314 if ( fix.excused ) { 315 card.append( el( 'p', 'speed-doctor-note', fill( text.excused, [ String( fix.excused ) ] ) ) ); 316 } 317 273 318 if ( fix.problems.length ) { 274 319 const list = el( 'ul', 'speed-doctor-facts' ); … … 280 325 const table = el( 'table', 'speed-doctor-proof' ); 281 326 const top = el( 'tr' ); 282 // A player the fix holds back loads inside its own frame, which the page weight does not count: show the frames when they changed.283 327 const framed = fix.pages.some( ( page ) => page.frames && page.frames[ 0 ] !== page.frames[ 1 ] ); 284 [ text.page, text.calls, text.files, text.weight, text.blocking ].concat( framed ? [ text.frames ] : [] ).forEach( ( label ) => top.append( el( 'th', '', label ) ) ); 328 const held = fix.pages.some( ( page ) => page.lcp && page.lcp[ 0 ] !== page.lcp[ 1 ] ); 329 const sized = fix.pages.some( ( page ) => page.sizes && page.sizes[ 0 ] !== page.sizes[ 1 ] ); 330 const hosted = fix.pages.some( ( page ) => page.hosts && page.hosts[ 0 ] !== page.hosts[ 1 ] ); 331 [ text.page, text.calls, text.files, text.weight, text.blocking ].concat( framed ? [ text.frames ] : [], held ? [ text.lcp ] : [], sized ? [ text.sizes ] : [], hosted ? [ text.hosts ] : [] ).forEach( ( label ) => top.append( el( 'th', '', label ) ) ); 285 332 table.append( top ); 286 333 fix.pages.forEach( ( page ) => { … … 290 337 row.append( pair( page.frames || [ 0, 0 ] ) ); 291 338 } 339 if ( held ) { 340 row.append( pair( page.lcp || [ 0, 0 ] ) ); 341 } 342 if ( sized ) { 343 row.append( pair( page.sizes || [ 0, 0 ] ) ); 344 } 345 if ( hosted ) { 346 row.append( pair( page.hosts || [ 0, 0 ] ) ); 347 } 292 348 table.append( row ); 293 349 } ); … … 339 395 } 340 396 } ); 341 } 342 343 // Shows one tab and remembers it in the address, so a reload or a shared link opens the same tab. 397 398 askReview( rescue ); 399 } 400 401 function askReview( working ) { 402 const shown = document.getElementById( 'speed-doctor-review' ); 403 const help = document.querySelector( '.speed-doctor-help' ); 404 405 if ( shown ) { 406 shown.remove(); 407 } 408 409 if ( ! working || ! help || ! data.review || ! data.review.due ) { 410 return; 411 } 412 413 const box = el( 'section', 'speed-doctor-review' ); 414 const go = el( 'a', 'button button-primary', text.review_go ); 415 const no = el( 'button', 'button-link', text.review_no ); 416 const done = () => { 417 data.review.due = false; 418 box.remove(); 419 call( '/review', 'POST' ).catch( () => {} ); 420 }; 421 422 box.id = 'speed-doctor-review'; 423 go.href = data.review.url; 424 go.target = '_blank'; 425 go.rel = 'noopener'; 426 no.type = 'button'; 427 go.addEventListener( 'click', done ); 428 no.addEventListener( 'click', done ); 429 box.append( el( 'p', '', text.review_ask ), go, no ); 430 help.before( box ); 431 } 432 344 433 function activate( name, focus ) { 345 434 if ( ! tabs.some( ( tab ) => tab.dataset.tab === name ) ) { … … 380 469 el( 'p', 'speed-doctor-note', text.cache_lead ), 381 470 el( 'p', '', cache.plugins.length ? fill( text.cache_found, [ cache.plugins.join( ', ' ) ] ) : text.cache_none ), 471 ...( cache.off && cache.off.length ? [ el( 'p', '', fill( text.cache_off, [ cache.off.join( ', ' ) ] ) ) ] : [] ), 382 472 el( 'p', '', cache.ready ? text.cache_ready : text.cache_unset ), 383 473 actions, … … 386 476 } 387 477 388 // Opens the home page twice the way a first-time visitor would (no cookies) and reports what the server said.389 478 async function visit( result, button ) { 390 479 const headers = [ 'age', 'cf-cache-status', 'x-cache', 'x-cache-status', 'x-litespeed-cache', 'x-proxy-cache' ]; … … 428 517 function refresh() { 429 518 showFixes(); 519 showTidy(); 430 520 show( lastReport ); 521 } 522 523 function size( bytes ) { 524 return bytes >= 1048576 ? fill( text.mb, [ ( bytes / 1048576 ).toLocaleString( undefined, { maximumFractionDigits: 1 } ) ] ) : Math.max( 1, Math.round( bytes / 1024 ) ).toLocaleString() + ' ' + text.kb; 525 } 526 527 function tidyButton( label, step, primary ) { 528 const go = el( 'button', primary ? 'button button-primary' : 'button', label ); 529 go.type = 'button'; 530 go.disabled = busy; 531 go.addEventListener( 'click', () => cleanUp( step ) ); 532 return go; 533 } 534 535 function showTidy() { 536 tidyBox.replaceChildren(); 537 tidyBox.hidden = ! tidy && ! tidyNote; 538 539 if ( tidyNote ) { 540 tidyBox.append( el( 'p', 'speed-doctor-tidy-note', tidyNote ) ); 541 } 542 543 if ( ! tidy ) { 544 return; 545 } 546 547 const actions = el( 'div', 'speed-doctor-actions' ); 548 let lead = fill( text.tidy_done, [ tidy.rows.toLocaleString(), new Date( tidy.time * 1000 ).toLocaleString(), size( tidy.bytes ) ] ); 549 550 if ( tidy.restoring ) { 551 lead = fill( text.tidy_halfway, [ tidy.restored.toLocaleString() ] ); 552 actions.append( tidyButton( text.tidy_on, 'undo', true ) ); 553 } else if ( ! tidy.done ) { 554 lead = fill( text.tidy_partial, [ tidy.rows.toLocaleString(), tidy.left.toLocaleString() ] ); 555 actions.append( tidyButton( text.tidy_on, 'run', true ), tidyButton( text.tidy_undo, 'undo' ) ); 556 } else { 557 actions.append( tidyButton( text.tidy_undo, 'undo' ) ); 558 } 559 560 actions.append( tidyButton( text.tidy_forget, 'forget' ) ); 561 tidyBox.append( el( 'p', '', lead ), actions ); 562 } 563 564 async function cleanUp( step ) { 565 if ( busy || ( step === 'forget' && ! window.confirm( text.tidy_confirm ) ) ) { 566 return; 567 } 568 569 busy = true; 570 tidyNote = ''; 571 refresh(); 572 573 try { 574 for ( ;; ) { 575 const answer = ( await call( '/tidy/revisions/' + step, 'POST' ) ).tidy; 576 577 if ( step === 'forget' || ! answer ) { 578 tidy = null; 579 tidyNote = text.tidy_forgotten; 580 break; 581 } 582 583 if ( answer.finished ) { 584 tidy = null; 585 tidyNote = fill( text.tidy_restored, [ answer.restored.toLocaleString(), answer.skipped.toLocaleString() ] ); 586 break; 587 } 588 589 tidy = answer; 590 591 if ( step === 'run' && answer.done ) { 592 break; 593 } 594 595 tidyNote = fill( step === 'run' ? text.tidy_removing : text.tidy_restoring, [ ( step === 'run' ? answer.rows : answer.restored ).toLocaleString() ] ); 596 showTidy(); 597 } 598 599 if ( step === 'run' ) { 600 tidyNote = ''; 601 } 602 } catch ( error ) { 603 tidyNote = error.reason || text.tidy_failed; 604 } 605 606 busy = false; 607 refresh(); 431 608 } 432 609 … … 439 616 } 440 617 441 // Opens a page in a hidden frame and waits for assets/probe.js inside it to say what the browser saw.442 618 function open( load ) { 443 619 return new Promise( ( resolve ) => { 444 const frame = el( 'iframe', 'speed-doctor-frame' + ( load.screen === 'phone' ? ' is-phone' : '' ) );620 const frame = el( 'iframe', 'speed-doctor-frame' + ( load.screen === 'phone' ? ' is-phone' : '' ) + ( load.scan ? ' is-scan' : '' ) ); 445 621 let timer = 0; 446 622 … … 463 639 window.addEventListener( 'message', hear ); 464 640 timer = setTimeout( () => finish( null ), 45000 ); 641 642 frame.addEventListener( 'load', () => { 643 clearTimeout( timer ); 644 timer = setTimeout( () => finish( null ), load.admin ? 300 : 20000 ); 645 } ); 465 646 frame.src = load.url; 466 647 document.body.append( frame ); … … 489 670 } 490 671 491 // Compares only the parts of a page that were identical on both untouched views: a part that changes492 // on its own (random products, a rotating banner) says nothing about the fix.493 672 function changed( before, control, after ) { 494 673 const first = groups( before ); … … 531 710 } 532 711 533 // How many boxes are somewhere else, beyond the slack given. Boxes are matched in page order, spot by spot.534 712 function apart( one, other, slack ) { 535 713 let count = Math.abs( one.length - other.length ); … … 547 725 } 548 726 549 // The layout, compared with a little slack: a picture half a pixel taller pushes the rest of the page down550 // by half a pixel, which nobody can see and which is not a change. Spots that move between the two551 // untouched views are left out, as in changed().552 727 function shifted( before, control, after ) { 553 728 const first = groups( before, true ); … … 585 760 } 586 761 587 // Listeners both untouched views agree on that are gone with the fix, although the element they were on is588 // still there. A listener that left together with its element is a change of the page, and is caught as one.589 762 function deaf( before, control, after ) { 590 763 const count = ( list ) => ( list || [] ).reduce( ( map, key ) => map.set( key, ( map.get( key ) || 0 ) + 1 ), new Map() ); … … 608 781 } 609 782 610 function judge( page, problems, numbers ) {783 function judge( page, problems, numbers, phone ) { 611 784 const before = page.seen.before; 612 785 const control = page.seen.control; … … 618 791 } 619 792 620 added( before.errors, control.errors, after.errors ).slice( 0, 3 ).forEach( ( error ) => problems.push( { label: page.label, kind: 'error', detail: error } ) );793 added( before.errors, control.errors, after.errors ).slice( 0, 3 ).forEach( ( error ) => problems.push( { label: page.label, kind: 'error', detail: error, file: ( after.sources || {} )[ error ] || '' } ) ); 621 794 622 795 const words = changed( before.texts, control.texts, after.texts ); … … 626 799 } 627 800 628 // A page that holds still is compared by where things are on the page. One that moves by itself (a629 // random product with a longer name pushes the rest down) is compared by where things are in their parent.630 801 const still = before.boxes.page.length === control.boxes.page.length && shifted( before.boxes.page, control.boxes.page, [] ).steady === before.boxes.page.length; 631 802 const frame = still ? 'page' : 'parent'; … … 644 815 deaf( before, control, after ).slice( 0, 2 ).forEach( ( lost ) => problems.push( { label: page.label, kind: 'listener', detail: lost } ) ); 645 816 646 // The narrow view is there to catch what breaks; what the fix saves is counted once, on the wide one. 817 const held = ( view ) => ( ( view && view.lcp ) || [] ).filter( ( item ) => item.held ).length; 818 const sizeless = ( view ) => ( ( view && view.sizes ) || [] ).length; 819 const hosts = ( view ) => new Set( view.files.map( ( file ) => { 820 try { 821 return new URL( file ).host; 822 } catch ( error ) { 823 return ''; 824 } 825 } ).filter( ( host ) => host && host !== window.location.host ) ); 826 const early = hosts( before ); 827 const plain = hosts( control ); 828 const later = hosts( after ); 829 const steady = Array.from( early ).filter( ( host ) => plain.has( host ) ); 830 831 if ( held( after ) > Math.max( held( before ), held( control ) ) ) { 832 problems.push( { label: page.label, kind: 'lcp', detail: '' } ); 833 } 834 647 835 if ( page.screen === 'phone' ) { 648 836 return; 649 837 } 650 838 651 // Scripts and styles, counted on what both untouched views agree on, so a file that comes and goes 652 // by itself is not credited to the fix. 653 const code = ( view ) => view.files.filter( ( file ) => /\.(js|css)$/.test( file ) ); 839 const code = ( view ) => view.files.filter( ( file ) => /\.(js|css)$/.test( file ) || /^https?:\/\/fonts\.googleapis\.com\/css2?$/.test( file ) ); 654 840 const files = both( code( before ), code( control ) ); 655 841 const kept = new Set( after.files ); … … 665 851 blocking: [ control.blocking || 0, after.blocking || 0 ], 666 852 frames: [ control.frames || 0, after.frames || 0 ], 853 lcp: [ held( control ) + held( phone && phone.seen.control ), held( after ) + held( phone && phone.seen.after ) ], 854 sizes: [ sizeless( control ) + sizeless( phone && phone.seen.control ), sizeless( after ) + sizeless( phone && phone.seen.after ) ], 855 hosts: [ steady.length, steady.filter( ( host ) => later.has( host ) ).length + Array.from( later ).filter( ( host ) => ! early.has( host ) && ! plain.has( host ) ).length ], 667 856 } ); 668 857 } … … 677 866 678 867 try { 679 say( fix, text.starting ); 680 681 const plan = await call( '/fixes/' + fix.id + '/trial', 'POST' ); 682 const seen = []; 683 let prepared = ! plan.evidence; 684 685 for ( let i = 0; i < plan.loads.length; i++ ) { 686 const load = plan.loads[ i ]; 687 688 // The untouched views are in: the fix gets what they showed before any view with it is opened. 689 if ( ! prepared && load.step === 'after' ) { 690 const widest = new Map(); 691 692 seen.forEach( ( page ) => Object.keys( page.seen ).forEach( ( step ) => ( ( page.seen[ step ] || {} )[ plan.evidence ] || [] ).forEach( ( item ) => { 693 const known = widest.get( item.src ); 694 695 if ( ! known || item.shown > known.shown ) { 696 widest.set( item.src, item ); 697 } 698 } ) ) ); 699 700 say( fix, text.preparing ); 701 await call( '/fixes/' + fix.id + '/prepare', 'POST', { evidence: Array.from( widest.values() ) } ); 702 prepared = true; 703 } 704 let page = seen.find( ( item ) => item.kind === load.kind ); 705 706 if ( ! page ) { 707 page = { kind: load.kind, label: load.label, screen: load.screen, seen: {} }; 708 seen.push( page ); 709 } 710 711 say( fix, fill( text.testing, [ String( i + 1 ), String( plan.loads.length ), load.label ] ) ); 712 page.seen[ load.step ] = await open( load ); 713 } 714 715 say( fix, text.deciding ); 716 717 const problems = []; 718 const numbers = []; 719 seen.forEach( ( page ) => judge( page, problems, numbers ) ); 720 721 const answer = await call( '/fixes/' + fix.id + '/verdict', 'POST', { tokens: plan.loads.map( ( load ) => load.token ), pages: numbers, problems: problems } ); 722 fixes = answer.fixes; 868 for ( let round = 0; ; round++ ) { 869 say( fix, round ? text.aside : text.starting ); 870 871 const plan = await call( '/fixes/' + fix.id + '/trial', 'POST' ); 872 const seen = []; 873 let prepared = ! plan.evidence; 874 875 for ( let i = 0; i < plan.loads.length; i++ ) { 876 const load = plan.loads[ i ]; 877 878 if ( ! prepared && load.step === 'after' ) { 879 const widest = new Map(); 880 881 seen.forEach( ( page ) => Object.keys( page.seen ).forEach( ( step ) => ( ( page.seen[ step ] || {} )[ plan.evidence ] || [] ).forEach( ( item ) => { 882 const key = item.src + ' ' + ( item.page || '' ); 883 const known = widest.get( key ); 884 885 if ( ! known || item.shown > known.shown ) { 886 widest.set( key, item ); 887 } 888 } ) ) ); 889 890 say( fix, text.preparing ); 891 await call( '/fixes/' + fix.id + '/prepare', 'POST', { evidence: Array.from( widest.values() ) } ); 892 prepared = true; 893 } 894 let page = seen.find( ( item ) => item.kind === load.kind ); 895 896 if ( ! page ) { 897 page = { kind: load.kind, label: load.label, screen: load.screen, seen: {} }; 898 seen.push( page ); 899 } 900 901 say( fix, fill( text.testing, [ String( i + 1 ), String( plan.loads.length ), load.label ] ) ); 902 page.seen[ load.step ] = await open( load ); 903 } 904 905 say( fix, text.deciding ); 906 907 const problems = []; 908 const numbers = []; 909 seen.forEach( ( page ) => judge( page, problems, numbers, seen.find( ( item ) => item.kind === page.kind + '@phone' ) ) ); 910 911 const answer = await call( '/fixes/' + fix.id + '/verdict', 'POST', { tokens: plan.loads.map( ( load ) => load.token ), pages: numbers, problems: problems } ); 912 const outcome = answer.fixes.find( ( item ) => item.id === fix.id ); 913 fixes = answer.fixes; 914 915 if ( ! outcome || ! outcome.aside || round >= 3 ) { 916 break; 917 } 918 } 919 723 920 busy = false; 724 921 refresh(); … … 761 958 } ); 762 959 960 window.speedDoctor = { 961 fixes: () => fixes.slice(), 962 busy: () => busy, 963 trial: async ( id ) => { 964 const fix = fixes.find( ( item ) => item.id === id ); 965 966 if ( fix && ! busy ) { 967 await trial( fix ); 968 } 969 970 return fixes.find( ( item ) => item.id === id ) || null; 971 }, 972 tidy: async ( step ) => { 973 await cleanUp( step ); 974 975 return { tidy: tidy, note: tidyNote }; 976 }, 977 }; 978 763 979 button.addEventListener( 'click', scan ); 764 980 activate( window.location.hash.slice( 1 ) ); 765 981 showFixes(); 766 982 showCache(); 983 showTidy(); 767 984 show( data.report ); 768 985 } )(); -
imzura-site-check/tags/1.3.0/assets/probe.js
r3797250 r3797325 7 7 8 8 const errors = []; 9 const sources = {}; 9 10 const calls = []; 10 11 const heard = []; … … 12 13 let fresh = true; 13 14 14 // Which elements scripts listen on. It is only noted; the listener is added as asked. An element counts15 // once per kind of event: a script that sets itself up again whenever the page changes adds the same16 // listener many times, and how many times depends on what else is on the page.17 15 const listen = EventTarget.prototype.addEventListener; 18 16 const told = new WeakMap(); … … 52 50 } 53 51 54 // The page runs as a first-time visitor: it cannot read or change what the administrator's browser holds.55 52 try { 56 53 Object.defineProperty( document, 'cookie', { configurable: true, get: () => '', set: () => {} } ); … … 61 58 } 62 59 63 // The same "random" choices on every view, so a page that shuffles its content can be compared with itself.64 60 let seed = 1; 65 61 … … 71 67 }; 72 68 73 // The same failure reaches the browser as "Uncaught Error: X" or as a rejected promise saying "X", 74 // depending on timing; both are recorded as "X" so one is not mistaken for a new error. 75 function note( kind, detail ) { 69 const painted = { lcp: null, shift: 0, shifters: [] }; 70 71 function observe( type, take ) { 72 try { 73 if ( ( PerformanceObserver.supportedEntryTypes || [] ).indexOf( type ) !== -1 ) { 74 new PerformanceObserver( ( list ) => list.getEntries().forEach( take ) ).observe( { type: type, buffered: true } ); 75 } 76 } catch ( error ) { 77 } 78 } 79 80 if ( window.PerformanceObserver ) { 81 observe( 'largest-contentful-paint', ( entry ) => { 82 painted.lcp = entry; 83 } ); 84 } 85 86 if ( probe.scan && window.PerformanceObserver ) { 87 observe( 'layout-shift', ( entry ) => { 88 if ( ! entry.hadRecentInput ) { 89 painted.shift += entry.value; 90 ( entry.sources || [] ).forEach( ( source ) => source.node && painted.shifters.push( [ entry.value, source.node ] ) ); 91 } 92 } ); 93 } 94 95 function note( kind, detail, file ) { 76 96 const said = String( detail ).replace( /^Uncaught\s+/, '' ).replace( /^\(in promise\)\s+/, '' ).replace( /^[A-Za-z]*Error:\s+/, '' ); 97 const key = kind + ': ' + said.slice( 0, 160 ); 77 98 78 99 if ( errors.length < 40 ) { 79 errors.push( kind + ': ' + said.slice( 0, 160 ) ); 100 errors.push( key ); 101 } 102 103 if ( file && ! sources[ key ] ) { 104 sources[ key ] = String( file ).split( /[?#]/ )[ 0 ]; 80 105 } 81 106 } … … 87 112 88 113 if ( target && target !== window && ( target.src || target.href ) ) { 89 note( 'not loaded', String( target.src || target.href ).split( /[?#]/ )[ 0 ] );114 note( 'not loaded', String( target.src || target.href ).split( /[?#]/ )[ 0 ], target.src || target.href ); 90 115 } else if ( event.message ) { 91 note( 'script', event.message );116 note( 'script', event.message, event.filename ); 92 117 } 93 118 }, … … 126 151 request.setRequestHeader( probe.header, probe.token ); 127 152 } catch ( error ) { 128 // The request was not opened; the browser reports that itself.129 153 } 130 154 } … … 166 190 const paths = new WeakMap(); 167 191 168 // Where an element sits in the page, by tag names only, so the same spot gets the same name on every view.169 192 function path( node ) { 170 193 if ( ! node || node === document.body ) { … … 196 219 } 197 220 198 // Every visible element twice: where it is on the page, and where it is inside its parent. The second199 // survives a part of the page above it changing height by itself.200 221 function boxes() { 201 222 const all = document.body.getElementsByTagName( '*' ); … … 210 231 known.set( all[ i ], box ); 211 232 212 // A box of a single pixel is how text meant for screen readers is hidden: nobody sees it move.213 233 if ( box.width >= 1 && box.height >= 1 && ( box.width > 1 || box.height > 1 ) ) { 214 234 found.page.push( path( all[ i ] ) + '\t' + round( [ box.left + window.scrollX, box.top + window.scrollY, box.width, box.height ] ) ); … … 220 240 } 221 241 222 // Every picture from this site, with the width it takes on the page.223 242 function images() { 224 243 const found = []; … … 235 254 236 255 return found; 256 } 257 258 function sizes() { 259 const found = []; 260 261 for ( let i = 0; i < document.images.length && found.length < 300; i++ ) { 262 const image = document.images[ i ]; 263 const box = image.getBoundingClientRect(); 264 const src = String( image.currentSrc || image.src || '' ); 265 266 if ( box.width >= 2 && box.height >= 2 && src && src.indexOf( 'data:' ) !== 0 && ( ! image.hasAttribute( 'width' ) || ! image.hasAttribute( 'height' ) ) ) { 267 found.push( { src: String( image.src || src ).split( /[?#]/ )[ 0 ], shown: Math.ceil( box.width ) } ); 268 } 269 } 270 271 return found; 272 } 273 274 function google() { 275 return Array.prototype.map.call( document.querySelectorAll( 'link[href*="fonts.googleapis.com/css"]' ), ( link ) => ( { src: String( link.href ).slice( 0, 1000 ) } ) ).slice( 0, 10 ); 276 } 277 278 function main() { 279 const entry = painted.lcp || largest(); 280 const element = entry && entry.element; 281 282 if ( ! element || element.nodeName !== 'IMG' ) { 283 return []; 284 } 285 286 const src = String( element.currentSrc || element.src || '' ).split( /[?#]/ )[ 0 ]; 287 288 if ( ! src || ! own( src ) ) { 289 return []; 290 } 291 292 return [ { 293 src: src, 294 page: window.location.pathname, 295 shown: Math.ceil( element.getBoundingClientRect().width ), 296 held: element.getAttribute( 'loading' ) === 'lazy' || element.getAttribute( 'fetchpriority' ) !== 'high', 297 featured: element.classList.contains( 'wp-post-image' ), 298 narrow: window.innerWidth < 600, 299 } ]; 237 300 } 238 301 … … 251 314 } 252 315 253 // What scripts listen for on things the page has: each listener put on an element of the page, and each254 // rule of the kind "a click on anything matching X" (jQuery keeps those on the body or the document)255 // whose X matches something. A script that only acts when the visitor does something shows up nowhere else.256 316 function ears() { 257 317 const found = []; … … 289 349 } 290 350 291 // How many elements sit at each spot, so a listener that went with its element is told from one that was lost.292 351 function spots() { 293 352 const all = document.body.getElementsByTagName( '*' ); … … 302 361 } 303 362 363 function describe( node ) { 364 if ( ! node || ! node.nodeName ) { 365 return ''; 366 } 367 368 let text = node.nodeName.toLowerCase(); 369 370 if ( node.id ) { 371 text += '#' + node.id; 372 } else if ( typeof node.className === 'string' && node.className.trim() ) { 373 text += '.' + node.className.trim().split( /\s+/ ).slice( 0, 2 ).join( '.' ); 374 } 375 376 return text.slice( 0, 80 ); 377 } 378 379 function plain( url ) { 380 return String( url || '' ).split( /[?#]/ )[ 0 ].slice( 0, 300 ); 381 } 382 383 function largest() { 384 let found = null; 385 let most = 0; 386 387 for ( let i = 0; i < document.images.length; i++ ) { 388 const box = document.images[ i ].getBoundingClientRect(); 389 const area = Math.max( 0, Math.min( box.right, window.innerWidth ) - Math.max( box.left, 0 ) ) * Math.max( 0, Math.min( box.bottom, window.innerHeight ) - Math.max( box.top, 0 ) ); 390 391 if ( area > most && box.top + window.scrollY < window.innerHeight ) { 392 most = area; 393 found = document.images[ i ]; 394 } 395 } 396 397 return found ? { element: found, url: found.currentSrc || found.src, guessed: true } : null; 398 } 399 400 function usage() { 401 const found = []; 402 const states = /::?(before|after|hover|focus|focus-visible|focus-within|active|visited|link|any-link|placeholder|selection|marker|backdrop|first-letter|first-line|checked|disabled|enabled|target|invalid|valid|required|optional|indeterminate|placeholder-shown|read-only|read-write|open|popover-open|-webkit-[\w-]+|-moz-[\w-]+|-ms-[\w-]+)(\([^)]*\))?/g; 403 let budget = 20000; 404 405 function walk( list, count ) { 406 for ( let i = 0; i < list.length && budget > 0; i++ ) { 407 const rule = list[ i ]; 408 409 if ( rule.selectorText ) { 410 budget--; 411 count.rules++; 412 count.used += rule.selectorText.split( ',' ).some( ( selector ) => { 413 try { 414 return document.querySelector( selector.replace( states, '' ).trim() || '*' ) !== null; 415 } catch ( error ) { 416 return true; 417 } 418 } ) ? 1 : 0; 419 } else if ( rule.cssRules ) { 420 walk( rule.cssRules, count ); 421 } 422 } 423 } 424 425 Array.prototype.forEach.call( document.styleSheets, ( sheet ) => { 426 let rules = null; 427 428 if ( ! sheet.href || ! own( sheet.href ) || found.length >= 60 ) { 429 return; 430 } 431 432 try { 433 rules = sheet.cssRules; 434 } catch ( error ) { 435 return; 436 } 437 438 const count = { url: plain( sheet.href ), rules: 0, used: 0 }; 439 walk( rules, count ); 440 found.push( count ); 441 } ); 442 443 return found; 444 } 445 446 function moving() { 447 const found = []; 448 const known = new Set(); 449 450 ( document.getAnimations ? document.getAnimations() : [] ).forEach( ( animation ) => { 451 const effect = animation.effect; 452 const target = effect && effect.target; 453 const timing = effect && effect.getComputedTiming ? effect.getComputedTiming() : {}; 454 455 if ( found.length >= 10 || ! target || timing.iterations !== Infinity || animation.playState !== 'running' ) { 456 return; 457 } 458 459 const box = target.getBoundingClientRect(); 460 const what = describe( target ) + ( animation.animationName ? ' (' + String( animation.animationName ).slice( 0, 40 ) + ')' : '' ); 461 462 if ( box.width * box.height >= 2500 && box.bottom > 0 && box.top < window.innerHeight && ! known.has( what ) ) { 463 known.add( what ); 464 found.push( what ); 465 } 466 } ); 467 468 return found; 469 } 470 471 function srcsets() { 472 const found = []; 473 474 for ( let i = 0; i < document.images.length && found.length < 60; i++ ) { 475 const image = document.images[ i ]; 476 const width = image.getBoundingClientRect().width; 477 const options = []; 478 479 String( image.getAttribute( 'srcset' ) || '' ).split( /,\s+/ ).forEach( ( part ) => { 480 const bits = part.trim().split( /\s+/ ); 481 const size = bits.length === 2 && /^\d+w$/.test( bits[ 1 ] ) ? parseInt( bits[ 1 ], 10 ) : 0; 482 483 if ( size && options.length < 16 ) { 484 options.push( [ new URL( bits[ 0 ], document.baseURI ).href.split( /[?#]/ )[ 0 ], size ] ); 485 } 486 } ); 487 488 if ( width >= 2 && options.length > 1 && ! image.closest( 'picture' ) ) { 489 found.push( { slot: Math.round( width * 10 ) / 10, options: options } ); 490 } 491 } 492 493 return found; 494 } 495 496 function families( fonts ) { 497 const loaded = new Map(); 498 const chars = new Map(); 499 const styles = new Map(); 500 const walker = document.createTreeWalker( document.body, NodeFilter.SHOW_TEXT ); 501 const skip = [ 'SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE' ]; 502 let total = 0; 503 let node; 504 let seen = 0; 505 506 fonts.forEach( ( font ) => loaded.set( font.family.toLowerCase(), font.family ) ); 507 508 while ( ( node = walker.nextNode() ) && seen++ < 5000 ) { 509 const parent = node.parentElement; 510 const length = node.nodeValue.replace( /\s+/g, '' ).length; 511 512 if ( ! length || ! parent || skip.indexOf( parent.nodeName ) !== -1 || ! parent.getClientRects().length ) { 513 continue; 514 } 515 516 if ( ! styles.has( parent ) ) { 517 styles.set( parent, String( getComputedStyle( parent ).fontFamily.split( ',' )[ 0 ] || '' ).trim().replace( /^["']|["']$/g, '' ).toLowerCase() ); 518 } 519 520 const family = loaded.get( styles.get( parent ) ); 521 522 total += length; 523 524 if ( family ) { 525 chars.set( family, ( chars.get( family ) || 0 ) + length ); 526 } 527 } 528 529 return { total: total, used: Array.from( chars, ( pair ) => ( { family: pair[ 0 ], chars: pair[ 1 ] } ) ).slice( 0, 20 ) }; 530 } 531 532 function look() { 533 const fonts = []; 534 const lazyTop = []; 535 const sizeless = []; 536 const lcp = painted.lcp || largest(); 537 const element = lcp && lcp.element; 538 539 if ( document.fonts && document.fonts.forEach ) { 540 document.fonts.forEach( ( face ) => { 541 if ( face.status === 'loaded' && fonts.length < 60 ) { 542 fonts.push( { family: String( face.family ).replace( /^["']|["']$/g, '' ), weight: String( face.weight ), style: String( face.style ), display: String( face.display || '' ) } ); 543 } 544 } ); 545 } 546 547 for ( let i = 0; i < document.images.length; i++ ) { 548 const image = document.images[ i ]; 549 const box = image.getBoundingClientRect(); 550 const src = plain( image.currentSrc || image.src ); 551 552 if ( box.width < 2 || box.height < 2 || ! src || src.indexOf( 'data:' ) === 0 ) { 553 continue; 554 } 555 556 if ( image.getAttribute( 'loading' ) === 'lazy' && box.top + window.scrollY < window.innerHeight && lazyTop.length < 10 ) { 557 lazyTop.push( src ); 558 } 559 560 if ( ( ! image.hasAttribute( 'width' ) || ! image.hasAttribute( 'height' ) ) && sizeless.length < 20 ) { 561 sizeless.push( src ); 562 } 563 } 564 565 return { 566 resources: performance.getEntriesByType( 'resource' ).slice( 0, 400 ).map( ( entry ) => ( { 567 url: entry.name.indexOf( '//fonts.googleapis.com/' ) !== -1 ? entry.name.slice( 0, 500 ) : plain( entry.name ), 568 type: entry.initiatorType, 569 kb: Math.round( ( entry.encodedBodySize || 0 ) / 102.4 ) / 10, 570 blocking: entry.renderBlockingStatus === 'blocking', 571 } ) ), 572 fonts: fonts, 573 preloads: Array.prototype.map.call( document.querySelectorAll( 'link[rel="preload"][as="font"]' ), ( link ) => ( { url: plain( link.href ), crossorigin: link.hasAttribute( 'crossorigin' ) } ) ).slice( 0, 20 ), 574 lcp: lcp 575 ? { 576 kind: lcp.url ? 'image' : 'text', 577 tag: element ? element.nodeName.toLowerCase() : '', 578 src: plain( lcp.url ), 579 lazy: !! ( element && element.getAttribute && element.getAttribute( 'loading' ) === 'lazy' ), 580 priority: element && element.getAttribute ? String( element.getAttribute( 'fetchpriority' ) || '' ) : '', 581 what: describe( element ), 582 guessed: !! lcp.guessed, 583 } 584 : null, 585 shift: Math.round( painted.shift * 1000 ) / 1000, 586 shifters: painted.shifters.sort( ( a, b ) => b[ 0 ] - a[ 0 ] ).map( ( item ) => describe( item[ 1 ] ) ).filter( ( what, i, all ) => what && all.indexOf( what ) === i ).slice( 0, 3 ), 587 lazyTop: lazyTop, 588 sizeless: sizeless, 589 moving: moving(), 590 srcsets: srcsets(), 591 text: families( fonts ), 592 styles: usage(), 593 }; 594 } 595 304 596 function report() { 305 597 const files = []; 306 598 let bytes = 0; 307 // Files the browser had to have before it could show anything (Chromium reports this; elsewhere it stays 0).308 599 let blocking = 0; 309 600 … … 319 610 { 320 611 speedDoctor: probe.token, 321 report: { fresh: fresh, errors: errors, calls: calls, files: files, kb: Math.round( bytes / 1024 ), blocking: blocking, frames: document.querySelectorAll( 'iframe[src]:not([srcdoc])' ).length, texts: texts(), boxes: boxes(), marks: marks(), images: images(), ears: ears(), spots: spots()},612 report: { fresh: fresh, errors: errors, sources: sources, calls: calls, files: files, kb: Math.round( bytes / 1024 ), blocking: blocking, frames: document.querySelectorAll( 'iframe[src]:not([srcdoc])' ).length, texts: texts(), boxes: boxes(), marks: marks(), images: images(), lcp: main(), sizes: sizes(), google: google(), ears: ears(), spots: spots(), look: probe.scan ? look() : undefined }, 322 613 }, 323 614 probe.origin … … 325 616 } 326 617 327 // Report once the page has gone quiet: nothing in flight, fonts in, and nothing new fetched for a second.328 618 function settle() { 329 619 let last = -1; -
imzura-site-check/tags/1.3.0/imzura-site-check.php
r3797250 r3797325 2 2 /** 3 3 * Plugin Name: imzura Site Check – Speed & Site Optimization 4 * Plugin URI: https://imzura.com/products/speed-doctor 4 5 * Description: Finds what slows your site down, on the front end, in the admin and in WooCommerce, and proves every fix with a measurement. 5 * Version: 1. 0.06 * Version: 1.3.0 6 7 * Requires at least: 6.2 7 8 * Requires PHP: 7.4 … … 17 18 defined( 'ABSPATH' ) || exit; 18 19 19 define( 'SPEED_DOCTOR_VERSION', '1. 0.0' );20 define( 'SPEED_DOCTOR_VERSION', '1.3.0' ); 20 21 define( 'SPEED_DOCTOR_FILE', __FILE__ ); 21 22 define( 'SPEED_DOCTOR_DIR', plugin_dir_path( __FILE__ ) ); … … 23 24 24 25 spl_autoload_register( 25 static function ( $ class) {26 if ( strpos( $ class, 'SpeedDoctor\\' ) !== 0 ) {26 static function ( $wanted ) { 27 if ( strpos( $wanted, 'SpeedDoctor\\' ) !== 0 ) { 27 28 return; 28 29 } 29 30 30 $parts = explode( '\\', substr( $ class, 12 ) );31 $parts = explode( '\\', substr( $wanted, 12 ) ); 31 32 $name = 'class-' . strtolower( str_replace( '_', '-', array_pop( $parts ) ) ) . '.php'; 32 33 $path = SPEED_DOCTOR_DIR . 'includes/' . ( $parts ? strtolower( implode( '/', $parts ) ) . '/' : '' ) . $name; … … 38 39 ); 39 40 41 SpeedDoctor\Profiler::watch(); 42 40 43 add_action( 'plugins_loaded', array( 'SpeedDoctor\\Plugin', 'boot' ) ); 41 44 register_deactivation_hook( __FILE__, array( 'SpeedDoctor\\Fixer', 'retire' ) ); -
imzura-site-check/tags/1.3.0/includes/checks/class-autoload.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Options WordPress loads on every single request, cached page or not. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; -
imzura-site-check/tags/1.3.0/includes/checks/class-delivery.php
r3797250 r3797325 1 1 <?php 2 /** 3 * How the server delivers what the browser asked for: compressed or not, and whether browsers may keep files. 4 * 5 * Compression and browser caching are added by the web server or a CDN after WordPress has finished, so only 6 * the browser sees them. The admin screen reads the response headers of the home page and of one of this 7 * plugin's stylesheets (a plain static file, like any theme's) during the check and hands them over. 8 * 9 * @package SpeedDoctor 10 */ 2 // imzura Site Check by imzura.com 11 3 12 4 namespace SpeedDoctor\Checks; … … 18 10 final class Delivery extends Check { 19 11 20 // A static file that browsers may keep for less than this many seconds is fetched again too often.21 12 const KEEP = 604800; 22 13 … … 87 78 } 88 79 89 /**90 * @return array The headers the browser saw, lower-case names, or an empty array when it saw none.91 */92 80 private function headers( $kind ) { 93 81 if ( empty( $this->seen[ $kind ] ) || ! is_array( $this->seen[ $kind ] ) ) { … … 113 101 } 114 102 115 /**116 * @return int Seconds the browser may keep the file, from Cache-Control max-age or else Expires.117 */118 103 private function keep( array $headers ) { 119 104 if ( isset( $headers['cache-control'] ) ) { -
imzura-site-check/tags/1.3.0/includes/checks/class-environment.php
r3797250 r3797325 1 1 <?php 2 /** 3 * What the site runs on, and what is already speeding it up. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; … … 15 11 16 12 const CACHE_PLUGINS = array( 17 'litespeed-cache' => 'LiteSpeed Cache',18 'wp-rocket' => 'WP Rocket',19 'w3-total-cache' => 'W3 Total Cache',20 'wp-super-cache' => 'WP Super Cache',21 'wp-fastest-cache' => 'WP Fastest Cache',22 'wp-optimize' => 'WP-Optimize',23 'sg-cachepress' => 'Speed Optimizer',24 'breeze' => 'Breeze',25 'flying-press' => 'FlyingPress',26 'nitropack' => 'NitroPack',27 'cache-enabler' => 'Cache Enabler',13 'litespeed-cache' => 'LiteSpeed Cache', 14 'wp-rocket' => 'WP Rocket', 15 'w3-total-cache' => 'W3 Total Cache', 16 'wp-super-cache' => 'WP Super Cache', 17 'wp-fastest-cache' => 'WP Fastest Cache', 18 'wp-optimize' => 'WP-Optimize', 19 'sg-cachepress' => 'Speed Optimizer', 20 'breeze' => 'Breeze', 21 'flying-press' => 'FlyingPress', 22 'nitropack' => 'NitroPack', 23 'cache-enabler' => 'Cache Enabler', 28 24 'hummingbird-performance' => 'Hummingbird', 29 25 ); … … 54 50 55 51 $active = array(); 52 $off = array(); 56 53 57 54 foreach ( (array) get_option( 'active_plugins', array() ) as $file ) { 58 55 $slug = strtok( $file, '/' ); 59 56 60 if ( isset( self::CACHE_PLUGINS[ $slug ] ) ) { 57 if ( ! isset( self::CACHE_PLUGINS[ $slug ] ) ) { 58 continue; 59 } 60 61 $setting = self::caching_off( $slug ); 62 63 if ( $setting ) { 64 $off[] = $setting; 65 } else { 61 66 $active[] = self::CACHE_PLUGINS[ $slug ]; 62 67 } 68 } 69 70 if ( $off && ! $active ) { 71 $facts = array(); 72 73 foreach ( $off as $setting ) { 74 $facts[] = $setting['where']; 75 $facts[] = $setting['url']; 76 } 77 78 $findings[] = $this->finding( 79 'cache-off', 80 'front', 81 'medium', 82 /* translators: %s: plugin name */ 83 sprintf( __( '%s is active, but its page caching is switched off', 'imzura-site-check' ), $off[0]['name'] ), 84 __( 'Every visitor\'s page is built anew by WordPress and the plugins, although a ready copy could be sent. Switch page caching on in the plugin\'s settings, then open a few pages logged out to see that they look right; this plugin only reads this setting.', 'imzura-site-check' ), 85 $facts 86 ); 63 87 } 64 88 … … 85 109 return $findings; 86 110 } 111 112 public static function caching_off( $slug ) { 113 $where = ''; 114 $url = ''; 115 116 switch ( $slug ) { 117 case 'wp-super-cache': 118 if ( isset( $GLOBALS['cache_enabled'] ) && ! $GLOBALS['cache_enabled'] ) { 119 $where = __( 'Settings → WP Super Cache → Easy: Caching Off', 'imzura-site-check' ); 120 $url = admin_url( 'options-general.php?page=wpsupercache' ); 121 } 122 break; 123 124 case 'w3-total-cache': 125 $read = array( '\W3TC\Dispatcher', 'config' ); 126 $config = is_callable( $read ) ? call_user_func( $read ) : null; 127 128 if ( is_object( $config ) && is_callable( array( $config, 'get_boolean' ) ) && ! $config->get_boolean( 'pgcache.enabled' ) ) { 129 $where = __( 'Performance → General Settings → Page Cache: Enable is not checked', 'imzura-site-check' ); 130 $url = admin_url( 'admin.php?page=w3tc_general' ); 131 } 132 break; 133 134 case 'wp-fastest-cache': 135 $options = json_decode( (string) get_option( 'WpFastestCache', '' ), true ); 136 137 if ( ! is_array( $options ) || ! isset( $options['wpFastestCacheStatus'] ) ) { 138 $where = __( 'WP Fastest Cache → Settings: Cache System, Enable is not checked', 'imzura-site-check' ); 139 $url = admin_url( 'admin.php?page=wpfastestcacheoptions' ); 140 } 141 break; 142 143 case 'wp-optimize': 144 $read = array( 'WPO_Cache_Config', 'instance' ); 145 $config = is_callable( $read ) ? call_user_func( $read ) : null; 146 147 if ( is_object( $config ) && is_callable( array( $config, 'get_option' ) ) && ! $config->get_option( 'enable_page_caching' ) ) { 148 $where = __( 'WP-Optimize → Cache → Page cache: Enable page caching is off', 'imzura-site-check' ); 149 $url = admin_url( 'admin.php?page=wpo_cache' ); 150 } 151 break; 152 153 case 'breeze': 154 $read = array( 'Breeze_Options_Reader', 'get_option_value' ); 155 156 if ( is_callable( $read ) && '1' !== (string) call_user_func( $read, 'breeze-active' ) ) { 157 $where = __( 'Settings → Breeze → Basic Options: Cache System is off', 'imzura-site-check' ); 158 $url = admin_url( 'options-general.php?page=breeze' ); 159 } 160 break; 161 162 case 'hummingbird-performance': 163 $read = array( '\Hummingbird\Core\Settings', 'get_setting' ); 164 165 if ( is_callable( $read ) && ! call_user_func( $read, 'enabled', 'page_cache' ) ) { 166 $where = __( 'Hummingbird → Caching → Page Caching is not active', 'imzura-site-check' ); 167 $url = admin_url( 'admin.php?page=wphb-caching' ); 168 } 169 break; 170 } 171 172 if ( '' === $where ) { 173 return null; 174 } 175 176 return array( 177 'name' => self::CACHE_PLUGINS[ $slug ], 178 'where' => $where, 179 'url' => $url, 180 ); 181 } 87 182 } -
imzura-site-check/tags/1.3.0/includes/checks/class-front-pages.php
r3797250 r3797325 1 1 <?php 2 /** 3 * What each page a visitor opens is built from. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; … … 17 13 const VERY_SLOW_SECONDS = 2.0; 18 14 const HEAVY_PLUGIN_KB = 60; 15 const HEAVY_FILE_KB = array( 16 'styles' => array( 50, 100 ), 17 'scripts' => array( 100, 250 ), 18 ); 19 19 const HEAVY_PAGE_KB = 1024; 20 20 const HEAVY_IMAGE_KB = 300; 21 21 const HEAVY_VIDEO_KB = 1024; 22 22 23 /**24 * Files of WooCommerce that do a job on every page (they record where the visit came from), and the cart25 * script, which has a finding of its own.26 */27 23 const AT_WORK = array( 'sourcebuster-js', 'wc-order-attribution', 'wc-cart-fragments' ); 28 24 29 /**30 * What the pages showed, gathered per subject (slow server, a plugin's unused or heavy files) so that each31 * subject is one finding listing its pages, not one finding per page.32 */33 25 private $seen = array( 34 'slow' => array(), 35 'unused' => array(), 36 'heavy' => array(), 26 'slow' => array(), 27 'unused' => array(), 28 'heavy' => array(), 29 'styles' => array(), 30 'scripts' => array(), 37 31 ); 38 32 … … 53 47 $this->server( $page ); 54 48 $this->plugins( $page, ! $weight ); 49 $this->files( $page ); 55 50 56 51 if ( ! $page['context']['store_page'] && in_array( 'wc-cart-fragments', array_column( $page['scripts'], 'handle' ), true ) ) { … … 59 54 } 60 55 61 $findings = array_merge( $findings, $this->slow(), $this->unused(), $this->heavy() );56 $findings = array_merge( $findings, $this->slow(), $this->unused(), $this->heavy(), $this->heavy_files( 'styles' ), $this->heavy_files( 'scripts' ) ); 62 57 63 58 if ( $fragments ) { … … 130 125 } 131 126 132 /**133 * One finding per plugin that loads files on pages that do not use them.134 */135 127 private function unused() { 136 128 $findings = array(); … … 158 150 } 159 151 160 /**161 * One finding per plugin that adds a lot to pages that may well need it.162 */163 152 private function heavy() { 164 153 $findings = array(); … … 183 172 184 173 return $findings; 174 } 175 176 private function files( array $page ) { 177 foreach ( array( 'styles', 'scripts' ) as $type ) { 178 foreach ( $page[ $type ] as $file ) { 179 if ( null === $file['kb'] || $file['kb'] < self::HEAVY_FILE_KB[ $type ][0] ) { 180 continue; 181 } 182 183 if ( ! isset( $this->seen[ $type ][ $file['handle'] ] ) ) { 184 $this->seen[ $type ][ $file['handle'] ] = array( 185 'file' => isset( $file['file'] ) && '' !== $file['file'] ? $file['file'] : $file['handle'], 186 'owner' => $this->who( $file['owner'] ), 187 'kb' => (int) round( $file['kb'] ), 188 'pages' => 0, 189 ); 190 } 191 192 ++$this->seen[ $type ][ $file['handle'] ]['pages']; 193 } 194 } 195 } 196 197 private function heavy_files( $type ) { 198 $files = array_values( $this->seen[ $type ] ); 199 200 if ( ! $files ) { 201 return array(); 202 } 203 204 usort( 205 $files, 206 static function ( $a, $b ) { 207 return $b['kb'] <=> $a['kb']; 208 } 209 ); 210 211 $facts = array(); 212 $plain = false; 213 214 foreach ( array_slice( $files, 0, 8 ) as $file ) { 215 $plain = $plain || false === strpos( $file['file'], '.min.' ); 216 $facts[] = sprintf( 217 /* translators: 1: file name, 2: who adds it (a plugin, the theme), 3: size, 4: number of pages */ 218 _n( '%1$s (%2$s): %3$s, on %4$s page', '%1$s (%2$s): %3$s, on %4$s pages', $file['pages'], 'imzura-site-check' ), 219 $file['file'], 220 $file['owner'], 221 $this->kb( $file['kb'] ), 222 number_format_i18n( $file['pages'] ) 223 ); 224 } 225 226 $styles = 'styles' === $type; 227 $count = count( $files ); 228 229 if ( 1 === $count ) { 230 $title = $styles 231 /* translators: 1: file name, 2: who adds it (a plugin, the theme), 3: size */ 232 ? sprintf( __( 'A large stylesheet: %1$s from %2$s, %3$s before compression', 'imzura-site-check' ), $files[0]['file'], $files[0]['owner'], $this->kb( $files[0]['kb'] ) ) 233 /* translators: 1: file name, 2: who adds it (a plugin, the theme), 3: size */ 234 : sprintf( __( 'A large script: %1$s from %2$s, %3$s before compression', 'imzura-site-check' ), $files[0]['file'], $files[0]['owner'], $this->kb( $files[0]['kb'] ) ); 235 } else { 236 $title = $styles 237 /* translators: 1: number of stylesheets, 2: size, 3: who adds it (a plugin, the theme) */ 238 ? sprintf( _n( '%1$s large stylesheet, the largest %2$s from %3$s', '%1$s large stylesheets, the largest %2$s from %3$s', $count, 'imzura-site-check' ), number_format_i18n( $count ), $this->kb( $files[0]['kb'] ), $files[0]['owner'] ) 239 /* translators: 1: number of scripts, 2: size, 3: who adds it (a plugin, the theme) */ 240 : sprintf( _n( '%1$s large script, the largest %2$s from %3$s', '%1$s large scripts, the largest %2$s from %3$s', $count, 'imzura-site-check' ), number_format_i18n( $count ), $this->kb( $files[0]['kb'] ), $files[0]['owner'] ); 241 } 242 243 return array( 244 $this->finding( 245 'heavy-' . $type, 246 'front', 247 $files[0]['kb'] >= self::HEAVY_FILE_KB[ $type ][1] ? 'medium' : 'low', 248 $title, 249 $styles 250 ? __( 'A stylesheet linked in the head of a page holds up the first paint until it has loaded, so these are the stylesheets that weigh most on how fast your pages appear. This plugin does not take rules out of them, because which rules a site needs depends on every page, every state of a menu or form and every script, and a few pages cannot show that for sure. Minifying them is safe and is one of the fixes. Beyond that, ask the theme or plugin author whether it can load less, or switch off the parts you do not use in its settings.', 'imzura-site-check' ) 251 : __( 'A phone has to download, read and run each script before the page reacts to a tap, and a script in the head of the page also holds up the first paint. These are the largest scripts the pages load. Minifying them and loading them with defer are safe and are among the fixes. Beyond that, ask the theme or plugin author whether it can load less, or switch off the parts you do not use in its settings.', 'imzura-site-check' ), 252 $facts, 253 $plain ? ( $styles ? 'minify-css' : 'minify-js' ) : ( $styles ? '' : 'defer-js' ) 254 ), 255 ); 185 256 } 186 257 … … 246 317 } 247 318 248 /**249 * What WooCommerce loads on a page without the files that are at work there.250 */251 319 private function idle( array $page, array $handles ) { 252 320 $busy = self::AT_WORK; -
imzura-site-check/tags/1.3.0/includes/checks/class-scheduled-work.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Background jobs that pile up: WP-Cron and Action Scheduler. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; -
imzura-site-check/tags/1.3.0/includes/checks/class-store.php
r3797250 r3797325 1 1 <?php 2 /** 3 * The parts of a WooCommerce store that no page cache covers. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; -
imzura-site-check/tags/1.3.0/includes/class-admin-page.php
r3797250 r3797325 1 1 <?php 2 /** 3 * The one screen the plugin adds to the admin. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 56 52 'cache' => Cache::status(), 57 53 'asset' => SPEED_DOCTOR_URL . 'assets/admin.css', 54 'log' => esc_url_raw( Checks\Housekeeping::log_url() ), 58 55 'rescue' => esc_url_raw( Fixer::rescue_url() ), 56 'tidy' => Tidy::state(), 57 'review' => array( 58 'due' => ! get_user_meta( get_current_user_id(), Rest::REVIEW_META, true ), 59 'url' => 'https://wordpress.org/support/plugin/imzura-site-check/reviews/#new-post', 60 ), 59 61 'text' => array( 60 'scan' => __( 'Check my site', 'imzura-site-check' ), 61 'again' => __( 'Check again', 'imzura-site-check' ), 62 'starting' => __( 'Starting', 'imzura-site-check' ), 62 'review_ask' => __( 'A fix is on and working. If this plugin helped, a short review on WordPress.org helps other site owners find it.', 'imzura-site-check' ), 63 'review_go' => __( 'Write a review', 'imzura-site-check' ), 64 'review_no' => __( 'Do not ask again', 'imzura-site-check' ), 65 /* translators: %s: number of revisions */ 66 'tidy_go' => __( 'Remove %s old revisions', 'imzura-site-check' ), 67 'tidy_card' => __( 'Old revisions were removed; the copy is above.', 'imzura-site-check' ), 68 /* translators: 1: number of revisions, 2: date and time, 3: size of the copy */ 69 'tidy_done' => __( 'Removed %1$s old revisions on %2$s. A copy (%3$s) is kept in wp-content/uploads/imzura-site-check/copies, so they can be put back. Deleting this plugin deletes the copy too.', 'imzura-site-check' ), 70 /* translators: 1: number of revisions removed, 2: number left */ 71 'tidy_partial' => __( 'Removing old revisions stopped after %1$s; %2$s are left. The ones removed are in the copy.', 'imzura-site-check' ), 72 /* translators: %s: number of revisions */ 73 'tidy_halfway' => __( 'Putting revisions back stopped after %s. The rest are still in the copy.', 'imzura-site-check' ), 74 'tidy_on' => __( 'Go on', 'imzura-site-check' ), 75 'tidy_undo' => __( 'Put them back', 'imzura-site-check' ), 76 'tidy_forget' => __( 'Delete the copy', 'imzura-site-check' ), 77 'tidy_confirm' => __( 'Delete the copy of the old revisions? They cannot be put back after this.', 'imzura-site-check' ), 78 /* translators: %s: number of revisions */ 79 'tidy_removing' => __( 'Copying and removing old revisions: %s so far', 'imzura-site-check' ), 80 /* translators: %s: number of revisions */ 81 'tidy_restoring' => __( 'Putting revisions back: %s so far', 'imzura-site-check' ), 82 /* translators: 1: number of revisions put back, 2: number that were already there */ 83 'tidy_restored' => __( 'Put back %1$s revisions (%2$s were already there) and deleted the copy.', 'imzura-site-check' ), 84 'tidy_forgotten' => __( 'The copy of the old revisions is deleted.', 'imzura-site-check' ), 85 'tidy_failed' => __( 'That did not work. Nothing was removed without a copy; reload the page and try again.', 'imzura-site-check' ), 86 /* translators: %s: a size in megabytes, e.g. 1.4 */ 87 'mb' => __( '%s MB', 'imzura-site-check' ), 88 'scan' => __( 'Check my site', 'imzura-site-check' ), 89 'again' => __( 'Check again', 'imzura-site-check' ), 90 'starting' => __( 'Starting', 'imzura-site-check' ), 63 91 /* translators: 1: number of the page being read, 2: total pages, 3: page name */ 64 'reading' => __( 'Reading page %1$s of %2$s: %3$s', 'imzura-site-check' ),65 'judging' => __( 'Putting the report together', 'imzura-site-check' ),66 'failed' => __( 'The check did not finish. Reload the page and try again.', 'imzura-site-check' ),92 'reading' => __( 'Reading page %1$s of %2$s: %3$s', 'imzura-site-check' ), 93 'judging' => __( 'Putting the report together', 'imzura-site-check' ), 94 'failed' => __( 'The check did not finish. Reload the page and try again.', 'imzura-site-check' ), 67 95 /* translators: %s: date and time of the last check */ 68 'checked' => __( 'Last checked %s', 'imzura-site-check' ), 69 'none' => __( 'Nothing to fix in this group.', 'imzura-site-check' ), 70 'pages' => __( 'The pages that were read', 'imzura-site-check' ), 71 'page' => __( 'Page', 'imzura-site-check' ), 72 'server' => __( 'Server time', 'imzura-site-check' ), 73 'queries' => __( 'Queries', 'imzura-site-check' ), 74 'files' => __( 'Scripts and styles', 'imzura-site-check' ), 75 'unread' => __( 'not read', 'imzura-site-check' ), 76 'owners' => __( 'What each plugin and the theme add', 'imzura-site-check' ), 77 'owners_lead' => __( 'Scripts and styles only, heaviest first. The numbers are for the page where each one loads the most.', 'imzura-site-check' ), 78 'owner' => __( 'Plugin or theme', 'imzura-site-check' ), 79 'owner_kb' => __( 'Weight before compression', 'imzura-site-check' ), 80 'owner_pages' => __( 'Loads on', 'imzura-site-check' ), 96 'checked' => __( 'Last checked %s', 'imzura-site-check' ), 97 'none' => __( 'Nothing to fix in this group.', 'imzura-site-check' ), 98 'pages' => __( 'The pages that were read', 'imzura-site-check' ), 99 'page' => __( 'Page', 'imzura-site-check' ), 100 'server' => __( 'Server time', 'imzura-site-check' ), 101 'queries' => __( 'Queries', 'imzura-site-check' ), 102 'files' => __( 'Scripts and styles', 'imzura-site-check' ), 103 'unread' => __( 'not read', 'imzura-site-check' ), 104 'owners' => __( 'What each plugin and the theme add', 'imzura-site-check' ), 105 'owners_lead' => __( 'Busiest first. Server time is the longest its code ran while one page was built, with the database queries it made there; scripts and styles are for the page where it loads the most.', 'imzura-site-check' ), 106 'owner' => __( 'Plugin or theme', 'imzura-site-check' ), 107 'owner_ms' => __( 'Server time', 'imzura-site-check' ), 108 /* translators: 1: milliseconds, 2: number of database queries */ 109 'owner_code' => __( '%1$s ms · %2$s queries', 'imzura-site-check' ), 110 'owner_kb' => __( 'Weight before compression', 'imzura-site-check' ), 111 'owner_pages' => __( 'Loads on', 'imzura-site-check' ), 81 112 /* translators: %s: theme name */ 82 'owner_theme' => __( 'Theme: %s', 'imzura-site-check' ),113 'owner_theme' => __( 'Theme: %s', 'imzura-site-check' ), 83 114 /* translators: 1: number of pages it loads on, 2: number of pages that were read */ 84 'owner_of' => __( '%1$s of %2$s pages read', 'imzura-site-check' ),85 'seconds' => __( 's', 'imzura-site-check' ),86 'kb' => __( 'KB', 'imzura-site-check' ),87 'count_high' => __( 'to fix first', 'imzura-site-check' ),88 'count_medium' => __( 'worth fixing', 'imzura-site-check' ),89 'count_low' => __( 'to know about', 'imzura-site-check' ),90 'count_good' => __( 'in good shape', 'imzura-site-check' ),91 'fixes' => __( 'Fixes', 'imzura-site-check' ),92 'groups' => array(115 'owner_of' => __( '%1$s of %2$s pages read', 'imzura-site-check' ), 116 'seconds' => __( 's', 'imzura-site-check' ), 117 'kb' => __( 'KB', 'imzura-site-check' ), 118 'count_high' => __( 'to fix first', 'imzura-site-check' ), 119 'count_medium' => __( 'worth fixing', 'imzura-site-check' ), 120 'count_low' => __( 'to know about', 'imzura-site-check' ), 121 'count_good' => __( 'in good shape', 'imzura-site-check' ), 122 'fixes' => __( 'Fixes', 'imzura-site-check' ), 123 'groups' => array( 93 124 'code' => __( 'Stylesheets and scripts', 'imzura-site-check' ), 94 125 'images' => __( 'Images', 'imzura-site-check' ), … … 96 127 ), 97 128 /* translators: 1: plugin name, 2: the setting in that plugin's screen */ 98 'clash' => __( '%1$s already does this: %2$s. Turn it off there, or keep %1$s and leave this fix off. Two plugins doing the same thing to the same files is a common cause of broken pages.', 'imzura-site-check' ),129 'clash' => __( '%1$s already does this: %2$s. Turn it off there, or keep %1$s and leave this fix off. Two plugins doing the same thing to the same files is a common cause of broken pages.', 'imzura-site-check' ), 99 130 /* translators: %s: plugin name */ 100 'clash_maybe' => __( '%s is active and may already do this. If it does, keep only one of the two.', 'imzura-site-check' ),131 'clash_maybe' => __( '%s is active and may already do this. If it does, keep only one of the two.', 'imzura-site-check' ), 101 132 /* translators: %s: plugin name */ 102 'clash_open' => __( 'Open %s settings', 'imzura-site-check' ),133 'clash_open' => __( 'Open %s settings', 'imzura-site-check' ), 103 134 /* translators: %s: plugin name */ 104 'clash_off' => __( 'Deactivate %s', 'imzura-site-check' ),105 'cache_title' => __( 'Page cache', 'imzura-site-check' ),106 'cache_lead' => __( 'A page cache sends visitors a ready copy of each page instead of building it again. This plugin works next to your page cache and does not replace it; when a fix is switched on or off, it clears the cache for you.', 'imzura-site-check' ),135 'clash_off' => __( 'Deactivate %s', 'imzura-site-check' ), 136 'cache_title' => __( 'Page cache', 'imzura-site-check' ), 137 'cache_lead' => __( 'A page cache sends visitors a ready copy of each page instead of building it again. This plugin works next to your page cache and does not replace it; when a fix is switched on or off, it clears the cache for you.', 'imzura-site-check' ), 107 138 /* translators: %s: names of the page cache plugins that are active */ 108 'cache_found' => __( 'Active: %s.', 'imzura-site-check' ), 109 'cache_none' => __( 'No page cache plugin is active.', 'imzura-site-check' ), 110 'cache_ready' => __( 'WordPress is set up to answer from a page cache before plugins load.', 'imzura-site-check' ), 111 'cache_unset' => __( 'WordPress is not set up to answer from a page cache before plugins load.', 'imzura-site-check' ), 112 'cache_test' => __( 'Open the home page as a new visitor', 'imzura-site-check' ), 113 'cache_clear' => __( 'Clear the page cache', 'imzura-site-check' ), 139 'cache_found' => __( 'Active: %s.', 'imzura-site-check' ), 140 'cache_none' => __( 'No page cache plugin is active.', 'imzura-site-check' ), 141 /* translators: %s: plugin names */ 142 'cache_off' => __( 'Active, with its page caching switched off: %s.', 'imzura-site-check' ), 143 'cache_ready' => __( 'WordPress is set up to answer from a page cache before plugins load.', 'imzura-site-check' ), 144 'cache_unset' => __( 'WordPress is not set up to answer from a page cache before plugins load.', 'imzura-site-check' ), 145 'cache_test' => __( 'Open the home page as a new visitor', 'imzura-site-check' ), 146 'cache_clear' => __( 'Clear the page cache', 'imzura-site-check' ), 114 147 /* translators: 1: milliseconds for the first visit, 2: milliseconds for the second visit */ 115 'cache_times' => __( 'The server answered a first visit in %1$s ms and the same page again in %2$s ms. A page that comes from a cache usually answers much faster the second time.', 'imzura-site-check' ),148 'cache_times' => __( 'The server answered a first visit in %1$s ms and the same page again in %2$s ms. A page that comes from a cache usually answers much faster the second time.', 'imzura-site-check' ), 116 149 /* translators: %s: response headers about caching, e.g. cf-cache-status: HIT */ 117 'cache_headers' => __( 'Cache headers in the answer: %s', 'imzura-site-check' ),118 'cache_failed' => __( 'The home page could not be opened from here.', 'imzura-site-check' ),150 'cache_headers' => __( 'Cache headers in the answer: %s', 'imzura-site-check' ), 151 'cache_failed' => __( 'The home page could not be opened from here.', 'imzura-site-check' ), 119 152 /* translators: %s: names of the page caches that were cleared */ 120 'cache_done' => __( 'Cleared: %s.', 'imzura-site-check' ),121 'cache_nobody' => __( 'No page cache that this plugin knows how to clear answered.', 'imzura-site-check' ),122 'fixes_lead' => __( 'A fix is first tried on a test visitor only. Each page is opened without it and with it; the fix stays on only if the page looks the same, nothing broke, and something was saved.', 'imzura-site-check' ),123 'try' => __( 'Try it safely', 'imzura-site-check' ),124 'retry' => __( 'Test again', 'imzura-site-check' ),125 'off' => __( 'Turn off', 'imzura-site-check' ),126 'fix_it' => __( 'Fix it safely', 'imzura-site-check' ),127 'fixed' => __( 'The fix for this is on.', 'imzura-site-check' ),153 'cache_done' => __( 'Cleared: %s.', 'imzura-site-check' ), 154 'cache_nobody' => __( 'No page cache that this plugin knows how to clear answered.', 'imzura-site-check' ), 155 'fixes_lead' => __( 'A fix is first tried on a test visitor only. Each page is opened without it and with it; the fix stays on only if the page looks the same, nothing broke, and something was saved.', 'imzura-site-check' ), 156 'try' => __( 'Try it safely', 'imzura-site-check' ), 157 'retry' => __( 'Test again', 'imzura-site-check' ), 158 'off' => __( 'Turn off', 'imzura-site-check' ), 159 'fix_it' => __( 'Fix it safely', 'imzura-site-check' ), 160 'fixed' => __( 'The fix for this is on.', 'imzura-site-check' ), 128 161 /* translators: 1: number of the page view being tested, 2: total page views, 3: page name */ 129 'testing' => __( 'Testing %1$s of %2$s: %3$s', 'imzura-site-check' ), 130 'deciding' => __( 'Comparing the pages', 'imzura-site-check' ), 131 'preparing' => __( 'Getting the fix ready with what the pages showed', 'imzura-site-check' ), 132 'weight' => __( 'Weight of the files the page loads', 'imzura-site-check' ), 133 'blocking' => __( 'Files the page waits for before it shows anything', 'imzura-site-check' ), 134 'frames' => __( 'Video players and other embedded pages', 'imzura-site-check' ), 135 'trial_failed' => __( 'The test did not finish, so nothing was changed. Reload the page and try again.', 'imzura-site-check' ), 136 'idle' => __( 'The pages looked the same with it, but nothing was saved on this site, so it was left off.', 'imzura-site-check' ), 137 'kept' => __( 'Tested and kept on. What it changed, counted in your browser on a first visit:', 'imzura-site-check' ), 138 'calls' => __( 'Requests to the server after the page arrives', 'imzura-site-check' ), 162 'testing' => __( 'Testing %1$s of %2$s: %3$s', 'imzura-site-check' ), 163 'deciding' => __( 'Comparing the pages', 'imzura-site-check' ), 164 'preparing' => __( 'Getting the fix ready with what the pages showed', 'imzura-site-check' ), 165 'aside' => __( 'A file stopped working when minified. It is now sent as it is; testing again without it', 'imzura-site-check' ), 166 /* translators: %s: number of files */ 167 'excused' => __( 'Files sent as they are, because a test saw them stop working when minified: %s', 'imzura-site-check' ), 168 'weight' => __( 'Weight of the files the page loads', 'imzura-site-check' ), 169 'blocking' => __( 'Files the page waits for before it shows anything', 'imzura-site-check' ), 170 'frames' => __( 'Video players and other embedded pages', 'imzura-site-check' ), 171 'lcp' => __( 'Main picture held back (waits for lazy loading or is not marked urgent)', 'imzura-site-check' ), 172 'sizes' => __( 'Pictures without a width and height', 'imzura-site-check' ), 173 'hosts' => __( 'Other websites the page connects to', 'imzura-site-check' ), 174 'trial_failed' => __( 'The test did not finish, so nothing was changed. Reload the page and try again.', 'imzura-site-check' ), 175 'idle' => __( 'The pages looked the same with it, but nothing was saved on this site, so it was left off.', 'imzura-site-check' ), 176 'kept' => __( 'Tested and kept on. What it changed, counted in your browser on a first visit:', 'imzura-site-check' ), 177 'calls' => __( 'Requests to the server after the page arrives', 'imzura-site-check' ), 139 178 /* translators: %s: names of the page caches that were cleared */ 140 'cache_cleared' => __( 'The page cache was cleared (%s), so visitors get the pages as they are now.', 'imzura-site-check' ),141 'cache_left' => __( 'A page cache is active that this plugin does not know how to clear. Clear it yourself; until then visitors may get the pages from before this change.', 'imzura-site-check' ),179 'cache_cleared' => __( 'The page cache was cleared (%s), so visitors get the pages as they are now.', 'imzura-site-check' ), 180 'cache_left' => __( 'A page cache is active that this plugin does not know how to clear. Clear it yourself; until then visitors may get the pages from before this change.', 'imzura-site-check' ), 142 181 /* translators: %s: the address that switches every fix off */ 143 'rescue' => __( 'If the site ever misbehaves and you cannot reach this screen, open this address. It switches every fix off: %s', 'imzura-site-check' ),144 'states' => array(182 'rescue' => __( 'If the site ever misbehaves and you cannot reach this screen, open this address. It switches every fix off: %s', 'imzura-site-check' ), 183 'states' => array( 145 184 'off' => __( 'Off', 'imzura-site-check' ), 146 185 'on' => __( 'On', 'imzura-site-check' ), … … 149 188 'unavailable' => __( 'Not needed', 'imzura-site-check' ), 150 189 ), 151 'problems' => array(190 'problems' => array( 152 191 /* translators: %1$s: page name */ 153 'frame' => __( '%1$s could not be opened in the test frame, so it could not be compared.', 'imzura-site-check' ),192 'frame' => __( '%1$s could not be opened in the test frame, so it could not be compared.', 'imzura-site-check' ), 154 193 /* translators: %1$s: page name */ 155 'unread' => __( '%1$s was answered before WordPress saw the request, most likely by a page cache, so it could not be tested.', 'imzura-site-check' ),194 'unread' => __( '%1$s was answered before WordPress saw the request, most likely by a page cache, so it could not be tested.', 'imzura-site-check' ), 156 195 /* translators: 1: page name, 2: HTTP status code */ 157 'status' => __( '%1$s answered with status %2$s when the fix was on.', 'imzura-site-check' ),196 'status' => __( '%1$s answered with status %2$s when the fix was on.', 'imzura-site-check' ), 158 197 /* translators: 1: page name, 2: the error message */ 159 'error' => __( '%1$s: a new error appeared: %2$s', 'imzura-site-check' ),198 'error' => __( '%1$s: a new error appeared: %2$s', 'imzura-site-check' ), 160 199 /* translators: 1: page name, 2: the text that changed */ 161 'text' => __( '%1$s: the text on the page changed: %2$s', 'imzura-site-check' ),200 'text' => __( '%1$s: the text on the page changed: %2$s', 'imzura-site-check' ), 162 201 /* translators: 1: page name, 2: how many elements moved out of how many, e.g. 12 of 340 */ 163 'layout' => __( '%1$s: parts of the page moved or changed size (%2$s elements).', 'imzura-site-check' ),202 'layout' => __( '%1$s: parts of the page moved or changed size (%2$s elements).', 'imzura-site-check' ), 164 203 /* translators: 1: page name, 2: a CSS selector */ 165 'mark' => __( '%1$s: something a script sets up on the page was missing (%2$s).', 'imzura-site-check' ),204 'mark' => __( '%1$s: something a script sets up on the page was missing (%2$s).', 'imzura-site-check' ), 166 205 /* translators: 1: page name, 2: an event and where on the page it was listened for, e.g. click .add_to_cart_button */ 167 206 'listener' => __( '%1$s: a script no longer reacts to something that is still on the page (%2$s).', 'imzura-site-check' ), 168 ), 169 'areas' => array( 207 /* translators: %1$s: page name */ 208 'lcp' => __( '%1$s: the main picture now waits (lazy loading, or no longer marked urgent).', 'imzura-site-check' ), 209 ), 210 'areas' => array( 170 211 'front' => __( 'What visitors load', 'imzura-site-check' ), 171 212 'store' => __( 'Store: cart, checkout, orders', 'imzura-site-check' ), 172 213 'admin' => __( 'Admin and database', 'imzura-site-check' ), 173 214 ), 174 'levels' => array(215 'levels' => array( 175 216 'high' => __( 'Fix first', 'imzura-site-check' ), 176 217 'medium' => __( 'Worth fixing', 'imzura-site-check' ), … … 183 224 } 184 225 185 /**186 * The tabs of the screen: the check, one per group of fixes (see Fix::group()), and the page cache.187 */188 226 private static function tabs() { 189 227 return array( … … 219 257 <section class="speed-doctor-counts" id="speed-doctor-counts" hidden></section> 220 258 259 <section class="speed-doctor-tidy" id="speed-doctor-tidy" hidden></section> 260 221 261 <div id="speed-doctor-findings"></div> 222 262 -
imzura-site-check/tags/1.3.0/includes/class-cache.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Tells page caches to forget their copies when a fix is switched on or off. 4 * 5 * A cached page is a copy of what the site sent before the change. Left alone, visitors keep getting it: 6 * without the fix they should have, or, after "Turn off", with the fix that was just removed. 7 * 8 * @package SpeedDoctor 9 */ 2 // imzura Site Check by imzura.com 10 3 11 4 namespace SpeedDoctor; … … 15 8 final class Cache { 16 9 17 /**18 * @return array cleared: the caches that were told to clear; left: a page cache is active and none of them was.19 */20 10 public static function clear() { 21 11 $cleared = array(); … … 27 17 } 28 18 } catch ( \Throwable $e ) { 29 // Another plugin's cache failing to clear must not stop a fix from being switched off.30 19 continue; 31 20 } 32 21 } 33 22 34 /**35 * Fires after a fix was switched on or off, for a host or a cache this plugin does not know.36 */37 23 do_action( 'speed_doctor_clear_cache' ); 38 24 … … 43 29 } 44 30 45 /**46 * What the Cache tab shows: the page cache plugins that are active, and whether WordPress loads a page cache.47 */48 31 public static function status() { 49 32 $names = Checks\Environment::CACHE_PLUGINS; … … 55 38 56 39 $found = array(); 40 $off = array(); 57 41 58 42 foreach ( $active as $file ) { … … 60 44 61 45 if ( isset( $names[ $slug ] ) ) { 62 $found[] = $names[ $slug ]; 46 if ( Checks\Environment::caching_off( $slug ) ) { 47 $off[] = $names[ $slug ]; 48 } else { 49 $found[] = $names[ $slug ]; 50 } 63 51 } 64 52 } … … 66 54 return array( 67 55 'plugins' => array_values( array_unique( $found ) ), 56 'off' => array_values( array_unique( $off ) ), 68 57 'ready' => self::page_cache(), 69 58 'home' => home_url( '/' ), … … 71 60 } 72 61 73 /**74 * Whether WordPress is set up to answer from a page cache before plugins load: it then loads the page cache's75 * drop-in, advanced-cache.php, on every request, this one included.76 */77 62 public static function page_cache() { 78 63 if ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) { … … 89 74 } 90 75 91 /**92 * Each entry answers true when its plugin is there and was asked to clear. The calls are the plugins' own93 * public ones; a plugin that is not active is skipped.94 */95 76 private static function known() { 96 77 return array( … … 153 134 } 154 135 155 /**156 * Calls a cache plugin's function when it is there. What the function returns says nothing about whether157 * the cache was cleared (some return nothing), so being called is what counts.158 */159 136 private static function call( $callback, ...$args ) { 160 137 if ( ! is_callable( $callback ) ) { -
imzura-site-check/tags/1.3.0/includes/class-check.php
r3797250 r3797325 1 1 <?php 2 /** 3 * One thing the scanner looks at. A check returns findings; it never changes the site. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 12 8 abstract class Check { 13 9 14 /**15 * @param array $pages What Probe recorded for each page the browser opened.16 * @return array[] Findings.17 */18 10 abstract public function run( array $pages ); 19 11 20 /**21 * Hands over what only the browser could see (response headers), before run(). Most checks need none of it.22 *23 * @param array $seen What the admin screen's browser reported.24 */25 12 public function browser( array $seen ) { 13 } 14 15 public function admin( array $pages ) { 26 16 } 27 17 … … 38 28 } 39 29 30 protected static function owner_by_prefix( $handle ) { 31 $owners = array(); 32 33 foreach ( (array) get_option( 'active_plugins', array() ) as $file ) { 34 $owners[ 'plugin:' . strtok( (string) $file, '/' ) ] = strtok( (string) $file, '/' ); 35 } 36 37 $owners[ 'theme:' . get_stylesheet() ] = get_stylesheet(); 38 $owners[ 'theme:' . get_template() ] = get_template(); 39 40 uasort( 41 $owners, 42 static function ( $a, $b ) { 43 return strlen( $b ) <=> strlen( $a ); 44 } 45 ); 46 47 foreach ( $owners as $owner => $slug ) { 48 if ( '' !== $slug && 0 === strpos( strtolower( $handle ), strtolower( $slug ) ) ) { 49 return $owner; 50 } 51 } 52 53 return ''; 54 } 55 40 56 protected function kb( $kb ) { 41 57 return $kb >= 1024 … … 49 65 return Plugin::plugin_name( $slug ); 50 66 } 67 68 protected function who( $owner ) { 69 $owner = (string) $owner; 70 71 if ( 0 === strpos( $owner, 'plugin:' ) ) { 72 return $this->plugin_name( substr( $owner, 7 ) ); 73 } 74 75 if ( 0 === strpos( $owner, 'theme:' ) ) { 76 $theme = wp_get_theme( substr( $owner, 6 ) ); 77 78 /* translators: %s: theme name */ 79 return sprintf( __( 'the %s theme', 'imzura-site-check' ), $theme->exists() ? $theme->get( 'Name' ) : substr( $owner, 6 ) ); 80 } 81 82 if ( 0 === strpos( $owner, 'external:' ) ) { 83 return substr( $owner, 9 ); 84 } 85 86 if ( 'core' === $owner ) { 87 return __( 'WordPress itself', 'imzura-site-check' ); 88 } 89 90 return 'inline' === $owner ? __( 'a script written into the page', 'imzura-site-check' ) : __( 'another source', 'imzura-site-check' ); 91 } 92 93 protected static function line( $value, $length = 200 ) { 94 return is_scalar( $value ) ? mb_substr( sanitize_text_field( (string) $value ), 0, $length ) : ''; 95 } 96 97 protected static function items( array $look, $key, $limit ) { 98 $items = isset( $look[ $key ] ) && is_array( $look[ $key ] ) ? array_slice( $look[ $key ], 0, $limit ) : array(); 99 100 return array_values( array_filter( $items, 'is_array' ) ); 101 } 102 103 protected static function look( array $looks, $kind ) { 104 return isset( $looks[ $kind ]['look'] ) && is_array( $looks[ $kind ]['look'] ) ? $looks[ $kind ]['look'] : array(); 105 } 106 107 protected static function looks( array $seen ) { 108 $looks = array(); 109 $pages = isset( $seen['pages'] ) && is_array( $seen['pages'] ) ? $seen['pages'] : array(); 110 111 foreach ( $pages as $kind => $page ) { 112 if ( is_string( $kind ) && is_array( $page ) ) { 113 $looks[ sanitize_key( $kind ) ] = $page; 114 } 115 } 116 117 return $looks; 118 } 51 119 } -
imzura-site-check/tags/1.3.0/includes/class-conflicts.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Other optimizers that already do what one of our fixes does. 4 * 5 * Two plugins minifying the same files, or delaying the same scripts, are a common cause of broken pages. So a 6 * fix whose job another active plugin already does is not tried: the screen names the plugin and the exact 7 * setting, and links to its settings page. This plugin only reads the other plugin's saved settings; it never 8 * changes them. The settings and labels below come from each plugin's own code (BRAIN conflict-map). 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor; … … 17 8 final class Conflicts { 18 9 19 /**20 * Plugins that may do the same and whose settings we cannot read (paid, or stored in a way we have not21 * checked). They do not block a fix; the screen asks to keep only one of the two.22 */23 10 const UNREAD = array( 24 11 'wp-rocket' => 'WP Rocket', … … 29 16 ); 30 17 31 /**32 * @param string $feature What a fix does, see Fix::overlaps().33 * @return array[] The active plugins that do it too: name, setting, url (their settings page), deactivate (a34 * link for plugins that do nothing else, or ''), sure (false when their settings could not be read).35 */36 18 public static function active( $feature ) { 37 19 if ( '' === $feature ) { … … 75 57 } 76 58 77 /**78 * @return array[] The conflicts that stop a fix from being tried.79 */80 59 public static function blocking( $feature ) { 81 60 $blocking = array(); … … 90 69 } 91 70 92 /**93 * @return string[] Active plugins: folder => plugin file.94 */95 71 private static function plugins() { 96 72 $files = (array) get_option( 'active_plugins', array() ); … … 109 85 } 110 86 111 /**112 * Per plugin folder: its name, its settings page, whether it does nothing but this (then it may be113 * deactivated from our screen), and per feature: the setting as its screen labels it, and a reader.114 */115 87 private static function known() { 116 88 $option = static function ( $name ) { … … 217 189 'minify-js' => array( 'Settings → Minify JavaScript', $in( 'wpassetcleanup_settings', 'minify_loaded_js' ) ), 218 190 'minify-css' => array( 'Settings → Minify CSS', $in( 'wpassetcleanup_settings', 'minify_loaded_css' ) ), 191 'unload' => array( 'CSS & JS Manager', '__return_true' ), 219 192 ), 220 193 ), … … 228 201 ), 229 202 ), 203 'wp-retina-2x' => array( 204 'name' => 'Perfect Images', 205 'page' => 'admin.php?page=wr2x_settings', 206 'features' => array( 207 'image-subsizes' => array( 208 'Meow Apps → Perfect Images → Image Sizes', 209 static function () { 210 $settings = self::decode( get_option( 'wr2x_options' ) ); 211 212 return ! empty( $settings['disabled_sizes'] ); 213 }, 214 ), 215 ), 216 ), 217 'disable-media-sizes' => array( 218 'name' => 'Disable Media Sizes', 219 'page' => 'options-general.php?page=disable-media-sizes', 220 'features' => array( 221 'image-subsizes' => array( 222 'Settings → Disable Media Sizes', 223 static function () { 224 $settings = self::decode( get_option( 'disable_media_sizes_options' ) ); 225 226 foreach ( array( 'thumbnail', 'medium', 'large', 'medium-large', '1536x1536', '2048x2048' ) as $size ) { 227 if ( ! empty( $settings[ 'disable-size-' . $size ] ) ) { 228 return true; 229 } 230 } 231 232 return false; 233 }, 234 ), 235 ), 236 ), 230 237 ); 231 238 } 232 239 233 /**234 * W3 Total Cache keeps its settings in a file, as JSON after a line that stops PHP from running it.235 */236 240 private static function w3tc( $key ) { 237 241 return static function () use ( $key ) { … … 265 269 } 266 270 267 /**268 * Settings saved as an array, an object, or JSON text.269 */270 271 private static function decode( $value ) { 271 272 if ( is_string( $value ) ) { -
imzura-site-check/tags/1.3.0/includes/class-fix.php
r3797250 r3797325 1 1 <?php 2 /** 3 * One change this plugin can make to the site. 4 * 5 * A fix stays off until it has been tried on a test visitor, compared with the untouched page, and found 6 * to save something without changing what the visitor sees. Fixer runs that trial. 7 * 8 * @package SpeedDoctor 9 */ 2 // imzura Site Check by imzura.com 10 3 11 4 namespace SpeedDoctor; … … 21 14 abstract public function summary(); 22 15 23 /**24 * Adds the WordPress hooks that make the change. Called only on requests where the fix is on.25 */26 16 abstract public function hook(); 27 17 28 /**29 * @return string Why the fix cannot run on this site, or an empty string when it can.30 */31 18 public function unavailable() { 32 19 return ''; 33 20 } 34 21 35 /**36 * For a fix that changes the finished page: WordPress hands it to plugins from 6.9 on, through the template37 * enhancement output buffer.38 *39 * @return string Why such a fix cannot run on this site, or an empty string when it can.40 */41 22 protected static function needs_finished_page() { 42 23 return function_exists( 'wp_start_template_enhancement_output_buffer' ) ? '' : __( 'This fix needs WordPress 6.9 or newer.', 'imzura-site-check' ); 43 24 } 44 25 45 /**46 * Runs on every front-end request, fix on or off. For noticing things only: it must not change the page.47 */48 26 public function watch() { 49 27 } 50 28 51 /**52 * @return string Why a trial cannot start yet, or an empty string. Unlike unavailable(), this may be slow:53 * it is only asked on the admin screen.54 */55 29 public function untestable() { 56 30 return ''; 57 31 } 58 32 59 /**60 * @return string The part of the browser's report a trial hands to prepare() before the fix is switched on61 * ('images'), or an empty string when the fix needs nothing measured first.62 */63 33 public function evidence() { 64 34 return ''; 65 35 } 66 36 67 /**68 * Gets ready with what the untouched pages showed. Runs once in a trial, before the views with the fix.69 *70 * @param array $evidence What the browser reported under evidence(). It comes from a browser: treat it as input.71 */72 37 public function prepare( array $evidence ) { 73 38 } 74 39 75 /** 76 * @return string What the fix does that another optimizer may already do ('minify-css', 'minify-js', 'defer-js'), or an 77 * empty string. Such a fix is not tried while the other plugin has it on (see Conflicts). 78 */ 40 public function excuse( array $files ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- a fix that can excuse a file reads it. 41 return array(); 42 } 43 44 public function excused() { 45 return 0; 46 } 47 79 48 public function overlaps() { 80 49 return ''; 81 50 } 82 51 83 /**84 * @return string The admin tab the fix is listed in: 'code' (stylesheets and scripts), 'images' or 'plugins'.85 */86 52 public function group() { 87 53 return 'plugins'; 88 54 } 89 55 90 /**91 * @return string[] The kinds of pages (see Probe::targets()) the fix is tried on.92 */93 56 public function kinds() { 94 57 return array( 'home', 'post', 'shop', 'product' ); 95 58 } 96 59 97 /**98 * @return array[] Pages of its own the fix is tried on as well: kind, label, url.99 */100 60 public function targets() { 101 61 return array(); 102 62 } 103 63 104 /**105 * @return string[] CSS selectors for things a script sets up. A page must match each of them as many106 * times with the fix as without it.107 */108 64 public function marks() { 109 65 return array(); -
imzura-site-check/tags/1.3.0/includes/class-fixer.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Tries a fix, keeps it only when it is proven, and remembers the outcome. 4 * 5 * A trial opens each page three times as a first-time visitor: twice untouched ("before" and "control", which 6 * together show what the page does on its own) and once with the fix switched on for that request only ("after"). 7 * The admin's browser compares what it saw; this class adds what the server saw and decides. 8 * 9 * @package SpeedDoctor 10 */ 2 // imzura Site Check by imzura.com 11 3 12 4 namespace SpeedDoctor; … … 19 11 const RESCUE = 'speed_doctor_rescue'; 20 12 const STEPS = array( 'before', 'control', 'after' ); 21 const SEEN = array( 'frame', 'error', 'text', 'layout', 'mark', 'listener' ); 22 23 // A page counts as lighter when it weighs under this share of what it did: less than that is noise. 13 const SEEN = array( 'frame', 'error', 'text', 'layout', 'mark', 'listener', 'lcp' ); 14 24 15 const LIGHTER = 0.95; 25 16 26 // Bytes a fix measured itself taking off a page (see hold()). Exact, so no noise margin: 1 KB counts.27 17 const EXACT = 1024; 28 18 … … 34 24 $fixes = array(); 35 25 36 foreach ( apply_filters( 'speed_doctor_fixes', array( new Fixes\Cart_Script(), new Fixes\WooCommerce_Files(), new Fixes\Contact_Form_7(), new Fixes\Heavy_Images(), new Fixes\ Minify_Styles(), new Fixes\Minify_Scripts(), new Fixes\Emoji(), new Fixes\Jquery_Migrate(), new Fixes\Defer_Scripts(), new Fixes\Video_Facade() ) ) as $fix ) {26 foreach ( apply_filters( 'speed_doctor_fixes', array( new Fixes\Cart_Script(), new Fixes\WooCommerce_Files(), new Fixes\Contact_Form_7(), new Fixes\Heavy_Images(), new Fixes\Lcp_Image(), new Fixes\Image_Sizes(), new Fixes\Local_Fonts(), new Fixes\Minify_Styles(), new Fixes\Minify_Scripts(), new Fixes\Emoji(), new Fixes\Visitor_Heartbeat(), new Fixes\Jquery_Migrate(), new Fixes\Defer_Scripts(), new Fixes\Video_Facade() ) ) as $fix ) { 37 27 if ( $fix instanceof Fix ) { 38 28 $fixes[ $fix->id() ] = $fix; … … 66 56 } 67 57 68 /**69 * A fix calls this when it changes a page. With a script handle, or a stylesheet handle written as70 * 'style:handle', the report also stops counting that file: the fix took its tag out of the page, so71 * WordPress still lists it as printed. A fix that knows exactly how many bytes it took off (a smaller72 * copy of a file, code it left out) passes them as well; a trial counts those as a saving.73 */74 58 public static function hold( $id, $handle = '', $bytes = 0 ) { 75 59 if ( ! isset( self::$held[ $id ] ) ) { … … 79 63 if ( '' !== $handle ) { 80 64 self::$held[ $id ][] = $handle; 65 Pipeline::mark( $handle, $id ); 81 66 } 82 67 … … 90 75 } 91 76 92 /**93 * @return int[] Per fix, the bytes it measured taking off this page.94 */95 77 public static function savings() { 96 78 return self::$saved; … … 117 99 'problems' => isset( $entry['problems'] ) ? $entry['problems'] : array(), 118 100 'cache' => isset( $entry['cache'] ) ? $entry['cache'] : null, 101 'aside' => isset( $entry['aside'] ) ? (int) $entry['aside'] : 0, 102 'excused' => '' !== $why ? 0 : $fix->excused(), 119 103 ); 120 104 } … … 136 120 $targets = array_merge( $targets, $fix->targets() ); 137 121 138 // Every page is tried twice, wide and narrow: a stylesheet can hold rules that only apply on a phone.139 122 foreach ( $targets as $target ) { 140 123 $views[] = $target + array( 'screen' => 'wide' ); … … 148 131 } 149 132 150 // Every untouched view comes first: a fix may need what they show before it is switched on.151 133 foreach ( array( array( 'before', 'control' ), array( 'after' ) ) as $steps ) { 152 134 foreach ( $views as $view ) { … … 174 156 } 175 157 176 /**177 * @param Fix $fix The fix that was tried.178 * @param array $tokens The probe tokens trial() handed out.179 * @param array $pages Per page, what the browser counted without and with the fix.180 * @param array $problems What the browser saw change.181 */182 158 public static function verdict( Fix $fix, array $tokens, array $pages, array $problems ) { 183 159 $id = $fix->id(); … … 192 168 $problems = self::problems( $problems ); 193 169 $pages = self::numbers( $pages ); 170 $aside = $problems ? $fix->excuse( array_values( array_filter( array_column( $problems, 'file' ) ) ) ) : array(); 194 171 $acted = false; 195 172 $saves = false; … … 220 197 221 198 foreach ( $pages as $page ) { 222 $saves = $saves || $page['calls'][1] < $page['calls'][0] || $page['files'][1] < $page['files'][0] || $page['kb'][1] < self::LIGHTER * $page['kb'][0] || $page['blocking'][1] < $page['blocking'][0] || $page['frames'][1] < $page['frames'][0] ;199 $saves = $saves || $page['calls'][1] < $page['calls'][0] || $page['files'][1] < $page['files'][0] || $page['kb'][1] < self::LIGHTER * $page['kb'][0] || $page['blocking'][1] < $page['blocking'][0] || $page['frames'][1] < $page['frames'][0] || $page['lcp'][1] < $page['lcp'][0] || $page['sizes'][1] < $page['sizes'][0] || $page['hosts'][1] < $page['hosts'][0]; 223 200 } 224 201 … … 230 207 'pages' => $pages, 231 208 'problems' => $problems, 209 'aside' => count( $aside ), 232 210 ) 233 211 ); … … 248 226 } 249 227 250 /**251 * Runs when the plugin is deactivated: pages cached while a fix was on must not outlive it.252 */253 228 public static function retire() { 254 229 foreach ( self::saved() as $entry ) { … … 261 236 } 262 237 263 /**264 * Saves the outcome of a trial or a "Turn off". When the fix was on before or is on now, the pages visitors265 * get have changed, so page caches are told to drop their copies, after the new state is saved.266 */267 238 private static function settle( $id, array $entry ) { 268 239 $saved = self::saved(); … … 279 250 } 280 251 281 /**282 * The address that switches every fix off without logging in.283 */284 252 public static function rescue_url() { 285 253 $key = (string) get_option( self::RESCUE, '' ); … … 309 277 update_option( self::OPTION, array(), true ); 310 278 311 // Cache plugins are not all loaded yet, and the copies they hold are the pages the owner is escaping from.312 279 add_action( 313 280 'wp_loaded', … … 338 305 foreach ( array_slice( $seen, 0, 20 ) as $item ) { 339 306 if ( is_array( $item ) && isset( $item['kind'] ) && in_array( $item['kind'], self::SEEN, true ) ) { 340 $problem s[]= self::problem(307 $problem = self::problem( 341 308 isset( $item['label'] ) ? self::line( $item['label'], 100 ) : '', 342 309 $item['kind'], 343 310 isset( $item['detail'] ) ? self::line( $item['detail'], 200 ) : '' 344 311 ); 312 313 if ( 'error' === $item['kind'] && ! empty( $item['file'] ) && is_string( $item['file'] ) ) { 314 $problem['file'] = esc_url_raw( substr( $item['file'], 0, 500 ) ); 315 } 316 317 $problems[] = $problem; 345 318 } 346 319 } … … 359 332 $page = array( 'label' => isset( $item['label'] ) ? self::line( $item['label'], 100 ) : '' ); 360 333 361 foreach ( array( 'calls', 'files', 'kb', 'ms', 'blocking', 'frames' ) as $key ) {334 foreach ( array( 'calls', 'files', 'kb', 'ms', 'blocking', 'frames', 'lcp', 'sizes', 'hosts' ) as $key ) { 362 335 $pair = isset( $item[ $key ] ) && is_array( $item[ $key ] ) ? array_values( $item[ $key ] ) : array(); 363 336 $page[ $key ] = array( isset( $pair[0] ) ? absint( $pair[0] ) : 0, isset( $pair[1] ) ? absint( $pair[1] ) : 0 ); -
imzura-site-check/tags/1.3.0/includes/class-paths.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Where a file that a page loads lives on this server, found with WordPress's own location functions. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 12 8 final class Paths { 13 9 14 /**15 * @var array<string, string>|null16 */17 10 private static $roots = null; 18 11 19 /** 20 * @param string $url An address, with or without its scheme. 21 * @param string $base What an address that starts with a single slash is relative to. 22 * @return string The file on this server the address points to (it may not exist), or an empty string when the 23 * address points elsewhere or climbs out of its folder. 24 */ 12 private static $must = null; 13 25 14 public static function file( $url, $base = '' ) { 26 15 $url = (string) strtok( (string) $url, '?#' ); … … 39 28 $rest = rawurldecode( substr( $url, strlen( $root ) ) ); 40 29 41 if ( '' === $rest || false !== strpos( $rest, "\0" ) || preg_match( '#(^|[/\\\\])\.\.([/\\\\]|$)#', $rest ) ) { 42 return ''; 43 } 44 45 return $dir . $rest; 30 return '' !== $rest && self::safe( $rest ) ? $dir . $rest : ''; 46 31 } 47 32 … … 49 34 } 50 35 51 /** 52 * @return string The address without its scheme, so that http and https addresses compare equal. 53 */ 36 public static function dir( $url ) { 37 $url = self::folder( (string) strtok( (string) $url, '?#' ) ); 38 39 foreach ( self::roots() as $root => $dir ) { 40 if ( 0 === strpos( $url, $root ) ) { 41 $rest = rawurldecode( substr( $url, strlen( $root ) ) ); 42 43 return self::safe( $rest ) ? $dir . $rest : ''; 44 } 45 } 46 47 foreach ( self::roots() as $root => $dir ) { 48 if ( 0 !== strpos( $root, $url ) ) { 49 continue; 50 } 51 52 $tail = rawurldecode( substr( $root, strlen( $url ) ) ); 53 54 if ( self::safe( $tail ) && substr( $dir, -strlen( $tail ) ) === $tail ) { 55 return substr( $dir, 0, -strlen( $tail ) ); 56 } 57 } 58 59 return ''; 60 } 61 54 62 public static function bare( $url ) { 55 63 return (string) preg_replace( '#^https?:#i', '', (string) $url ); 56 64 } 57 65 58 /**59 * @return string The folder address, without the scheme and with a trailing slash.60 */61 66 public static function folder( $url ) { 62 67 return trailingslashit( self::bare( $url ) ); 63 68 } 64 69 65 /** 66 * Folder addresses mapped to the folders they serve, the longest first: a site may keep its plugins, themes or 67 * uploads away from the WordPress folder. 68 * 69 * @return array<string, string> 70 */ 70 public static function plugins() { 71 $base = self::folder( plugins_url() ); 72 $url = self::folder( plugin_dir_url( SPEED_DOCTOR_FILE ) ); 73 $dir = wp_normalize_path( plugin_dir_path( SPEED_DOCTOR_FILE ) ); 74 75 if ( 0 !== strpos( $url, $base ) ) { 76 return ''; 77 } 78 79 $tail = rawurldecode( substr( $url, strlen( $base ) ) ); 80 81 if ( '' === $tail || substr( $dir, -strlen( $tail ) ) !== $tail ) { 82 return ''; 83 } 84 85 return substr( $dir, 0, -strlen( $tail ) ); 86 } 87 88 public static function must_use() { 89 if ( null === self::$must ) { 90 $files = wp_get_mu_plugins(); 91 self::$must = $files ? trailingslashit( wp_normalize_path( dirname( (string) reset( $files ) ) ) ) : ''; 92 } 93 94 return self::$must; 95 } 96 97 private static function safe( $rest ) { 98 return false === strpos( $rest, "\0" ) && ! preg_match( '#(^|[/\\\\])\.\.([/\\\\]|$)#', $rest ); 99 } 100 71 101 private static function roots() { 72 102 if ( null !== self::$roots ) { … … 107 137 return $roots; 108 138 } 109 110 /**111 * The plugins folder, found from where this plugin lives and the address it is served from.112 *113 * @return string The folder, or an empty string when this plugin is not in it (a must-use plugin, say).114 */115 private static function plugins() {116 $base = self::folder( plugins_url() );117 $url = self::folder( plugin_dir_url( SPEED_DOCTOR_FILE ) );118 $dir = wp_normalize_path( plugin_dir_path( SPEED_DOCTOR_FILE ) );119 120 if ( 0 !== strpos( $url, $base ) ) {121 return '';122 }123 124 $tail = rawurldecode( substr( $url, strlen( $base ) ) );125 126 if ( '' === $tail || substr( $dir, -strlen( $tail ) ) !== $tail ) {127 return '';128 }129 130 return substr( $dir, 0, -strlen( $tail ) );131 }132 139 } -
imzura-site-check/tags/1.3.0/includes/class-plugin.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Wires the plugin together. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 19 15 Probe::listen(); 20 16 21 // Fixes say in words why one cannot run, and WordPress loads translations from init on.22 17 add_action( 'init', array( Fixer::class, 'boot' ), 0 ); 23 18 … … 55 50 new Checks\Environment(), 56 51 new Checks\Delivery(), 52 new Checks\Errors(), 53 new Checks\Images(), 54 new Checks\Image_Copies(), 55 new Checks\Fonts(), 56 new Checks\Scripts(), 57 new Checks\Idle_Styles(), 58 new Checks\Code_Time(), 59 new Checks\Plugin_Settings(), 60 new Checks\Housekeeping(), 61 new Checks\Heartbeat(), 57 62 ); 58 63 -
imzura-site-check/tags/1.3.0/includes/class-probe.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Reads a page from the inside while a visitor's browser loads it. 4 * 5 * The admin screen asks the browser to open each page logged out, with a one-time token in the address. 6 * When that request arrives, this class records what the page was built from: the files it queued and who 7 * queued them, the media in its HTML, how long the server took. No request from the server to itself is 8 * needed, so it works on hosts that block loopback requests. 9 * 10 * When a fix is on trial the page is opened in a hidden frame of the admin screen instead, as a stranger: 11 * the request is served without the administrator's cookies and sets none, and assets/probe.js reports 12 * back what the browser saw. 13 * 14 * @package SpeedDoctor 15 */ 2 // imzura Site Check by imzura.com 16 3 17 4 namespace SpeedDoctor; … … 30 17 private static $html = null; 31 18 19 private static $folders = null; 20 32 21 private static $trial = array(); 33 22 34 23 private static $forced = array(); 24 25 private static $errors = array(); 35 26 36 27 public static function targets() { … … 92 83 } 93 84 94 /** 95 * @param array $targets Pages to read. 96 * @param array $trial For a fix on trial: fix, apply, step, stranger. 97 */ 85 public static function admin_targets() { 86 $targets = array( 87 array( 88 'kind' => 'admin-dashboard', 89 'label' => __( 'Dashboard (admin)', 'imzura-site-check' ), 90 'url' => admin_url( 'index.php' ), 91 ), 92 array( 93 'kind' => 'admin-posts', 94 'label' => __( 'Posts list (admin)', 'imzura-site-check' ), 95 'url' => admin_url( 'edit.php' ), 96 ), 97 ); 98 99 if ( function_exists( 'wc_get_page_permalink' ) ) { 100 $targets[] = array( 101 'kind' => 'admin-products', 102 'label' => __( 'Products list (admin)', 'imzura-site-check' ), 103 'url' => admin_url( 'edit.php?post_type=product' ), 104 ); 105 106 $orders = '\Automattic\WooCommerce\Utilities\OrderUtil'; 107 108 $targets[] = array( 109 'kind' => 'admin-orders', 110 'label' => __( 'Orders list (admin)', 'imzura-site-check' ), 111 'url' => class_exists( $orders ) && $orders::custom_orders_table_usage_is_enabled() ? admin_url( 'admin.php?page=wc-orders' ) : admin_url( 'edit.php?post_type=shop_order' ), 112 ); 113 } 114 115 return apply_filters( 'speed_doctor_admin_targets', $targets ); 116 } 117 98 118 public static function issue( array $targets, array $trial = array() ) { 99 119 $issued = array(); … … 122 142 'token' => $token, 123 143 'url' => add_query_arg( self::PARAM, $token, $target['url'] ), 144 'admin' => ! empty( $trial['admin'] ), 124 145 ); 125 146 } … … 147 168 } 148 169 149 /**150 * @return bool|null Whether a trial switches this fix on or off for the current request; null when it does neither.151 */152 170 public static function forced( $fix ) { 153 171 return isset( self::$forced[ $fix ] ) ? self::$forced[ $fix ] : null; … … 160 178 $inner = false; 161 179 162 // A request the page under trial made itself (assets/probe.js adds the header).163 180 if ( '' === $token && isset( $_SERVER[ $key ] ) ) { 164 181 $token = sanitize_key( wp_unslash( $_SERVER[ $key ] ) ); … … 189 206 190 207 self::$token = $token; 208 self::catch_errors(); 191 209 192 210 if ( ! defined( 'DONOTCACHEPAGE' ) ) { … … 204 222 } 205 223 206 /**207 * Serves the request as a first-time visitor: the administrator's cookies are not read and none are set,208 * so a trial can neither see nor disturb the session of the person running it.209 */210 224 private static function stranger() { 211 225 $_COOKIE = array(); … … 224 238 ); 225 239 226 // The same "random" choices on every view, so a page that shuffles its content (related products,227 // a random post) can be compared with itself. assets/probe.js does the same for Math.random.228 240 // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_seeding_mt_srand -- a fixed seed is the point; it applies to this test request only. 229 241 mt_srand( 1 ); … … 245 257 'origin' => isset( self::$trial['origin'] ) ? (string) self::$trial['origin'] : '', 246 258 'marks' => isset( self::$trial['marks'] ) ? array_values( (array) self::$trial['marks'] ) : array(), 259 'scan' => ! empty( self::$trial['scan'] ), 247 260 ) 248 ) . ';' . file_get_contents( SPEED_DOCTOR_DIR . 'assets/probe.js' ) 261 ) . ';' . file_get_contents( SPEED_DOCTOR_DIR . 'assets/probe.js' ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a file of this plugin, only read. 262 ); 263 } 264 265 private static function catch_errors() { 266 $previous = null; 267 268 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- only on a page the admin screen opened to read it; every error still goes on to the handler that was set before, or to PHP. 269 $previous = set_error_handler( 270 static function ( $level, $message, $file = '', $line = 0 ) use ( &$previous ) { 271 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure_error_reporting, WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting, PluginCheck.CodeAnalysis.PHPErrorReporting.DirectErrorReportingCall -- only reads the level the site reports, to note no more than that; nothing is changed. 272 if ( error_reporting() & $level ) { 273 self::note_error( (int) $level, (string) $message, (string) $file, (int) $line ); 274 } 275 276 return is_callable( $previous ) ? (bool) call_user_func( $previous, $level, $message, $file, $line ) : false; 277 } 278 ); 279 280 add_filter( 281 'wp_should_handle_php_error', 282 static function ( $handle, $error ) { 283 if ( is_array( $error ) && isset( $error['type'], $error['message'], $error['file'], $error['line'] ) ) { 284 self::note_error( (int) $error['type'], (string) $error['message'], (string) $error['file'], (int) $error['line'] ); 285 self::record(); 286 } 287 288 return $handle; 289 }, 290 PHP_INT_MAX, 291 2 292 ); 293 } 294 295 private static function note_error( $level, $message, $file, $line ) { 296 $key = md5( $level . '|' . $file . '|' . $line . '|' . $message ); 297 298 if ( isset( self::$errors[ $key ] ) ) { 299 ++self::$errors[ $key ]['count']; 300 return; 301 } 302 303 if ( count( self::$errors ) >= 30 ) { 304 return; 305 } 306 307 $fatal = in_array( $level, array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ); 308 $kind = $fatal ? 'fatal' : ( in_array( $level, array( E_DEPRECATED, E_USER_DEPRECATED ), true ) ? 'deprecated' : ( in_array( $level, array( E_NOTICE, E_USER_NOTICE ), true ) ? 'notice' : 'warning' ) ); 309 310 self::$errors[ $key ] = array( 311 'kind' => $kind, 312 'message' => mb_substr( (string) preg_replace( '/ in \/\S+:\d+$/', '', trim( (string) strtok( wp_strip_all_tags( $message ), "\n" ) ) ), 0, 300 ), 313 'file' => ltrim( str_replace( wp_normalize_path( ABSPATH ), '', wp_normalize_path( $file ) ), '/' ), 314 'line' => $line, 315 'owner' => '' === $file ? 'other' : self::owner_of_path( $file ), 316 'count' => 1, 249 317 ); 250 318 } … … 267 335 } 268 336 269 /**270 * The finished page as it is sent. WordPress 6.9 and later hand it over from the template enhancement output271 * buffer; without it, what is read from the page itself is left unknown.272 */273 337 public static function keep( $html ) { 274 338 self::$html = (string) $html; … … 284 348 $seen = null !== self::$html; 285 349 $html = (string) self::$html; 286 287 $page['pending'] = false; 288 $page['seconds'] = (float) timer_stop( 0, 3 ); 289 $page['queries'] = (int) get_num_queries(); 290 $page['memory'] = (int) memory_get_peak_usage(); 291 $page['html_kb'] = $seen ? (int) round( strlen( $html ) / 1024 ) : null; 292 $page['status'] = (int) http_response_code(); 293 $page['scripts'] = self::files( wp_scripts() ); 294 $page['styles'] = self::files( wp_styles() ); 295 $page['media'] = $seen ? self::media( $html ) : array(); 296 $page['applied'] = array_keys( Fixer::held() ); 297 $page['saved'] = Fixer::savings(); 298 $page['context'] = array( 350 $last = error_get_last(); 351 352 if ( is_array( $last ) && in_array( $last['type'], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR ), true ) ) { 353 self::note_error( (int) $last['type'], (string) $last['message'], (string) $last['file'], (int) $last['line'] ); 354 } 355 356 $seconds = microtime( true ) - (float) $GLOBALS['timestart'] - Profiler::cost(); 357 358 $page['pending'] = false; 359 $page['php'] = array_values( self::$errors ); 360 $page['code'] = Profiler::report(); 361 $page['seconds'] = round( max( 0, $seconds ), 3 ); 362 $page['queries'] = (int) get_num_queries(); 363 $page['memory'] = (int) memory_get_peak_usage(); 364 $page['html_kb'] = $seen ? (int) round( strlen( $html ) / 1024 ) : null; 365 $page['status'] = (int) http_response_code(); 366 $page['scripts'] = self::files( wp_scripts() ); 367 $page['styles'] = self::files( wp_styles() ); 368 $page['media'] = $seen ? self::media( $html ) : array(); 369 $page['pulse'] = self::pulse( wp_scripts() ); 370 $page['listeners'] = wp_doing_ajax() ? self::listeners() : array(); 371 $page['applied'] = array_keys( Fixer::held() ); 372 $page['saved'] = Fixer::savings(); 373 $page['manifest'] = Pipeline::manifest(); 374 $page['context'] = array( 299 375 'store_page' => function_exists( 'is_woocommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ), 300 376 'cart' => function_exists( 'is_cart' ) && ( is_cart() || is_checkout() ), … … 316 392 } 317 393 318 $src = $registry->registered[ $handle ]->src;394 $src = $registry->registered[ $handle ]->src; 319 395 list( $owner, $path ) = self::owner( $src, $registry->base_url ); 320 396 321 $file s[]= array(397 $file = array( 322 398 'handle' => (string) $handle, 399 'file' => wp_basename( (string) strtok( $src, '?#' ) ), 323 400 'owner' => $owner, 324 401 'kb' => $path && is_file( $path ) ? round( filesize( $path ) / 1024, 1 ) : null, 325 402 ); 403 404 if ( null === $path ) { 405 $file['url'] = esc_url_raw( (string) $src ); 406 } 407 408 $files[] = $file; 326 409 } 327 410 … … 329 412 } 330 413 414 private static function pulse( $registry ) { 415 if ( ! in_array( 'heartbeat', $registry->done, true ) || empty( $registry->registered['heartbeat']->src ) ) { 416 return null; 417 } 418 419 $settings = array(); 420 $data = (string) $registry->get_data( 'heartbeat', 'data' ); 421 422 if ( preg_match( '/heartbeatSettings\s*=\s*(\{.*?\});/s', $data, $found ) ) { 423 $settings = (array) json_decode( $found[1], true ); 424 } 425 426 $interval = isset( $settings['interval'] ) && is_numeric( $settings['interval'] ) ? (int) $settings['interval'] : 60; 427 $by = array(); 428 429 foreach ( array_unique( $registry->done ) as $handle ) { 430 if ( 'heartbeat' !== $handle && ! empty( $registry->registered[ $handle ] ) && in_array( 'heartbeat', (array) $registry->registered[ $handle ]->deps, true ) ) { 431 $src = (string) $registry->registered[ $handle ]->src; 432 $by[] = array( 433 'handle' => (string) $handle, 434 'owner' => '' === $src ? self::owner_by_name( (string) $handle ) : self::who( $src ), 435 ); 436 } 437 } 438 439 return array( 440 'interval' => min( 3600, max( 1, $interval ) ), 441 'by' => $by, 442 'setters' => self::setters( 'heartbeat_settings' ), 443 ); 444 } 445 446 private static function owner_by_name( $handle ) { 447 return in_array( $handle, array( 'autosave', 'wp-auth-check' ), true ) ? 'core' : 'other'; 448 } 449 450 private static function setters( $hook, array $skip = array( 'core', 'self', 'other' ) ) { 451 global $wp_filter; 452 453 $owners = array(); 454 455 if ( empty( $wp_filter[ $hook ] ) ) { 456 return $owners; 457 } 458 459 foreach ( $wp_filter[ $hook ]->callbacks as $callbacks ) { 460 foreach ( $callbacks as $callback ) { 461 $owner = self::callback_owner( $callback['function'] ); 462 463 if ( ! in_array( $owner, $skip, true ) ) { 464 $owners[] = $owner; 465 } 466 } 467 } 468 469 return array_values( array_unique( $owners ) ); 470 } 471 472 private static function listeners() { 473 $owners = array(); 474 475 foreach ( array( 'wp_ajax_nopriv_heartbeat', 'heartbeat_nopriv_received', 'heartbeat_nopriv_send', 'heartbeat_nopriv_tick' ) as $hook ) { 476 $owners = array_merge( $owners, self::setters( $hook, array( 'core', 'self' ) ) ); 477 } 478 479 return array_values( array_unique( $owners ) ); 480 } 481 482 public static function visitor_heartbeat() { 483 $issued = self::issue( 484 array( 485 array( 486 'kind' => 'heartbeat', 487 'label' => 'Heartbeat', 488 'url' => admin_url( 'admin-ajax.php' ), 489 ), 490 ) 491 ); 492 $headers = array( 'Cache-Control' => 'no-cache' ); 493 494 if ( isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) { 495 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- HTTP Basic authentication for a site behind a password, as WordPress Site Health sends it on its own loopback request. 496 $headers['Authorization'] = 'Basic ' . base64_encode( sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) ) . ':' . sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_PW'] ) ) ); 497 } 498 499 $response = wp_remote_post( 500 $issued[0]['url'], 501 array( 502 'timeout' => 20, 503 'headers' => $headers, 504 'cookies' => array(), 505 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- the WordPress core filter for a request to this site, applied the way Site Health applies it. 506 'body' => array( 507 'action' => 'heartbeat', 508 'screen_id' => 'front', 509 ), 510 ) 511 ); 512 513 if ( ! wp_using_ext_object_cache() ) { 514 wp_cache_delete( '_transient_' . self::PREFIX . $issued[0]['token'], 'options' ); 515 wp_cache_delete( '_transient_timeout_' . self::PREFIX . $issued[0]['token'], 'options' ); 516 } 517 518 $page = self::results( array( $issued[0]['token'] ) ); 519 520 if ( is_wp_error( $response ) ) { 521 return array( 'error' => $response->get_error_message() ); 522 } 523 524 $answer = json_decode( (string) wp_remote_retrieve_body( $response ), true ); 525 526 if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) || ! is_array( $answer ) || ! isset( $answer['server_time'] ) ) { 527 /* translators: %s: HTTP status code */ 528 return array( 'error' => sprintf( __( 'the answer was not Heartbeat\'s (HTTP %s)', 'imzura-site-check' ), (int) wp_remote_retrieve_response_code( $response ) ) ); 529 } 530 531 if ( ! $page || ! empty( $page[0]['pending'] ) || ! isset( $page[0]['listeners'] ) ) { 532 return array( 'error' => __( 'the request did not report back', 'imzura-site-check' ) ); 533 } 534 535 return array( 'listeners' => (array) $page[0]['listeners'] ); 536 } 537 538 private static function callback_owner( $callback ) { 539 try { 540 if ( is_string( $callback ) && false !== strpos( $callback, '::' ) ) { 541 $callback = explode( '::', $callback, 2 ); 542 } 543 544 if ( is_array( $callback ) && 2 === count( $callback ) ) { 545 $reflection = new \ReflectionMethod( $callback[0], (string) $callback[1] ); 546 } elseif ( $callback instanceof \Closure || ( is_string( $callback ) && function_exists( $callback ) ) ) { 547 $reflection = new \ReflectionFunction( $callback ); 548 } elseif ( is_object( $callback ) && method_exists( $callback, '__invoke' ) ) { 549 $reflection = new \ReflectionMethod( $callback, '__invoke' ); 550 } else { 551 return 'other'; 552 } 553 } catch ( \ReflectionException $error ) { 554 return 'other'; 555 } 556 557 $file = $reflection->getFileName(); 558 559 if ( false === $file ) { 560 return 'core'; 561 } 562 563 return 0 === strpos( wp_normalize_path( $file ), wp_normalize_path( SPEED_DOCTOR_DIR ) ) ? 'self' : self::owner_of_path( $file ); 564 } 565 331 566 private static function owner( $src, $base ) { 567 if ( strpos( $src, '//' ) === 0 ) { 568 $src = ( is_ssl() ? 'https:' : 'http:' ) . $src; 569 } elseif ( strpos( $src, '/' ) === 0 ) { 570 $src = untrailingslashit( $base ) . $src; 571 } 572 332 573 $src = (string) strtok( $src, '?#' ); 333 $path = Paths::file( $src, $base ); 334 $bare = Paths::bare( 0 === strpos( $src, '/' ) && 0 !== strpos( $src, '//' ) ? untrailingslashit( $base ) . $src : $src ); 335 336 $owners = array( 337 'plugin:' => plugins_url(), 338 'theme:' => get_theme_root_uri(), 339 ); 340 341 foreach ( $owners as $kind => $root ) { 342 $root = Paths::folder( $root ); 343 344 if ( 0 === strpos( $bare, $root ) ) { 345 return array( $kind . strtok( substr( $bare, strlen( $root ) ), '/' ), self::found( $path ) ); 346 } 347 } 348 349 foreach ( array( includes_url(), admin_url() ) as $root ) { 350 if ( 0 === strpos( $bare, Paths::folder( $root ) ) ) { 351 return array( 'core', self::found( $path ) ); 352 } 353 } 354 355 $host = (string) wp_parse_url( 'http:' . $bare, PHP_URL_HOST ); 356 357 if ( '' === $path && strtolower( $host ) !== strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) ) { 358 return array( 'external:' . $host, null ); 359 } 360 361 return array( 'other', self::found( $path ) ); 362 } 363 364 private static function found( $path ) { 365 return '' === $path ? null : $path; 574 $path = Paths::file( $src ); 575 $host = (string) wp_parse_url( $src, PHP_URL_HOST ); 576 577 if ( '' === $path ) { 578 return array( strtolower( $host ) === strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) ? 'other' : 'external:' . $host, null ); 579 } 580 581 $path = wp_normalize_path( $path ); 582 583 return array( self::owner_of_path( $path ), $path ); 584 } 585 586 public static function owner_of_path( $path ) { 587 $path = wp_normalize_path( (string) $path ); 588 $folders = self::folders(); 589 590 if ( '' !== $folders['plugins'] && strpos( $path, $folders['plugins'] ) === 0 ) { 591 return 'plugin:' . strtok( substr( $path, strlen( $folders['plugins'] ) ), '/' ); 592 } 593 594 if ( '' !== $folders['must'] && strpos( $path, $folders['must'] ) === 0 ) { 595 return 'plugin:' . preg_replace( '/\.php$/', '', (string) strtok( substr( $path, strlen( $folders['must'] ) ), '/' ) ); 596 } 597 598 if ( '' !== $folders['themes'] && strpos( $path, $folders['themes'] ) === 0 ) { 599 return 'theme:' . strtok( substr( $path, strlen( $folders['themes'] ) ), '/' ); 600 } 601 602 foreach ( $folders['core'] as $core ) { 603 if ( '' !== $core && strpos( $path, $core ) === 0 ) { 604 return 'core'; 605 } 606 } 607 608 return 'other'; 609 } 610 611 private static function folders() { 612 if ( null === self::$folders ) { 613 self::$folders = array( 614 'plugins' => Paths::plugins(), 615 'must' => Paths::must_use(), 616 'themes' => trailingslashit( wp_normalize_path( get_theme_root() ) ), 617 'core' => array( Paths::dir( includes_url() ), Paths::dir( admin_url() ) ), 618 ); 619 } 620 621 return self::$folders; 622 } 623 624 public static function who( $url ) { 625 $url = (string) strtok( (string) $url, '?#' ); 626 627 if ( '' === $url ) { 628 return 'other'; 629 } 630 631 return self::owner( $url, home_url( '/' ) )[0]; 366 632 } 367 633 … … 381 647 $path = Paths::file( $url, $origin ); 382 648 383 // A copy the heavy-pictures fix made is already the size it is shown at: there is nothing left to say about it.384 649 if ( '' === $path || ! is_file( $path ) || isset( $found[ $path ] ) || false !== strpos( wp_normalize_path( $path ), '/' . Fixes\Heavy_Images::FOLDER . '/' ) ) { 385 650 continue; -
imzura-site-check/tags/1.3.0/includes/class-rest.php
r3797250 r3797325 1 1 <?php 2 /** 3 * The calls the admin screen makes. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 14 10 const SPACE = 'imzura-site-check/v1'; 15 11 12 const REVIEW_META = 'speed_doctor_review'; 13 16 14 public static function register() { 15 register_rest_route( 16 self::SPACE, 17 '/review', 18 array( 19 'methods' => 'POST', 20 'permission_callback' => array( __CLASS__, 'allowed' ), 21 'callback' => static function () { 22 update_user_meta( get_current_user_id(), self::REVIEW_META, time() ); 23 24 return rest_ensure_response( array( 'done' => true ) ); 25 }, 26 ) 27 ); 28 17 29 register_rest_route( 18 30 self::SPACE, … … 22 34 'permission_callback' => array( __CLASS__, 'allowed' ), 23 35 'callback' => static function () { 24 return rest_ensure_response( array( 'targets' => Probe::issue( Probe::targets() ) ) ); 36 return rest_ensure_response( 37 array( 38 'targets' => array_merge( 39 Probe::issue( 40 Probe::targets(), 41 array( 42 'stranger' => true, 43 'scan' => true, 44 ) 45 ), 46 Probe::issue( 47 Probe::admin_targets(), 48 array( 49 'admin' => true, 50 'scan' => true, 51 ) 52 ) 53 ), 54 ) 55 ); 25 56 }, 26 57 ) … … 73 104 register_rest_route( 74 105 self::SPACE, 106 '/tidy/revisions/(?P<step>run|undo|forget)', 107 array( 108 'methods' => 'POST', 109 'permission_callback' => array( __CLASS__, 'allowed' ), 110 'callback' => array( __CLASS__, 'tidy' ), 111 ) 112 ); 113 114 register_rest_route( 115 self::SPACE, 75 116 '/fixes/(?P<id>[a-z0-9-]+)/(?P<step>trial|prepare|verdict|off)', 76 117 array( … … 80 121 ) 81 122 ); 123 } 124 125 public static function tidy( \WP_REST_Request $request ) { 126 if ( 'run' === $request['step'] ) { 127 $result = Tidy::run(); 128 } elseif ( 'undo' === $request['step'] ) { 129 $result = Tidy::undo(); 130 } else { 131 Tidy::forget(); 132 $result = null; 133 } 134 135 return is_wp_error( $result ) ? $result : rest_ensure_response( array( 'tidy' => $result ) ); 82 136 } 83 137 -
imzura-site-check/tags/1.3.0/includes/class-scanner.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Runs every check and keeps the last report. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 19 15 ); 20 16 21 /**22 * @param array $tokens The probe tokens the browser read the pages with.23 * @param array $seen What the browser saw that the server cannot (see Check::browser()).24 */25 17 public static function run( array $tokens, array $seen = array() ) { 26 18 $pages = Probe::results( $tokens ); 19 $front = array(); 20 $admin = array(); 27 21 $findings = array(); 22 23 foreach ( $pages as $page ) { 24 if ( self::is_admin( $page ) ) { 25 $admin[] = $page; 26 } else { 27 $front[] = $page; 28 } 29 } 28 30 29 31 foreach ( Plugin::checks() as $check ) { 30 32 $check->browser( $seen ); 33 $check->admin( $admin ); 31 34 32 foreach ( $check->run( $ pages) as $finding ) {35 foreach ( $check->run( $front ) as $finding ) { 33 36 $findings[] = $finding; 34 37 } 35 38 } 39 40 do_action( 'speed_doctor_seen', $seen ); 36 41 37 42 usort( … … 46 51 'time' => time(), 47 52 'pages' => array_map( array( __CLASS__, 'summary' ), $pages ), 48 'owners' => self::owners( $ pages),53 'owners' => self::owners( $front ), 49 54 'findings' => $findings, 50 55 ); … … 55 60 } 56 61 57 /**58 * What each plugin and the theme put on the pages that were read, heaviest first. Files and weight are59 * those of the page where it loads the most, so the numbers are something a visitor really downloads.60 */61 62 private static function owners( array $pages ) { 62 63 $owners = array(); … … 85 86 } 86 87 88 $code = isset( $page['code'] ) && is_array( $page['code'] ) ? $page['code'] : array(); 89 90 foreach ( $code as $owner => $spent ) { 91 if ( ( strpos( $owner, 'plugin:' ) === 0 || strpos( $owner, 'theme:' ) === 0 ) && $spent['ms'] >= 1 && ! isset( $here[ $owner ] ) ) { 92 $here[ $owner ] = array( 93 'files' => 0, 94 'kb' => 0.0, 95 ); 96 } 97 } 98 87 99 foreach ( $here as $owner => $load ) { 88 100 if ( ! isset( $owners[ $owner ] ) ) { … … 90 102 91 103 $owners[ $owner ] = array( 92 'kind' => $kind, 93 'name' => 'theme' === $kind ? wp_get_theme( $slug )->get( 'Name' ) : Plugin::plugin_name( $slug ), 94 'files' => 0, 95 'kb' => 0, 96 'pages' => 0, 104 'kind' => $kind, 105 'name' => 'theme' === $kind ? wp_get_theme( $slug )->get( 'Name' ) : Plugin::plugin_name( $slug ), 106 'files' => 0, 107 'kb' => 0, 108 'pages' => 0, 109 'ms' => null, 110 'queries' => 0, 97 111 ); 98 112 } … … 101 115 $owners[ $owner ]['files'] = $load['files']; 102 116 $owners[ $owner ]['kb'] = (int) round( $load['kb'] ); 117 } 118 119 if ( isset( $code[ $owner ] ) && $code[ $owner ]['ms'] >= (float) $owners[ $owner ]['ms'] ) { 120 $owners[ $owner ]['ms'] = (int) round( $code[ $owner ]['ms'] ); 121 $owners[ $owner ]['queries'] = (int) $code[ $owner ]['queries']; 103 122 } 104 123 … … 112 131 $owners, 113 132 static function ( $a, $b ) { 114 return $b['kb'] <=> $a['kb'];133 return array( (int) $b['ms'], $b['kb'] ) <=> array( (int) $a['ms'], $a['kb'] ); 115 134 } 116 135 ); 117 136 118 137 return $owners; 138 } 139 140 public static function is_admin( array $page ) { 141 return ! empty( $page['admin'] ); 119 142 } 120 143 … … 128 151 'url' => $page['url'], 129 152 'read' => $read, 153 'admin' => self::is_admin( $page ), 130 154 'seconds' => $read ? $page['seconds'] : null, 131 155 'queries' => $read ? $page['queries'] : null, … … 133 157 'files' => count( $files ), 134 158 'kb' => (int) round( array_sum( array_column( $files, 'kb' ) ) ), 159 'assets' => $read ? array_merge( self::assets( $page['scripts'], 'js' ), self::assets( $page['styles'], 'css' ) ) : array(), 160 ); 161 } 162 163 private static function assets( array $files, $type ) { 164 return array_map( 165 static function ( $file ) use ( $type ) { 166 return array( 167 'type' => $type, 168 'handle' => $file['handle'], 169 'owner' => $file['owner'], 170 'kb' => $file['kb'], 171 ); 172 }, 173 $files 135 174 ); 136 175 } -
imzura-site-check/tags/1.3.0/includes/fixes/class-cart-script.php
r3797250 r3797325 1 1 <?php 2 /** 3 * WooCommerce's cart script, loaded only when the cart is in use. 4 * 5 * cart-fragments.js asks the server for the header cart on a visitor's first page view, whatever is in the cart. 6 * With this fix the page carries the empty cart itself (built the way WC_AJAX::get_refreshed_fragments() builds 7 * it), and assets/cart.js loads the real script as soon as the visitor has, or adds, something. 8 * 9 * @package SpeedDoctor 10 */ 2 // imzura Site Check by imzura.com 11 3 12 4 namespace SpeedDoctor\Fixes; … … 57 49 ), 58 50 JSON_HEX_TAG | JSON_HEX_AMP 59 ) . ';' . file_get_contents( SPEED_DOCTOR_DIR . 'assets/cart.js' ), 51 ) . ';' . file_get_contents( SPEED_DOCTOR_DIR . 'assets/cart.js' ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a file of this plugin, only read. 60 52 array( 'id' => 'speed-doctor-cart-js' ) 61 53 ); 62 54 63 // Only the script file is swapped: inline code other plugins attached to this handle stays in place.64 55 $swapped = preg_replace_callback( 65 56 '#<script\b[^>]*\bid=(["\'])' . self::HANDLE . '-js\1[^>]*>\s*</script>#', … … 81 72 } 82 73 83 /**84 * @return array|null What the cart script would have fetched for an empty cart, or null when the cart is not empty.85 */86 74 private function empty_cart() { 87 75 if ( ! function_exists( 'WC' ) || ! function_exists( 'woocommerce_mini_cart' ) || ! WC()->cart || ! WC()->cart->is_empty() ) { -
imzura-site-check/tags/1.3.0/includes/fixes/class-contact-form-7.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Contact Form 7's files, loaded only on pages that show a form. 4 * 5 * Contact Form 7 queues its script and stylesheet on every page, and the reCAPTCHA or Turnstile script with 6 * them when one is connected. Footer scripts are decided on evidence: they are taken out just before they 7 * would print, and only if no form was built on the page. The stylesheet prints in the head, before the page 8 * is built, so it is taken out when the page's own content holds no form; if a form turns up anyway (a widget, 9 * a page builder), the stylesheet is printed right in front of it. 10 * 11 * @package SpeedDoctor 12 */ 2 // imzura Site Check by imzura.com 13 3 14 4 namespace SpeedDoctor\Fixes; … … 57 47 } 58 48 59 /**60 * Both ways a form reaches a page are tried: written in the page's content, and added from outside it.61 */62 49 public function targets() { 63 50 $targets = array(); … … 101 88 } 102 89 103 /**104 * Remembers one page that shows a form its content does not hold (a widget, a page builder, a template),105 * so a trial can open it. A search finds the other kind.106 */107 90 public function sight( $elements ) { 108 91 if ( is_singular() && ! $this->expected() && ! get_option( self::SEEN ) ) { … … 122 105 123 106 public function head() { 124 // A block theme builds the page before it prints the head, so by now a form may already be known.125 107 if ( $this->built || $this->expected() ) { 126 108 return; … … 153 135 } 154 136 155 // For a form that did not come from a shortcode; style() has usually printed them by now.156 137 foreach ( $this->styles as $handle ) { 157 138 if ( ! wp_style_is( $handle, 'done' ) ) { … … 201 182 } 202 183 203 /**204 * A form built after the footer scripts were printed still gets its scripts.205 */206 184 public function late() { 207 185 $waiting = array(); -
imzura-site-check/tags/1.3.0/includes/fixes/class-defer-scripts.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Scripts in the head loaded with defer, so they stop holding up the first paint. 4 * 5 * The fix asks WordPress's own loading strategies (WordPress 6.3+) to defer every script queued for the head, 6 * jQuery aside: code written into the page often calls jQuery straight away. WordPress keeps a script blocking 7 * when something that depends on it is blocking, and holds back inline code that follows a deferred script, so 8 * the order scripts run in is kept. What a deferred script would have held up is counted in bytes for the trial. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; … … 15 6 use SpeedDoctor\Fix; 16 7 use SpeedDoctor\Fixer; 8 use SpeedDoctor\Pipeline; 17 9 18 10 defined( 'ABSPATH' ) || exit; … … 57 49 } 58 50 59 /**60 * Every head script, and every head script they need, is asked to load with defer.61 */62 51 public function ask() { 63 52 $scripts = wp_scripts(); … … 73 62 } 74 63 75 /**76 * A head script that came out deferred no longer holds up the first paint: its file is what the page saved.77 */78 64 public function count( $tag, $handle, $src ) { 79 65 if ( isset( $this->asked[ $handle ] ) && ! did_action( 'wp_footer' ) && preg_match( '/<script[^>]*\sdefer[\s>=]/i', (string) $tag ) ) { … … 81 67 82 68 Fixer::hold( $this->id(), '', '' === $file ? 0 : (int) filesize( $file ) ); 69 Pipeline::mark( $handle, $this->id() ); 83 70 } 84 71 -
imzura-site-check/tags/1.3.0/includes/fixes/class-emoji.php
r3797250 r3797325 1 1 <?php 2 /** 3 * WordPress's emoji script and styles, left out of the page. 4 * 5 * Every page carries a script that checks whether the browser can draw emoji and, if not, loads pictures for 6 * them, plus a few styles for those pictures. Browsers in use today draw emoji themselves. Older WordPress 7 * prints the script in the head, newer WordPress in the footer; unhooking the head call stops both. What the 8 * script would have weighed is measured into a buffer that is thrown away, so the trial knows what was saved. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; -
imzura-site-check/tags/1.3.0/includes/fixes/class-heavy-images.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Heavy pictures, served at the size they are shown. 4 * 5 * The size is measured, not guessed: during a trial the browser reports how wide each picture is on the page, 6 * and prepare() writes a WebP copy twice that wide (for sharp screens), never wider than the original. On the 7 * page only the address in the <img> changes; the original file is not touched. Pictures with a srcset are left 8 * alone: WordPress already picks a size for each screen there. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; … … 15 6 use SpeedDoctor\Fix; 16 7 use SpeedDoctor\Fixer; 8 use SpeedDoctor\Pipeline; 17 9 18 10 defined( 'ABSPATH' ) || exit; … … 86 78 87 79 public function hook() { 88 add_action( 'template_redirect', array( $this, 'buffer' ), 1 ); 89 } 90 91 public function buffer() { 92 if ( ! is_feed() && $this->map() ) { 93 add_filter( 'wp_template_enhancement_output_buffer', array( $this, 'swap' ), 30, 1 ); 94 } 80 Pipeline::page( $this->id(), 'images', array( $this, 'swap' ), array( $this, 'wants' ) ); 81 } 82 83 public function wants() { 84 return ! is_feed() && $this->map(); 95 85 } 96 86 … … 122 112 $copy = $uploads['basedir'] . '/' . $entry['copy']; 123 113 124 // A picture that was replaced or removed since the copy was made is served as it is.125 114 if ( ! is_file( $copy ) || ! is_file( $original ) || filesize( $original ) !== $entry['bytes'] ) { 126 115 return $tag; … … 129 118 $tag = str_replace( $src[0], ' src="' . esc_url( $uploads['baseurl'] . '/' . $entry['copy'] ) . '"', $tag ); 130 119 131 // Without a width the browser would lay the page out by the copy's own, smaller, width.132 // No height is added: it would stretch the picture in a theme that does not reset it.133 120 if ( ! preg_match( '#\swidth\s*=#i', $tag ) ) { 134 121 $tag = preg_replace( '#^<img\b#i', '<img width="' . (int) $entry['width'] . '"', $tag ); … … 149 136 } 150 137 151 /**152 * @return string The picture's path inside the uploads folder, or an empty string when it is not a JPEG or PNG there.153 */154 138 private function relative( $src, array $uploads ) { 155 139 $base = preg_replace( '#^https?:#', '', $uploads['baseurl'] ); … … 171 155 } 172 156 173 /**174 * The copy's width: at least twice the width shown, and one at which the height comes out a whole number of175 * pixels (near enough). A copy whose shape is a fraction off makes the picture a fraction taller or shorter176 * on the page, and everything under it moves. When no such width is close, the copy keeps the full size.177 */178 157 private function width( $shown, $wide, $tall ) { 179 158 $least = (int) ( 100 * ceil( $shown * self::SHARP / 100 ) ); … … 190 169 } 191 170 192 /**193 * @return array|null Where the copy is, with the original's width and size, or null when a copy is not worth serving.194 */195 171 private function copy( $path, $shown, array $uploads ) { 196 172 $file = $uploads['basedir'] . '/' . $path; -
imzura-site-check/tags/1.3.0/includes/fixes/class-jquery-migrate.php
r3797250 r3797325 1 1 <?php 2 /** 3 * jQuery without jQuery Migrate. 4 * 5 * WordPress loads jQuery Migrate with jQuery on every page that uses jQuery. It keeps code written for old 6 * versions of jQuery working and is not needed by code that follows the current one. The fix takes it out of 7 * jQuery's dependencies only; a script that asks for it by name still gets it. Code that needed it fails in the 8 * browser, and the trial sees that. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; … … 56 47 } 57 48 58 /**59 * After the footer scripts: the fix changed the page only if jQuery was sent and Migrate was not.60 */61 49 public function count() { 62 50 if ( ! wp_script_is( 'jquery', 'done' ) || wp_script_is( self::HANDLE, 'done' ) ) { -
imzura-site-check/tags/1.3.0/includes/fixes/class-minify-scripts.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Scripts sent minified. How the copies are made and kept: see Minify. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Fixes; … … 21 17 22 18 public function summary() { 23 return __( 'Takes the spaces, line breaks and comments out of the scripts your theme and plugins send, and sends a smaller copy of each file instead. Files that are minified already, files from other sites and scripts written into the page are left alone. If a script stops working after minifying, the trial sees it and the fix is not kept. On a server that compresses files the gain is smaller; the trial shows what it is on your pages.', 'imzura-site-check' );19 return __( 'Takes the spaces, line breaks and comments out of the scripts your theme and plugins send, and sends a smaller copy of each file instead. Files that are minified already, files from other sites and scripts written into the page are left alone. If a script stops working after minifying, the trial sees it: that file is sent as it is from then on and the trial runs again, and the fix is kept only when the pages work. On a server that compresses files the gain is smaller; the trial shows what it is on your pages.', 'imzura-site-check' ); 24 20 } 25 21 -
imzura-site-check/tags/1.3.0/includes/fixes/class-minify-styles.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Stylesheets sent minified. How the copies are made and kept: see Minify. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Fixes; -
imzura-site-check/tags/1.3.0/includes/fixes/class-minify.php
r3797250 r3797325 1 1 <?php 2 /** 3 * What the two minify fixes share: stylesheets (Minify_Styles) or scripts (Minify_Scripts) sent minified, the same 4 * code without spaces, line breaks and comments. They are separate fixes, so that each is tried, kept or turned 5 * off on its own. 6 * 7 * Only a site's own files are touched, and only when WordPress prints them as a tag of their own: the address of 8 * the tag changes to a minified copy in uploads/imzura-site-check/min/, and everything else about the file stays as it 9 * was (its handle, its place in the queue, what it depends on and the inline code around it). Files that are 10 * minified already, files from other sites and inline code are left alone, and so is a file the minifier could 11 * not shrink by at least a twentieth. A copy is made on the first page that needs it, within a time limit per 12 * page, and made again when the file changes. 13 * 14 * @package SpeedDoctor 15 */ 2 // imzura Site Check by imzura.com 16 3 17 4 namespace SpeedDoctor\Fixes; … … 20 7 use SpeedDoctor\Fixer; 21 8 use SpeedDoctor\Paths; 9 use SpeedDoctor\Pipeline; 22 10 use SpeedDoctor\Probe; 23 11 use SpeedDoctor\Vendor\MatthiasMullie\Minify as Minifier; … … 29 17 const FOLDER = 'imzura-site-check/min'; 30 18 31 // Bigger files are most often bundles that are minified already, and minifying them would hold the page up.32 19 const LARGEST = 1048576; 33 20 34 // A copy has to come out under this share of the file, or the file is left as it is.35 21 const SMALLER = 0.95; 36 22 37 // Seconds a page may spend making copies, stylesheets and scripts together. The files left over are sent as38 // they are and copied on later pages.39 23 const BUDGET = 1.0; 40 24 41 25 private static $spent = 0.0; 42 26 43 /**44 * @return string 'css' or 'js'.45 */46 27 abstract protected function type(); 47 28 … … 58 39 $dir = $uploads['basedir']; 59 40 60 // A new site has no uploads folder until the first upload: what counts is that it can be made.61 41 while ( ! is_dir( $dir ) && dirname( $dir ) !== $dir ) { 62 42 $dir = dirname( $dir ); … … 71 51 } 72 52 73 add_filter( 'css' === $this->type() ? 'style_loader_src' : 'script_loader_src', array( $this, 'swap' ), PHP_INT_MAX, 2);53 Pipeline::source( $this->id(), 'minify', $this->type(), array( $this, 'swap' ) ); 74 54 } 75 55 … … 98 78 $name = self::name( $handle, $src, $file, $type ); 99 79 100 if ( is_file( $folder['dir'] . '/' . $name . '.skip' ) ) {80 if ( is_file( $folder['dir'] . '/' . $name . '.skip' ) || is_file( $folder['dir'] . '/' . $name . '.bad' ) ) { 101 81 return $src; 102 82 } … … 111 91 } 112 92 113 /** 114 * Writes the minified copy, or a .skip marker when the file does not get smaller. 115 * 116 * @return bool Whether a copy is there to send. 117 */ 93 public function excuse( array $files ) { 94 $folder = self::folder(); 95 $base = preg_replace( '#^https?:#i', '', $folder['url'] ) . '/'; 96 $aside = array(); 97 98 foreach ( array_unique( array_map( 'strval', $files ) ) as $file ) { 99 $path = preg_replace( '#^https?:#i', '', (string) strtok( $file, '?#' ) ); 100 $name = 0 === strpos( $path, $base ) ? rawurldecode( substr( $path, strlen( $base ) ) ) : ''; 101 102 if ( '' === $name || basename( $name ) !== $name || ! preg_match( '/-[0-9a-f]{12}\.' . $this->type() . '$/', $name ) || ! is_file( $folder['dir'] . '/' . $name ) ) { 103 continue; 104 } 105 106 self::filesystem()->put_contents( $folder['dir'] . '/' . $name . '.bad', '', 0644 ); 107 wp_delete_file( $folder['dir'] . '/' . $name ); 108 109 $aside[] = $name; 110 } 111 112 return $aside; 113 } 114 115 public function excused() { 116 $marks = glob( self::folder()['dir'] . '/*.' . $this->type() . '.bad' ); 117 118 return is_array( $marks ) ? count( $marks ) : 0; 119 } 120 118 121 private function make( $dir, $name, $src, $file, $type ) { 119 // A trial has to see the whole fix at once; a visitor's page only spends its budget.120 122 if ( true !== Probe::forced( $this->id() ) && self::$spent >= self::BUDGET ) { 121 123 return false; … … 142 144 $filesystem = self::filesystem(); 143 145 144 // Older copies of the same file, from before it changed, are not needed any more.145 146 $older = glob( $dir . '/' . self::stem( $name ) . '-????????????.' . $type . '*' ); 146 147 … … 155 156 } 156 157 157 // Written under a temporary name first, so that no visitor is sent a half-written file.158 158 $temporary = $dir . '/.' . $name . '.' . wp_generate_password( 6, false ); 159 159 … … 164 164 $base = self::base( $src ); 165 165 166 // The copy lives in another folder, so every address in the file has to stop being relative to it.167 166 $code = preg_replace_callback( 168 167 '/url\(\s*([\'"]?)([^\'")]*)\1\s*\)/i', … … 196 195 } 197 196 198 /**199 * @return string The file on this server an address points to, or an empty string when it points elsewhere.200 */201 197 public static function local( $src ) { 202 198 $file = Paths::file( $src ); … … 205 201 } 206 202 207 /**208 * The copy's file name: the handle, then a hash of the address and of the file as it is now, so that a209 * changed file or a new version gets a new copy.210 */211 203 private static function name( $handle, $src, $file, $type ) { 212 204 $stem = substr( sanitize_file_name( $handle ), 0, 60 ); … … 220 212 } 221 213 222 /**223 * @return string The folder of the address, with a trailing slash and without the scheme.224 */225 214 private static function base( $src ) { 226 215 $url = (string) strtok( $src, '?#' ); -
imzura-site-check/tags/1.3.0/includes/fixes/class-video-facade.php
r3797250 r3797325 1 1 <?php 2 /** 3 * YouTube videos shown as a picture until the visitor presses play. 4 * 5 * An embedded YouTube player loads its own scripts and styles as soon as the page opens, whether or not anyone 6 * watches. With this fix the iframe stays exactly where and how it was (same element, same attributes, so the 7 * theme's styles still size it), but it first shows a light document of its own (srcdoc): the video's picture 8 * and a play button that opens the real player. Nothing else in the page changes. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; … … 15 6 use SpeedDoctor\Fix; 16 7 use SpeedDoctor\Fixer; 8 use SpeedDoctor\Pipeline; 17 9 18 10 defined( 'ABSPATH' ) || exit; … … 44 36 } 45 37 46 /**47 * The fix is tried on a page that has a YouTube video.48 */49 38 public function targets() { 50 39 $found = get_posts( … … 88 77 } 89 78 90 add_action( 'template_redirect', array( $this, 'buffer' ), PHP_INT_MAX);79 Pipeline::page( $this->id(), 'embeds', array( $this, 'swap' ), array( $this, 'wants' ) ); 91 80 } 92 81 93 public function buffer() { 94 if ( is_feed() || is_embed() || is_customize_preview() ) { 95 return; 96 } 97 98 add_filter( 'wp_template_enhancement_output_buffer', array( $this, 'swap' ), 10, 1 ); 82 public function wants() { 83 return ! ( is_feed() || is_embed() || is_customize_preview() ); 99 84 } 100 85 … … 102 87 $head = stripos( $html, '</head>' ); 103 88 104 // Anything but one whole page (a page flushed in parts, a file download) passes through as it is.105 89 if ( false === $head || false === stripos( $html, '</body>', $head ) || false === stripos( $html, 'youtube', $head ) ) { 106 90 return $html; … … 116 100 } 117 101 118 /** 119 * The same iframe tag with a srcdoc: the video's picture, linking to the player with autoplay on. 120 */ 121 private function facade( array $match ) { 122 $tag = $match[0]; 123 $src = html_entity_decode( $match[2], ENT_QUOTES ); 102 private function facade( array $iframe ) { 103 $tag = $iframe[0]; 104 $src = html_entity_decode( $iframe[2], ENT_QUOTES ); 124 105 $play = add_query_arg( 'autoplay', '1', 0 === strpos( $src, '//' ) ? 'https:' . $src : $src ); 125 106 $title = preg_match( '#\stitle=(["\'])(.*?)\1#is', $tag, $found ) ? html_entity_decode( $found[2], ENT_QUOTES ) : ''; … … 128 109 $doc = '<body style="margin:0;overflow:hidden;background:#000">' 129 110 . '<a href="' . esc_url( $play ) . '" aria-label="' . esc_attr( $label ) . '" style="display:block;position:relative;height:100vh">' 130 . '<img src="' . esc_url( 'https://i.ytimg.com/vi/' . $ match[3] . '/hqdefault.jpg' ) . '" alt="' . esc_attr( $title ) . '" loading="lazy" style="display:block;width:100%;height:100%;object-fit:cover">'111 . '<img src="' . esc_url( 'https://i.ytimg.com/vi/' . $iframe[3] . '/hqdefault.jpg' ) . '" alt="' . esc_attr( $title ) . '" loading="lazy" style="display:block;width:100%;height:100%;object-fit:cover">' 131 112 . '<svg width="68" height="48" viewBox="0 0 68 48" aria-hidden="true" style="position:absolute;top:50%;left:50%;margin:-24px 0 0 -34px;opacity:.9">' 132 113 . '<rect width="68" height="48" rx="12" fill="#f00"/><path d="M27 14v20l18-10z" fill="#fff"/></svg></a></body>'; -
imzura-site-check/tags/1.3.0/includes/fixes/class-woocommerce-files.php
r3797250 r3797325 1 1 <?php 2 /** 3 * WooCommerce's stylesheets and scripts, sent only with pages that show something from the store. 4 * 5 * WooCommerce queues them on every page of the site. Whether a page needs them is decided on evidence, from 6 * the finished page: when nothing in it carries a WooCommerce class and no WooCommerce template, block or 7 * widget was rendered, the tags are taken out before the page leaves. Store pages are never touched. 8 * 9 * Two things are left alone on purpose. A stylesheet a theme has swapped for its own also styles parts of 10 * the theme (Astra's sets the size of the mobile menu button), so only WooCommerce's own files are taken out. 11 * The scripts that record where a visitor came from stay on every page: an order is credited to the first 12 * page of the visit, which is often not a store page. 13 * 14 * @package SpeedDoctor 15 */ 2 // imzura Site Check by imzura.com 16 3 17 4 namespace SpeedDoctor\Fixes; … … 19 6 use SpeedDoctor\Fix; 20 7 use SpeedDoctor\Fixer; 8 use SpeedDoctor\Pipeline; 21 9 22 10 defined( 'ABSPATH' ) || exit; … … 24 12 final class WooCommerce_Files extends Fix { 25 13 26 const STYLES = array( 'woocommerce-layout', 'woocommerce-smallscreen', 'woocommerce-general', 'woocommerce-blocktheme' );14 const STYLES = array( 'woocommerce-layout', 'woocommerce-smallscreen', 'woocommerce-general', 'woocommerce-blocktheme', 'wc-blocks-style' ); 27 15 const INLINE = 'woocommerce-inline'; 28 16 const SCRIPTS = array( 'wc-add-to-cart', 'woocommerce' ); … … 31 19 const NEEDLES = array( 'wp:woocommerce/', '[product', '_products', '[add_to_cart' ); 32 20 33 /**34 * Classes WooCommerce or a theme puts on the body of every page. They say nothing about the page.35 */36 21 const ALWAYS = array( 'woocommerce-no-js', 'woocommerce-js', 'woocommerce-active', 'woocommerce-uses-block-theme', 'woocommerce-block-theme-has-button-styles' ); 37 22 38 /** 39 * Classes without a WooCommerce prefix that its stylesheets or scripts still act on. 40 */ 23 const ACCOUNT = '/^(?:wp-block-woocommerce-customer-account|wc-block-customer-account)/'; 24 41 25 const SIGNS = array( 'add_to_cart_button', 'added_to_cart', 'remove_from_cart_button', 'demo_store', 'show-password-input', 'coming-soon-footer-banner-dismiss', 'qty', 'star-rating', 'onsale', 'products', 'shop_table', 'cart_list', 'product_list_widget', 'widget_shopping_cart', 'widget_shopping_cart_content' ); 42 26 … … 59 43 } 60 44 61 /**62 * A page outside the store that shows products is tried as well: it has to come through unchanged.63 */64 45 public function targets() { 65 46 if ( ! function_exists( 'wc_get_page_id' ) ) { … … 85 66 ); 86 67 87 // The cart and checkout pages hold a WooCommerce block or shortcode too, and they are the store.88 68 $outside = array_values( array_diff( array_map( 'intval', $found ), $store ) ); 89 69 … … 107 87 108 88 public function hook() { 109 add_action( 'template_redirect', array( $this, 'buffer' ), PHP_INT_MAX);89 Pipeline::page( $this->id(), 'remove', array( $this, 'strip' ), array( $this, 'wants' ) ); 110 90 add_action( 'woocommerce_before_template_part', array( $this, 'rendered' ), 10, 0 ); 111 91 add_filter( 'render_block', array( $this, 'block' ), 10, 2 ); … … 118 98 119 99 public function block( $content, $block ) { 120 if ( isset( $block['blockName'] ) && 0 === strpos( (string) $block['blockName'], 'woocommerce/' ) && ' ' !== trim( (string) $content ) ) {100 if ( isset( $block['blockName'] ) && 0 === strpos( (string) $block['blockName'], 'woocommerce/' ) && 'woocommerce/customer-account' !== $block['blockName'] && '' !== trim( (string) $content ) ) { 121 101 $this->rendered = true; 122 102 } … … 133 113 } 134 114 135 public function buffer() { 136 if ( is_feed() || is_embed() || is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) { 137 return; 138 } 139 140 add_filter( 'wp_template_enhancement_output_buffer', array( $this, 'strip' ), 20, 1 ); 141 } 142 143 /** 144 * @param string $html The finished page. 145 * @return bool Whether anything in the page is WooCommerce's to style or to run. 146 */ 115 public function wants() { 116 return ! ( is_feed() || is_embed() || is_woocommerce() || is_cart() || is_checkout() || is_account_page() ); 117 } 118 147 119 public static function in_page( $html ) { 148 120 $head = stripos( $html, '</head>' ); … … 160 132 161 133 foreach ( array_diff( array_unique( $classes ), self::ALWAYS ) as $class ) { 134 if ( preg_match( self::ACCOUNT, $class ) ) { 135 continue; 136 } 137 162 138 if ( in_array( $class, self::SIGNS, true ) || preg_match( '/^(?:woocommerce|wc[-_]|wp-block-woocommerce)/', $class ) ) { 163 139 return true; … … 171 147 $head = stripos( $html, '</head>' ); 172 148 173 // Anything but one whole page (a page flushed in parts, a file download) passes through as it is.174 149 if ( false === $head || false === stripos( $html, '</body>', $head ) || $this->rendered || self::in_page( $html ) ) { 175 150 return $html; … … 210 185 } 211 186 212 /**213 * The script tags that can go: WooCommerce's own files that no other script on the page was queued after.214 *215 * @return array Tag => handle, or an empty string for a tag that only carries a script's settings.216 */217 187 private function scripts( $html, $own ) { 218 188 $tops = array(); … … 230 200 } 231 201 232 // A script elsewhere on the page that reads WooCommerce's settings would break without them.233 202 $rest = str_replace( array_keys( $gone ), '', $html ); 234 203 … … 258 227 } 259 228 260 /**261 * @param string[] $tops The scripts of WooCommerce found on the page that may go.262 * @return string[] Those of them, and of the libraries they bring along, that nothing else on the page needs.263 */264 229 private function spare( array $tops ) { 265 230 $scripts = wp_scripts(); -
imzura-site-check/tags/1.3.0/readme.txt
r3797250 r3797325 5 5 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 Stable tag: 1. 0.07 Stable tag: 1.3.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html 10 10 11 Find s what slows your site down and tests every fix on your own pages before it stays on. Works next to your cache plugin.11 Find what makes WordPress slow and fix it safely: Core Web Vitals, PageSpeed, WooCommerce. Every fix is tested on your own pages before it stays on. 12 12 13 13 == Description == 14 14 15 A slow WordPress site usually has a few causes: a plugin that loads its files on every page, a heavy picture, a font from another server, a script that keeps the browser busy, a database that has piled up. PageSpeed Insights gives a score; this plugin names the plugin, theme or file behind each problem on your own pages, and fixes what can be fixed safely. It works next to the cache plugin you already have. 16 15 17 This plugin does two things. 16 18 … … 23 25 = What the check looks at = 24 26 27 The check opens your pages in a hidden frame of its own screen, at phone width and as a first-time visitor, so it sees what a visitor's browser sees, not only what the server sends. 28 25 29 * Front-end pages: time the server takes, the weight of each page and whose files make it up, plugins that load on pages that do not use them, heavy pictures and videos 26 * A table of what each plugin and the theme add to your pages: number of scripts and stylesheets, weight, on how many pages 30 * A table of what each plugin and the theme cost your pages: how long its code keeps the server busy and how many database queries it makes, its scripts and stylesheets and their weight, on how many pages 31 * Plugins that keep the server busy: a plugin (or the theme) whose code takes 100 ms or more of a page (or 50 ms that are a quarter of the page's time), or makes 100 or more database queries there, is named, with the numbers for each page 32 * The same for the admin: the check also opens the dashboard and the posts list (and, with WooCommerce, the products and orders lists) as you, and names the plugins whose code, dashboard widgets included, keeps those screens waiting 33 * Plugins that load their files on pages where nothing of them is shown: the browser checks every rule of each plugin's stylesheets against the page, and a plugin none of whose rules apply there is named, with the weight it adds 34 * The largest stylesheets (50 KB or more) and scripts (100 KB or more), with the plugin or theme that adds each one 35 * Errors on your pages, each put down to the plugin or theme it comes from: PHP errors raised while the page is built (fatal ones included), script errors in the browser, and files the page asks for that do not load 36 * Pictures: the largest picture on the first screen waiting for lazy loading or not marked as urgent, other lazy pictures on the first screen, pictures without a width and height, how far the page jumps while it loads (layout shift), with what moved it, animations on the first screen that never stop, how much more phones with 3× screens download than 2× screens (from each picture's srcset and the files on your server), and how many copies of each upload WordPress keeps and the disk space they take, with the theme or plugin behind each picture size 37 * Fonts: fonts loaded from Google's servers, how many font files a page loads, text that waits for its font, Font Awesome loaded more than once, the same font loaded twice (from two Google Fonts stylesheets, or the same font file from two places) with the theme or plugin behind each, font preloads that cannot be used, and a font loaded for less than 3% of the text next to the font the rest of the page uses 38 * Scripts: files from other websites, the same library loaded twice, and which plugin's scripts keep the browser busy while the page loads (in Chrome, Edge and other Chromium-based browsers) 39 * Speed settings of well-known plugins that are off: Elementor (Inline Font Icons, CSS Print Method, optimized image loading, lazy background images, local Google Fonts and their font-display), Formidable Forms styles on every page, Wordfence logging every visit, and the local Google Fonts setting of Astra, Kadence, OceanWP and Spectra (or GeneratePress font-display) when their fonts come from Google. This plugin only reads these settings and names them as each plugin's screen does 40 * Housekeeping: the PHP error log (whether strangers can download it, its size, the plugins that fill it), backup and archive files left in wp-content, the largest database tables, plugins that are installed but switched off, and two SEO plugins active at once 27 41 * Settings WordPress loads on every request (autoloaded options) and the largest of them 28 42 * Scheduled jobs that have fallen behind, including the Action Scheduler queue 29 43 * WooCommerce: order storage, sessions that pile up, the cart script running outside the shop 30 * The environment: PHP version, object cache, the page cache in use 44 * The environment: PHP version, object cache, the page cache in use, and a cache plugin that is active with its page caching switched off (WP Super Cache, W3 Total Cache, WP Fastest Cache, WP-Optimize, Breeze, Hummingbird), named with the setting 31 45 * How the server delivers pages and files: compressed (gzip, Brotli) or not, and whether browsers may keep stylesheets and scripts 32 46 … … 37 51 * **Load Contact Form 7 only on pages that have a form.** Its script and stylesheet, and the reCAPTCHA or Turnstile script when one is connected, load only where a form is shown. Forms added by a template or placed with Elementor are covered. 38 52 * **Serve heavy pictures at the size they are shown.** A JPEG or PNG far larger than the space it fills is replaced, for visitors, by a copy of the right size in WebP. The original stays in the media library untouched. Needs WordPress 6.9 or newer. 39 * **Minify stylesheets (CSS)** and **Minify scripts (JavaScript)**, two separate fixes. The spaces, line breaks and comments come out of your theme's and plugins' own files, and a smaller copy is sent instead, under the same handle. Files that are minified already, files from other sites and code written into the page are left alone, and so is a file that would not get at least 5% smaller. Addresses inside stylesheets are rewritten, so fonts and pictures still load. 53 * **Load the main picture of each page first.** The largest picture on the first screen is what Google times as the page's Largest Contentful Paint. Some themes and page builders make it wait for lazy loading or leave it unmarked, so the browser fetches it last. The fix finds that picture on your pages as visitors see them and has the browser fetch it first; on posts and products whose main picture is the featured image, it does the same for every such page. Where WordPress already does this, the fix has nothing to do. Needs WordPress 6.9 or newer. 54 * **Serve Google Fonts from your own server.** Fonts from Google make the browser connect to two more servers before text in them can show, and they pass each visitor's IP address to Google. When you start its test, the fix downloads the fonts your pages load from Google into your uploads folder, adds font-display swap where it is missing, and points your pages at the copies. Fonts that a stylesheet pulls in with @import are left as they are. Needs WordPress 6.9 or newer. 55 * **Keep room for pictures while they load.** A picture without a width and height in its tag takes no room until it arrives, and the text around it jumps when it does (Cumulative Layout Shift). The fix reads the size of each such picture from its file on your server and writes it into the tag. Pictures that load only when scrolled to get it once the test has seen them load; pictures from other websites are left as they are. Needs WordPress 6.9 or newer. 56 * **Minify stylesheets (CSS)** and **Minify scripts (JavaScript)**, two separate fixes. The spaces, line breaks and comments come out of your theme's and plugins' own files, and a smaller copy is sent instead, under the same handle. Files that are minified already, files from other sites and code written into the page are left alone, and so is a file that would not get at least 5% smaller. Addresses inside stylesheets are rewritten, so fonts and pictures still load. If a script stops working once minified, the trial sees the error in that file, sends it as it is from then on and tests again; the fix is kept only if the pages then work, and the screen shows how many files are sent as they are. 40 57 * **Leave out the emoji script.** WordPress adds a script and styles to every page so that very old browsers can show emoji as pictures. Browsers in use today show emoji themselves. 41 58 * **Load jQuery without jQuery Migrate.** Migrate only helps code written for old versions of jQuery. If a script on your site still needs it, the test sees it fail and the fix is not kept. … … 76 93 This plugin sends nothing from your site anywhere. There is no account, no cloud service and no tracking. Everything it measures stays in your own database. The only link that leads away is one block on the plugin's own screen, for people who would rather have the work done for them. 77 94 95 One fix fetches files from outside, and only when you start its test: "Serve Google Fonts from your own server" downloads the font stylesheets your pages already load from Google Fonts (fonts.googleapis.com) and the font files they name (fonts.gstatic.com) into your uploads folder. The request carries nothing about your site or visitors beyond what any browser sends for those files. Google Fonts terms: https://developers.google.com/fonts/terms · Google privacy policy: https://policies.google.com/privacy 96 97 The fix "Show YouTube videos as a picture until play is pressed" changes what visitors' browsers load from YouTube, not whether they do: the page shows the video's picture from YouTube's image server (i.ytimg.com), and the YouTube player loads only when the visitor presses play. YouTube terms: https://www.youtube.com/t/terms 98 78 99 == Installation == 79 100 … … 84 105 == Frequently Asked Questions == 85 106 107 = Will it raise my PageSpeed Insights score? = 108 109 It removes causes the score is built from, such as files a page does not use, the main picture loading late, pictures without a size and fonts from another server, and each fix shows what it saved on your pages. How much the score moves depends on what your site had to begin with, so this plugin makes no promise about the number. 110 111 = Can someone fix my site for me? = 112 113 Yes. The people behind this plugin fix slow WordPress and WooCommerce sites for a fixed price, on a copy of the site first, with a before-and-after measurement: [imzura.com](https://imzura.com/services/wordpress-speed-optimization?utm_source=imzura-site-check&utm_medium=readme). The plugin works the same whether you ask or not. 114 86 115 = Does it replace my cache plugin? = 87 116 … … 121 150 122 151 The two scripts that record where a visitor came from. WooCommerce credits an order to the first page of the visit, and that is often a blog post or the homepage, so they are never removed. A stylesheet your theme has replaced with its own version is left alone as well: such a file also styles parts of the theme. On those themes the fix says "Nothing to gain". 152 153 = Why does this plugin not remove unused CSS? = 154 155 Because nobody can know for sure which rules a site will never need. A rule unused on the pages a tool looks at may be needed on another page, by a menu or a form once a visitor taps it, or by a script that adds a class later; a site broken that way can look fine in every test. So this plugin tells you which stylesheets are the largest and who adds them, minifies them if you want, and leaves the rules alone. 156 157 = How does this plugin time each plugin? = 158 159 Only while the check reads a page or an admin screen, this plugin places a marker in WordPress's hooks wherever the code passes from one plugin to another, and notes the time at each marker. A plugin's time is what its own code took, without the time of the other plugins it called on the way; its database queries are those its code asked for, directly or through WordPress. Dashboard widgets are not run from a hook, so each one is timed on its own. The plugins themselves are not changed or wrapped, and the visits of real visitors are not measured. The time it takes to load a plugin's files is not counted, so its real cost is a little higher than shown. 123 160 124 161 = Which pictures does the picture fix leave alone? = … … 139 176 140 177 == Changelog == 178 179 = 1.3.0 = 180 * After a fix is kept, the plugin's screen asks once, below the fixes, for a review on WordPress.org. "Do not ask again" hides it for good; nothing else on the admin changes. 181 * The fix that keeps room for pictures leaves alone pictures marked sizes="auto". Browsers that support it, such as Chrome, already give them a fixed box, and writing a size into the tag changed how they look, so the trial did not keep the fix. 182 * Add-ons can read what the check's browser saw on each page (the speed_doctor_seen action), and the list of plugins that may already do a job names Perfect Images and Disable Media Sizes when they choose which picture sizes are made. 183 * The fixes that change the finished page (WooCommerce files, heavy pictures, the main picture, Google Fonts, room for pictures, YouTube) use the page WordPress hands to plugins from 6.9 on, and need WordPress 6.9 or newer. The plugin opens no output buffer of its own across the page. 184 * New fix: stop Heartbeat calls from visitors. When a plugin loads WordPress Heartbeat on public pages, every open tab calls admin-ajax.php again and again, past any page cache. Before the trial this plugin calls admin-ajax.php itself as a visitor and looks at what answers Heartbeat there; only when nothing does is the Heartbeat file left out, with a stand-in that keeps the scripts that asked for it working. Logged-in users keep Heartbeat. 185 * Old revisions can be removed from the check's report: all but the newest 5 of each post (or the number set in WP_POST_REVISIONS) go, autosaves stay. They are first written to a compressed copy in wp-content/uploads/imzura-site-check/copies, so "Put them back" restores the same rows and fields; "Delete the copy" removes it. 186 * The check names Heartbeat loaded for visitors (how often it calls and who asks for it), admin screens where a plugin made Heartbeat call more often than every 60 seconds, and old revisions piling up in the database (how many, how much text, and the posts with the most). 187 * New fix: serve Google Fonts from your own server. The trial counts the other websites each page connects to, among those both untouched views connected to; fewer is a gain and shows as a column in the table. 188 * The check names the setting that keeps Google Fonts on your server in Astra, Kadence, OceanWP and Spectra, and GeneratePress font-display, when the fonts they load come from Google. 189 * The check weighs what phones with 3× screens download next to 2× screens, from the srcset of each picture and the sizes of its files on the server. PageSpeed Insights tests phones at a lower density and does not show it. 190 * The check counts the copies WordPress keeps of each picture (one per size the theme and plugins ask for) and the disk space they take, and names who added each size. It does not say which sizes to drop: pages pick sizes by screen width. 191 * The check names a font that is loaded twice: asked from Google Fonts by two stylesheets (with the theme or plugin that likely adds each), or the same font file downloaded from two places. 192 * The check also names animations on the first screen that never stop, and a font loaded for less than 3% of the text on pages where another font draws at least 10%. 193 * New fix: keep room for pictures while they load. Pictures without a width and height get them from their file on the server; the trial counts such pictures on each page and shows the count in the table. 194 * New fix: load the main picture of each page first. The trial counts a main picture that waits (lazy loading, or not marked urgent) and shows it in the table; a fix that makes the main picture wait is not kept. 195 * The WooCommerce files fix now works on block themes such as Twenty Twenty-Five. WooCommerce adds its account link to the header of block themes, so every page counted as showing something from the store and the fix found nothing to leave out. The plain account link no longer counts; an account menu that opens still does. 196 * The WooCommerce files fix also leaves out WooCommerce's block stylesheet on pages without anything from the store. 197 * The report keeps, for each page, which scripts and stylesheets it loaded, and the admin screen lets add-ons run a fix's trial as its own button does. 198 199 = 1.2.0 = 200 * The check now opens each page in a hidden frame, at phone width and as a first-time visitor, and reports what only a browser sees. 201 * Errors on your pages, each put down to the plugin or theme it comes from: PHP errors while the page is built (a fatal error is named as such, with the pages it stops), script errors, and files that do not load. 202 * Pictures: the largest picture on the first screen waiting for lazy loading or not marked as urgent, other lazy pictures on the first screen, pictures without a width and height, and layout shift with what moved the page. 203 * Fonts: Google's servers, many font files, text that waits for its font, Font Awesome loaded more than once, font preloads that cannot be used. 204 * Scripts: files from other websites, the same library loaded twice, scripts that keep the browser busy; the largest stylesheets and scripts with the plugin or theme behind each. 205 * Plugins that load their stylesheets on pages where none of their rules apply, and block styles on pages without blocks. 206 * Plugins that keep the server busy: how long the code of each plugin and the theme ran while each page was built, and how many database queries it made. The table of plugins shows it for all of them. The same for the dashboard, the posts list and, with WooCommerce, the products and orders lists, opened as the administrator. 207 * Speed settings that are off in Elementor, Formidable Forms and Wordfence, named as each plugin's own screen names them. 208 * A cache plugin that is active with its page caching switched off is named, with the setting. It no longer counts as a second cache next to the one in use (WP-Optimize kept for its database tools, for example). 209 * Housekeeping: the PHP error log (public, large, who fills it), backup files in wp-content, the largest database tables, switched-off plugins, two SEO plugins at once. 210 * The FAQ explains why this plugin minifies stylesheets but does not remove the parts a page does not use. 211 212 = 1.1.0 = 213 * Minify scripts: a file that stops working once minified is now sent as it is, and the trial runs again without it, instead of the whole fix being turned off. The fix shows how many files are sent as they are. 214 * Every fix now changes the page and its files in one pass, in a fixed order: files left out first, then the rest minified, then pictures and video players. Each fix works on what the fixes before it left, and a fix that fails leaves the page as the fixes before it made it. 141 215 142 216 = 1.0.0 = -
imzura-site-check/tags/1.3.0/uninstall.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Leaves nothing behind. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 defined( 'WP_UNINSTALL_PLUGIN' ) || exit; … … 15 11 delete_option( 'speed_doctor_form_page' ); 16 12 delete_option( 'speed_doctor_images' ); 13 delete_option( 'speed_doctor_lcp' ); 14 delete_option( 'speed_doctor_sizes' ); 15 delete_option( 'speed_doctor_fonts' ); 16 delete_option( 'speed_doctor_tidy' ); 17 delete_metadata( 'user', 0, 'speed_doctor_review', '', true ); 17 18 18 19 $speed_doctor_uploads = wp_get_upload_dir(); … … 31 32 } 32 33 34 if ( is_dir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/fonts' ) ) { 35 foreach ( array_diff( (array) scandir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/fonts' ), array( '.', '..' ) ) as $speed_doctor_copy ) { 36 wp_delete_file( $speed_doctor_uploads['basedir'] . '/imzura-site-check/fonts/' . $speed_doctor_copy ); 37 } 38 39 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- removes this plugin's own, now empty, folder of font copies. 40 rmdir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/fonts' ); 41 } 42 43 if ( is_dir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/copies' ) ) { 44 foreach ( array_diff( (array) scandir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/copies' ), array( '.', '..' ) ) as $speed_doctor_copy ) { 45 wp_delete_file( $speed_doctor_uploads['basedir'] . '/imzura-site-check/copies/' . $speed_doctor_copy ); 46 } 47 48 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- removes this plugin's own, now empty, folder of revision copies. 49 rmdir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/copies' ); 50 } 51 33 52 if ( is_dir( $speed_doctor_uploads['basedir'] . '/imzura-site-check' ) ) { 34 53 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- removes this plugin's own, now empty, folder of picture copies. -
imzura-site-check/trunk/assets/admin.css
r3797250 r3797325 1 /* imzura Site Check by imzura.com */ 1 2 .imzura-site-check { 2 3 --sp-ink: #14213d; … … 180 181 } 181 182 183 .speed-doctor-review { 184 display: flex; 185 flex-wrap: wrap; 186 align-items: center; 187 gap: 8px 16px; 188 margin-top: 32px; 189 padding: 16px 20px; 190 border: 1px solid var(--sp-line); 191 border-radius: 6px; 192 background: #fff; 193 } 194 195 .speed-doctor-review p { 196 flex: 1 1 32ch; 197 max-width: 70ch; 198 margin: 0; 199 font-size: 14px; 200 line-height: 1.6; 201 } 202 203 .speed-doctor-review + .speed-doctor-help { 204 margin-top: 16px; 205 } 206 182 207 .speed-doctor-help h2 { 183 208 margin: 0 0 8px; … … 268 293 font-weight: 600; 269 294 color: var(--sp-good); 295 } 296 297 .speed-doctor-tidy { 298 margin: 0 0 16px; 299 padding: 16px 16px 16px 20px; 300 border: 1px solid var(--sp-line); 301 border-radius: 6px; 302 background: var(--sp-card); 303 } 304 305 .speed-doctor-tidy p { 306 max-width: 78ch; 307 margin: 0; 308 font-size: 14px; 309 line-height: 1.6; 310 } 311 312 .speed-doctor-tidy p + p { 313 margin-top: 8px; 270 314 } 271 315 … … 308 352 } 309 353 354 .speed-doctor-frame.is-scan { 355 opacity: 0.01; 356 } 357 310 358 .speed-doctor-tabs { 311 359 margin: 0 0 20px; -
imzura-site-check/trunk/assets/admin.js
r3797250 r3797325 1 // imzura Site Check by imzura.com 1 2 ( function () { 2 3 'use strict'; … … 13 14 const cacheBox = document.getElementById( 'speed-doctor-cache' ); 14 15 const tabs = Array.from( document.querySelectorAll( '.speed-doctor-tabs [role="tab"]' ) ); 16 const tidyBox = document.getElementById( 'speed-doctor-tidy' ); 15 17 let fixes = data.fixes || []; 18 let tidy = data.tidy || null; 19 let tidyNote = ''; 16 20 let lastReport = data.report; 17 21 let busy = false; … … 98 102 const fix = finding.fix && fixes.find( ( item ) => item.id === finding.fix ); 99 103 100 if ( fix && fix.state === 'on' ) { 104 if ( finding.fix === 'tidy:revisions' && tidy ) { 105 card.append( el( 'p', '', text.tidy_card ) ); 106 } else if ( finding.fix === 'tidy:revisions' ) { 107 const actions = el( 'div', 'speed-doctor-actions' ); 108 actions.append( tidyButton( text.tidy_go, 'run', true ) ); 109 card.append( actions ); 110 } else if ( fix && fix.state === 'on' ) { 101 111 card.append( el( 'p', '', text.fixed ) ); 102 112 } else if ( fix && fix.state !== 'unavailable' ) { … … 125 135 const thead = el( 'thead' ); 126 136 const body = el( 'tbody' ); 127 const read = report.pages.filter( ( page ) => page.read ).length;128 129 [ text.owner, text. files, text.owner_kb, text.owner_pages ].forEach( ( label ) => top.append( el( 'th', '', label ) ) );137 const read = report.pages.filter( ( page ) => page.read && ! page.admin ).length; 138 139 [ text.owner, text.owner_ms, text.files, text.owner_kb, text.owner_pages ].forEach( ( label ) => top.append( el( 'th', '', label ) ) ); 130 140 thead.append( top ); 131 141 … … 134 144 row.append( 135 145 el( 'td', '', owner.kind === 'theme' ? fill( text.owner_theme, [ owner.name ] ) : owner.name ), 146 el( 'td', '', owner.ms === null || owner.ms === undefined ? '—' : fill( text.owner_code, [ String( owner.ms ), String( owner.queries ) ] ) ), 136 147 el( 'td', '', String( owner.files ) ), 137 148 el( 'td', '', owner.kb + ' ' + text.kb ), … … 184 195 const started = await call( '/scan', 'POST' ); 185 196 const targets = started.targets; 197 const pages = {}; 198 const busy = {}; 199 let reading = ''; 200 let watcher = null; 201 202 try { 203 watcher = new PerformanceObserver( ( list ) => list.getEntries().forEach( ( entry ) => ( entry.scripts || [] ).forEach( ( script ) => { 204 const url = String( script.sourceURL || '' ).split( /[?#]/ )[ 0 ]; 205 206 if ( reading && url && script.windowAttribution === 'descendant' ) { 207 busy[ reading ][ url ] = ( busy[ reading ][ url ] || 0 ) + script.duration; 208 } 209 } ) ) ); 210 watcher.observe( { type: 'long-animation-frame' } ); 211 } catch ( error ) { 212 watcher = null; 213 } 186 214 187 215 for ( let i = 0; i < targets.length; i++ ) { 188 216 status.textContent = fill( text.reading, [ String( i + 1 ), String( targets.length ), targets[ i ].label ] ); 189 try { 190 const page = await fetch( targets[ i ].url, { credentials: 'omit', cache: 'no-store', redirect: 'follow' } ); 191 await page.text(); 192 } catch ( error ) { 193 // A page that cannot be opened is reported as not read; the rest of the check goes on. 194 } 217 reading = targets[ i ].kind; 218 busy[ reading ] = {}; 219 220 const seen = await open( Object.assign( { screen: 'phone', scan: true }, targets[ i ] ) ); 221 222 if ( seen ) { 223 pages[ reading ] = { errors: seen.errors, sources: seen.sources, look: Object.assign( {}, seen.look, { busy: Object.keys( busy[ reading ] ).map( ( url ) => ( { url: url.slice( 0, 300 ), ms: Math.round( busy[ reading ][ url ] ) } ) ).sort( ( a, b ) => b.ms - a.ms ).slice( 0, 20 ) } ) }; 224 } 225 } 226 227 reading = ''; 228 229 if ( watcher ) { 230 watcher.disconnect(); 195 231 } 196 232 197 233 status.textContent = text.judging; 198 const seen = await headersOf( { page: data.cache.home, asset: data.asset } ); 234 const urls = { page: data.cache.home, asset: data.asset }; 235 236 if ( data.log ) { 237 urls.log = data.log; 238 } 239 240 const seen = await headersOf( urls ); 241 seen.pages = pages; 199 242 const report = await call( '/report', 'POST', { tokens: targets.map( ( target ) => target.token ), seen: seen } ); 200 243 show( report ); … … 206 249 } 207 250 208 // Compression and browser caching are added by the server after WordPress has finished: only the browser sees them.209 251 async function headersOf( urls ) { 210 252 const seen = {}; … … 212 254 for ( const [ kind, url ] of Object.entries( urls ) ) { 213 255 try { 214 const answer = await fetch( url, { credentials: 'omit', cache: 'no-store' } );256 const answer = await fetch( url, { credentials: 'omit', cache: 'no-store', method: kind === 'log' ? 'HEAD' : 'GET' } ); 215 257 await answer.arrayBuffer(); 216 seen[ kind ] = { };258 seen[ kind ] = { status: answer.status }; 217 259 [ 'content-encoding', 'cache-control', 'expires', 'date' ].forEach( ( name ) => { 218 260 if ( answer.headers.has( name ) ) { … … 221 263 } ); 222 264 } catch ( error ) { 223 // The check goes on without what this file would have shown.224 265 } 225 266 } … … 271 312 } ); 272 313 314 if ( fix.excused ) { 315 card.append( el( 'p', 'speed-doctor-note', fill( text.excused, [ String( fix.excused ) ] ) ) ); 316 } 317 273 318 if ( fix.problems.length ) { 274 319 const list = el( 'ul', 'speed-doctor-facts' ); … … 280 325 const table = el( 'table', 'speed-doctor-proof' ); 281 326 const top = el( 'tr' ); 282 // A player the fix holds back loads inside its own frame, which the page weight does not count: show the frames when they changed.283 327 const framed = fix.pages.some( ( page ) => page.frames && page.frames[ 0 ] !== page.frames[ 1 ] ); 284 [ text.page, text.calls, text.files, text.weight, text.blocking ].concat( framed ? [ text.frames ] : [] ).forEach( ( label ) => top.append( el( 'th', '', label ) ) ); 328 const held = fix.pages.some( ( page ) => page.lcp && page.lcp[ 0 ] !== page.lcp[ 1 ] ); 329 const sized = fix.pages.some( ( page ) => page.sizes && page.sizes[ 0 ] !== page.sizes[ 1 ] ); 330 const hosted = fix.pages.some( ( page ) => page.hosts && page.hosts[ 0 ] !== page.hosts[ 1 ] ); 331 [ text.page, text.calls, text.files, text.weight, text.blocking ].concat( framed ? [ text.frames ] : [], held ? [ text.lcp ] : [], sized ? [ text.sizes ] : [], hosted ? [ text.hosts ] : [] ).forEach( ( label ) => top.append( el( 'th', '', label ) ) ); 285 332 table.append( top ); 286 333 fix.pages.forEach( ( page ) => { … … 290 337 row.append( pair( page.frames || [ 0, 0 ] ) ); 291 338 } 339 if ( held ) { 340 row.append( pair( page.lcp || [ 0, 0 ] ) ); 341 } 342 if ( sized ) { 343 row.append( pair( page.sizes || [ 0, 0 ] ) ); 344 } 345 if ( hosted ) { 346 row.append( pair( page.hosts || [ 0, 0 ] ) ); 347 } 292 348 table.append( row ); 293 349 } ); … … 339 395 } 340 396 } ); 341 } 342 343 // Shows one tab and remembers it in the address, so a reload or a shared link opens the same tab. 397 398 askReview( rescue ); 399 } 400 401 function askReview( working ) { 402 const shown = document.getElementById( 'speed-doctor-review' ); 403 const help = document.querySelector( '.speed-doctor-help' ); 404 405 if ( shown ) { 406 shown.remove(); 407 } 408 409 if ( ! working || ! help || ! data.review || ! data.review.due ) { 410 return; 411 } 412 413 const box = el( 'section', 'speed-doctor-review' ); 414 const go = el( 'a', 'button button-primary', text.review_go ); 415 const no = el( 'button', 'button-link', text.review_no ); 416 const done = () => { 417 data.review.due = false; 418 box.remove(); 419 call( '/review', 'POST' ).catch( () => {} ); 420 }; 421 422 box.id = 'speed-doctor-review'; 423 go.href = data.review.url; 424 go.target = '_blank'; 425 go.rel = 'noopener'; 426 no.type = 'button'; 427 go.addEventListener( 'click', done ); 428 no.addEventListener( 'click', done ); 429 box.append( el( 'p', '', text.review_ask ), go, no ); 430 help.before( box ); 431 } 432 344 433 function activate( name, focus ) { 345 434 if ( ! tabs.some( ( tab ) => tab.dataset.tab === name ) ) { … … 380 469 el( 'p', 'speed-doctor-note', text.cache_lead ), 381 470 el( 'p', '', cache.plugins.length ? fill( text.cache_found, [ cache.plugins.join( ', ' ) ] ) : text.cache_none ), 471 ...( cache.off && cache.off.length ? [ el( 'p', '', fill( text.cache_off, [ cache.off.join( ', ' ) ] ) ) ] : [] ), 382 472 el( 'p', '', cache.ready ? text.cache_ready : text.cache_unset ), 383 473 actions, … … 386 476 } 387 477 388 // Opens the home page twice the way a first-time visitor would (no cookies) and reports what the server said.389 478 async function visit( result, button ) { 390 479 const headers = [ 'age', 'cf-cache-status', 'x-cache', 'x-cache-status', 'x-litespeed-cache', 'x-proxy-cache' ]; … … 428 517 function refresh() { 429 518 showFixes(); 519 showTidy(); 430 520 show( lastReport ); 521 } 522 523 function size( bytes ) { 524 return bytes >= 1048576 ? fill( text.mb, [ ( bytes / 1048576 ).toLocaleString( undefined, { maximumFractionDigits: 1 } ) ] ) : Math.max( 1, Math.round( bytes / 1024 ) ).toLocaleString() + ' ' + text.kb; 525 } 526 527 function tidyButton( label, step, primary ) { 528 const go = el( 'button', primary ? 'button button-primary' : 'button', label ); 529 go.type = 'button'; 530 go.disabled = busy; 531 go.addEventListener( 'click', () => cleanUp( step ) ); 532 return go; 533 } 534 535 function showTidy() { 536 tidyBox.replaceChildren(); 537 tidyBox.hidden = ! tidy && ! tidyNote; 538 539 if ( tidyNote ) { 540 tidyBox.append( el( 'p', 'speed-doctor-tidy-note', tidyNote ) ); 541 } 542 543 if ( ! tidy ) { 544 return; 545 } 546 547 const actions = el( 'div', 'speed-doctor-actions' ); 548 let lead = fill( text.tidy_done, [ tidy.rows.toLocaleString(), new Date( tidy.time * 1000 ).toLocaleString(), size( tidy.bytes ) ] ); 549 550 if ( tidy.restoring ) { 551 lead = fill( text.tidy_halfway, [ tidy.restored.toLocaleString() ] ); 552 actions.append( tidyButton( text.tidy_on, 'undo', true ) ); 553 } else if ( ! tidy.done ) { 554 lead = fill( text.tidy_partial, [ tidy.rows.toLocaleString(), tidy.left.toLocaleString() ] ); 555 actions.append( tidyButton( text.tidy_on, 'run', true ), tidyButton( text.tidy_undo, 'undo' ) ); 556 } else { 557 actions.append( tidyButton( text.tidy_undo, 'undo' ) ); 558 } 559 560 actions.append( tidyButton( text.tidy_forget, 'forget' ) ); 561 tidyBox.append( el( 'p', '', lead ), actions ); 562 } 563 564 async function cleanUp( step ) { 565 if ( busy || ( step === 'forget' && ! window.confirm( text.tidy_confirm ) ) ) { 566 return; 567 } 568 569 busy = true; 570 tidyNote = ''; 571 refresh(); 572 573 try { 574 for ( ;; ) { 575 const answer = ( await call( '/tidy/revisions/' + step, 'POST' ) ).tidy; 576 577 if ( step === 'forget' || ! answer ) { 578 tidy = null; 579 tidyNote = text.tidy_forgotten; 580 break; 581 } 582 583 if ( answer.finished ) { 584 tidy = null; 585 tidyNote = fill( text.tidy_restored, [ answer.restored.toLocaleString(), answer.skipped.toLocaleString() ] ); 586 break; 587 } 588 589 tidy = answer; 590 591 if ( step === 'run' && answer.done ) { 592 break; 593 } 594 595 tidyNote = fill( step === 'run' ? text.tidy_removing : text.tidy_restoring, [ ( step === 'run' ? answer.rows : answer.restored ).toLocaleString() ] ); 596 showTidy(); 597 } 598 599 if ( step === 'run' ) { 600 tidyNote = ''; 601 } 602 } catch ( error ) { 603 tidyNote = error.reason || text.tidy_failed; 604 } 605 606 busy = false; 607 refresh(); 431 608 } 432 609 … … 439 616 } 440 617 441 // Opens a page in a hidden frame and waits for assets/probe.js inside it to say what the browser saw.442 618 function open( load ) { 443 619 return new Promise( ( resolve ) => { 444 const frame = el( 'iframe', 'speed-doctor-frame' + ( load.screen === 'phone' ? ' is-phone' : '' ) );620 const frame = el( 'iframe', 'speed-doctor-frame' + ( load.screen === 'phone' ? ' is-phone' : '' ) + ( load.scan ? ' is-scan' : '' ) ); 445 621 let timer = 0; 446 622 … … 463 639 window.addEventListener( 'message', hear ); 464 640 timer = setTimeout( () => finish( null ), 45000 ); 641 642 frame.addEventListener( 'load', () => { 643 clearTimeout( timer ); 644 timer = setTimeout( () => finish( null ), load.admin ? 300 : 20000 ); 645 } ); 465 646 frame.src = load.url; 466 647 document.body.append( frame ); … … 489 670 } 490 671 491 // Compares only the parts of a page that were identical on both untouched views: a part that changes492 // on its own (random products, a rotating banner) says nothing about the fix.493 672 function changed( before, control, after ) { 494 673 const first = groups( before ); … … 531 710 } 532 711 533 // How many boxes are somewhere else, beyond the slack given. Boxes are matched in page order, spot by spot.534 712 function apart( one, other, slack ) { 535 713 let count = Math.abs( one.length - other.length ); … … 547 725 } 548 726 549 // The layout, compared with a little slack: a picture half a pixel taller pushes the rest of the page down550 // by half a pixel, which nobody can see and which is not a change. Spots that move between the two551 // untouched views are left out, as in changed().552 727 function shifted( before, control, after ) { 553 728 const first = groups( before, true ); … … 585 760 } 586 761 587 // Listeners both untouched views agree on that are gone with the fix, although the element they were on is588 // still there. A listener that left together with its element is a change of the page, and is caught as one.589 762 function deaf( before, control, after ) { 590 763 const count = ( list ) => ( list || [] ).reduce( ( map, key ) => map.set( key, ( map.get( key ) || 0 ) + 1 ), new Map() ); … … 608 781 } 609 782 610 function judge( page, problems, numbers ) {783 function judge( page, problems, numbers, phone ) { 611 784 const before = page.seen.before; 612 785 const control = page.seen.control; … … 618 791 } 619 792 620 added( before.errors, control.errors, after.errors ).slice( 0, 3 ).forEach( ( error ) => problems.push( { label: page.label, kind: 'error', detail: error } ) );793 added( before.errors, control.errors, after.errors ).slice( 0, 3 ).forEach( ( error ) => problems.push( { label: page.label, kind: 'error', detail: error, file: ( after.sources || {} )[ error ] || '' } ) ); 621 794 622 795 const words = changed( before.texts, control.texts, after.texts ); … … 626 799 } 627 800 628 // A page that holds still is compared by where things are on the page. One that moves by itself (a629 // random product with a longer name pushes the rest down) is compared by where things are in their parent.630 801 const still = before.boxes.page.length === control.boxes.page.length && shifted( before.boxes.page, control.boxes.page, [] ).steady === before.boxes.page.length; 631 802 const frame = still ? 'page' : 'parent'; … … 644 815 deaf( before, control, after ).slice( 0, 2 ).forEach( ( lost ) => problems.push( { label: page.label, kind: 'listener', detail: lost } ) ); 645 816 646 // The narrow view is there to catch what breaks; what the fix saves is counted once, on the wide one. 817 const held = ( view ) => ( ( view && view.lcp ) || [] ).filter( ( item ) => item.held ).length; 818 const sizeless = ( view ) => ( ( view && view.sizes ) || [] ).length; 819 const hosts = ( view ) => new Set( view.files.map( ( file ) => { 820 try { 821 return new URL( file ).host; 822 } catch ( error ) { 823 return ''; 824 } 825 } ).filter( ( host ) => host && host !== window.location.host ) ); 826 const early = hosts( before ); 827 const plain = hosts( control ); 828 const later = hosts( after ); 829 const steady = Array.from( early ).filter( ( host ) => plain.has( host ) ); 830 831 if ( held( after ) > Math.max( held( before ), held( control ) ) ) { 832 problems.push( { label: page.label, kind: 'lcp', detail: '' } ); 833 } 834 647 835 if ( page.screen === 'phone' ) { 648 836 return; 649 837 } 650 838 651 // Scripts and styles, counted on what both untouched views agree on, so a file that comes and goes 652 // by itself is not credited to the fix. 653 const code = ( view ) => view.files.filter( ( file ) => /\.(js|css)$/.test( file ) ); 839 const code = ( view ) => view.files.filter( ( file ) => /\.(js|css)$/.test( file ) || /^https?:\/\/fonts\.googleapis\.com\/css2?$/.test( file ) ); 654 840 const files = both( code( before ), code( control ) ); 655 841 const kept = new Set( after.files ); … … 665 851 blocking: [ control.blocking || 0, after.blocking || 0 ], 666 852 frames: [ control.frames || 0, after.frames || 0 ], 853 lcp: [ held( control ) + held( phone && phone.seen.control ), held( after ) + held( phone && phone.seen.after ) ], 854 sizes: [ sizeless( control ) + sizeless( phone && phone.seen.control ), sizeless( after ) + sizeless( phone && phone.seen.after ) ], 855 hosts: [ steady.length, steady.filter( ( host ) => later.has( host ) ).length + Array.from( later ).filter( ( host ) => ! early.has( host ) && ! plain.has( host ) ).length ], 667 856 } ); 668 857 } … … 677 866 678 867 try { 679 say( fix, text.starting ); 680 681 const plan = await call( '/fixes/' + fix.id + '/trial', 'POST' ); 682 const seen = []; 683 let prepared = ! plan.evidence; 684 685 for ( let i = 0; i < plan.loads.length; i++ ) { 686 const load = plan.loads[ i ]; 687 688 // The untouched views are in: the fix gets what they showed before any view with it is opened. 689 if ( ! prepared && load.step === 'after' ) { 690 const widest = new Map(); 691 692 seen.forEach( ( page ) => Object.keys( page.seen ).forEach( ( step ) => ( ( page.seen[ step ] || {} )[ plan.evidence ] || [] ).forEach( ( item ) => { 693 const known = widest.get( item.src ); 694 695 if ( ! known || item.shown > known.shown ) { 696 widest.set( item.src, item ); 697 } 698 } ) ) ); 699 700 say( fix, text.preparing ); 701 await call( '/fixes/' + fix.id + '/prepare', 'POST', { evidence: Array.from( widest.values() ) } ); 702 prepared = true; 703 } 704 let page = seen.find( ( item ) => item.kind === load.kind ); 705 706 if ( ! page ) { 707 page = { kind: load.kind, label: load.label, screen: load.screen, seen: {} }; 708 seen.push( page ); 709 } 710 711 say( fix, fill( text.testing, [ String( i + 1 ), String( plan.loads.length ), load.label ] ) ); 712 page.seen[ load.step ] = await open( load ); 713 } 714 715 say( fix, text.deciding ); 716 717 const problems = []; 718 const numbers = []; 719 seen.forEach( ( page ) => judge( page, problems, numbers ) ); 720 721 const answer = await call( '/fixes/' + fix.id + '/verdict', 'POST', { tokens: plan.loads.map( ( load ) => load.token ), pages: numbers, problems: problems } ); 722 fixes = answer.fixes; 868 for ( let round = 0; ; round++ ) { 869 say( fix, round ? text.aside : text.starting ); 870 871 const plan = await call( '/fixes/' + fix.id + '/trial', 'POST' ); 872 const seen = []; 873 let prepared = ! plan.evidence; 874 875 for ( let i = 0; i < plan.loads.length; i++ ) { 876 const load = plan.loads[ i ]; 877 878 if ( ! prepared && load.step === 'after' ) { 879 const widest = new Map(); 880 881 seen.forEach( ( page ) => Object.keys( page.seen ).forEach( ( step ) => ( ( page.seen[ step ] || {} )[ plan.evidence ] || [] ).forEach( ( item ) => { 882 const key = item.src + ' ' + ( item.page || '' ); 883 const known = widest.get( key ); 884 885 if ( ! known || item.shown > known.shown ) { 886 widest.set( key, item ); 887 } 888 } ) ) ); 889 890 say( fix, text.preparing ); 891 await call( '/fixes/' + fix.id + '/prepare', 'POST', { evidence: Array.from( widest.values() ) } ); 892 prepared = true; 893 } 894 let page = seen.find( ( item ) => item.kind === load.kind ); 895 896 if ( ! page ) { 897 page = { kind: load.kind, label: load.label, screen: load.screen, seen: {} }; 898 seen.push( page ); 899 } 900 901 say( fix, fill( text.testing, [ String( i + 1 ), String( plan.loads.length ), load.label ] ) ); 902 page.seen[ load.step ] = await open( load ); 903 } 904 905 say( fix, text.deciding ); 906 907 const problems = []; 908 const numbers = []; 909 seen.forEach( ( page ) => judge( page, problems, numbers, seen.find( ( item ) => item.kind === page.kind + '@phone' ) ) ); 910 911 const answer = await call( '/fixes/' + fix.id + '/verdict', 'POST', { tokens: plan.loads.map( ( load ) => load.token ), pages: numbers, problems: problems } ); 912 const outcome = answer.fixes.find( ( item ) => item.id === fix.id ); 913 fixes = answer.fixes; 914 915 if ( ! outcome || ! outcome.aside || round >= 3 ) { 916 break; 917 } 918 } 919 723 920 busy = false; 724 921 refresh(); … … 761 958 } ); 762 959 960 window.speedDoctor = { 961 fixes: () => fixes.slice(), 962 busy: () => busy, 963 trial: async ( id ) => { 964 const fix = fixes.find( ( item ) => item.id === id ); 965 966 if ( fix && ! busy ) { 967 await trial( fix ); 968 } 969 970 return fixes.find( ( item ) => item.id === id ) || null; 971 }, 972 tidy: async ( step ) => { 973 await cleanUp( step ); 974 975 return { tidy: tidy, note: tidyNote }; 976 }, 977 }; 978 763 979 button.addEventListener( 'click', scan ); 764 980 activate( window.location.hash.slice( 1 ) ); 765 981 showFixes(); 766 982 showCache(); 983 showTidy(); 767 984 show( data.report ); 768 985 } )(); -
imzura-site-check/trunk/assets/probe.js
r3797250 r3797325 7 7 8 8 const errors = []; 9 const sources = {}; 9 10 const calls = []; 10 11 const heard = []; … … 12 13 let fresh = true; 13 14 14 // Which elements scripts listen on. It is only noted; the listener is added as asked. An element counts15 // once per kind of event: a script that sets itself up again whenever the page changes adds the same16 // listener many times, and how many times depends on what else is on the page.17 15 const listen = EventTarget.prototype.addEventListener; 18 16 const told = new WeakMap(); … … 52 50 } 53 51 54 // The page runs as a first-time visitor: it cannot read or change what the administrator's browser holds.55 52 try { 56 53 Object.defineProperty( document, 'cookie', { configurable: true, get: () => '', set: () => {} } ); … … 61 58 } 62 59 63 // The same "random" choices on every view, so a page that shuffles its content can be compared with itself.64 60 let seed = 1; 65 61 … … 71 67 }; 72 68 73 // The same failure reaches the browser as "Uncaught Error: X" or as a rejected promise saying "X", 74 // depending on timing; both are recorded as "X" so one is not mistaken for a new error. 75 function note( kind, detail ) { 69 const painted = { lcp: null, shift: 0, shifters: [] }; 70 71 function observe( type, take ) { 72 try { 73 if ( ( PerformanceObserver.supportedEntryTypes || [] ).indexOf( type ) !== -1 ) { 74 new PerformanceObserver( ( list ) => list.getEntries().forEach( take ) ).observe( { type: type, buffered: true } ); 75 } 76 } catch ( error ) { 77 } 78 } 79 80 if ( window.PerformanceObserver ) { 81 observe( 'largest-contentful-paint', ( entry ) => { 82 painted.lcp = entry; 83 } ); 84 } 85 86 if ( probe.scan && window.PerformanceObserver ) { 87 observe( 'layout-shift', ( entry ) => { 88 if ( ! entry.hadRecentInput ) { 89 painted.shift += entry.value; 90 ( entry.sources || [] ).forEach( ( source ) => source.node && painted.shifters.push( [ entry.value, source.node ] ) ); 91 } 92 } ); 93 } 94 95 function note( kind, detail, file ) { 76 96 const said = String( detail ).replace( /^Uncaught\s+/, '' ).replace( /^\(in promise\)\s+/, '' ).replace( /^[A-Za-z]*Error:\s+/, '' ); 97 const key = kind + ': ' + said.slice( 0, 160 ); 77 98 78 99 if ( errors.length < 40 ) { 79 errors.push( kind + ': ' + said.slice( 0, 160 ) ); 100 errors.push( key ); 101 } 102 103 if ( file && ! sources[ key ] ) { 104 sources[ key ] = String( file ).split( /[?#]/ )[ 0 ]; 80 105 } 81 106 } … … 87 112 88 113 if ( target && target !== window && ( target.src || target.href ) ) { 89 note( 'not loaded', String( target.src || target.href ).split( /[?#]/ )[ 0 ] );114 note( 'not loaded', String( target.src || target.href ).split( /[?#]/ )[ 0 ], target.src || target.href ); 90 115 } else if ( event.message ) { 91 note( 'script', event.message );116 note( 'script', event.message, event.filename ); 92 117 } 93 118 }, … … 126 151 request.setRequestHeader( probe.header, probe.token ); 127 152 } catch ( error ) { 128 // The request was not opened; the browser reports that itself.129 153 } 130 154 } … … 166 190 const paths = new WeakMap(); 167 191 168 // Where an element sits in the page, by tag names only, so the same spot gets the same name on every view.169 192 function path( node ) { 170 193 if ( ! node || node === document.body ) { … … 196 219 } 197 220 198 // Every visible element twice: where it is on the page, and where it is inside its parent. The second199 // survives a part of the page above it changing height by itself.200 221 function boxes() { 201 222 const all = document.body.getElementsByTagName( '*' ); … … 210 231 known.set( all[ i ], box ); 211 232 212 // A box of a single pixel is how text meant for screen readers is hidden: nobody sees it move.213 233 if ( box.width >= 1 && box.height >= 1 && ( box.width > 1 || box.height > 1 ) ) { 214 234 found.page.push( path( all[ i ] ) + '\t' + round( [ box.left + window.scrollX, box.top + window.scrollY, box.width, box.height ] ) ); … … 220 240 } 221 241 222 // Every picture from this site, with the width it takes on the page.223 242 function images() { 224 243 const found = []; … … 235 254 236 255 return found; 256 } 257 258 function sizes() { 259 const found = []; 260 261 for ( let i = 0; i < document.images.length && found.length < 300; i++ ) { 262 const image = document.images[ i ]; 263 const box = image.getBoundingClientRect(); 264 const src = String( image.currentSrc || image.src || '' ); 265 266 if ( box.width >= 2 && box.height >= 2 && src && src.indexOf( 'data:' ) !== 0 && ( ! image.hasAttribute( 'width' ) || ! image.hasAttribute( 'height' ) ) ) { 267 found.push( { src: String( image.src || src ).split( /[?#]/ )[ 0 ], shown: Math.ceil( box.width ) } ); 268 } 269 } 270 271 return found; 272 } 273 274 function google() { 275 return Array.prototype.map.call( document.querySelectorAll( 'link[href*="fonts.googleapis.com/css"]' ), ( link ) => ( { src: String( link.href ).slice( 0, 1000 ) } ) ).slice( 0, 10 ); 276 } 277 278 function main() { 279 const entry = painted.lcp || largest(); 280 const element = entry && entry.element; 281 282 if ( ! element || element.nodeName !== 'IMG' ) { 283 return []; 284 } 285 286 const src = String( element.currentSrc || element.src || '' ).split( /[?#]/ )[ 0 ]; 287 288 if ( ! src || ! own( src ) ) { 289 return []; 290 } 291 292 return [ { 293 src: src, 294 page: window.location.pathname, 295 shown: Math.ceil( element.getBoundingClientRect().width ), 296 held: element.getAttribute( 'loading' ) === 'lazy' || element.getAttribute( 'fetchpriority' ) !== 'high', 297 featured: element.classList.contains( 'wp-post-image' ), 298 narrow: window.innerWidth < 600, 299 } ]; 237 300 } 238 301 … … 251 314 } 252 315 253 // What scripts listen for on things the page has: each listener put on an element of the page, and each254 // rule of the kind "a click on anything matching X" (jQuery keeps those on the body or the document)255 // whose X matches something. A script that only acts when the visitor does something shows up nowhere else.256 316 function ears() { 257 317 const found = []; … … 289 349 } 290 350 291 // How many elements sit at each spot, so a listener that went with its element is told from one that was lost.292 351 function spots() { 293 352 const all = document.body.getElementsByTagName( '*' ); … … 302 361 } 303 362 363 function describe( node ) { 364 if ( ! node || ! node.nodeName ) { 365 return ''; 366 } 367 368 let text = node.nodeName.toLowerCase(); 369 370 if ( node.id ) { 371 text += '#' + node.id; 372 } else if ( typeof node.className === 'string' && node.className.trim() ) { 373 text += '.' + node.className.trim().split( /\s+/ ).slice( 0, 2 ).join( '.' ); 374 } 375 376 return text.slice( 0, 80 ); 377 } 378 379 function plain( url ) { 380 return String( url || '' ).split( /[?#]/ )[ 0 ].slice( 0, 300 ); 381 } 382 383 function largest() { 384 let found = null; 385 let most = 0; 386 387 for ( let i = 0; i < document.images.length; i++ ) { 388 const box = document.images[ i ].getBoundingClientRect(); 389 const area = Math.max( 0, Math.min( box.right, window.innerWidth ) - Math.max( box.left, 0 ) ) * Math.max( 0, Math.min( box.bottom, window.innerHeight ) - Math.max( box.top, 0 ) ); 390 391 if ( area > most && box.top + window.scrollY < window.innerHeight ) { 392 most = area; 393 found = document.images[ i ]; 394 } 395 } 396 397 return found ? { element: found, url: found.currentSrc || found.src, guessed: true } : null; 398 } 399 400 function usage() { 401 const found = []; 402 const states = /::?(before|after|hover|focus|focus-visible|focus-within|active|visited|link|any-link|placeholder|selection|marker|backdrop|first-letter|first-line|checked|disabled|enabled|target|invalid|valid|required|optional|indeterminate|placeholder-shown|read-only|read-write|open|popover-open|-webkit-[\w-]+|-moz-[\w-]+|-ms-[\w-]+)(\([^)]*\))?/g; 403 let budget = 20000; 404 405 function walk( list, count ) { 406 for ( let i = 0; i < list.length && budget > 0; i++ ) { 407 const rule = list[ i ]; 408 409 if ( rule.selectorText ) { 410 budget--; 411 count.rules++; 412 count.used += rule.selectorText.split( ',' ).some( ( selector ) => { 413 try { 414 return document.querySelector( selector.replace( states, '' ).trim() || '*' ) !== null; 415 } catch ( error ) { 416 return true; 417 } 418 } ) ? 1 : 0; 419 } else if ( rule.cssRules ) { 420 walk( rule.cssRules, count ); 421 } 422 } 423 } 424 425 Array.prototype.forEach.call( document.styleSheets, ( sheet ) => { 426 let rules = null; 427 428 if ( ! sheet.href || ! own( sheet.href ) || found.length >= 60 ) { 429 return; 430 } 431 432 try { 433 rules = sheet.cssRules; 434 } catch ( error ) { 435 return; 436 } 437 438 const count = { url: plain( sheet.href ), rules: 0, used: 0 }; 439 walk( rules, count ); 440 found.push( count ); 441 } ); 442 443 return found; 444 } 445 446 function moving() { 447 const found = []; 448 const known = new Set(); 449 450 ( document.getAnimations ? document.getAnimations() : [] ).forEach( ( animation ) => { 451 const effect = animation.effect; 452 const target = effect && effect.target; 453 const timing = effect && effect.getComputedTiming ? effect.getComputedTiming() : {}; 454 455 if ( found.length >= 10 || ! target || timing.iterations !== Infinity || animation.playState !== 'running' ) { 456 return; 457 } 458 459 const box = target.getBoundingClientRect(); 460 const what = describe( target ) + ( animation.animationName ? ' (' + String( animation.animationName ).slice( 0, 40 ) + ')' : '' ); 461 462 if ( box.width * box.height >= 2500 && box.bottom > 0 && box.top < window.innerHeight && ! known.has( what ) ) { 463 known.add( what ); 464 found.push( what ); 465 } 466 } ); 467 468 return found; 469 } 470 471 function srcsets() { 472 const found = []; 473 474 for ( let i = 0; i < document.images.length && found.length < 60; i++ ) { 475 const image = document.images[ i ]; 476 const width = image.getBoundingClientRect().width; 477 const options = []; 478 479 String( image.getAttribute( 'srcset' ) || '' ).split( /,\s+/ ).forEach( ( part ) => { 480 const bits = part.trim().split( /\s+/ ); 481 const size = bits.length === 2 && /^\d+w$/.test( bits[ 1 ] ) ? parseInt( bits[ 1 ], 10 ) : 0; 482 483 if ( size && options.length < 16 ) { 484 options.push( [ new URL( bits[ 0 ], document.baseURI ).href.split( /[?#]/ )[ 0 ], size ] ); 485 } 486 } ); 487 488 if ( width >= 2 && options.length > 1 && ! image.closest( 'picture' ) ) { 489 found.push( { slot: Math.round( width * 10 ) / 10, options: options } ); 490 } 491 } 492 493 return found; 494 } 495 496 function families( fonts ) { 497 const loaded = new Map(); 498 const chars = new Map(); 499 const styles = new Map(); 500 const walker = document.createTreeWalker( document.body, NodeFilter.SHOW_TEXT ); 501 const skip = [ 'SCRIPT', 'STYLE', 'NOSCRIPT', 'TEMPLATE' ]; 502 let total = 0; 503 let node; 504 let seen = 0; 505 506 fonts.forEach( ( font ) => loaded.set( font.family.toLowerCase(), font.family ) ); 507 508 while ( ( node = walker.nextNode() ) && seen++ < 5000 ) { 509 const parent = node.parentElement; 510 const length = node.nodeValue.replace( /\s+/g, '' ).length; 511 512 if ( ! length || ! parent || skip.indexOf( parent.nodeName ) !== -1 || ! parent.getClientRects().length ) { 513 continue; 514 } 515 516 if ( ! styles.has( parent ) ) { 517 styles.set( parent, String( getComputedStyle( parent ).fontFamily.split( ',' )[ 0 ] || '' ).trim().replace( /^["']|["']$/g, '' ).toLowerCase() ); 518 } 519 520 const family = loaded.get( styles.get( parent ) ); 521 522 total += length; 523 524 if ( family ) { 525 chars.set( family, ( chars.get( family ) || 0 ) + length ); 526 } 527 } 528 529 return { total: total, used: Array.from( chars, ( pair ) => ( { family: pair[ 0 ], chars: pair[ 1 ] } ) ).slice( 0, 20 ) }; 530 } 531 532 function look() { 533 const fonts = []; 534 const lazyTop = []; 535 const sizeless = []; 536 const lcp = painted.lcp || largest(); 537 const element = lcp && lcp.element; 538 539 if ( document.fonts && document.fonts.forEach ) { 540 document.fonts.forEach( ( face ) => { 541 if ( face.status === 'loaded' && fonts.length < 60 ) { 542 fonts.push( { family: String( face.family ).replace( /^["']|["']$/g, '' ), weight: String( face.weight ), style: String( face.style ), display: String( face.display || '' ) } ); 543 } 544 } ); 545 } 546 547 for ( let i = 0; i < document.images.length; i++ ) { 548 const image = document.images[ i ]; 549 const box = image.getBoundingClientRect(); 550 const src = plain( image.currentSrc || image.src ); 551 552 if ( box.width < 2 || box.height < 2 || ! src || src.indexOf( 'data:' ) === 0 ) { 553 continue; 554 } 555 556 if ( image.getAttribute( 'loading' ) === 'lazy' && box.top + window.scrollY < window.innerHeight && lazyTop.length < 10 ) { 557 lazyTop.push( src ); 558 } 559 560 if ( ( ! image.hasAttribute( 'width' ) || ! image.hasAttribute( 'height' ) ) && sizeless.length < 20 ) { 561 sizeless.push( src ); 562 } 563 } 564 565 return { 566 resources: performance.getEntriesByType( 'resource' ).slice( 0, 400 ).map( ( entry ) => ( { 567 url: entry.name.indexOf( '//fonts.googleapis.com/' ) !== -1 ? entry.name.slice( 0, 500 ) : plain( entry.name ), 568 type: entry.initiatorType, 569 kb: Math.round( ( entry.encodedBodySize || 0 ) / 102.4 ) / 10, 570 blocking: entry.renderBlockingStatus === 'blocking', 571 } ) ), 572 fonts: fonts, 573 preloads: Array.prototype.map.call( document.querySelectorAll( 'link[rel="preload"][as="font"]' ), ( link ) => ( { url: plain( link.href ), crossorigin: link.hasAttribute( 'crossorigin' ) } ) ).slice( 0, 20 ), 574 lcp: lcp 575 ? { 576 kind: lcp.url ? 'image' : 'text', 577 tag: element ? element.nodeName.toLowerCase() : '', 578 src: plain( lcp.url ), 579 lazy: !! ( element && element.getAttribute && element.getAttribute( 'loading' ) === 'lazy' ), 580 priority: element && element.getAttribute ? String( element.getAttribute( 'fetchpriority' ) || '' ) : '', 581 what: describe( element ), 582 guessed: !! lcp.guessed, 583 } 584 : null, 585 shift: Math.round( painted.shift * 1000 ) / 1000, 586 shifters: painted.shifters.sort( ( a, b ) => b[ 0 ] - a[ 0 ] ).map( ( item ) => describe( item[ 1 ] ) ).filter( ( what, i, all ) => what && all.indexOf( what ) === i ).slice( 0, 3 ), 587 lazyTop: lazyTop, 588 sizeless: sizeless, 589 moving: moving(), 590 srcsets: srcsets(), 591 text: families( fonts ), 592 styles: usage(), 593 }; 594 } 595 304 596 function report() { 305 597 const files = []; 306 598 let bytes = 0; 307 // Files the browser had to have before it could show anything (Chromium reports this; elsewhere it stays 0).308 599 let blocking = 0; 309 600 … … 319 610 { 320 611 speedDoctor: probe.token, 321 report: { fresh: fresh, errors: errors, calls: calls, files: files, kb: Math.round( bytes / 1024 ), blocking: blocking, frames: document.querySelectorAll( 'iframe[src]:not([srcdoc])' ).length, texts: texts(), boxes: boxes(), marks: marks(), images: images(), ears: ears(), spots: spots()},612 report: { fresh: fresh, errors: errors, sources: sources, calls: calls, files: files, kb: Math.round( bytes / 1024 ), blocking: blocking, frames: document.querySelectorAll( 'iframe[src]:not([srcdoc])' ).length, texts: texts(), boxes: boxes(), marks: marks(), images: images(), lcp: main(), sizes: sizes(), google: google(), ears: ears(), spots: spots(), look: probe.scan ? look() : undefined }, 322 613 }, 323 614 probe.origin … … 325 616 } 326 617 327 // Report once the page has gone quiet: nothing in flight, fonts in, and nothing new fetched for a second.328 618 function settle() { 329 619 let last = -1; -
imzura-site-check/trunk/imzura-site-check.php
r3797250 r3797325 2 2 /** 3 3 * Plugin Name: imzura Site Check – Speed & Site Optimization 4 * Plugin URI: https://imzura.com/products/speed-doctor 4 5 * Description: Finds what slows your site down, on the front end, in the admin and in WooCommerce, and proves every fix with a measurement. 5 * Version: 1. 0.06 * Version: 1.3.0 6 7 * Requires at least: 6.2 7 8 * Requires PHP: 7.4 … … 17 18 defined( 'ABSPATH' ) || exit; 18 19 19 define( 'SPEED_DOCTOR_VERSION', '1. 0.0' );20 define( 'SPEED_DOCTOR_VERSION', '1.3.0' ); 20 21 define( 'SPEED_DOCTOR_FILE', __FILE__ ); 21 22 define( 'SPEED_DOCTOR_DIR', plugin_dir_path( __FILE__ ) ); … … 23 24 24 25 spl_autoload_register( 25 static function ( $ class) {26 if ( strpos( $ class, 'SpeedDoctor\\' ) !== 0 ) {26 static function ( $wanted ) { 27 if ( strpos( $wanted, 'SpeedDoctor\\' ) !== 0 ) { 27 28 return; 28 29 } 29 30 30 $parts = explode( '\\', substr( $ class, 12 ) );31 $parts = explode( '\\', substr( $wanted, 12 ) ); 31 32 $name = 'class-' . strtolower( str_replace( '_', '-', array_pop( $parts ) ) ) . '.php'; 32 33 $path = SPEED_DOCTOR_DIR . 'includes/' . ( $parts ? strtolower( implode( '/', $parts ) ) . '/' : '' ) . $name; … … 38 39 ); 39 40 41 SpeedDoctor\Profiler::watch(); 42 40 43 add_action( 'plugins_loaded', array( 'SpeedDoctor\\Plugin', 'boot' ) ); 41 44 register_deactivation_hook( __FILE__, array( 'SpeedDoctor\\Fixer', 'retire' ) ); -
imzura-site-check/trunk/includes/checks/class-autoload.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Options WordPress loads on every single request, cached page or not. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; -
imzura-site-check/trunk/includes/checks/class-delivery.php
r3797250 r3797325 1 1 <?php 2 /** 3 * How the server delivers what the browser asked for: compressed or not, and whether browsers may keep files. 4 * 5 * Compression and browser caching are added by the web server or a CDN after WordPress has finished, so only 6 * the browser sees them. The admin screen reads the response headers of the home page and of one of this 7 * plugin's stylesheets (a plain static file, like any theme's) during the check and hands them over. 8 * 9 * @package SpeedDoctor 10 */ 2 // imzura Site Check by imzura.com 11 3 12 4 namespace SpeedDoctor\Checks; … … 18 10 final class Delivery extends Check { 19 11 20 // A static file that browsers may keep for less than this many seconds is fetched again too often.21 12 const KEEP = 604800; 22 13 … … 87 78 } 88 79 89 /**90 * @return array The headers the browser saw, lower-case names, or an empty array when it saw none.91 */92 80 private function headers( $kind ) { 93 81 if ( empty( $this->seen[ $kind ] ) || ! is_array( $this->seen[ $kind ] ) ) { … … 113 101 } 114 102 115 /**116 * @return int Seconds the browser may keep the file, from Cache-Control max-age or else Expires.117 */118 103 private function keep( array $headers ) { 119 104 if ( isset( $headers['cache-control'] ) ) { -
imzura-site-check/trunk/includes/checks/class-environment.php
r3797250 r3797325 1 1 <?php 2 /** 3 * What the site runs on, and what is already speeding it up. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; … … 15 11 16 12 const CACHE_PLUGINS = array( 17 'litespeed-cache' => 'LiteSpeed Cache',18 'wp-rocket' => 'WP Rocket',19 'w3-total-cache' => 'W3 Total Cache',20 'wp-super-cache' => 'WP Super Cache',21 'wp-fastest-cache' => 'WP Fastest Cache',22 'wp-optimize' => 'WP-Optimize',23 'sg-cachepress' => 'Speed Optimizer',24 'breeze' => 'Breeze',25 'flying-press' => 'FlyingPress',26 'nitropack' => 'NitroPack',27 'cache-enabler' => 'Cache Enabler',13 'litespeed-cache' => 'LiteSpeed Cache', 14 'wp-rocket' => 'WP Rocket', 15 'w3-total-cache' => 'W3 Total Cache', 16 'wp-super-cache' => 'WP Super Cache', 17 'wp-fastest-cache' => 'WP Fastest Cache', 18 'wp-optimize' => 'WP-Optimize', 19 'sg-cachepress' => 'Speed Optimizer', 20 'breeze' => 'Breeze', 21 'flying-press' => 'FlyingPress', 22 'nitropack' => 'NitroPack', 23 'cache-enabler' => 'Cache Enabler', 28 24 'hummingbird-performance' => 'Hummingbird', 29 25 ); … … 54 50 55 51 $active = array(); 52 $off = array(); 56 53 57 54 foreach ( (array) get_option( 'active_plugins', array() ) as $file ) { 58 55 $slug = strtok( $file, '/' ); 59 56 60 if ( isset( self::CACHE_PLUGINS[ $slug ] ) ) { 57 if ( ! isset( self::CACHE_PLUGINS[ $slug ] ) ) { 58 continue; 59 } 60 61 $setting = self::caching_off( $slug ); 62 63 if ( $setting ) { 64 $off[] = $setting; 65 } else { 61 66 $active[] = self::CACHE_PLUGINS[ $slug ]; 62 67 } 68 } 69 70 if ( $off && ! $active ) { 71 $facts = array(); 72 73 foreach ( $off as $setting ) { 74 $facts[] = $setting['where']; 75 $facts[] = $setting['url']; 76 } 77 78 $findings[] = $this->finding( 79 'cache-off', 80 'front', 81 'medium', 82 /* translators: %s: plugin name */ 83 sprintf( __( '%s is active, but its page caching is switched off', 'imzura-site-check' ), $off[0]['name'] ), 84 __( 'Every visitor\'s page is built anew by WordPress and the plugins, although a ready copy could be sent. Switch page caching on in the plugin\'s settings, then open a few pages logged out to see that they look right; this plugin only reads this setting.', 'imzura-site-check' ), 85 $facts 86 ); 63 87 } 64 88 … … 85 109 return $findings; 86 110 } 111 112 public static function caching_off( $slug ) { 113 $where = ''; 114 $url = ''; 115 116 switch ( $slug ) { 117 case 'wp-super-cache': 118 if ( isset( $GLOBALS['cache_enabled'] ) && ! $GLOBALS['cache_enabled'] ) { 119 $where = __( 'Settings → WP Super Cache → Easy: Caching Off', 'imzura-site-check' ); 120 $url = admin_url( 'options-general.php?page=wpsupercache' ); 121 } 122 break; 123 124 case 'w3-total-cache': 125 $read = array( '\W3TC\Dispatcher', 'config' ); 126 $config = is_callable( $read ) ? call_user_func( $read ) : null; 127 128 if ( is_object( $config ) && is_callable( array( $config, 'get_boolean' ) ) && ! $config->get_boolean( 'pgcache.enabled' ) ) { 129 $where = __( 'Performance → General Settings → Page Cache: Enable is not checked', 'imzura-site-check' ); 130 $url = admin_url( 'admin.php?page=w3tc_general' ); 131 } 132 break; 133 134 case 'wp-fastest-cache': 135 $options = json_decode( (string) get_option( 'WpFastestCache', '' ), true ); 136 137 if ( ! is_array( $options ) || ! isset( $options['wpFastestCacheStatus'] ) ) { 138 $where = __( 'WP Fastest Cache → Settings: Cache System, Enable is not checked', 'imzura-site-check' ); 139 $url = admin_url( 'admin.php?page=wpfastestcacheoptions' ); 140 } 141 break; 142 143 case 'wp-optimize': 144 $read = array( 'WPO_Cache_Config', 'instance' ); 145 $config = is_callable( $read ) ? call_user_func( $read ) : null; 146 147 if ( is_object( $config ) && is_callable( array( $config, 'get_option' ) ) && ! $config->get_option( 'enable_page_caching' ) ) { 148 $where = __( 'WP-Optimize → Cache → Page cache: Enable page caching is off', 'imzura-site-check' ); 149 $url = admin_url( 'admin.php?page=wpo_cache' ); 150 } 151 break; 152 153 case 'breeze': 154 $read = array( 'Breeze_Options_Reader', 'get_option_value' ); 155 156 if ( is_callable( $read ) && '1' !== (string) call_user_func( $read, 'breeze-active' ) ) { 157 $where = __( 'Settings → Breeze → Basic Options: Cache System is off', 'imzura-site-check' ); 158 $url = admin_url( 'options-general.php?page=breeze' ); 159 } 160 break; 161 162 case 'hummingbird-performance': 163 $read = array( '\Hummingbird\Core\Settings', 'get_setting' ); 164 165 if ( is_callable( $read ) && ! call_user_func( $read, 'enabled', 'page_cache' ) ) { 166 $where = __( 'Hummingbird → Caching → Page Caching is not active', 'imzura-site-check' ); 167 $url = admin_url( 'admin.php?page=wphb-caching' ); 168 } 169 break; 170 } 171 172 if ( '' === $where ) { 173 return null; 174 } 175 176 return array( 177 'name' => self::CACHE_PLUGINS[ $slug ], 178 'where' => $where, 179 'url' => $url, 180 ); 181 } 87 182 } -
imzura-site-check/trunk/includes/checks/class-front-pages.php
r3797250 r3797325 1 1 <?php 2 /** 3 * What each page a visitor opens is built from. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; … … 17 13 const VERY_SLOW_SECONDS = 2.0; 18 14 const HEAVY_PLUGIN_KB = 60; 15 const HEAVY_FILE_KB = array( 16 'styles' => array( 50, 100 ), 17 'scripts' => array( 100, 250 ), 18 ); 19 19 const HEAVY_PAGE_KB = 1024; 20 20 const HEAVY_IMAGE_KB = 300; 21 21 const HEAVY_VIDEO_KB = 1024; 22 22 23 /**24 * Files of WooCommerce that do a job on every page (they record where the visit came from), and the cart25 * script, which has a finding of its own.26 */27 23 const AT_WORK = array( 'sourcebuster-js', 'wc-order-attribution', 'wc-cart-fragments' ); 28 24 29 /**30 * What the pages showed, gathered per subject (slow server, a plugin's unused or heavy files) so that each31 * subject is one finding listing its pages, not one finding per page.32 */33 25 private $seen = array( 34 'slow' => array(), 35 'unused' => array(), 36 'heavy' => array(), 26 'slow' => array(), 27 'unused' => array(), 28 'heavy' => array(), 29 'styles' => array(), 30 'scripts' => array(), 37 31 ); 38 32 … … 53 47 $this->server( $page ); 54 48 $this->plugins( $page, ! $weight ); 49 $this->files( $page ); 55 50 56 51 if ( ! $page['context']['store_page'] && in_array( 'wc-cart-fragments', array_column( $page['scripts'], 'handle' ), true ) ) { … … 59 54 } 60 55 61 $findings = array_merge( $findings, $this->slow(), $this->unused(), $this->heavy() );56 $findings = array_merge( $findings, $this->slow(), $this->unused(), $this->heavy(), $this->heavy_files( 'styles' ), $this->heavy_files( 'scripts' ) ); 62 57 63 58 if ( $fragments ) { … … 130 125 } 131 126 132 /**133 * One finding per plugin that loads files on pages that do not use them.134 */135 127 private function unused() { 136 128 $findings = array(); … … 158 150 } 159 151 160 /**161 * One finding per plugin that adds a lot to pages that may well need it.162 */163 152 private function heavy() { 164 153 $findings = array(); … … 183 172 184 173 return $findings; 174 } 175 176 private function files( array $page ) { 177 foreach ( array( 'styles', 'scripts' ) as $type ) { 178 foreach ( $page[ $type ] as $file ) { 179 if ( null === $file['kb'] || $file['kb'] < self::HEAVY_FILE_KB[ $type ][0] ) { 180 continue; 181 } 182 183 if ( ! isset( $this->seen[ $type ][ $file['handle'] ] ) ) { 184 $this->seen[ $type ][ $file['handle'] ] = array( 185 'file' => isset( $file['file'] ) && '' !== $file['file'] ? $file['file'] : $file['handle'], 186 'owner' => $this->who( $file['owner'] ), 187 'kb' => (int) round( $file['kb'] ), 188 'pages' => 0, 189 ); 190 } 191 192 ++$this->seen[ $type ][ $file['handle'] ]['pages']; 193 } 194 } 195 } 196 197 private function heavy_files( $type ) { 198 $files = array_values( $this->seen[ $type ] ); 199 200 if ( ! $files ) { 201 return array(); 202 } 203 204 usort( 205 $files, 206 static function ( $a, $b ) { 207 return $b['kb'] <=> $a['kb']; 208 } 209 ); 210 211 $facts = array(); 212 $plain = false; 213 214 foreach ( array_slice( $files, 0, 8 ) as $file ) { 215 $plain = $plain || false === strpos( $file['file'], '.min.' ); 216 $facts[] = sprintf( 217 /* translators: 1: file name, 2: who adds it (a plugin, the theme), 3: size, 4: number of pages */ 218 _n( '%1$s (%2$s): %3$s, on %4$s page', '%1$s (%2$s): %3$s, on %4$s pages', $file['pages'], 'imzura-site-check' ), 219 $file['file'], 220 $file['owner'], 221 $this->kb( $file['kb'] ), 222 number_format_i18n( $file['pages'] ) 223 ); 224 } 225 226 $styles = 'styles' === $type; 227 $count = count( $files ); 228 229 if ( 1 === $count ) { 230 $title = $styles 231 /* translators: 1: file name, 2: who adds it (a plugin, the theme), 3: size */ 232 ? sprintf( __( 'A large stylesheet: %1$s from %2$s, %3$s before compression', 'imzura-site-check' ), $files[0]['file'], $files[0]['owner'], $this->kb( $files[0]['kb'] ) ) 233 /* translators: 1: file name, 2: who adds it (a plugin, the theme), 3: size */ 234 : sprintf( __( 'A large script: %1$s from %2$s, %3$s before compression', 'imzura-site-check' ), $files[0]['file'], $files[0]['owner'], $this->kb( $files[0]['kb'] ) ); 235 } else { 236 $title = $styles 237 /* translators: 1: number of stylesheets, 2: size, 3: who adds it (a plugin, the theme) */ 238 ? sprintf( _n( '%1$s large stylesheet, the largest %2$s from %3$s', '%1$s large stylesheets, the largest %2$s from %3$s', $count, 'imzura-site-check' ), number_format_i18n( $count ), $this->kb( $files[0]['kb'] ), $files[0]['owner'] ) 239 /* translators: 1: number of scripts, 2: size, 3: who adds it (a plugin, the theme) */ 240 : sprintf( _n( '%1$s large script, the largest %2$s from %3$s', '%1$s large scripts, the largest %2$s from %3$s', $count, 'imzura-site-check' ), number_format_i18n( $count ), $this->kb( $files[0]['kb'] ), $files[0]['owner'] ); 241 } 242 243 return array( 244 $this->finding( 245 'heavy-' . $type, 246 'front', 247 $files[0]['kb'] >= self::HEAVY_FILE_KB[ $type ][1] ? 'medium' : 'low', 248 $title, 249 $styles 250 ? __( 'A stylesheet linked in the head of a page holds up the first paint until it has loaded, so these are the stylesheets that weigh most on how fast your pages appear. This plugin does not take rules out of them, because which rules a site needs depends on every page, every state of a menu or form and every script, and a few pages cannot show that for sure. Minifying them is safe and is one of the fixes. Beyond that, ask the theme or plugin author whether it can load less, or switch off the parts you do not use in its settings.', 'imzura-site-check' ) 251 : __( 'A phone has to download, read and run each script before the page reacts to a tap, and a script in the head of the page also holds up the first paint. These are the largest scripts the pages load. Minifying them and loading them with defer are safe and are among the fixes. Beyond that, ask the theme or plugin author whether it can load less, or switch off the parts you do not use in its settings.', 'imzura-site-check' ), 252 $facts, 253 $plain ? ( $styles ? 'minify-css' : 'minify-js' ) : ( $styles ? '' : 'defer-js' ) 254 ), 255 ); 185 256 } 186 257 … … 246 317 } 247 318 248 /**249 * What WooCommerce loads on a page without the files that are at work there.250 */251 319 private function idle( array $page, array $handles ) { 252 320 $busy = self::AT_WORK; -
imzura-site-check/trunk/includes/checks/class-scheduled-work.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Background jobs that pile up: WP-Cron and Action Scheduler. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; -
imzura-site-check/trunk/includes/checks/class-store.php
r3797250 r3797325 1 1 <?php 2 /** 3 * The parts of a WooCommerce store that no page cache covers. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Checks; -
imzura-site-check/trunk/includes/class-admin-page.php
r3797250 r3797325 1 1 <?php 2 /** 3 * The one screen the plugin adds to the admin. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 56 52 'cache' => Cache::status(), 57 53 'asset' => SPEED_DOCTOR_URL . 'assets/admin.css', 54 'log' => esc_url_raw( Checks\Housekeeping::log_url() ), 58 55 'rescue' => esc_url_raw( Fixer::rescue_url() ), 56 'tidy' => Tidy::state(), 57 'review' => array( 58 'due' => ! get_user_meta( get_current_user_id(), Rest::REVIEW_META, true ), 59 'url' => 'https://wordpress.org/support/plugin/imzura-site-check/reviews/#new-post', 60 ), 59 61 'text' => array( 60 'scan' => __( 'Check my site', 'imzura-site-check' ), 61 'again' => __( 'Check again', 'imzura-site-check' ), 62 'starting' => __( 'Starting', 'imzura-site-check' ), 62 'review_ask' => __( 'A fix is on and working. If this plugin helped, a short review on WordPress.org helps other site owners find it.', 'imzura-site-check' ), 63 'review_go' => __( 'Write a review', 'imzura-site-check' ), 64 'review_no' => __( 'Do not ask again', 'imzura-site-check' ), 65 /* translators: %s: number of revisions */ 66 'tidy_go' => __( 'Remove %s old revisions', 'imzura-site-check' ), 67 'tidy_card' => __( 'Old revisions were removed; the copy is above.', 'imzura-site-check' ), 68 /* translators: 1: number of revisions, 2: date and time, 3: size of the copy */ 69 'tidy_done' => __( 'Removed %1$s old revisions on %2$s. A copy (%3$s) is kept in wp-content/uploads/imzura-site-check/copies, so they can be put back. Deleting this plugin deletes the copy too.', 'imzura-site-check' ), 70 /* translators: 1: number of revisions removed, 2: number left */ 71 'tidy_partial' => __( 'Removing old revisions stopped after %1$s; %2$s are left. The ones removed are in the copy.', 'imzura-site-check' ), 72 /* translators: %s: number of revisions */ 73 'tidy_halfway' => __( 'Putting revisions back stopped after %s. The rest are still in the copy.', 'imzura-site-check' ), 74 'tidy_on' => __( 'Go on', 'imzura-site-check' ), 75 'tidy_undo' => __( 'Put them back', 'imzura-site-check' ), 76 'tidy_forget' => __( 'Delete the copy', 'imzura-site-check' ), 77 'tidy_confirm' => __( 'Delete the copy of the old revisions? They cannot be put back after this.', 'imzura-site-check' ), 78 /* translators: %s: number of revisions */ 79 'tidy_removing' => __( 'Copying and removing old revisions: %s so far', 'imzura-site-check' ), 80 /* translators: %s: number of revisions */ 81 'tidy_restoring' => __( 'Putting revisions back: %s so far', 'imzura-site-check' ), 82 /* translators: 1: number of revisions put back, 2: number that were already there */ 83 'tidy_restored' => __( 'Put back %1$s revisions (%2$s were already there) and deleted the copy.', 'imzura-site-check' ), 84 'tidy_forgotten' => __( 'The copy of the old revisions is deleted.', 'imzura-site-check' ), 85 'tidy_failed' => __( 'That did not work. Nothing was removed without a copy; reload the page and try again.', 'imzura-site-check' ), 86 /* translators: %s: a size in megabytes, e.g. 1.4 */ 87 'mb' => __( '%s MB', 'imzura-site-check' ), 88 'scan' => __( 'Check my site', 'imzura-site-check' ), 89 'again' => __( 'Check again', 'imzura-site-check' ), 90 'starting' => __( 'Starting', 'imzura-site-check' ), 63 91 /* translators: 1: number of the page being read, 2: total pages, 3: page name */ 64 'reading' => __( 'Reading page %1$s of %2$s: %3$s', 'imzura-site-check' ),65 'judging' => __( 'Putting the report together', 'imzura-site-check' ),66 'failed' => __( 'The check did not finish. Reload the page and try again.', 'imzura-site-check' ),92 'reading' => __( 'Reading page %1$s of %2$s: %3$s', 'imzura-site-check' ), 93 'judging' => __( 'Putting the report together', 'imzura-site-check' ), 94 'failed' => __( 'The check did not finish. Reload the page and try again.', 'imzura-site-check' ), 67 95 /* translators: %s: date and time of the last check */ 68 'checked' => __( 'Last checked %s', 'imzura-site-check' ), 69 'none' => __( 'Nothing to fix in this group.', 'imzura-site-check' ), 70 'pages' => __( 'The pages that were read', 'imzura-site-check' ), 71 'page' => __( 'Page', 'imzura-site-check' ), 72 'server' => __( 'Server time', 'imzura-site-check' ), 73 'queries' => __( 'Queries', 'imzura-site-check' ), 74 'files' => __( 'Scripts and styles', 'imzura-site-check' ), 75 'unread' => __( 'not read', 'imzura-site-check' ), 76 'owners' => __( 'What each plugin and the theme add', 'imzura-site-check' ), 77 'owners_lead' => __( 'Scripts and styles only, heaviest first. The numbers are for the page where each one loads the most.', 'imzura-site-check' ), 78 'owner' => __( 'Plugin or theme', 'imzura-site-check' ), 79 'owner_kb' => __( 'Weight before compression', 'imzura-site-check' ), 80 'owner_pages' => __( 'Loads on', 'imzura-site-check' ), 96 'checked' => __( 'Last checked %s', 'imzura-site-check' ), 97 'none' => __( 'Nothing to fix in this group.', 'imzura-site-check' ), 98 'pages' => __( 'The pages that were read', 'imzura-site-check' ), 99 'page' => __( 'Page', 'imzura-site-check' ), 100 'server' => __( 'Server time', 'imzura-site-check' ), 101 'queries' => __( 'Queries', 'imzura-site-check' ), 102 'files' => __( 'Scripts and styles', 'imzura-site-check' ), 103 'unread' => __( 'not read', 'imzura-site-check' ), 104 'owners' => __( 'What each plugin and the theme add', 'imzura-site-check' ), 105 'owners_lead' => __( 'Busiest first. Server time is the longest its code ran while one page was built, with the database queries it made there; scripts and styles are for the page where it loads the most.', 'imzura-site-check' ), 106 'owner' => __( 'Plugin or theme', 'imzura-site-check' ), 107 'owner_ms' => __( 'Server time', 'imzura-site-check' ), 108 /* translators: 1: milliseconds, 2: number of database queries */ 109 'owner_code' => __( '%1$s ms · %2$s queries', 'imzura-site-check' ), 110 'owner_kb' => __( 'Weight before compression', 'imzura-site-check' ), 111 'owner_pages' => __( 'Loads on', 'imzura-site-check' ), 81 112 /* translators: %s: theme name */ 82 'owner_theme' => __( 'Theme: %s', 'imzura-site-check' ),113 'owner_theme' => __( 'Theme: %s', 'imzura-site-check' ), 83 114 /* translators: 1: number of pages it loads on, 2: number of pages that were read */ 84 'owner_of' => __( '%1$s of %2$s pages read', 'imzura-site-check' ),85 'seconds' => __( 's', 'imzura-site-check' ),86 'kb' => __( 'KB', 'imzura-site-check' ),87 'count_high' => __( 'to fix first', 'imzura-site-check' ),88 'count_medium' => __( 'worth fixing', 'imzura-site-check' ),89 'count_low' => __( 'to know about', 'imzura-site-check' ),90 'count_good' => __( 'in good shape', 'imzura-site-check' ),91 'fixes' => __( 'Fixes', 'imzura-site-check' ),92 'groups' => array(115 'owner_of' => __( '%1$s of %2$s pages read', 'imzura-site-check' ), 116 'seconds' => __( 's', 'imzura-site-check' ), 117 'kb' => __( 'KB', 'imzura-site-check' ), 118 'count_high' => __( 'to fix first', 'imzura-site-check' ), 119 'count_medium' => __( 'worth fixing', 'imzura-site-check' ), 120 'count_low' => __( 'to know about', 'imzura-site-check' ), 121 'count_good' => __( 'in good shape', 'imzura-site-check' ), 122 'fixes' => __( 'Fixes', 'imzura-site-check' ), 123 'groups' => array( 93 124 'code' => __( 'Stylesheets and scripts', 'imzura-site-check' ), 94 125 'images' => __( 'Images', 'imzura-site-check' ), … … 96 127 ), 97 128 /* translators: 1: plugin name, 2: the setting in that plugin's screen */ 98 'clash' => __( '%1$s already does this: %2$s. Turn it off there, or keep %1$s and leave this fix off. Two plugins doing the same thing to the same files is a common cause of broken pages.', 'imzura-site-check' ),129 'clash' => __( '%1$s already does this: %2$s. Turn it off there, or keep %1$s and leave this fix off. Two plugins doing the same thing to the same files is a common cause of broken pages.', 'imzura-site-check' ), 99 130 /* translators: %s: plugin name */ 100 'clash_maybe' => __( '%s is active and may already do this. If it does, keep only one of the two.', 'imzura-site-check' ),131 'clash_maybe' => __( '%s is active and may already do this. If it does, keep only one of the two.', 'imzura-site-check' ), 101 132 /* translators: %s: plugin name */ 102 'clash_open' => __( 'Open %s settings', 'imzura-site-check' ),133 'clash_open' => __( 'Open %s settings', 'imzura-site-check' ), 103 134 /* translators: %s: plugin name */ 104 'clash_off' => __( 'Deactivate %s', 'imzura-site-check' ),105 'cache_title' => __( 'Page cache', 'imzura-site-check' ),106 'cache_lead' => __( 'A page cache sends visitors a ready copy of each page instead of building it again. This plugin works next to your page cache and does not replace it; when a fix is switched on or off, it clears the cache for you.', 'imzura-site-check' ),135 'clash_off' => __( 'Deactivate %s', 'imzura-site-check' ), 136 'cache_title' => __( 'Page cache', 'imzura-site-check' ), 137 'cache_lead' => __( 'A page cache sends visitors a ready copy of each page instead of building it again. This plugin works next to your page cache and does not replace it; when a fix is switched on or off, it clears the cache for you.', 'imzura-site-check' ), 107 138 /* translators: %s: names of the page cache plugins that are active */ 108 'cache_found' => __( 'Active: %s.', 'imzura-site-check' ), 109 'cache_none' => __( 'No page cache plugin is active.', 'imzura-site-check' ), 110 'cache_ready' => __( 'WordPress is set up to answer from a page cache before plugins load.', 'imzura-site-check' ), 111 'cache_unset' => __( 'WordPress is not set up to answer from a page cache before plugins load.', 'imzura-site-check' ), 112 'cache_test' => __( 'Open the home page as a new visitor', 'imzura-site-check' ), 113 'cache_clear' => __( 'Clear the page cache', 'imzura-site-check' ), 139 'cache_found' => __( 'Active: %s.', 'imzura-site-check' ), 140 'cache_none' => __( 'No page cache plugin is active.', 'imzura-site-check' ), 141 /* translators: %s: plugin names */ 142 'cache_off' => __( 'Active, with its page caching switched off: %s.', 'imzura-site-check' ), 143 'cache_ready' => __( 'WordPress is set up to answer from a page cache before plugins load.', 'imzura-site-check' ), 144 'cache_unset' => __( 'WordPress is not set up to answer from a page cache before plugins load.', 'imzura-site-check' ), 145 'cache_test' => __( 'Open the home page as a new visitor', 'imzura-site-check' ), 146 'cache_clear' => __( 'Clear the page cache', 'imzura-site-check' ), 114 147 /* translators: 1: milliseconds for the first visit, 2: milliseconds for the second visit */ 115 'cache_times' => __( 'The server answered a first visit in %1$s ms and the same page again in %2$s ms. A page that comes from a cache usually answers much faster the second time.', 'imzura-site-check' ),148 'cache_times' => __( 'The server answered a first visit in %1$s ms and the same page again in %2$s ms. A page that comes from a cache usually answers much faster the second time.', 'imzura-site-check' ), 116 149 /* translators: %s: response headers about caching, e.g. cf-cache-status: HIT */ 117 'cache_headers' => __( 'Cache headers in the answer: %s', 'imzura-site-check' ),118 'cache_failed' => __( 'The home page could not be opened from here.', 'imzura-site-check' ),150 'cache_headers' => __( 'Cache headers in the answer: %s', 'imzura-site-check' ), 151 'cache_failed' => __( 'The home page could not be opened from here.', 'imzura-site-check' ), 119 152 /* translators: %s: names of the page caches that were cleared */ 120 'cache_done' => __( 'Cleared: %s.', 'imzura-site-check' ),121 'cache_nobody' => __( 'No page cache that this plugin knows how to clear answered.', 'imzura-site-check' ),122 'fixes_lead' => __( 'A fix is first tried on a test visitor only. Each page is opened without it and with it; the fix stays on only if the page looks the same, nothing broke, and something was saved.', 'imzura-site-check' ),123 'try' => __( 'Try it safely', 'imzura-site-check' ),124 'retry' => __( 'Test again', 'imzura-site-check' ),125 'off' => __( 'Turn off', 'imzura-site-check' ),126 'fix_it' => __( 'Fix it safely', 'imzura-site-check' ),127 'fixed' => __( 'The fix for this is on.', 'imzura-site-check' ),153 'cache_done' => __( 'Cleared: %s.', 'imzura-site-check' ), 154 'cache_nobody' => __( 'No page cache that this plugin knows how to clear answered.', 'imzura-site-check' ), 155 'fixes_lead' => __( 'A fix is first tried on a test visitor only. Each page is opened without it and with it; the fix stays on only if the page looks the same, nothing broke, and something was saved.', 'imzura-site-check' ), 156 'try' => __( 'Try it safely', 'imzura-site-check' ), 157 'retry' => __( 'Test again', 'imzura-site-check' ), 158 'off' => __( 'Turn off', 'imzura-site-check' ), 159 'fix_it' => __( 'Fix it safely', 'imzura-site-check' ), 160 'fixed' => __( 'The fix for this is on.', 'imzura-site-check' ), 128 161 /* translators: 1: number of the page view being tested, 2: total page views, 3: page name */ 129 'testing' => __( 'Testing %1$s of %2$s: %3$s', 'imzura-site-check' ), 130 'deciding' => __( 'Comparing the pages', 'imzura-site-check' ), 131 'preparing' => __( 'Getting the fix ready with what the pages showed', 'imzura-site-check' ), 132 'weight' => __( 'Weight of the files the page loads', 'imzura-site-check' ), 133 'blocking' => __( 'Files the page waits for before it shows anything', 'imzura-site-check' ), 134 'frames' => __( 'Video players and other embedded pages', 'imzura-site-check' ), 135 'trial_failed' => __( 'The test did not finish, so nothing was changed. Reload the page and try again.', 'imzura-site-check' ), 136 'idle' => __( 'The pages looked the same with it, but nothing was saved on this site, so it was left off.', 'imzura-site-check' ), 137 'kept' => __( 'Tested and kept on. What it changed, counted in your browser on a first visit:', 'imzura-site-check' ), 138 'calls' => __( 'Requests to the server after the page arrives', 'imzura-site-check' ), 162 'testing' => __( 'Testing %1$s of %2$s: %3$s', 'imzura-site-check' ), 163 'deciding' => __( 'Comparing the pages', 'imzura-site-check' ), 164 'preparing' => __( 'Getting the fix ready with what the pages showed', 'imzura-site-check' ), 165 'aside' => __( 'A file stopped working when minified. It is now sent as it is; testing again without it', 'imzura-site-check' ), 166 /* translators: %s: number of files */ 167 'excused' => __( 'Files sent as they are, because a test saw them stop working when minified: %s', 'imzura-site-check' ), 168 'weight' => __( 'Weight of the files the page loads', 'imzura-site-check' ), 169 'blocking' => __( 'Files the page waits for before it shows anything', 'imzura-site-check' ), 170 'frames' => __( 'Video players and other embedded pages', 'imzura-site-check' ), 171 'lcp' => __( 'Main picture held back (waits for lazy loading or is not marked urgent)', 'imzura-site-check' ), 172 'sizes' => __( 'Pictures without a width and height', 'imzura-site-check' ), 173 'hosts' => __( 'Other websites the page connects to', 'imzura-site-check' ), 174 'trial_failed' => __( 'The test did not finish, so nothing was changed. Reload the page and try again.', 'imzura-site-check' ), 175 'idle' => __( 'The pages looked the same with it, but nothing was saved on this site, so it was left off.', 'imzura-site-check' ), 176 'kept' => __( 'Tested and kept on. What it changed, counted in your browser on a first visit:', 'imzura-site-check' ), 177 'calls' => __( 'Requests to the server after the page arrives', 'imzura-site-check' ), 139 178 /* translators: %s: names of the page caches that were cleared */ 140 'cache_cleared' => __( 'The page cache was cleared (%s), so visitors get the pages as they are now.', 'imzura-site-check' ),141 'cache_left' => __( 'A page cache is active that this plugin does not know how to clear. Clear it yourself; until then visitors may get the pages from before this change.', 'imzura-site-check' ),179 'cache_cleared' => __( 'The page cache was cleared (%s), so visitors get the pages as they are now.', 'imzura-site-check' ), 180 'cache_left' => __( 'A page cache is active that this plugin does not know how to clear. Clear it yourself; until then visitors may get the pages from before this change.', 'imzura-site-check' ), 142 181 /* translators: %s: the address that switches every fix off */ 143 'rescue' => __( 'If the site ever misbehaves and you cannot reach this screen, open this address. It switches every fix off: %s', 'imzura-site-check' ),144 'states' => array(182 'rescue' => __( 'If the site ever misbehaves and you cannot reach this screen, open this address. It switches every fix off: %s', 'imzura-site-check' ), 183 'states' => array( 145 184 'off' => __( 'Off', 'imzura-site-check' ), 146 185 'on' => __( 'On', 'imzura-site-check' ), … … 149 188 'unavailable' => __( 'Not needed', 'imzura-site-check' ), 150 189 ), 151 'problems' => array(190 'problems' => array( 152 191 /* translators: %1$s: page name */ 153 'frame' => __( '%1$s could not be opened in the test frame, so it could not be compared.', 'imzura-site-check' ),192 'frame' => __( '%1$s could not be opened in the test frame, so it could not be compared.', 'imzura-site-check' ), 154 193 /* translators: %1$s: page name */ 155 'unread' => __( '%1$s was answered before WordPress saw the request, most likely by a page cache, so it could not be tested.', 'imzura-site-check' ),194 'unread' => __( '%1$s was answered before WordPress saw the request, most likely by a page cache, so it could not be tested.', 'imzura-site-check' ), 156 195 /* translators: 1: page name, 2: HTTP status code */ 157 'status' => __( '%1$s answered with status %2$s when the fix was on.', 'imzura-site-check' ),196 'status' => __( '%1$s answered with status %2$s when the fix was on.', 'imzura-site-check' ), 158 197 /* translators: 1: page name, 2: the error message */ 159 'error' => __( '%1$s: a new error appeared: %2$s', 'imzura-site-check' ),198 'error' => __( '%1$s: a new error appeared: %2$s', 'imzura-site-check' ), 160 199 /* translators: 1: page name, 2: the text that changed */ 161 'text' => __( '%1$s: the text on the page changed: %2$s', 'imzura-site-check' ),200 'text' => __( '%1$s: the text on the page changed: %2$s', 'imzura-site-check' ), 162 201 /* translators: 1: page name, 2: how many elements moved out of how many, e.g. 12 of 340 */ 163 'layout' => __( '%1$s: parts of the page moved or changed size (%2$s elements).', 'imzura-site-check' ),202 'layout' => __( '%1$s: parts of the page moved or changed size (%2$s elements).', 'imzura-site-check' ), 164 203 /* translators: 1: page name, 2: a CSS selector */ 165 'mark' => __( '%1$s: something a script sets up on the page was missing (%2$s).', 'imzura-site-check' ),204 'mark' => __( '%1$s: something a script sets up on the page was missing (%2$s).', 'imzura-site-check' ), 166 205 /* translators: 1: page name, 2: an event and where on the page it was listened for, e.g. click .add_to_cart_button */ 167 206 'listener' => __( '%1$s: a script no longer reacts to something that is still on the page (%2$s).', 'imzura-site-check' ), 168 ), 169 'areas' => array( 207 /* translators: %1$s: page name */ 208 'lcp' => __( '%1$s: the main picture now waits (lazy loading, or no longer marked urgent).', 'imzura-site-check' ), 209 ), 210 'areas' => array( 170 211 'front' => __( 'What visitors load', 'imzura-site-check' ), 171 212 'store' => __( 'Store: cart, checkout, orders', 'imzura-site-check' ), 172 213 'admin' => __( 'Admin and database', 'imzura-site-check' ), 173 214 ), 174 'levels' => array(215 'levels' => array( 175 216 'high' => __( 'Fix first', 'imzura-site-check' ), 176 217 'medium' => __( 'Worth fixing', 'imzura-site-check' ), … … 183 224 } 184 225 185 /**186 * The tabs of the screen: the check, one per group of fixes (see Fix::group()), and the page cache.187 */188 226 private static function tabs() { 189 227 return array( … … 219 257 <section class="speed-doctor-counts" id="speed-doctor-counts" hidden></section> 220 258 259 <section class="speed-doctor-tidy" id="speed-doctor-tidy" hidden></section> 260 221 261 <div id="speed-doctor-findings"></div> 222 262 -
imzura-site-check/trunk/includes/class-cache.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Tells page caches to forget their copies when a fix is switched on or off. 4 * 5 * A cached page is a copy of what the site sent before the change. Left alone, visitors keep getting it: 6 * without the fix they should have, or, after "Turn off", with the fix that was just removed. 7 * 8 * @package SpeedDoctor 9 */ 2 // imzura Site Check by imzura.com 10 3 11 4 namespace SpeedDoctor; … … 15 8 final class Cache { 16 9 17 /**18 * @return array cleared: the caches that were told to clear; left: a page cache is active and none of them was.19 */20 10 public static function clear() { 21 11 $cleared = array(); … … 27 17 } 28 18 } catch ( \Throwable $e ) { 29 // Another plugin's cache failing to clear must not stop a fix from being switched off.30 19 continue; 31 20 } 32 21 } 33 22 34 /**35 * Fires after a fix was switched on or off, for a host or a cache this plugin does not know.36 */37 23 do_action( 'speed_doctor_clear_cache' ); 38 24 … … 43 29 } 44 30 45 /**46 * What the Cache tab shows: the page cache plugins that are active, and whether WordPress loads a page cache.47 */48 31 public static function status() { 49 32 $names = Checks\Environment::CACHE_PLUGINS; … … 55 38 56 39 $found = array(); 40 $off = array(); 57 41 58 42 foreach ( $active as $file ) { … … 60 44 61 45 if ( isset( $names[ $slug ] ) ) { 62 $found[] = $names[ $slug ]; 46 if ( Checks\Environment::caching_off( $slug ) ) { 47 $off[] = $names[ $slug ]; 48 } else { 49 $found[] = $names[ $slug ]; 50 } 63 51 } 64 52 } … … 66 54 return array( 67 55 'plugins' => array_values( array_unique( $found ) ), 56 'off' => array_values( array_unique( $off ) ), 68 57 'ready' => self::page_cache(), 69 58 'home' => home_url( '/' ), … … 71 60 } 72 61 73 /**74 * Whether WordPress is set up to answer from a page cache before plugins load: it then loads the page cache's75 * drop-in, advanced-cache.php, on every request, this one included.76 */77 62 public static function page_cache() { 78 63 if ( ! defined( 'WP_CACHE' ) || ! WP_CACHE ) { … … 89 74 } 90 75 91 /**92 * Each entry answers true when its plugin is there and was asked to clear. The calls are the plugins' own93 * public ones; a plugin that is not active is skipped.94 */95 76 private static function known() { 96 77 return array( … … 153 134 } 154 135 155 /**156 * Calls a cache plugin's function when it is there. What the function returns says nothing about whether157 * the cache was cleared (some return nothing), so being called is what counts.158 */159 136 private static function call( $callback, ...$args ) { 160 137 if ( ! is_callable( $callback ) ) { -
imzura-site-check/trunk/includes/class-check.php
r3797250 r3797325 1 1 <?php 2 /** 3 * One thing the scanner looks at. A check returns findings; it never changes the site. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 12 8 abstract class Check { 13 9 14 /**15 * @param array $pages What Probe recorded for each page the browser opened.16 * @return array[] Findings.17 */18 10 abstract public function run( array $pages ); 19 11 20 /**21 * Hands over what only the browser could see (response headers), before run(). Most checks need none of it.22 *23 * @param array $seen What the admin screen's browser reported.24 */25 12 public function browser( array $seen ) { 13 } 14 15 public function admin( array $pages ) { 26 16 } 27 17 … … 38 28 } 39 29 30 protected static function owner_by_prefix( $handle ) { 31 $owners = array(); 32 33 foreach ( (array) get_option( 'active_plugins', array() ) as $file ) { 34 $owners[ 'plugin:' . strtok( (string) $file, '/' ) ] = strtok( (string) $file, '/' ); 35 } 36 37 $owners[ 'theme:' . get_stylesheet() ] = get_stylesheet(); 38 $owners[ 'theme:' . get_template() ] = get_template(); 39 40 uasort( 41 $owners, 42 static function ( $a, $b ) { 43 return strlen( $b ) <=> strlen( $a ); 44 } 45 ); 46 47 foreach ( $owners as $owner => $slug ) { 48 if ( '' !== $slug && 0 === strpos( strtolower( $handle ), strtolower( $slug ) ) ) { 49 return $owner; 50 } 51 } 52 53 return ''; 54 } 55 40 56 protected function kb( $kb ) { 41 57 return $kb >= 1024 … … 49 65 return Plugin::plugin_name( $slug ); 50 66 } 67 68 protected function who( $owner ) { 69 $owner = (string) $owner; 70 71 if ( 0 === strpos( $owner, 'plugin:' ) ) { 72 return $this->plugin_name( substr( $owner, 7 ) ); 73 } 74 75 if ( 0 === strpos( $owner, 'theme:' ) ) { 76 $theme = wp_get_theme( substr( $owner, 6 ) ); 77 78 /* translators: %s: theme name */ 79 return sprintf( __( 'the %s theme', 'imzura-site-check' ), $theme->exists() ? $theme->get( 'Name' ) : substr( $owner, 6 ) ); 80 } 81 82 if ( 0 === strpos( $owner, 'external:' ) ) { 83 return substr( $owner, 9 ); 84 } 85 86 if ( 'core' === $owner ) { 87 return __( 'WordPress itself', 'imzura-site-check' ); 88 } 89 90 return 'inline' === $owner ? __( 'a script written into the page', 'imzura-site-check' ) : __( 'another source', 'imzura-site-check' ); 91 } 92 93 protected static function line( $value, $length = 200 ) { 94 return is_scalar( $value ) ? mb_substr( sanitize_text_field( (string) $value ), 0, $length ) : ''; 95 } 96 97 protected static function items( array $look, $key, $limit ) { 98 $items = isset( $look[ $key ] ) && is_array( $look[ $key ] ) ? array_slice( $look[ $key ], 0, $limit ) : array(); 99 100 return array_values( array_filter( $items, 'is_array' ) ); 101 } 102 103 protected static function look( array $looks, $kind ) { 104 return isset( $looks[ $kind ]['look'] ) && is_array( $looks[ $kind ]['look'] ) ? $looks[ $kind ]['look'] : array(); 105 } 106 107 protected static function looks( array $seen ) { 108 $looks = array(); 109 $pages = isset( $seen['pages'] ) && is_array( $seen['pages'] ) ? $seen['pages'] : array(); 110 111 foreach ( $pages as $kind => $page ) { 112 if ( is_string( $kind ) && is_array( $page ) ) { 113 $looks[ sanitize_key( $kind ) ] = $page; 114 } 115 } 116 117 return $looks; 118 } 51 119 } -
imzura-site-check/trunk/includes/class-conflicts.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Other optimizers that already do what one of our fixes does. 4 * 5 * Two plugins minifying the same files, or delaying the same scripts, are a common cause of broken pages. So a 6 * fix whose job another active plugin already does is not tried: the screen names the plugin and the exact 7 * setting, and links to its settings page. This plugin only reads the other plugin's saved settings; it never 8 * changes them. The settings and labels below come from each plugin's own code (BRAIN conflict-map). 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor; … … 17 8 final class Conflicts { 18 9 19 /**20 * Plugins that may do the same and whose settings we cannot read (paid, or stored in a way we have not21 * checked). They do not block a fix; the screen asks to keep only one of the two.22 */23 10 const UNREAD = array( 24 11 'wp-rocket' => 'WP Rocket', … … 29 16 ); 30 17 31 /**32 * @param string $feature What a fix does, see Fix::overlaps().33 * @return array[] The active plugins that do it too: name, setting, url (their settings page), deactivate (a34 * link for plugins that do nothing else, or ''), sure (false when their settings could not be read).35 */36 18 public static function active( $feature ) { 37 19 if ( '' === $feature ) { … … 75 57 } 76 58 77 /**78 * @return array[] The conflicts that stop a fix from being tried.79 */80 59 public static function blocking( $feature ) { 81 60 $blocking = array(); … … 90 69 } 91 70 92 /**93 * @return string[] Active plugins: folder => plugin file.94 */95 71 private static function plugins() { 96 72 $files = (array) get_option( 'active_plugins', array() ); … … 109 85 } 110 86 111 /**112 * Per plugin folder: its name, its settings page, whether it does nothing but this (then it may be113 * deactivated from our screen), and per feature: the setting as its screen labels it, and a reader.114 */115 87 private static function known() { 116 88 $option = static function ( $name ) { … … 217 189 'minify-js' => array( 'Settings → Minify JavaScript', $in( 'wpassetcleanup_settings', 'minify_loaded_js' ) ), 218 190 'minify-css' => array( 'Settings → Minify CSS', $in( 'wpassetcleanup_settings', 'minify_loaded_css' ) ), 191 'unload' => array( 'CSS & JS Manager', '__return_true' ), 219 192 ), 220 193 ), … … 228 201 ), 229 202 ), 203 'wp-retina-2x' => array( 204 'name' => 'Perfect Images', 205 'page' => 'admin.php?page=wr2x_settings', 206 'features' => array( 207 'image-subsizes' => array( 208 'Meow Apps → Perfect Images → Image Sizes', 209 static function () { 210 $settings = self::decode( get_option( 'wr2x_options' ) ); 211 212 return ! empty( $settings['disabled_sizes'] ); 213 }, 214 ), 215 ), 216 ), 217 'disable-media-sizes' => array( 218 'name' => 'Disable Media Sizes', 219 'page' => 'options-general.php?page=disable-media-sizes', 220 'features' => array( 221 'image-subsizes' => array( 222 'Settings → Disable Media Sizes', 223 static function () { 224 $settings = self::decode( get_option( 'disable_media_sizes_options' ) ); 225 226 foreach ( array( 'thumbnail', 'medium', 'large', 'medium-large', '1536x1536', '2048x2048' ) as $size ) { 227 if ( ! empty( $settings[ 'disable-size-' . $size ] ) ) { 228 return true; 229 } 230 } 231 232 return false; 233 }, 234 ), 235 ), 236 ), 230 237 ); 231 238 } 232 239 233 /**234 * W3 Total Cache keeps its settings in a file, as JSON after a line that stops PHP from running it.235 */236 240 private static function w3tc( $key ) { 237 241 return static function () use ( $key ) { … … 265 269 } 266 270 267 /**268 * Settings saved as an array, an object, or JSON text.269 */270 271 private static function decode( $value ) { 271 272 if ( is_string( $value ) ) { -
imzura-site-check/trunk/includes/class-fix.php
r3797250 r3797325 1 1 <?php 2 /** 3 * One change this plugin can make to the site. 4 * 5 * A fix stays off until it has been tried on a test visitor, compared with the untouched page, and found 6 * to save something without changing what the visitor sees. Fixer runs that trial. 7 * 8 * @package SpeedDoctor 9 */ 2 // imzura Site Check by imzura.com 10 3 11 4 namespace SpeedDoctor; … … 21 14 abstract public function summary(); 22 15 23 /**24 * Adds the WordPress hooks that make the change. Called only on requests where the fix is on.25 */26 16 abstract public function hook(); 27 17 28 /**29 * @return string Why the fix cannot run on this site, or an empty string when it can.30 */31 18 public function unavailable() { 32 19 return ''; 33 20 } 34 21 35 /**36 * For a fix that changes the finished page: WordPress hands it to plugins from 6.9 on, through the template37 * enhancement output buffer.38 *39 * @return string Why such a fix cannot run on this site, or an empty string when it can.40 */41 22 protected static function needs_finished_page() { 42 23 return function_exists( 'wp_start_template_enhancement_output_buffer' ) ? '' : __( 'This fix needs WordPress 6.9 or newer.', 'imzura-site-check' ); 43 24 } 44 25 45 /**46 * Runs on every front-end request, fix on or off. For noticing things only: it must not change the page.47 */48 26 public function watch() { 49 27 } 50 28 51 /**52 * @return string Why a trial cannot start yet, or an empty string. Unlike unavailable(), this may be slow:53 * it is only asked on the admin screen.54 */55 29 public function untestable() { 56 30 return ''; 57 31 } 58 32 59 /**60 * @return string The part of the browser's report a trial hands to prepare() before the fix is switched on61 * ('images'), or an empty string when the fix needs nothing measured first.62 */63 33 public function evidence() { 64 34 return ''; 65 35 } 66 36 67 /**68 * Gets ready with what the untouched pages showed. Runs once in a trial, before the views with the fix.69 *70 * @param array $evidence What the browser reported under evidence(). It comes from a browser: treat it as input.71 */72 37 public function prepare( array $evidence ) { 73 38 } 74 39 75 /** 76 * @return string What the fix does that another optimizer may already do ('minify-css', 'minify-js', 'defer-js'), or an 77 * empty string. Such a fix is not tried while the other plugin has it on (see Conflicts). 78 */ 40 public function excuse( array $files ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- a fix that can excuse a file reads it. 41 return array(); 42 } 43 44 public function excused() { 45 return 0; 46 } 47 79 48 public function overlaps() { 80 49 return ''; 81 50 } 82 51 83 /**84 * @return string The admin tab the fix is listed in: 'code' (stylesheets and scripts), 'images' or 'plugins'.85 */86 52 public function group() { 87 53 return 'plugins'; 88 54 } 89 55 90 /**91 * @return string[] The kinds of pages (see Probe::targets()) the fix is tried on.92 */93 56 public function kinds() { 94 57 return array( 'home', 'post', 'shop', 'product' ); 95 58 } 96 59 97 /**98 * @return array[] Pages of its own the fix is tried on as well: kind, label, url.99 */100 60 public function targets() { 101 61 return array(); 102 62 } 103 63 104 /**105 * @return string[] CSS selectors for things a script sets up. A page must match each of them as many106 * times with the fix as without it.107 */108 64 public function marks() { 109 65 return array(); -
imzura-site-check/trunk/includes/class-fixer.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Tries a fix, keeps it only when it is proven, and remembers the outcome. 4 * 5 * A trial opens each page three times as a first-time visitor: twice untouched ("before" and "control", which 6 * together show what the page does on its own) and once with the fix switched on for that request only ("after"). 7 * The admin's browser compares what it saw; this class adds what the server saw and decides. 8 * 9 * @package SpeedDoctor 10 */ 2 // imzura Site Check by imzura.com 11 3 12 4 namespace SpeedDoctor; … … 19 11 const RESCUE = 'speed_doctor_rescue'; 20 12 const STEPS = array( 'before', 'control', 'after' ); 21 const SEEN = array( 'frame', 'error', 'text', 'layout', 'mark', 'listener' ); 22 23 // A page counts as lighter when it weighs under this share of what it did: less than that is noise. 13 const SEEN = array( 'frame', 'error', 'text', 'layout', 'mark', 'listener', 'lcp' ); 14 24 15 const LIGHTER = 0.95; 25 16 26 // Bytes a fix measured itself taking off a page (see hold()). Exact, so no noise margin: 1 KB counts.27 17 const EXACT = 1024; 28 18 … … 34 24 $fixes = array(); 35 25 36 foreach ( apply_filters( 'speed_doctor_fixes', array( new Fixes\Cart_Script(), new Fixes\WooCommerce_Files(), new Fixes\Contact_Form_7(), new Fixes\Heavy_Images(), new Fixes\ Minify_Styles(), new Fixes\Minify_Scripts(), new Fixes\Emoji(), new Fixes\Jquery_Migrate(), new Fixes\Defer_Scripts(), new Fixes\Video_Facade() ) ) as $fix ) {26 foreach ( apply_filters( 'speed_doctor_fixes', array( new Fixes\Cart_Script(), new Fixes\WooCommerce_Files(), new Fixes\Contact_Form_7(), new Fixes\Heavy_Images(), new Fixes\Lcp_Image(), new Fixes\Image_Sizes(), new Fixes\Local_Fonts(), new Fixes\Minify_Styles(), new Fixes\Minify_Scripts(), new Fixes\Emoji(), new Fixes\Visitor_Heartbeat(), new Fixes\Jquery_Migrate(), new Fixes\Defer_Scripts(), new Fixes\Video_Facade() ) ) as $fix ) { 37 27 if ( $fix instanceof Fix ) { 38 28 $fixes[ $fix->id() ] = $fix; … … 66 56 } 67 57 68 /**69 * A fix calls this when it changes a page. With a script handle, or a stylesheet handle written as70 * 'style:handle', the report also stops counting that file: the fix took its tag out of the page, so71 * WordPress still lists it as printed. A fix that knows exactly how many bytes it took off (a smaller72 * copy of a file, code it left out) passes them as well; a trial counts those as a saving.73 */74 58 public static function hold( $id, $handle = '', $bytes = 0 ) { 75 59 if ( ! isset( self::$held[ $id ] ) ) { … … 79 63 if ( '' !== $handle ) { 80 64 self::$held[ $id ][] = $handle; 65 Pipeline::mark( $handle, $id ); 81 66 } 82 67 … … 90 75 } 91 76 92 /**93 * @return int[] Per fix, the bytes it measured taking off this page.94 */95 77 public static function savings() { 96 78 return self::$saved; … … 117 99 'problems' => isset( $entry['problems'] ) ? $entry['problems'] : array(), 118 100 'cache' => isset( $entry['cache'] ) ? $entry['cache'] : null, 101 'aside' => isset( $entry['aside'] ) ? (int) $entry['aside'] : 0, 102 'excused' => '' !== $why ? 0 : $fix->excused(), 119 103 ); 120 104 } … … 136 120 $targets = array_merge( $targets, $fix->targets() ); 137 121 138 // Every page is tried twice, wide and narrow: a stylesheet can hold rules that only apply on a phone.139 122 foreach ( $targets as $target ) { 140 123 $views[] = $target + array( 'screen' => 'wide' ); … … 148 131 } 149 132 150 // Every untouched view comes first: a fix may need what they show before it is switched on.151 133 foreach ( array( array( 'before', 'control' ), array( 'after' ) ) as $steps ) { 152 134 foreach ( $views as $view ) { … … 174 156 } 175 157 176 /**177 * @param Fix $fix The fix that was tried.178 * @param array $tokens The probe tokens trial() handed out.179 * @param array $pages Per page, what the browser counted without and with the fix.180 * @param array $problems What the browser saw change.181 */182 158 public static function verdict( Fix $fix, array $tokens, array $pages, array $problems ) { 183 159 $id = $fix->id(); … … 192 168 $problems = self::problems( $problems ); 193 169 $pages = self::numbers( $pages ); 170 $aside = $problems ? $fix->excuse( array_values( array_filter( array_column( $problems, 'file' ) ) ) ) : array(); 194 171 $acted = false; 195 172 $saves = false; … … 220 197 221 198 foreach ( $pages as $page ) { 222 $saves = $saves || $page['calls'][1] < $page['calls'][0] || $page['files'][1] < $page['files'][0] || $page['kb'][1] < self::LIGHTER * $page['kb'][0] || $page['blocking'][1] < $page['blocking'][0] || $page['frames'][1] < $page['frames'][0] ;199 $saves = $saves || $page['calls'][1] < $page['calls'][0] || $page['files'][1] < $page['files'][0] || $page['kb'][1] < self::LIGHTER * $page['kb'][0] || $page['blocking'][1] < $page['blocking'][0] || $page['frames'][1] < $page['frames'][0] || $page['lcp'][1] < $page['lcp'][0] || $page['sizes'][1] < $page['sizes'][0] || $page['hosts'][1] < $page['hosts'][0]; 223 200 } 224 201 … … 230 207 'pages' => $pages, 231 208 'problems' => $problems, 209 'aside' => count( $aside ), 232 210 ) 233 211 ); … … 248 226 } 249 227 250 /**251 * Runs when the plugin is deactivated: pages cached while a fix was on must not outlive it.252 */253 228 public static function retire() { 254 229 foreach ( self::saved() as $entry ) { … … 261 236 } 262 237 263 /**264 * Saves the outcome of a trial or a "Turn off". When the fix was on before or is on now, the pages visitors265 * get have changed, so page caches are told to drop their copies, after the new state is saved.266 */267 238 private static function settle( $id, array $entry ) { 268 239 $saved = self::saved(); … … 279 250 } 280 251 281 /**282 * The address that switches every fix off without logging in.283 */284 252 public static function rescue_url() { 285 253 $key = (string) get_option( self::RESCUE, '' ); … … 309 277 update_option( self::OPTION, array(), true ); 310 278 311 // Cache plugins are not all loaded yet, and the copies they hold are the pages the owner is escaping from.312 279 add_action( 313 280 'wp_loaded', … … 338 305 foreach ( array_slice( $seen, 0, 20 ) as $item ) { 339 306 if ( is_array( $item ) && isset( $item['kind'] ) && in_array( $item['kind'], self::SEEN, true ) ) { 340 $problem s[]= self::problem(307 $problem = self::problem( 341 308 isset( $item['label'] ) ? self::line( $item['label'], 100 ) : '', 342 309 $item['kind'], 343 310 isset( $item['detail'] ) ? self::line( $item['detail'], 200 ) : '' 344 311 ); 312 313 if ( 'error' === $item['kind'] && ! empty( $item['file'] ) && is_string( $item['file'] ) ) { 314 $problem['file'] = esc_url_raw( substr( $item['file'], 0, 500 ) ); 315 } 316 317 $problems[] = $problem; 345 318 } 346 319 } … … 359 332 $page = array( 'label' => isset( $item['label'] ) ? self::line( $item['label'], 100 ) : '' ); 360 333 361 foreach ( array( 'calls', 'files', 'kb', 'ms', 'blocking', 'frames' ) as $key ) {334 foreach ( array( 'calls', 'files', 'kb', 'ms', 'blocking', 'frames', 'lcp', 'sizes', 'hosts' ) as $key ) { 362 335 $pair = isset( $item[ $key ] ) && is_array( $item[ $key ] ) ? array_values( $item[ $key ] ) : array(); 363 336 $page[ $key ] = array( isset( $pair[0] ) ? absint( $pair[0] ) : 0, isset( $pair[1] ) ? absint( $pair[1] ) : 0 ); -
imzura-site-check/trunk/includes/class-paths.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Where a file that a page loads lives on this server, found with WordPress's own location functions. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 12 8 final class Paths { 13 9 14 /**15 * @var array<string, string>|null16 */17 10 private static $roots = null; 18 11 19 /** 20 * @param string $url An address, with or without its scheme. 21 * @param string $base What an address that starts with a single slash is relative to. 22 * @return string The file on this server the address points to (it may not exist), or an empty string when the 23 * address points elsewhere or climbs out of its folder. 24 */ 12 private static $must = null; 13 25 14 public static function file( $url, $base = '' ) { 26 15 $url = (string) strtok( (string) $url, '?#' ); … … 39 28 $rest = rawurldecode( substr( $url, strlen( $root ) ) ); 40 29 41 if ( '' === $rest || false !== strpos( $rest, "\0" ) || preg_match( '#(^|[/\\\\])\.\.([/\\\\]|$)#', $rest ) ) { 42 return ''; 43 } 44 45 return $dir . $rest; 30 return '' !== $rest && self::safe( $rest ) ? $dir . $rest : ''; 46 31 } 47 32 … … 49 34 } 50 35 51 /** 52 * @return string The address without its scheme, so that http and https addresses compare equal. 53 */ 36 public static function dir( $url ) { 37 $url = self::folder( (string) strtok( (string) $url, '?#' ) ); 38 39 foreach ( self::roots() as $root => $dir ) { 40 if ( 0 === strpos( $url, $root ) ) { 41 $rest = rawurldecode( substr( $url, strlen( $root ) ) ); 42 43 return self::safe( $rest ) ? $dir . $rest : ''; 44 } 45 } 46 47 foreach ( self::roots() as $root => $dir ) { 48 if ( 0 !== strpos( $root, $url ) ) { 49 continue; 50 } 51 52 $tail = rawurldecode( substr( $root, strlen( $url ) ) ); 53 54 if ( self::safe( $tail ) && substr( $dir, -strlen( $tail ) ) === $tail ) { 55 return substr( $dir, 0, -strlen( $tail ) ); 56 } 57 } 58 59 return ''; 60 } 61 54 62 public static function bare( $url ) { 55 63 return (string) preg_replace( '#^https?:#i', '', (string) $url ); 56 64 } 57 65 58 /**59 * @return string The folder address, without the scheme and with a trailing slash.60 */61 66 public static function folder( $url ) { 62 67 return trailingslashit( self::bare( $url ) ); 63 68 } 64 69 65 /** 66 * Folder addresses mapped to the folders they serve, the longest first: a site may keep its plugins, themes or 67 * uploads away from the WordPress folder. 68 * 69 * @return array<string, string> 70 */ 70 public static function plugins() { 71 $base = self::folder( plugins_url() ); 72 $url = self::folder( plugin_dir_url( SPEED_DOCTOR_FILE ) ); 73 $dir = wp_normalize_path( plugin_dir_path( SPEED_DOCTOR_FILE ) ); 74 75 if ( 0 !== strpos( $url, $base ) ) { 76 return ''; 77 } 78 79 $tail = rawurldecode( substr( $url, strlen( $base ) ) ); 80 81 if ( '' === $tail || substr( $dir, -strlen( $tail ) ) !== $tail ) { 82 return ''; 83 } 84 85 return substr( $dir, 0, -strlen( $tail ) ); 86 } 87 88 public static function must_use() { 89 if ( null === self::$must ) { 90 $files = wp_get_mu_plugins(); 91 self::$must = $files ? trailingslashit( wp_normalize_path( dirname( (string) reset( $files ) ) ) ) : ''; 92 } 93 94 return self::$must; 95 } 96 97 private static function safe( $rest ) { 98 return false === strpos( $rest, "\0" ) && ! preg_match( '#(^|[/\\\\])\.\.([/\\\\]|$)#', $rest ); 99 } 100 71 101 private static function roots() { 72 102 if ( null !== self::$roots ) { … … 107 137 return $roots; 108 138 } 109 110 /**111 * The plugins folder, found from where this plugin lives and the address it is served from.112 *113 * @return string The folder, or an empty string when this plugin is not in it (a must-use plugin, say).114 */115 private static function plugins() {116 $base = self::folder( plugins_url() );117 $url = self::folder( plugin_dir_url( SPEED_DOCTOR_FILE ) );118 $dir = wp_normalize_path( plugin_dir_path( SPEED_DOCTOR_FILE ) );119 120 if ( 0 !== strpos( $url, $base ) ) {121 return '';122 }123 124 $tail = rawurldecode( substr( $url, strlen( $base ) ) );125 126 if ( '' === $tail || substr( $dir, -strlen( $tail ) ) !== $tail ) {127 return '';128 }129 130 return substr( $dir, 0, -strlen( $tail ) );131 }132 139 } -
imzura-site-check/trunk/includes/class-plugin.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Wires the plugin together. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 19 15 Probe::listen(); 20 16 21 // Fixes say in words why one cannot run, and WordPress loads translations from init on.22 17 add_action( 'init', array( Fixer::class, 'boot' ), 0 ); 23 18 … … 55 50 new Checks\Environment(), 56 51 new Checks\Delivery(), 52 new Checks\Errors(), 53 new Checks\Images(), 54 new Checks\Image_Copies(), 55 new Checks\Fonts(), 56 new Checks\Scripts(), 57 new Checks\Idle_Styles(), 58 new Checks\Code_Time(), 59 new Checks\Plugin_Settings(), 60 new Checks\Housekeeping(), 61 new Checks\Heartbeat(), 57 62 ); 58 63 -
imzura-site-check/trunk/includes/class-probe.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Reads a page from the inside while a visitor's browser loads it. 4 * 5 * The admin screen asks the browser to open each page logged out, with a one-time token in the address. 6 * When that request arrives, this class records what the page was built from: the files it queued and who 7 * queued them, the media in its HTML, how long the server took. No request from the server to itself is 8 * needed, so it works on hosts that block loopback requests. 9 * 10 * When a fix is on trial the page is opened in a hidden frame of the admin screen instead, as a stranger: 11 * the request is served without the administrator's cookies and sets none, and assets/probe.js reports 12 * back what the browser saw. 13 * 14 * @package SpeedDoctor 15 */ 2 // imzura Site Check by imzura.com 16 3 17 4 namespace SpeedDoctor; … … 30 17 private static $html = null; 31 18 19 private static $folders = null; 20 32 21 private static $trial = array(); 33 22 34 23 private static $forced = array(); 24 25 private static $errors = array(); 35 26 36 27 public static function targets() { … … 92 83 } 93 84 94 /** 95 * @param array $targets Pages to read. 96 * @param array $trial For a fix on trial: fix, apply, step, stranger. 97 */ 85 public static function admin_targets() { 86 $targets = array( 87 array( 88 'kind' => 'admin-dashboard', 89 'label' => __( 'Dashboard (admin)', 'imzura-site-check' ), 90 'url' => admin_url( 'index.php' ), 91 ), 92 array( 93 'kind' => 'admin-posts', 94 'label' => __( 'Posts list (admin)', 'imzura-site-check' ), 95 'url' => admin_url( 'edit.php' ), 96 ), 97 ); 98 99 if ( function_exists( 'wc_get_page_permalink' ) ) { 100 $targets[] = array( 101 'kind' => 'admin-products', 102 'label' => __( 'Products list (admin)', 'imzura-site-check' ), 103 'url' => admin_url( 'edit.php?post_type=product' ), 104 ); 105 106 $orders = '\Automattic\WooCommerce\Utilities\OrderUtil'; 107 108 $targets[] = array( 109 'kind' => 'admin-orders', 110 'label' => __( 'Orders list (admin)', 'imzura-site-check' ), 111 'url' => class_exists( $orders ) && $orders::custom_orders_table_usage_is_enabled() ? admin_url( 'admin.php?page=wc-orders' ) : admin_url( 'edit.php?post_type=shop_order' ), 112 ); 113 } 114 115 return apply_filters( 'speed_doctor_admin_targets', $targets ); 116 } 117 98 118 public static function issue( array $targets, array $trial = array() ) { 99 119 $issued = array(); … … 122 142 'token' => $token, 123 143 'url' => add_query_arg( self::PARAM, $token, $target['url'] ), 144 'admin' => ! empty( $trial['admin'] ), 124 145 ); 125 146 } … … 147 168 } 148 169 149 /**150 * @return bool|null Whether a trial switches this fix on or off for the current request; null when it does neither.151 */152 170 public static function forced( $fix ) { 153 171 return isset( self::$forced[ $fix ] ) ? self::$forced[ $fix ] : null; … … 160 178 $inner = false; 161 179 162 // A request the page under trial made itself (assets/probe.js adds the header).163 180 if ( '' === $token && isset( $_SERVER[ $key ] ) ) { 164 181 $token = sanitize_key( wp_unslash( $_SERVER[ $key ] ) ); … … 189 206 190 207 self::$token = $token; 208 self::catch_errors(); 191 209 192 210 if ( ! defined( 'DONOTCACHEPAGE' ) ) { … … 204 222 } 205 223 206 /**207 * Serves the request as a first-time visitor: the administrator's cookies are not read and none are set,208 * so a trial can neither see nor disturb the session of the person running it.209 */210 224 private static function stranger() { 211 225 $_COOKIE = array(); … … 224 238 ); 225 239 226 // The same "random" choices on every view, so a page that shuffles its content (related products,227 // a random post) can be compared with itself. assets/probe.js does the same for Math.random.228 240 // phpcs:ignore WordPress.WP.AlternativeFunctions.rand_seeding_mt_srand -- a fixed seed is the point; it applies to this test request only. 229 241 mt_srand( 1 ); … … 245 257 'origin' => isset( self::$trial['origin'] ) ? (string) self::$trial['origin'] : '', 246 258 'marks' => isset( self::$trial['marks'] ) ? array_values( (array) self::$trial['marks'] ) : array(), 259 'scan' => ! empty( self::$trial['scan'] ), 247 260 ) 248 ) . ';' . file_get_contents( SPEED_DOCTOR_DIR . 'assets/probe.js' ) 261 ) . ';' . file_get_contents( SPEED_DOCTOR_DIR . 'assets/probe.js' ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a file of this plugin, only read. 262 ); 263 } 264 265 private static function catch_errors() { 266 $previous = null; 267 268 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- only on a page the admin screen opened to read it; every error still goes on to the handler that was set before, or to PHP. 269 $previous = set_error_handler( 270 static function ( $level, $message, $file = '', $line = 0 ) use ( &$previous ) { 271 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure_error_reporting, WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting, PluginCheck.CodeAnalysis.PHPErrorReporting.DirectErrorReportingCall -- only reads the level the site reports, to note no more than that; nothing is changed. 272 if ( error_reporting() & $level ) { 273 self::note_error( (int) $level, (string) $message, (string) $file, (int) $line ); 274 } 275 276 return is_callable( $previous ) ? (bool) call_user_func( $previous, $level, $message, $file, $line ) : false; 277 } 278 ); 279 280 add_filter( 281 'wp_should_handle_php_error', 282 static function ( $handle, $error ) { 283 if ( is_array( $error ) && isset( $error['type'], $error['message'], $error['file'], $error['line'] ) ) { 284 self::note_error( (int) $error['type'], (string) $error['message'], (string) $error['file'], (int) $error['line'] ); 285 self::record(); 286 } 287 288 return $handle; 289 }, 290 PHP_INT_MAX, 291 2 292 ); 293 } 294 295 private static function note_error( $level, $message, $file, $line ) { 296 $key = md5( $level . '|' . $file . '|' . $line . '|' . $message ); 297 298 if ( isset( self::$errors[ $key ] ) ) { 299 ++self::$errors[ $key ]['count']; 300 return; 301 } 302 303 if ( count( self::$errors ) >= 30 ) { 304 return; 305 } 306 307 $fatal = in_array( $level, array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ); 308 $kind = $fatal ? 'fatal' : ( in_array( $level, array( E_DEPRECATED, E_USER_DEPRECATED ), true ) ? 'deprecated' : ( in_array( $level, array( E_NOTICE, E_USER_NOTICE ), true ) ? 'notice' : 'warning' ) ); 309 310 self::$errors[ $key ] = array( 311 'kind' => $kind, 312 'message' => mb_substr( (string) preg_replace( '/ in \/\S+:\d+$/', '', trim( (string) strtok( wp_strip_all_tags( $message ), "\n" ) ) ), 0, 300 ), 313 'file' => ltrim( str_replace( wp_normalize_path( ABSPATH ), '', wp_normalize_path( $file ) ), '/' ), 314 'line' => $line, 315 'owner' => '' === $file ? 'other' : self::owner_of_path( $file ), 316 'count' => 1, 249 317 ); 250 318 } … … 267 335 } 268 336 269 /**270 * The finished page as it is sent. WordPress 6.9 and later hand it over from the template enhancement output271 * buffer; without it, what is read from the page itself is left unknown.272 */273 337 public static function keep( $html ) { 274 338 self::$html = (string) $html; … … 284 348 $seen = null !== self::$html; 285 349 $html = (string) self::$html; 286 287 $page['pending'] = false; 288 $page['seconds'] = (float) timer_stop( 0, 3 ); 289 $page['queries'] = (int) get_num_queries(); 290 $page['memory'] = (int) memory_get_peak_usage(); 291 $page['html_kb'] = $seen ? (int) round( strlen( $html ) / 1024 ) : null; 292 $page['status'] = (int) http_response_code(); 293 $page['scripts'] = self::files( wp_scripts() ); 294 $page['styles'] = self::files( wp_styles() ); 295 $page['media'] = $seen ? self::media( $html ) : array(); 296 $page['applied'] = array_keys( Fixer::held() ); 297 $page['saved'] = Fixer::savings(); 298 $page['context'] = array( 350 $last = error_get_last(); 351 352 if ( is_array( $last ) && in_array( $last['type'], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR ), true ) ) { 353 self::note_error( (int) $last['type'], (string) $last['message'], (string) $last['file'], (int) $last['line'] ); 354 } 355 356 $seconds = microtime( true ) - (float) $GLOBALS['timestart'] - Profiler::cost(); 357 358 $page['pending'] = false; 359 $page['php'] = array_values( self::$errors ); 360 $page['code'] = Profiler::report(); 361 $page['seconds'] = round( max( 0, $seconds ), 3 ); 362 $page['queries'] = (int) get_num_queries(); 363 $page['memory'] = (int) memory_get_peak_usage(); 364 $page['html_kb'] = $seen ? (int) round( strlen( $html ) / 1024 ) : null; 365 $page['status'] = (int) http_response_code(); 366 $page['scripts'] = self::files( wp_scripts() ); 367 $page['styles'] = self::files( wp_styles() ); 368 $page['media'] = $seen ? self::media( $html ) : array(); 369 $page['pulse'] = self::pulse( wp_scripts() ); 370 $page['listeners'] = wp_doing_ajax() ? self::listeners() : array(); 371 $page['applied'] = array_keys( Fixer::held() ); 372 $page['saved'] = Fixer::savings(); 373 $page['manifest'] = Pipeline::manifest(); 374 $page['context'] = array( 299 375 'store_page' => function_exists( 'is_woocommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ), 300 376 'cart' => function_exists( 'is_cart' ) && ( is_cart() || is_checkout() ), … … 316 392 } 317 393 318 $src = $registry->registered[ $handle ]->src;394 $src = $registry->registered[ $handle ]->src; 319 395 list( $owner, $path ) = self::owner( $src, $registry->base_url ); 320 396 321 $file s[]= array(397 $file = array( 322 398 'handle' => (string) $handle, 399 'file' => wp_basename( (string) strtok( $src, '?#' ) ), 323 400 'owner' => $owner, 324 401 'kb' => $path && is_file( $path ) ? round( filesize( $path ) / 1024, 1 ) : null, 325 402 ); 403 404 if ( null === $path ) { 405 $file['url'] = esc_url_raw( (string) $src ); 406 } 407 408 $files[] = $file; 326 409 } 327 410 … … 329 412 } 330 413 414 private static function pulse( $registry ) { 415 if ( ! in_array( 'heartbeat', $registry->done, true ) || empty( $registry->registered['heartbeat']->src ) ) { 416 return null; 417 } 418 419 $settings = array(); 420 $data = (string) $registry->get_data( 'heartbeat', 'data' ); 421 422 if ( preg_match( '/heartbeatSettings\s*=\s*(\{.*?\});/s', $data, $found ) ) { 423 $settings = (array) json_decode( $found[1], true ); 424 } 425 426 $interval = isset( $settings['interval'] ) && is_numeric( $settings['interval'] ) ? (int) $settings['interval'] : 60; 427 $by = array(); 428 429 foreach ( array_unique( $registry->done ) as $handle ) { 430 if ( 'heartbeat' !== $handle && ! empty( $registry->registered[ $handle ] ) && in_array( 'heartbeat', (array) $registry->registered[ $handle ]->deps, true ) ) { 431 $src = (string) $registry->registered[ $handle ]->src; 432 $by[] = array( 433 'handle' => (string) $handle, 434 'owner' => '' === $src ? self::owner_by_name( (string) $handle ) : self::who( $src ), 435 ); 436 } 437 } 438 439 return array( 440 'interval' => min( 3600, max( 1, $interval ) ), 441 'by' => $by, 442 'setters' => self::setters( 'heartbeat_settings' ), 443 ); 444 } 445 446 private static function owner_by_name( $handle ) { 447 return in_array( $handle, array( 'autosave', 'wp-auth-check' ), true ) ? 'core' : 'other'; 448 } 449 450 private static function setters( $hook, array $skip = array( 'core', 'self', 'other' ) ) { 451 global $wp_filter; 452 453 $owners = array(); 454 455 if ( empty( $wp_filter[ $hook ] ) ) { 456 return $owners; 457 } 458 459 foreach ( $wp_filter[ $hook ]->callbacks as $callbacks ) { 460 foreach ( $callbacks as $callback ) { 461 $owner = self::callback_owner( $callback['function'] ); 462 463 if ( ! in_array( $owner, $skip, true ) ) { 464 $owners[] = $owner; 465 } 466 } 467 } 468 469 return array_values( array_unique( $owners ) ); 470 } 471 472 private static function listeners() { 473 $owners = array(); 474 475 foreach ( array( 'wp_ajax_nopriv_heartbeat', 'heartbeat_nopriv_received', 'heartbeat_nopriv_send', 'heartbeat_nopriv_tick' ) as $hook ) { 476 $owners = array_merge( $owners, self::setters( $hook, array( 'core', 'self' ) ) ); 477 } 478 479 return array_values( array_unique( $owners ) ); 480 } 481 482 public static function visitor_heartbeat() { 483 $issued = self::issue( 484 array( 485 array( 486 'kind' => 'heartbeat', 487 'label' => 'Heartbeat', 488 'url' => admin_url( 'admin-ajax.php' ), 489 ), 490 ) 491 ); 492 $headers = array( 'Cache-Control' => 'no-cache' ); 493 494 if ( isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) { 495 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- HTTP Basic authentication for a site behind a password, as WordPress Site Health sends it on its own loopback request. 496 $headers['Authorization'] = 'Basic ' . base64_encode( sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) ) . ':' . sanitize_text_field( wp_unslash( $_SERVER['PHP_AUTH_PW'] ) ) ); 497 } 498 499 $response = wp_remote_post( 500 $issued[0]['url'], 501 array( 502 'timeout' => 20, 503 'headers' => $headers, 504 'cookies' => array(), 505 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- the WordPress core filter for a request to this site, applied the way Site Health applies it. 506 'body' => array( 507 'action' => 'heartbeat', 508 'screen_id' => 'front', 509 ), 510 ) 511 ); 512 513 if ( ! wp_using_ext_object_cache() ) { 514 wp_cache_delete( '_transient_' . self::PREFIX . $issued[0]['token'], 'options' ); 515 wp_cache_delete( '_transient_timeout_' . self::PREFIX . $issued[0]['token'], 'options' ); 516 } 517 518 $page = self::results( array( $issued[0]['token'] ) ); 519 520 if ( is_wp_error( $response ) ) { 521 return array( 'error' => $response->get_error_message() ); 522 } 523 524 $answer = json_decode( (string) wp_remote_retrieve_body( $response ), true ); 525 526 if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) || ! is_array( $answer ) || ! isset( $answer['server_time'] ) ) { 527 /* translators: %s: HTTP status code */ 528 return array( 'error' => sprintf( __( 'the answer was not Heartbeat\'s (HTTP %s)', 'imzura-site-check' ), (int) wp_remote_retrieve_response_code( $response ) ) ); 529 } 530 531 if ( ! $page || ! empty( $page[0]['pending'] ) || ! isset( $page[0]['listeners'] ) ) { 532 return array( 'error' => __( 'the request did not report back', 'imzura-site-check' ) ); 533 } 534 535 return array( 'listeners' => (array) $page[0]['listeners'] ); 536 } 537 538 private static function callback_owner( $callback ) { 539 try { 540 if ( is_string( $callback ) && false !== strpos( $callback, '::' ) ) { 541 $callback = explode( '::', $callback, 2 ); 542 } 543 544 if ( is_array( $callback ) && 2 === count( $callback ) ) { 545 $reflection = new \ReflectionMethod( $callback[0], (string) $callback[1] ); 546 } elseif ( $callback instanceof \Closure || ( is_string( $callback ) && function_exists( $callback ) ) ) { 547 $reflection = new \ReflectionFunction( $callback ); 548 } elseif ( is_object( $callback ) && method_exists( $callback, '__invoke' ) ) { 549 $reflection = new \ReflectionMethod( $callback, '__invoke' ); 550 } else { 551 return 'other'; 552 } 553 } catch ( \ReflectionException $error ) { 554 return 'other'; 555 } 556 557 $file = $reflection->getFileName(); 558 559 if ( false === $file ) { 560 return 'core'; 561 } 562 563 return 0 === strpos( wp_normalize_path( $file ), wp_normalize_path( SPEED_DOCTOR_DIR ) ) ? 'self' : self::owner_of_path( $file ); 564 } 565 331 566 private static function owner( $src, $base ) { 567 if ( strpos( $src, '//' ) === 0 ) { 568 $src = ( is_ssl() ? 'https:' : 'http:' ) . $src; 569 } elseif ( strpos( $src, '/' ) === 0 ) { 570 $src = untrailingslashit( $base ) . $src; 571 } 572 332 573 $src = (string) strtok( $src, '?#' ); 333 $path = Paths::file( $src, $base ); 334 $bare = Paths::bare( 0 === strpos( $src, '/' ) && 0 !== strpos( $src, '//' ) ? untrailingslashit( $base ) . $src : $src ); 335 336 $owners = array( 337 'plugin:' => plugins_url(), 338 'theme:' => get_theme_root_uri(), 339 ); 340 341 foreach ( $owners as $kind => $root ) { 342 $root = Paths::folder( $root ); 343 344 if ( 0 === strpos( $bare, $root ) ) { 345 return array( $kind . strtok( substr( $bare, strlen( $root ) ), '/' ), self::found( $path ) ); 346 } 347 } 348 349 foreach ( array( includes_url(), admin_url() ) as $root ) { 350 if ( 0 === strpos( $bare, Paths::folder( $root ) ) ) { 351 return array( 'core', self::found( $path ) ); 352 } 353 } 354 355 $host = (string) wp_parse_url( 'http:' . $bare, PHP_URL_HOST ); 356 357 if ( '' === $path && strtolower( $host ) !== strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) ) { 358 return array( 'external:' . $host, null ); 359 } 360 361 return array( 'other', self::found( $path ) ); 362 } 363 364 private static function found( $path ) { 365 return '' === $path ? null : $path; 574 $path = Paths::file( $src ); 575 $host = (string) wp_parse_url( $src, PHP_URL_HOST ); 576 577 if ( '' === $path ) { 578 return array( strtolower( $host ) === strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ) ? 'other' : 'external:' . $host, null ); 579 } 580 581 $path = wp_normalize_path( $path ); 582 583 return array( self::owner_of_path( $path ), $path ); 584 } 585 586 public static function owner_of_path( $path ) { 587 $path = wp_normalize_path( (string) $path ); 588 $folders = self::folders(); 589 590 if ( '' !== $folders['plugins'] && strpos( $path, $folders['plugins'] ) === 0 ) { 591 return 'plugin:' . strtok( substr( $path, strlen( $folders['plugins'] ) ), '/' ); 592 } 593 594 if ( '' !== $folders['must'] && strpos( $path, $folders['must'] ) === 0 ) { 595 return 'plugin:' . preg_replace( '/\.php$/', '', (string) strtok( substr( $path, strlen( $folders['must'] ) ), '/' ) ); 596 } 597 598 if ( '' !== $folders['themes'] && strpos( $path, $folders['themes'] ) === 0 ) { 599 return 'theme:' . strtok( substr( $path, strlen( $folders['themes'] ) ), '/' ); 600 } 601 602 foreach ( $folders['core'] as $core ) { 603 if ( '' !== $core && strpos( $path, $core ) === 0 ) { 604 return 'core'; 605 } 606 } 607 608 return 'other'; 609 } 610 611 private static function folders() { 612 if ( null === self::$folders ) { 613 self::$folders = array( 614 'plugins' => Paths::plugins(), 615 'must' => Paths::must_use(), 616 'themes' => trailingslashit( wp_normalize_path( get_theme_root() ) ), 617 'core' => array( Paths::dir( includes_url() ), Paths::dir( admin_url() ) ), 618 ); 619 } 620 621 return self::$folders; 622 } 623 624 public static function who( $url ) { 625 $url = (string) strtok( (string) $url, '?#' ); 626 627 if ( '' === $url ) { 628 return 'other'; 629 } 630 631 return self::owner( $url, home_url( '/' ) )[0]; 366 632 } 367 633 … … 381 647 $path = Paths::file( $url, $origin ); 382 648 383 // A copy the heavy-pictures fix made is already the size it is shown at: there is nothing left to say about it.384 649 if ( '' === $path || ! is_file( $path ) || isset( $found[ $path ] ) || false !== strpos( wp_normalize_path( $path ), '/' . Fixes\Heavy_Images::FOLDER . '/' ) ) { 385 650 continue; -
imzura-site-check/trunk/includes/class-rest.php
r3797250 r3797325 1 1 <?php 2 /** 3 * The calls the admin screen makes. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 14 10 const SPACE = 'imzura-site-check/v1'; 15 11 12 const REVIEW_META = 'speed_doctor_review'; 13 16 14 public static function register() { 15 register_rest_route( 16 self::SPACE, 17 '/review', 18 array( 19 'methods' => 'POST', 20 'permission_callback' => array( __CLASS__, 'allowed' ), 21 'callback' => static function () { 22 update_user_meta( get_current_user_id(), self::REVIEW_META, time() ); 23 24 return rest_ensure_response( array( 'done' => true ) ); 25 }, 26 ) 27 ); 28 17 29 register_rest_route( 18 30 self::SPACE, … … 22 34 'permission_callback' => array( __CLASS__, 'allowed' ), 23 35 'callback' => static function () { 24 return rest_ensure_response( array( 'targets' => Probe::issue( Probe::targets() ) ) ); 36 return rest_ensure_response( 37 array( 38 'targets' => array_merge( 39 Probe::issue( 40 Probe::targets(), 41 array( 42 'stranger' => true, 43 'scan' => true, 44 ) 45 ), 46 Probe::issue( 47 Probe::admin_targets(), 48 array( 49 'admin' => true, 50 'scan' => true, 51 ) 52 ) 53 ), 54 ) 55 ); 25 56 }, 26 57 ) … … 73 104 register_rest_route( 74 105 self::SPACE, 106 '/tidy/revisions/(?P<step>run|undo|forget)', 107 array( 108 'methods' => 'POST', 109 'permission_callback' => array( __CLASS__, 'allowed' ), 110 'callback' => array( __CLASS__, 'tidy' ), 111 ) 112 ); 113 114 register_rest_route( 115 self::SPACE, 75 116 '/fixes/(?P<id>[a-z0-9-]+)/(?P<step>trial|prepare|verdict|off)', 76 117 array( … … 80 121 ) 81 122 ); 123 } 124 125 public static function tidy( \WP_REST_Request $request ) { 126 if ( 'run' === $request['step'] ) { 127 $result = Tidy::run(); 128 } elseif ( 'undo' === $request['step'] ) { 129 $result = Tidy::undo(); 130 } else { 131 Tidy::forget(); 132 $result = null; 133 } 134 135 return is_wp_error( $result ) ? $result : rest_ensure_response( array( 'tidy' => $result ) ); 82 136 } 83 137 -
imzura-site-check/trunk/includes/class-scanner.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Runs every check and keeps the last report. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor; … … 19 15 ); 20 16 21 /**22 * @param array $tokens The probe tokens the browser read the pages with.23 * @param array $seen What the browser saw that the server cannot (see Check::browser()).24 */25 17 public static function run( array $tokens, array $seen = array() ) { 26 18 $pages = Probe::results( $tokens ); 19 $front = array(); 20 $admin = array(); 27 21 $findings = array(); 22 23 foreach ( $pages as $page ) { 24 if ( self::is_admin( $page ) ) { 25 $admin[] = $page; 26 } else { 27 $front[] = $page; 28 } 29 } 28 30 29 31 foreach ( Plugin::checks() as $check ) { 30 32 $check->browser( $seen ); 33 $check->admin( $admin ); 31 34 32 foreach ( $check->run( $ pages) as $finding ) {35 foreach ( $check->run( $front ) as $finding ) { 33 36 $findings[] = $finding; 34 37 } 35 38 } 39 40 do_action( 'speed_doctor_seen', $seen ); 36 41 37 42 usort( … … 46 51 'time' => time(), 47 52 'pages' => array_map( array( __CLASS__, 'summary' ), $pages ), 48 'owners' => self::owners( $ pages),53 'owners' => self::owners( $front ), 49 54 'findings' => $findings, 50 55 ); … … 55 60 } 56 61 57 /**58 * What each plugin and the theme put on the pages that were read, heaviest first. Files and weight are59 * those of the page where it loads the most, so the numbers are something a visitor really downloads.60 */61 62 private static function owners( array $pages ) { 62 63 $owners = array(); … … 85 86 } 86 87 88 $code = isset( $page['code'] ) && is_array( $page['code'] ) ? $page['code'] : array(); 89 90 foreach ( $code as $owner => $spent ) { 91 if ( ( strpos( $owner, 'plugin:' ) === 0 || strpos( $owner, 'theme:' ) === 0 ) && $spent['ms'] >= 1 && ! isset( $here[ $owner ] ) ) { 92 $here[ $owner ] = array( 93 'files' => 0, 94 'kb' => 0.0, 95 ); 96 } 97 } 98 87 99 foreach ( $here as $owner => $load ) { 88 100 if ( ! isset( $owners[ $owner ] ) ) { … … 90 102 91 103 $owners[ $owner ] = array( 92 'kind' => $kind, 93 'name' => 'theme' === $kind ? wp_get_theme( $slug )->get( 'Name' ) : Plugin::plugin_name( $slug ), 94 'files' => 0, 95 'kb' => 0, 96 'pages' => 0, 104 'kind' => $kind, 105 'name' => 'theme' === $kind ? wp_get_theme( $slug )->get( 'Name' ) : Plugin::plugin_name( $slug ), 106 'files' => 0, 107 'kb' => 0, 108 'pages' => 0, 109 'ms' => null, 110 'queries' => 0, 97 111 ); 98 112 } … … 101 115 $owners[ $owner ]['files'] = $load['files']; 102 116 $owners[ $owner ]['kb'] = (int) round( $load['kb'] ); 117 } 118 119 if ( isset( $code[ $owner ] ) && $code[ $owner ]['ms'] >= (float) $owners[ $owner ]['ms'] ) { 120 $owners[ $owner ]['ms'] = (int) round( $code[ $owner ]['ms'] ); 121 $owners[ $owner ]['queries'] = (int) $code[ $owner ]['queries']; 103 122 } 104 123 … … 112 131 $owners, 113 132 static function ( $a, $b ) { 114 return $b['kb'] <=> $a['kb'];133 return array( (int) $b['ms'], $b['kb'] ) <=> array( (int) $a['ms'], $a['kb'] ); 115 134 } 116 135 ); 117 136 118 137 return $owners; 138 } 139 140 public static function is_admin( array $page ) { 141 return ! empty( $page['admin'] ); 119 142 } 120 143 … … 128 151 'url' => $page['url'], 129 152 'read' => $read, 153 'admin' => self::is_admin( $page ), 130 154 'seconds' => $read ? $page['seconds'] : null, 131 155 'queries' => $read ? $page['queries'] : null, … … 133 157 'files' => count( $files ), 134 158 'kb' => (int) round( array_sum( array_column( $files, 'kb' ) ) ), 159 'assets' => $read ? array_merge( self::assets( $page['scripts'], 'js' ), self::assets( $page['styles'], 'css' ) ) : array(), 160 ); 161 } 162 163 private static function assets( array $files, $type ) { 164 return array_map( 165 static function ( $file ) use ( $type ) { 166 return array( 167 'type' => $type, 168 'handle' => $file['handle'], 169 'owner' => $file['owner'], 170 'kb' => $file['kb'], 171 ); 172 }, 173 $files 135 174 ); 136 175 } -
imzura-site-check/trunk/includes/fixes/class-cart-script.php
r3797250 r3797325 1 1 <?php 2 /** 3 * WooCommerce's cart script, loaded only when the cart is in use. 4 * 5 * cart-fragments.js asks the server for the header cart on a visitor's first page view, whatever is in the cart. 6 * With this fix the page carries the empty cart itself (built the way WC_AJAX::get_refreshed_fragments() builds 7 * it), and assets/cart.js loads the real script as soon as the visitor has, or adds, something. 8 * 9 * @package SpeedDoctor 10 */ 2 // imzura Site Check by imzura.com 11 3 12 4 namespace SpeedDoctor\Fixes; … … 57 49 ), 58 50 JSON_HEX_TAG | JSON_HEX_AMP 59 ) . ';' . file_get_contents( SPEED_DOCTOR_DIR . 'assets/cart.js' ), 51 ) . ';' . file_get_contents( SPEED_DOCTOR_DIR . 'assets/cart.js' ), // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- a file of this plugin, only read. 60 52 array( 'id' => 'speed-doctor-cart-js' ) 61 53 ); 62 54 63 // Only the script file is swapped: inline code other plugins attached to this handle stays in place.64 55 $swapped = preg_replace_callback( 65 56 '#<script\b[^>]*\bid=(["\'])' . self::HANDLE . '-js\1[^>]*>\s*</script>#', … … 81 72 } 82 73 83 /**84 * @return array|null What the cart script would have fetched for an empty cart, or null when the cart is not empty.85 */86 74 private function empty_cart() { 87 75 if ( ! function_exists( 'WC' ) || ! function_exists( 'woocommerce_mini_cart' ) || ! WC()->cart || ! WC()->cart->is_empty() ) { -
imzura-site-check/trunk/includes/fixes/class-contact-form-7.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Contact Form 7's files, loaded only on pages that show a form. 4 * 5 * Contact Form 7 queues its script and stylesheet on every page, and the reCAPTCHA or Turnstile script with 6 * them when one is connected. Footer scripts are decided on evidence: they are taken out just before they 7 * would print, and only if no form was built on the page. The stylesheet prints in the head, before the page 8 * is built, so it is taken out when the page's own content holds no form; if a form turns up anyway (a widget, 9 * a page builder), the stylesheet is printed right in front of it. 10 * 11 * @package SpeedDoctor 12 */ 2 // imzura Site Check by imzura.com 13 3 14 4 namespace SpeedDoctor\Fixes; … … 57 47 } 58 48 59 /**60 * Both ways a form reaches a page are tried: written in the page's content, and added from outside it.61 */62 49 public function targets() { 63 50 $targets = array(); … … 101 88 } 102 89 103 /**104 * Remembers one page that shows a form its content does not hold (a widget, a page builder, a template),105 * so a trial can open it. A search finds the other kind.106 */107 90 public function sight( $elements ) { 108 91 if ( is_singular() && ! $this->expected() && ! get_option( self::SEEN ) ) { … … 122 105 123 106 public function head() { 124 // A block theme builds the page before it prints the head, so by now a form may already be known.125 107 if ( $this->built || $this->expected() ) { 126 108 return; … … 153 135 } 154 136 155 // For a form that did not come from a shortcode; style() has usually printed them by now.156 137 foreach ( $this->styles as $handle ) { 157 138 if ( ! wp_style_is( $handle, 'done' ) ) { … … 201 182 } 202 183 203 /**204 * A form built after the footer scripts were printed still gets its scripts.205 */206 184 public function late() { 207 185 $waiting = array(); -
imzura-site-check/trunk/includes/fixes/class-defer-scripts.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Scripts in the head loaded with defer, so they stop holding up the first paint. 4 * 5 * The fix asks WordPress's own loading strategies (WordPress 6.3+) to defer every script queued for the head, 6 * jQuery aside: code written into the page often calls jQuery straight away. WordPress keeps a script blocking 7 * when something that depends on it is blocking, and holds back inline code that follows a deferred script, so 8 * the order scripts run in is kept. What a deferred script would have held up is counted in bytes for the trial. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; … … 15 6 use SpeedDoctor\Fix; 16 7 use SpeedDoctor\Fixer; 8 use SpeedDoctor\Pipeline; 17 9 18 10 defined( 'ABSPATH' ) || exit; … … 57 49 } 58 50 59 /**60 * Every head script, and every head script they need, is asked to load with defer.61 */62 51 public function ask() { 63 52 $scripts = wp_scripts(); … … 73 62 } 74 63 75 /**76 * A head script that came out deferred no longer holds up the first paint: its file is what the page saved.77 */78 64 public function count( $tag, $handle, $src ) { 79 65 if ( isset( $this->asked[ $handle ] ) && ! did_action( 'wp_footer' ) && preg_match( '/<script[^>]*\sdefer[\s>=]/i', (string) $tag ) ) { … … 81 67 82 68 Fixer::hold( $this->id(), '', '' === $file ? 0 : (int) filesize( $file ) ); 69 Pipeline::mark( $handle, $this->id() ); 83 70 } 84 71 -
imzura-site-check/trunk/includes/fixes/class-emoji.php
r3797250 r3797325 1 1 <?php 2 /** 3 * WordPress's emoji script and styles, left out of the page. 4 * 5 * Every page carries a script that checks whether the browser can draw emoji and, if not, loads pictures for 6 * them, plus a few styles for those pictures. Browsers in use today draw emoji themselves. Older WordPress 7 * prints the script in the head, newer WordPress in the footer; unhooking the head call stops both. What the 8 * script would have weighed is measured into a buffer that is thrown away, so the trial knows what was saved. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; -
imzura-site-check/trunk/includes/fixes/class-heavy-images.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Heavy pictures, served at the size they are shown. 4 * 5 * The size is measured, not guessed: during a trial the browser reports how wide each picture is on the page, 6 * and prepare() writes a WebP copy twice that wide (for sharp screens), never wider than the original. On the 7 * page only the address in the <img> changes; the original file is not touched. Pictures with a srcset are left 8 * alone: WordPress already picks a size for each screen there. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; … … 15 6 use SpeedDoctor\Fix; 16 7 use SpeedDoctor\Fixer; 8 use SpeedDoctor\Pipeline; 17 9 18 10 defined( 'ABSPATH' ) || exit; … … 86 78 87 79 public function hook() { 88 add_action( 'template_redirect', array( $this, 'buffer' ), 1 ); 89 } 90 91 public function buffer() { 92 if ( ! is_feed() && $this->map() ) { 93 add_filter( 'wp_template_enhancement_output_buffer', array( $this, 'swap' ), 30, 1 ); 94 } 80 Pipeline::page( $this->id(), 'images', array( $this, 'swap' ), array( $this, 'wants' ) ); 81 } 82 83 public function wants() { 84 return ! is_feed() && $this->map(); 95 85 } 96 86 … … 122 112 $copy = $uploads['basedir'] . '/' . $entry['copy']; 123 113 124 // A picture that was replaced or removed since the copy was made is served as it is.125 114 if ( ! is_file( $copy ) || ! is_file( $original ) || filesize( $original ) !== $entry['bytes'] ) { 126 115 return $tag; … … 129 118 $tag = str_replace( $src[0], ' src="' . esc_url( $uploads['baseurl'] . '/' . $entry['copy'] ) . '"', $tag ); 130 119 131 // Without a width the browser would lay the page out by the copy's own, smaller, width.132 // No height is added: it would stretch the picture in a theme that does not reset it.133 120 if ( ! preg_match( '#\swidth\s*=#i', $tag ) ) { 134 121 $tag = preg_replace( '#^<img\b#i', '<img width="' . (int) $entry['width'] . '"', $tag ); … … 149 136 } 150 137 151 /**152 * @return string The picture's path inside the uploads folder, or an empty string when it is not a JPEG or PNG there.153 */154 138 private function relative( $src, array $uploads ) { 155 139 $base = preg_replace( '#^https?:#', '', $uploads['baseurl'] ); … … 171 155 } 172 156 173 /**174 * The copy's width: at least twice the width shown, and one at which the height comes out a whole number of175 * pixels (near enough). A copy whose shape is a fraction off makes the picture a fraction taller or shorter176 * on the page, and everything under it moves. When no such width is close, the copy keeps the full size.177 */178 157 private function width( $shown, $wide, $tall ) { 179 158 $least = (int) ( 100 * ceil( $shown * self::SHARP / 100 ) ); … … 190 169 } 191 170 192 /**193 * @return array|null Where the copy is, with the original's width and size, or null when a copy is not worth serving.194 */195 171 private function copy( $path, $shown, array $uploads ) { 196 172 $file = $uploads['basedir'] . '/' . $path; -
imzura-site-check/trunk/includes/fixes/class-jquery-migrate.php
r3797250 r3797325 1 1 <?php 2 /** 3 * jQuery without jQuery Migrate. 4 * 5 * WordPress loads jQuery Migrate with jQuery on every page that uses jQuery. It keeps code written for old 6 * versions of jQuery working and is not needed by code that follows the current one. The fix takes it out of 7 * jQuery's dependencies only; a script that asks for it by name still gets it. Code that needed it fails in the 8 * browser, and the trial sees that. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; … … 56 47 } 57 48 58 /**59 * After the footer scripts: the fix changed the page only if jQuery was sent and Migrate was not.60 */61 49 public function count() { 62 50 if ( ! wp_script_is( 'jquery', 'done' ) || wp_script_is( self::HANDLE, 'done' ) ) { -
imzura-site-check/trunk/includes/fixes/class-minify-scripts.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Scripts sent minified. How the copies are made and kept: see Minify. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Fixes; … … 21 17 22 18 public function summary() { 23 return __( 'Takes the spaces, line breaks and comments out of the scripts your theme and plugins send, and sends a smaller copy of each file instead. Files that are minified already, files from other sites and scripts written into the page are left alone. If a script stops working after minifying, the trial sees it and the fix is not kept. On a server that compresses files the gain is smaller; the trial shows what it is on your pages.', 'imzura-site-check' );19 return __( 'Takes the spaces, line breaks and comments out of the scripts your theme and plugins send, and sends a smaller copy of each file instead. Files that are minified already, files from other sites and scripts written into the page are left alone. If a script stops working after minifying, the trial sees it: that file is sent as it is from then on and the trial runs again, and the fix is kept only when the pages work. On a server that compresses files the gain is smaller; the trial shows what it is on your pages.', 'imzura-site-check' ); 24 20 } 25 21 -
imzura-site-check/trunk/includes/fixes/class-minify-styles.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Stylesheets sent minified. How the copies are made and kept: see Minify. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 namespace SpeedDoctor\Fixes; -
imzura-site-check/trunk/includes/fixes/class-minify.php
r3797250 r3797325 1 1 <?php 2 /** 3 * What the two minify fixes share: stylesheets (Minify_Styles) or scripts (Minify_Scripts) sent minified, the same 4 * code without spaces, line breaks and comments. They are separate fixes, so that each is tried, kept or turned 5 * off on its own. 6 * 7 * Only a site's own files are touched, and only when WordPress prints them as a tag of their own: the address of 8 * the tag changes to a minified copy in uploads/imzura-site-check/min/, and everything else about the file stays as it 9 * was (its handle, its place in the queue, what it depends on and the inline code around it). Files that are 10 * minified already, files from other sites and inline code are left alone, and so is a file the minifier could 11 * not shrink by at least a twentieth. A copy is made on the first page that needs it, within a time limit per 12 * page, and made again when the file changes. 13 * 14 * @package SpeedDoctor 15 */ 2 // imzura Site Check by imzura.com 16 3 17 4 namespace SpeedDoctor\Fixes; … … 20 7 use SpeedDoctor\Fixer; 21 8 use SpeedDoctor\Paths; 9 use SpeedDoctor\Pipeline; 22 10 use SpeedDoctor\Probe; 23 11 use SpeedDoctor\Vendor\MatthiasMullie\Minify as Minifier; … … 29 17 const FOLDER = 'imzura-site-check/min'; 30 18 31 // Bigger files are most often bundles that are minified already, and minifying them would hold the page up.32 19 const LARGEST = 1048576; 33 20 34 // A copy has to come out under this share of the file, or the file is left as it is.35 21 const SMALLER = 0.95; 36 22 37 // Seconds a page may spend making copies, stylesheets and scripts together. The files left over are sent as38 // they are and copied on later pages.39 23 const BUDGET = 1.0; 40 24 41 25 private static $spent = 0.0; 42 26 43 /**44 * @return string 'css' or 'js'.45 */46 27 abstract protected function type(); 47 28 … … 58 39 $dir = $uploads['basedir']; 59 40 60 // A new site has no uploads folder until the first upload: what counts is that it can be made.61 41 while ( ! is_dir( $dir ) && dirname( $dir ) !== $dir ) { 62 42 $dir = dirname( $dir ); … … 71 51 } 72 52 73 add_filter( 'css' === $this->type() ? 'style_loader_src' : 'script_loader_src', array( $this, 'swap' ), PHP_INT_MAX, 2);53 Pipeline::source( $this->id(), 'minify', $this->type(), array( $this, 'swap' ) ); 74 54 } 75 55 … … 98 78 $name = self::name( $handle, $src, $file, $type ); 99 79 100 if ( is_file( $folder['dir'] . '/' . $name . '.skip' ) ) {80 if ( is_file( $folder['dir'] . '/' . $name . '.skip' ) || is_file( $folder['dir'] . '/' . $name . '.bad' ) ) { 101 81 return $src; 102 82 } … … 111 91 } 112 92 113 /** 114 * Writes the minified copy, or a .skip marker when the file does not get smaller. 115 * 116 * @return bool Whether a copy is there to send. 117 */ 93 public function excuse( array $files ) { 94 $folder = self::folder(); 95 $base = preg_replace( '#^https?:#i', '', $folder['url'] ) . '/'; 96 $aside = array(); 97 98 foreach ( array_unique( array_map( 'strval', $files ) ) as $file ) { 99 $path = preg_replace( '#^https?:#i', '', (string) strtok( $file, '?#' ) ); 100 $name = 0 === strpos( $path, $base ) ? rawurldecode( substr( $path, strlen( $base ) ) ) : ''; 101 102 if ( '' === $name || basename( $name ) !== $name || ! preg_match( '/-[0-9a-f]{12}\.' . $this->type() . '$/', $name ) || ! is_file( $folder['dir'] . '/' . $name ) ) { 103 continue; 104 } 105 106 self::filesystem()->put_contents( $folder['dir'] . '/' . $name . '.bad', '', 0644 ); 107 wp_delete_file( $folder['dir'] . '/' . $name ); 108 109 $aside[] = $name; 110 } 111 112 return $aside; 113 } 114 115 public function excused() { 116 $marks = glob( self::folder()['dir'] . '/*.' . $this->type() . '.bad' ); 117 118 return is_array( $marks ) ? count( $marks ) : 0; 119 } 120 118 121 private function make( $dir, $name, $src, $file, $type ) { 119 // A trial has to see the whole fix at once; a visitor's page only spends its budget.120 122 if ( true !== Probe::forced( $this->id() ) && self::$spent >= self::BUDGET ) { 121 123 return false; … … 142 144 $filesystem = self::filesystem(); 143 145 144 // Older copies of the same file, from before it changed, are not needed any more.145 146 $older = glob( $dir . '/' . self::stem( $name ) . '-????????????.' . $type . '*' ); 146 147 … … 155 156 } 156 157 157 // Written under a temporary name first, so that no visitor is sent a half-written file.158 158 $temporary = $dir . '/.' . $name . '.' . wp_generate_password( 6, false ); 159 159 … … 164 164 $base = self::base( $src ); 165 165 166 // The copy lives in another folder, so every address in the file has to stop being relative to it.167 166 $code = preg_replace_callback( 168 167 '/url\(\s*([\'"]?)([^\'")]*)\1\s*\)/i', … … 196 195 } 197 196 198 /**199 * @return string The file on this server an address points to, or an empty string when it points elsewhere.200 */201 197 public static function local( $src ) { 202 198 $file = Paths::file( $src ); … … 205 201 } 206 202 207 /**208 * The copy's file name: the handle, then a hash of the address and of the file as it is now, so that a209 * changed file or a new version gets a new copy.210 */211 203 private static function name( $handle, $src, $file, $type ) { 212 204 $stem = substr( sanitize_file_name( $handle ), 0, 60 ); … … 220 212 } 221 213 222 /**223 * @return string The folder of the address, with a trailing slash and without the scheme.224 */225 214 private static function base( $src ) { 226 215 $url = (string) strtok( $src, '?#' ); -
imzura-site-check/trunk/includes/fixes/class-video-facade.php
r3797250 r3797325 1 1 <?php 2 /** 3 * YouTube videos shown as a picture until the visitor presses play. 4 * 5 * An embedded YouTube player loads its own scripts and styles as soon as the page opens, whether or not anyone 6 * watches. With this fix the iframe stays exactly where and how it was (same element, same attributes, so the 7 * theme's styles still size it), but it first shows a light document of its own (srcdoc): the video's picture 8 * and a play button that opens the real player. Nothing else in the page changes. 9 * 10 * @package SpeedDoctor 11 */ 2 // imzura Site Check by imzura.com 12 3 13 4 namespace SpeedDoctor\Fixes; … … 15 6 use SpeedDoctor\Fix; 16 7 use SpeedDoctor\Fixer; 8 use SpeedDoctor\Pipeline; 17 9 18 10 defined( 'ABSPATH' ) || exit; … … 44 36 } 45 37 46 /**47 * The fix is tried on a page that has a YouTube video.48 */49 38 public function targets() { 50 39 $found = get_posts( … … 88 77 } 89 78 90 add_action( 'template_redirect', array( $this, 'buffer' ), PHP_INT_MAX);79 Pipeline::page( $this->id(), 'embeds', array( $this, 'swap' ), array( $this, 'wants' ) ); 91 80 } 92 81 93 public function buffer() { 94 if ( is_feed() || is_embed() || is_customize_preview() ) { 95 return; 96 } 97 98 add_filter( 'wp_template_enhancement_output_buffer', array( $this, 'swap' ), 10, 1 ); 82 public function wants() { 83 return ! ( is_feed() || is_embed() || is_customize_preview() ); 99 84 } 100 85 … … 102 87 $head = stripos( $html, '</head>' ); 103 88 104 // Anything but one whole page (a page flushed in parts, a file download) passes through as it is.105 89 if ( false === $head || false === stripos( $html, '</body>', $head ) || false === stripos( $html, 'youtube', $head ) ) { 106 90 return $html; … … 116 100 } 117 101 118 /** 119 * The same iframe tag with a srcdoc: the video's picture, linking to the player with autoplay on. 120 */ 121 private function facade( array $match ) { 122 $tag = $match[0]; 123 $src = html_entity_decode( $match[2], ENT_QUOTES ); 102 private function facade( array $iframe ) { 103 $tag = $iframe[0]; 104 $src = html_entity_decode( $iframe[2], ENT_QUOTES ); 124 105 $play = add_query_arg( 'autoplay', '1', 0 === strpos( $src, '//' ) ? 'https:' . $src : $src ); 125 106 $title = preg_match( '#\stitle=(["\'])(.*?)\1#is', $tag, $found ) ? html_entity_decode( $found[2], ENT_QUOTES ) : ''; … … 128 109 $doc = '<body style="margin:0;overflow:hidden;background:#000">' 129 110 . '<a href="' . esc_url( $play ) . '" aria-label="' . esc_attr( $label ) . '" style="display:block;position:relative;height:100vh">' 130 . '<img src="' . esc_url( 'https://i.ytimg.com/vi/' . $ match[3] . '/hqdefault.jpg' ) . '" alt="' . esc_attr( $title ) . '" loading="lazy" style="display:block;width:100%;height:100%;object-fit:cover">'111 . '<img src="' . esc_url( 'https://i.ytimg.com/vi/' . $iframe[3] . '/hqdefault.jpg' ) . '" alt="' . esc_attr( $title ) . '" loading="lazy" style="display:block;width:100%;height:100%;object-fit:cover">' 131 112 . '<svg width="68" height="48" viewBox="0 0 68 48" aria-hidden="true" style="position:absolute;top:50%;left:50%;margin:-24px 0 0 -34px;opacity:.9">' 132 113 . '<rect width="68" height="48" rx="12" fill="#f00"/><path d="M27 14v20l18-10z" fill="#fff"/></svg></a></body>'; -
imzura-site-check/trunk/includes/fixes/class-woocommerce-files.php
r3797250 r3797325 1 1 <?php 2 /** 3 * WooCommerce's stylesheets and scripts, sent only with pages that show something from the store. 4 * 5 * WooCommerce queues them on every page of the site. Whether a page needs them is decided on evidence, from 6 * the finished page: when nothing in it carries a WooCommerce class and no WooCommerce template, block or 7 * widget was rendered, the tags are taken out before the page leaves. Store pages are never touched. 8 * 9 * Two things are left alone on purpose. A stylesheet a theme has swapped for its own also styles parts of 10 * the theme (Astra's sets the size of the mobile menu button), so only WooCommerce's own files are taken out. 11 * The scripts that record where a visitor came from stay on every page: an order is credited to the first 12 * page of the visit, which is often not a store page. 13 * 14 * @package SpeedDoctor 15 */ 2 // imzura Site Check by imzura.com 16 3 17 4 namespace SpeedDoctor\Fixes; … … 19 6 use SpeedDoctor\Fix; 20 7 use SpeedDoctor\Fixer; 8 use SpeedDoctor\Pipeline; 21 9 22 10 defined( 'ABSPATH' ) || exit; … … 24 12 final class WooCommerce_Files extends Fix { 25 13 26 const STYLES = array( 'woocommerce-layout', 'woocommerce-smallscreen', 'woocommerce-general', 'woocommerce-blocktheme' );14 const STYLES = array( 'woocommerce-layout', 'woocommerce-smallscreen', 'woocommerce-general', 'woocommerce-blocktheme', 'wc-blocks-style' ); 27 15 const INLINE = 'woocommerce-inline'; 28 16 const SCRIPTS = array( 'wc-add-to-cart', 'woocommerce' ); … … 31 19 const NEEDLES = array( 'wp:woocommerce/', '[product', '_products', '[add_to_cart' ); 32 20 33 /**34 * Classes WooCommerce or a theme puts on the body of every page. They say nothing about the page.35 */36 21 const ALWAYS = array( 'woocommerce-no-js', 'woocommerce-js', 'woocommerce-active', 'woocommerce-uses-block-theme', 'woocommerce-block-theme-has-button-styles' ); 37 22 38 /** 39 * Classes without a WooCommerce prefix that its stylesheets or scripts still act on. 40 */ 23 const ACCOUNT = '/^(?:wp-block-woocommerce-customer-account|wc-block-customer-account)/'; 24 41 25 const SIGNS = array( 'add_to_cart_button', 'added_to_cart', 'remove_from_cart_button', 'demo_store', 'show-password-input', 'coming-soon-footer-banner-dismiss', 'qty', 'star-rating', 'onsale', 'products', 'shop_table', 'cart_list', 'product_list_widget', 'widget_shopping_cart', 'widget_shopping_cart_content' ); 42 26 … … 59 43 } 60 44 61 /**62 * A page outside the store that shows products is tried as well: it has to come through unchanged.63 */64 45 public function targets() { 65 46 if ( ! function_exists( 'wc_get_page_id' ) ) { … … 85 66 ); 86 67 87 // The cart and checkout pages hold a WooCommerce block or shortcode too, and they are the store.88 68 $outside = array_values( array_diff( array_map( 'intval', $found ), $store ) ); 89 69 … … 107 87 108 88 public function hook() { 109 add_action( 'template_redirect', array( $this, 'buffer' ), PHP_INT_MAX);89 Pipeline::page( $this->id(), 'remove', array( $this, 'strip' ), array( $this, 'wants' ) ); 110 90 add_action( 'woocommerce_before_template_part', array( $this, 'rendered' ), 10, 0 ); 111 91 add_filter( 'render_block', array( $this, 'block' ), 10, 2 ); … … 118 98 119 99 public function block( $content, $block ) { 120 if ( isset( $block['blockName'] ) && 0 === strpos( (string) $block['blockName'], 'woocommerce/' ) && ' ' !== trim( (string) $content ) ) {100 if ( isset( $block['blockName'] ) && 0 === strpos( (string) $block['blockName'], 'woocommerce/' ) && 'woocommerce/customer-account' !== $block['blockName'] && '' !== trim( (string) $content ) ) { 121 101 $this->rendered = true; 122 102 } … … 133 113 } 134 114 135 public function buffer() { 136 if ( is_feed() || is_embed() || is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) { 137 return; 138 } 139 140 add_filter( 'wp_template_enhancement_output_buffer', array( $this, 'strip' ), 20, 1 ); 141 } 142 143 /** 144 * @param string $html The finished page. 145 * @return bool Whether anything in the page is WooCommerce's to style or to run. 146 */ 115 public function wants() { 116 return ! ( is_feed() || is_embed() || is_woocommerce() || is_cart() || is_checkout() || is_account_page() ); 117 } 118 147 119 public static function in_page( $html ) { 148 120 $head = stripos( $html, '</head>' ); … … 160 132 161 133 foreach ( array_diff( array_unique( $classes ), self::ALWAYS ) as $class ) { 134 if ( preg_match( self::ACCOUNT, $class ) ) { 135 continue; 136 } 137 162 138 if ( in_array( $class, self::SIGNS, true ) || preg_match( '/^(?:woocommerce|wc[-_]|wp-block-woocommerce)/', $class ) ) { 163 139 return true; … … 171 147 $head = stripos( $html, '</head>' ); 172 148 173 // Anything but one whole page (a page flushed in parts, a file download) passes through as it is.174 149 if ( false === $head || false === stripos( $html, '</body>', $head ) || $this->rendered || self::in_page( $html ) ) { 175 150 return $html; … … 210 185 } 211 186 212 /**213 * The script tags that can go: WooCommerce's own files that no other script on the page was queued after.214 *215 * @return array Tag => handle, or an empty string for a tag that only carries a script's settings.216 */217 187 private function scripts( $html, $own ) { 218 188 $tops = array(); … … 230 200 } 231 201 232 // A script elsewhere on the page that reads WooCommerce's settings would break without them.233 202 $rest = str_replace( array_keys( $gone ), '', $html ); 234 203 … … 258 227 } 259 228 260 /**261 * @param string[] $tops The scripts of WooCommerce found on the page that may go.262 * @return string[] Those of them, and of the libraries they bring along, that nothing else on the page needs.263 */264 229 private function spare( array $tops ) { 265 230 $scripts = wp_scripts(); -
imzura-site-check/trunk/readme.txt
r3797250 r3797325 5 5 Tested up to: 7.1 6 6 Requires PHP: 7.4 7 Stable tag: 1. 0.07 Stable tag: 1.3.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html 10 10 11 Find s what slows your site down and tests every fix on your own pages before it stays on. Works next to your cache plugin.11 Find what makes WordPress slow and fix it safely: Core Web Vitals, PageSpeed, WooCommerce. Every fix is tested on your own pages before it stays on. 12 12 13 13 == Description == 14 14 15 A slow WordPress site usually has a few causes: a plugin that loads its files on every page, a heavy picture, a font from another server, a script that keeps the browser busy, a database that has piled up. PageSpeed Insights gives a score; this plugin names the plugin, theme or file behind each problem on your own pages, and fixes what can be fixed safely. It works next to the cache plugin you already have. 16 15 17 This plugin does two things. 16 18 … … 23 25 = What the check looks at = 24 26 27 The check opens your pages in a hidden frame of its own screen, at phone width and as a first-time visitor, so it sees what a visitor's browser sees, not only what the server sends. 28 25 29 * Front-end pages: time the server takes, the weight of each page and whose files make it up, plugins that load on pages that do not use them, heavy pictures and videos 26 * A table of what each plugin and the theme add to your pages: number of scripts and stylesheets, weight, on how many pages 30 * A table of what each plugin and the theme cost your pages: how long its code keeps the server busy and how many database queries it makes, its scripts and stylesheets and their weight, on how many pages 31 * Plugins that keep the server busy: a plugin (or the theme) whose code takes 100 ms or more of a page (or 50 ms that are a quarter of the page's time), or makes 100 or more database queries there, is named, with the numbers for each page 32 * The same for the admin: the check also opens the dashboard and the posts list (and, with WooCommerce, the products and orders lists) as you, and names the plugins whose code, dashboard widgets included, keeps those screens waiting 33 * Plugins that load their files on pages where nothing of them is shown: the browser checks every rule of each plugin's stylesheets against the page, and a plugin none of whose rules apply there is named, with the weight it adds 34 * The largest stylesheets (50 KB or more) and scripts (100 KB or more), with the plugin or theme that adds each one 35 * Errors on your pages, each put down to the plugin or theme it comes from: PHP errors raised while the page is built (fatal ones included), script errors in the browser, and files the page asks for that do not load 36 * Pictures: the largest picture on the first screen waiting for lazy loading or not marked as urgent, other lazy pictures on the first screen, pictures without a width and height, how far the page jumps while it loads (layout shift), with what moved it, animations on the first screen that never stop, how much more phones with 3× screens download than 2× screens (from each picture's srcset and the files on your server), and how many copies of each upload WordPress keeps and the disk space they take, with the theme or plugin behind each picture size 37 * Fonts: fonts loaded from Google's servers, how many font files a page loads, text that waits for its font, Font Awesome loaded more than once, the same font loaded twice (from two Google Fonts stylesheets, or the same font file from two places) with the theme or plugin behind each, font preloads that cannot be used, and a font loaded for less than 3% of the text next to the font the rest of the page uses 38 * Scripts: files from other websites, the same library loaded twice, and which plugin's scripts keep the browser busy while the page loads (in Chrome, Edge and other Chromium-based browsers) 39 * Speed settings of well-known plugins that are off: Elementor (Inline Font Icons, CSS Print Method, optimized image loading, lazy background images, local Google Fonts and their font-display), Formidable Forms styles on every page, Wordfence logging every visit, and the local Google Fonts setting of Astra, Kadence, OceanWP and Spectra (or GeneratePress font-display) when their fonts come from Google. This plugin only reads these settings and names them as each plugin's screen does 40 * Housekeeping: the PHP error log (whether strangers can download it, its size, the plugins that fill it), backup and archive files left in wp-content, the largest database tables, plugins that are installed but switched off, and two SEO plugins active at once 27 41 * Settings WordPress loads on every request (autoloaded options) and the largest of them 28 42 * Scheduled jobs that have fallen behind, including the Action Scheduler queue 29 43 * WooCommerce: order storage, sessions that pile up, the cart script running outside the shop 30 * The environment: PHP version, object cache, the page cache in use 44 * The environment: PHP version, object cache, the page cache in use, and a cache plugin that is active with its page caching switched off (WP Super Cache, W3 Total Cache, WP Fastest Cache, WP-Optimize, Breeze, Hummingbird), named with the setting 31 45 * How the server delivers pages and files: compressed (gzip, Brotli) or not, and whether browsers may keep stylesheets and scripts 32 46 … … 37 51 * **Load Contact Form 7 only on pages that have a form.** Its script and stylesheet, and the reCAPTCHA or Turnstile script when one is connected, load only where a form is shown. Forms added by a template or placed with Elementor are covered. 38 52 * **Serve heavy pictures at the size they are shown.** A JPEG or PNG far larger than the space it fills is replaced, for visitors, by a copy of the right size in WebP. The original stays in the media library untouched. Needs WordPress 6.9 or newer. 39 * **Minify stylesheets (CSS)** and **Minify scripts (JavaScript)**, two separate fixes. The spaces, line breaks and comments come out of your theme's and plugins' own files, and a smaller copy is sent instead, under the same handle. Files that are minified already, files from other sites and code written into the page are left alone, and so is a file that would not get at least 5% smaller. Addresses inside stylesheets are rewritten, so fonts and pictures still load. 53 * **Load the main picture of each page first.** The largest picture on the first screen is what Google times as the page's Largest Contentful Paint. Some themes and page builders make it wait for lazy loading or leave it unmarked, so the browser fetches it last. The fix finds that picture on your pages as visitors see them and has the browser fetch it first; on posts and products whose main picture is the featured image, it does the same for every such page. Where WordPress already does this, the fix has nothing to do. Needs WordPress 6.9 or newer. 54 * **Serve Google Fonts from your own server.** Fonts from Google make the browser connect to two more servers before text in them can show, and they pass each visitor's IP address to Google. When you start its test, the fix downloads the fonts your pages load from Google into your uploads folder, adds font-display swap where it is missing, and points your pages at the copies. Fonts that a stylesheet pulls in with @import are left as they are. Needs WordPress 6.9 or newer. 55 * **Keep room for pictures while they load.** A picture without a width and height in its tag takes no room until it arrives, and the text around it jumps when it does (Cumulative Layout Shift). The fix reads the size of each such picture from its file on your server and writes it into the tag. Pictures that load only when scrolled to get it once the test has seen them load; pictures from other websites are left as they are. Needs WordPress 6.9 or newer. 56 * **Minify stylesheets (CSS)** and **Minify scripts (JavaScript)**, two separate fixes. The spaces, line breaks and comments come out of your theme's and plugins' own files, and a smaller copy is sent instead, under the same handle. Files that are minified already, files from other sites and code written into the page are left alone, and so is a file that would not get at least 5% smaller. Addresses inside stylesheets are rewritten, so fonts and pictures still load. If a script stops working once minified, the trial sees the error in that file, sends it as it is from then on and tests again; the fix is kept only if the pages then work, and the screen shows how many files are sent as they are. 40 57 * **Leave out the emoji script.** WordPress adds a script and styles to every page so that very old browsers can show emoji as pictures. Browsers in use today show emoji themselves. 41 58 * **Load jQuery without jQuery Migrate.** Migrate only helps code written for old versions of jQuery. If a script on your site still needs it, the test sees it fail and the fix is not kept. … … 76 93 This plugin sends nothing from your site anywhere. There is no account, no cloud service and no tracking. Everything it measures stays in your own database. The only link that leads away is one block on the plugin's own screen, for people who would rather have the work done for them. 77 94 95 One fix fetches files from outside, and only when you start its test: "Serve Google Fonts from your own server" downloads the font stylesheets your pages already load from Google Fonts (fonts.googleapis.com) and the font files they name (fonts.gstatic.com) into your uploads folder. The request carries nothing about your site or visitors beyond what any browser sends for those files. Google Fonts terms: https://developers.google.com/fonts/terms · Google privacy policy: https://policies.google.com/privacy 96 97 The fix "Show YouTube videos as a picture until play is pressed" changes what visitors' browsers load from YouTube, not whether they do: the page shows the video's picture from YouTube's image server (i.ytimg.com), and the YouTube player loads only when the visitor presses play. YouTube terms: https://www.youtube.com/t/terms 98 78 99 == Installation == 79 100 … … 84 105 == Frequently Asked Questions == 85 106 107 = Will it raise my PageSpeed Insights score? = 108 109 It removes causes the score is built from, such as files a page does not use, the main picture loading late, pictures without a size and fonts from another server, and each fix shows what it saved on your pages. How much the score moves depends on what your site had to begin with, so this plugin makes no promise about the number. 110 111 = Can someone fix my site for me? = 112 113 Yes. The people behind this plugin fix slow WordPress and WooCommerce sites for a fixed price, on a copy of the site first, with a before-and-after measurement: [imzura.com](https://imzura.com/services/wordpress-speed-optimization?utm_source=imzura-site-check&utm_medium=readme). The plugin works the same whether you ask or not. 114 86 115 = Does it replace my cache plugin? = 87 116 … … 121 150 122 151 The two scripts that record where a visitor came from. WooCommerce credits an order to the first page of the visit, and that is often a blog post or the homepage, so they are never removed. A stylesheet your theme has replaced with its own version is left alone as well: such a file also styles parts of the theme. On those themes the fix says "Nothing to gain". 152 153 = Why does this plugin not remove unused CSS? = 154 155 Because nobody can know for sure which rules a site will never need. A rule unused on the pages a tool looks at may be needed on another page, by a menu or a form once a visitor taps it, or by a script that adds a class later; a site broken that way can look fine in every test. So this plugin tells you which stylesheets are the largest and who adds them, minifies them if you want, and leaves the rules alone. 156 157 = How does this plugin time each plugin? = 158 159 Only while the check reads a page or an admin screen, this plugin places a marker in WordPress's hooks wherever the code passes from one plugin to another, and notes the time at each marker. A plugin's time is what its own code took, without the time of the other plugins it called on the way; its database queries are those its code asked for, directly or through WordPress. Dashboard widgets are not run from a hook, so each one is timed on its own. The plugins themselves are not changed or wrapped, and the visits of real visitors are not measured. The time it takes to load a plugin's files is not counted, so its real cost is a little higher than shown. 123 160 124 161 = Which pictures does the picture fix leave alone? = … … 139 176 140 177 == Changelog == 178 179 = 1.3.0 = 180 * After a fix is kept, the plugin's screen asks once, below the fixes, for a review on WordPress.org. "Do not ask again" hides it for good; nothing else on the admin changes. 181 * The fix that keeps room for pictures leaves alone pictures marked sizes="auto". Browsers that support it, such as Chrome, already give them a fixed box, and writing a size into the tag changed how they look, so the trial did not keep the fix. 182 * Add-ons can read what the check's browser saw on each page (the speed_doctor_seen action), and the list of plugins that may already do a job names Perfect Images and Disable Media Sizes when they choose which picture sizes are made. 183 * The fixes that change the finished page (WooCommerce files, heavy pictures, the main picture, Google Fonts, room for pictures, YouTube) use the page WordPress hands to plugins from 6.9 on, and need WordPress 6.9 or newer. The plugin opens no output buffer of its own across the page. 184 * New fix: stop Heartbeat calls from visitors. When a plugin loads WordPress Heartbeat on public pages, every open tab calls admin-ajax.php again and again, past any page cache. Before the trial this plugin calls admin-ajax.php itself as a visitor and looks at what answers Heartbeat there; only when nothing does is the Heartbeat file left out, with a stand-in that keeps the scripts that asked for it working. Logged-in users keep Heartbeat. 185 * Old revisions can be removed from the check's report: all but the newest 5 of each post (or the number set in WP_POST_REVISIONS) go, autosaves stay. They are first written to a compressed copy in wp-content/uploads/imzura-site-check/copies, so "Put them back" restores the same rows and fields; "Delete the copy" removes it. 186 * The check names Heartbeat loaded for visitors (how often it calls and who asks for it), admin screens where a plugin made Heartbeat call more often than every 60 seconds, and old revisions piling up in the database (how many, how much text, and the posts with the most). 187 * New fix: serve Google Fonts from your own server. The trial counts the other websites each page connects to, among those both untouched views connected to; fewer is a gain and shows as a column in the table. 188 * The check names the setting that keeps Google Fonts on your server in Astra, Kadence, OceanWP and Spectra, and GeneratePress font-display, when the fonts they load come from Google. 189 * The check weighs what phones with 3× screens download next to 2× screens, from the srcset of each picture and the sizes of its files on the server. PageSpeed Insights tests phones at a lower density and does not show it. 190 * The check counts the copies WordPress keeps of each picture (one per size the theme and plugins ask for) and the disk space they take, and names who added each size. It does not say which sizes to drop: pages pick sizes by screen width. 191 * The check names a font that is loaded twice: asked from Google Fonts by two stylesheets (with the theme or plugin that likely adds each), or the same font file downloaded from two places. 192 * The check also names animations on the first screen that never stop, and a font loaded for less than 3% of the text on pages where another font draws at least 10%. 193 * New fix: keep room for pictures while they load. Pictures without a width and height get them from their file on the server; the trial counts such pictures on each page and shows the count in the table. 194 * New fix: load the main picture of each page first. The trial counts a main picture that waits (lazy loading, or not marked urgent) and shows it in the table; a fix that makes the main picture wait is not kept. 195 * The WooCommerce files fix now works on block themes such as Twenty Twenty-Five. WooCommerce adds its account link to the header of block themes, so every page counted as showing something from the store and the fix found nothing to leave out. The plain account link no longer counts; an account menu that opens still does. 196 * The WooCommerce files fix also leaves out WooCommerce's block stylesheet on pages without anything from the store. 197 * The report keeps, for each page, which scripts and stylesheets it loaded, and the admin screen lets add-ons run a fix's trial as its own button does. 198 199 = 1.2.0 = 200 * The check now opens each page in a hidden frame, at phone width and as a first-time visitor, and reports what only a browser sees. 201 * Errors on your pages, each put down to the plugin or theme it comes from: PHP errors while the page is built (a fatal error is named as such, with the pages it stops), script errors, and files that do not load. 202 * Pictures: the largest picture on the first screen waiting for lazy loading or not marked as urgent, other lazy pictures on the first screen, pictures without a width and height, and layout shift with what moved the page. 203 * Fonts: Google's servers, many font files, text that waits for its font, Font Awesome loaded more than once, font preloads that cannot be used. 204 * Scripts: files from other websites, the same library loaded twice, scripts that keep the browser busy; the largest stylesheets and scripts with the plugin or theme behind each. 205 * Plugins that load their stylesheets on pages where none of their rules apply, and block styles on pages without blocks. 206 * Plugins that keep the server busy: how long the code of each plugin and the theme ran while each page was built, and how many database queries it made. The table of plugins shows it for all of them. The same for the dashboard, the posts list and, with WooCommerce, the products and orders lists, opened as the administrator. 207 * Speed settings that are off in Elementor, Formidable Forms and Wordfence, named as each plugin's own screen names them. 208 * A cache plugin that is active with its page caching switched off is named, with the setting. It no longer counts as a second cache next to the one in use (WP-Optimize kept for its database tools, for example). 209 * Housekeeping: the PHP error log (public, large, who fills it), backup files in wp-content, the largest database tables, switched-off plugins, two SEO plugins at once. 210 * The FAQ explains why this plugin minifies stylesheets but does not remove the parts a page does not use. 211 212 = 1.1.0 = 213 * Minify scripts: a file that stops working once minified is now sent as it is, and the trial runs again without it, instead of the whole fix being turned off. The fix shows how many files are sent as they are. 214 * Every fix now changes the page and its files in one pass, in a fixed order: files left out first, then the rest minified, then pictures and video players. Each fix works on what the fixes before it left, and a fix that fails leaves the page as the fixes before it made it. 141 215 142 216 = 1.0.0 = -
imzura-site-check/trunk/uninstall.php
r3797250 r3797325 1 1 <?php 2 /** 3 * Leaves nothing behind. 4 * 5 * @package SpeedDoctor 6 */ 2 // imzura Site Check by imzura.com 7 3 8 4 defined( 'WP_UNINSTALL_PLUGIN' ) || exit; … … 15 11 delete_option( 'speed_doctor_form_page' ); 16 12 delete_option( 'speed_doctor_images' ); 13 delete_option( 'speed_doctor_lcp' ); 14 delete_option( 'speed_doctor_sizes' ); 15 delete_option( 'speed_doctor_fonts' ); 16 delete_option( 'speed_doctor_tidy' ); 17 delete_metadata( 'user', 0, 'speed_doctor_review', '', true ); 17 18 18 19 $speed_doctor_uploads = wp_get_upload_dir(); … … 31 32 } 32 33 34 if ( is_dir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/fonts' ) ) { 35 foreach ( array_diff( (array) scandir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/fonts' ), array( '.', '..' ) ) as $speed_doctor_copy ) { 36 wp_delete_file( $speed_doctor_uploads['basedir'] . '/imzura-site-check/fonts/' . $speed_doctor_copy ); 37 } 38 39 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- removes this plugin's own, now empty, folder of font copies. 40 rmdir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/fonts' ); 41 } 42 43 if ( is_dir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/copies' ) ) { 44 foreach ( array_diff( (array) scandir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/copies' ), array( '.', '..' ) ) as $speed_doctor_copy ) { 45 wp_delete_file( $speed_doctor_uploads['basedir'] . '/imzura-site-check/copies/' . $speed_doctor_copy ); 46 } 47 48 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- removes this plugin's own, now empty, folder of revision copies. 49 rmdir( $speed_doctor_uploads['basedir'] . '/imzura-site-check/copies' ); 50 } 51 33 52 if ( is_dir( $speed_doctor_uploads['basedir'] . '/imzura-site-check' ) ) { 34 53 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir -- removes this plugin's own, now empty, folder of picture copies.
Note:
See TracChangeset
for help on using the changeset viewer.