Skip to content
← Back to Functions
Code

Asset Criticality Decider

Classifies assets as preload, lazy, normal, or omit based on page type and above-the-fold usage.

Function signature

ogDecideAssetCriticality(assets = array(), page_context = array())

Categories

  • File and Upload Safety

Parameters

assetsAsset rows classified for page-specific loading priority.page_contextPage type and page-specific facts used for asset criticality decisions.

Return value

Public-safe status string returned by the function for explicit controller branching or logging.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and context-specific escaping; validate admin actions, export fields, privacy plans, cache keys, templates, settings, routes, and ecommerce policies before production use.

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.
 */

/**
 * Classifies assets as preload, normal, lazy, or omit for the current page context.
 *
 * This is meant to prevent over-preloading. Only assets used immediately above the
 * fold should be classified as preload.
 *
 * @param array $assets Asset rows.
 * @param array $page_context Page context flags.
 * @return array Asset loading plan.
 */
function ogDecideAssetCriticality($assets = array(), $page_context = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($assets)) {
		$result['message'] = 'Assets must be an array.';
		return $result;
	}
	if (!is_array($page_context)) {
		$page_context = array();
	}

	$is_mobile = false;
	if (!empty($page_context['is_mobile'])) {
		$is_mobile = true;
	}

	$plan = array();
	foreach ($assets as $asset) {
		if (!is_array($asset)) {
			continue;
		}
		$href = '';
		if (!empty($asset['href'])) {
			$href = trim((string)$asset['href']);
		}
		if (empty($href)) {
			continue;
		}

		$type = '';
		if (!empty($asset['type'])) {
			$type = strtolower(trim((string)$asset['type']));
		}
		$above_fold = !empty($asset['above_fold']);
		$used_immediately = !empty($asset['used_immediately']);
		$mobile_hidden = !empty($asset['mobile_hidden']);

		$decision = 'normal';
		$reason = 'Default normal loading.';
		if ($is_mobile && $mobile_hidden) {
			$decision = 'omit';
			$reason = 'Asset is hidden on mobile.';
		} elseif ($above_fold && $used_immediately && ($type == 'image' || $type == 'font' || $type == 'style')) {
			$decision = 'preload';
			$reason = 'Asset is immediately needed above the fold.';
		} elseif (!$above_fold && $type == 'image') {
			$decision = 'lazy';
			$reason = 'Image is not above the fold.';
		}

		$plan[] = array(
			'href' => $href,
			'type' => $type,
			'decision' => $decision,
			'reason' => $reason
		);
	}

	$result['success'] = true;
	$result['message'] = 'Asset criticality decided.';
	$result['data'] = array('assets' => $plan);

	return $result;
}