$_) { if ($k !== $i) return false; $i++; } return true; } function a11ybridge_catalog_context(string $contextId): array { $ctx = a11ybridge_catalog_get_context($contextId); if (!$ctx) return []; // New format: ctx.options = [{value,label},...] if (isset($ctx['options']) && is_array($ctx['options'])) { $out = []; foreach ($ctx['options'] as $opt) { if (!is_array($opt)) continue; $v = (string)($opt['value'] ?? $opt['id'] ?? ''); $l = (string)($opt['label'] ?? $v); if ($v === '') continue; $out[] = ['value'=>$v, 'id'=>$v, 'label'=>$l]; } return $out; } // Legacy format: [[id,label],...] if (a11ybridge_is_list_array($ctx)) { $out = []; foreach ($ctx as $row) { if (!is_array($row)) continue; $v = (string)($row['value'] ?? $row['id'] ?? ''); $l = (string)($row['label'] ?? $v); if ($v === '') continue; $out[] = ['value'=>$v, 'id'=>$v, 'label'=>$l]; } return $out; } return []; } function a11ybridge_catalog_alias_group(string $group, string $value): string { $cat = a11ybridge_load_catalog(); $aliases = $cat['aliases'][$group] ?? null; if (!is_array($aliases)) return $value; $k = strtolower(trim((string)$value)); return isset($aliases[$k]) ? (string)$aliases[$k] : $value; } // Backward compat: // - if $key is alias-group, use it // - else infer group from context id function a11ybridge_catalog_alias(string $key, string $value): string { $cat = a11ybridge_load_catalog(); if (isset($cat['aliases'][$key]) && is_array($cat['aliases'][$key])) { return a11ybridge_catalog_alias_group($key, $value); } $lc = strtolower($key); if (strpos($lc, 'language') !== false) return a11ybridge_catalog_alias_group('languages', $value); if (strpos($lc, 'level') !== false) return a11ybridge_catalog_alias_group('levels', $value); return $value; } function a11ybridge_catalog_default(string $contextId, string $fallback = ''): string { $ctx = a11ybridge_catalog_get_context($contextId); $def = $ctx['default'] ?? $fallback; return is_string($def) ? $def : $fallback; } function a11ybridge_catalog_canonical(string $contextId, $value, string $fallbackDefault = ''): string { $value = is_string($value) ? $value : (string)$value; $lc = strtolower($contextId); if (strpos($lc, 'language') !== false) $value = a11ybridge_catalog_alias_group('languages', $value); if (strpos($lc, 'level') !== false) $value = a11ybridge_catalog_alias_group('levels', $value); $opts = a11ybridge_catalog_context($contextId); if ($opts) { $needle = strtolower(trim($value)); foreach ($opts as $opt) { $ov = (string)($opt['value'] ?? $opt['id'] ?? ''); if ($ov !== '' && strtolower($ov) === $needle) { return $ov; } } } $def = a11ybridge_catalog_default($contextId, $fallbackDefault); return $def !== '' ? $def : $fallbackDefault; } function a11ybridge_catalog_label(string $contextId, $value, string $fallback = ''): string { $canon = a11ybridge_catalog_canonical($contextId, (string)$value, $fallback); foreach (a11ybridge_catalog_context($contextId) as $opt) { $ov = (string)($opt['value'] ?? $opt['id'] ?? ''); if ($ov === $canon) return (string)($opt['label'] ?? $canon); } return $fallback !== '' ? $fallback : $canon; } function a11ybridge_catalog_options_html(string $contextId, $selected = null, string $fallbackDefault = ''): string { $sel = a11ybridge_catalog_canonical($contextId, (string)($selected ?? ''), $fallbackDefault); $html = ''; foreach (a11ybridge_catalog_context($contextId) as $opt) { $v = (string)($opt['value'] ?? $opt['id'] ?? ''); $l = (string)($opt['label'] ?? $v); if ($v === '') continue; $html .= sprintf( '', esc_attr($v), selected($v, $sel, false), esc_html($l) ); } return $html; }