Skip to content
← Back to Snippets
Code

Broken Asset Reference Finder

Finds CSS, JavaScript, image, and font references that do not exist in an approved asset manifest.

Purpose

Finds CSS, JavaScript, image, and font references that do not exist in an approved asset manifest.

Snippet details

ContextAssetLevelAdvancedCopy-and-paste statusMarked safe after review.

Categories

  • Security

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Broken Asset Reference Finder.
 *
 * Purpose:
 * Finds CSS, JavaScript, image, and font references that do not exist in an approved asset manifest.
 *
 * @param array $page_references Asset paths discovered in rendered pages.
 * @param array $manifest Asset manifest keyed by approved path.
 * @return array Missing asset references.
 */
function ogSnippetBrokenAssetReferenceFinder(array $page_references, array $manifest): array {
	$missing = array();
	foreach ($page_references as $page_slug => $references) {
		foreach ($references as $asset_path) {
			if (!isset($manifest[$asset_path])) {
				$missing[] = array('page' => (string) $page_slug, 'asset' => (string) $asset_path);
			}
		}
	}
	return $missing;
}

$references = array('risa-guide' => array('/css/starfleet.css', '/img/risa.webp'));
$manifest = array('/css/starfleet.css' => 'v9');
$missing_assets = ogSnippetBrokenAssetReferenceFinder($references, $manifest);
echo count($missing_assets);