Skip to content

Instantly share code, notes, and snippets.

@dmje
Created April 8, 2025 14:57
Show Gist options
  • Save dmje/6eb2b0a5ebf8b52ef572fdc90d3dca1a to your computer and use it in GitHub Desktop.
Save dmje/6eb2b0a5ebf8b52ef572fdc90d3dca1a to your computer and use it in GitHub Desktop.
MDS - Culture Object Spectrum display
<?php
function unserialize_meta_data($data) {
if (is_array($data)) {
foreach ($data as $key => &$item) {
if (is_array($item) && isset($item[0]) && is_serialized($item[0])) {
$item = maybe_unserialize($item[0]);
}
}
}
return $data;
}
// Get and unserialize post meta
$post_id = get_the_ID();
$data = unserialize_meta_data(get_post_meta($post_id));
$spectrumData = [];
// Function to recursively search through arrays
function findSpectrumData($array, &$results) {
if (!is_array($array)) {
return;
}
foreach ($array as $item) {
if (is_array($item)) {
// Check if this item has the required properties
if (isset($item['type']) &&
isset($item['label']) &&
isset($item['value']) &&
str_starts_with($item['type'], 'spectrum/')) {
$results[] = [
'label' => $item['label'],
'type' => $item['type'],
'value' => $item['value']
];
}
// If there's a units array, search through it
if (isset($item['units'])) {
findSpectrumData($item['units'], $results);
}
// Continue searching through other arrays
findSpectrumData($item, $results);
}
}
}
// Ensure `@document` is properly unserialized
if (isset($data['@document']) && is_array($data['@document'])) {
$documentData = $data['@document']; // It's already unserialized by unserialize_meta_data()
// Start the search with the unserialized data
if (isset($documentData['units'])) {
findSpectrumData($documentData['units'], $spectrumData);
}
}
// Check if ?spectrum is present in the URL
if (isset($_GET['spectrum'])) {
echo '<h1>Spectrum Data</h1>';
if (empty($spectrumData)) {
echo '<p>No spectrum data found</p>';
} else {
// Display results
foreach ($spectrumData as $item) {
echo "<h2>Label: {$item['label']}</h2>";
echo "<ul>";
echo "<li>Type: {$item['type']}</li>";
echo "<li>Value: {$item['value']}</li>";
echo "</ul>";
echo "<hr/>";
}
}
} else {
echo '<h1>Raw Data</h1>';
echo '<pre>';
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
echo '</pre>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment