Skip to content
← Back to Functions
Code

Feature Flag Resolver

Determines whether a feature is enabled by config, environment, role, and rollout rules.

Function signature

ogResolveFeatureFlag(feature_key, flags = array(), context = array())

Categories

  • Admin Tools

Parameters

feature_keyFeature flag key to resolve from the supplied flag set.flagsFeature flag definitions owned by configuration or storage.contextRuntime context used for feature, cache, or debug 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.
 */

/**
 * Determines whether a feature is enabled by config, environment, role, and rollout rules.
 *
 * Primary use case: Gradual launches and admin-only features.
 * Typical inputs: feature key, user context, config.
 * Typical output: enabled flag and reason.
 *
 * Implementation note: Default to disabled for unknown flags.
 *
 * @param string $feature_key Feature flag key.
 * @param array $flags Feature flag configuration map.
 * @param array $context Runtime context such as role, environment, and user_id.
 * @return array Feature flag decision.
 */
function ogResolveFeatureFlag($feature_key, $flags = array(), $context = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	$feature_key = trim((string)$feature_key);
	if (empty($feature_key)) {
		$result['message'] = 'Feature key is required.';
		return $result;
	}

	if (!is_array($flags)) {
		$flags = array();
	}
	if (!is_array($context)) {
		$context = array();
	}

	$enabled = false;
	$reason = 'Feature flag not found.';

	if (!empty($flags[$feature_key]) && is_array($flags[$feature_key])) {
		$flag = $flags[$feature_key];
		if (!empty($flag['enabled'])) {
			$enabled = true;
			$reason = 'Feature flag enabled.';
		}

		if (!empty($flag['roles']) && is_array($flag['roles'])) {
			$role = '';
			if (!empty($context['role'])) {
				$role = (string)$context['role'];
			}
			if (!in_array($role, $flag['roles'], true)) {
				$enabled = false;
				$reason = 'User role is not allowed.';
			}
		}

		if (!empty($flag['environments']) && is_array($flag['environments'])) {
			$environment = '';
			if (!empty($context['environment'])) {
				$environment = (string)$context['environment'];
			}
			if (!in_array($environment, $flag['environments'], true)) {
				$enabled = false;
				$reason = 'Environment is not allowed.';
			}
		}
	}

	$result['success'] = true;
	$result['message'] = 'Feature flag resolved.';
	$result['data'] = array('enabled' => $enabled, 'reason' => $reason);
	return $result;
}