-
-
Save etodanik/9604853 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function north_cast_api_data($content) { | |
if (is_numeric($content)) $content = intval($content); | |
else { | |
$unserialized_content = @unserialize($content); | |
// we got serialized content | |
if ($unserialized_content !== false) { | |
// make sure that integers are represented as such, instead of str | |
foreach ($unserialized_content as $fn => &$c) { | |
if (is_numeric($c)) $c = intval($c); | |
} | |
$content = $unserialized_content; | |
} | |
} | |
return $content; | |
} | |
add_filter( 'thermal_post_entity', function($data, &$post, $state ) { | |
if( $state === 'read' ){ | |
// get all of the post's metadata | |
$custom_fields_raw = (object) get_post_meta($post->ID); | |
// uncomment the following to include the raw data for testing | |
//$data->meta->custom_fields_raw = $custom_fields_raw; | |
// create an object for storing all the post data | |
$sections = new StdClass; | |
$custom_fields_additional = array(); | |
foreach ($custom_fields_raw as $fieldName => $content) { | |
// remove any fields that start with an underscore, as it's a private one | |
if (substr((string)$fieldName, 0, 1) == '_') continue; | |
// if the string matches the format string_X_string, where X is any | |
// integer then we need to put it into an object | |
if (preg_match('_\d+_', (string)$fieldName, $indexes) > 0) { | |
// we need a type of field, an index of that field, and an item | |
$index = (int) $indexes[0]; | |
$keys = explode('_'.$index.'_', (string)$fieldName); | |
$type = $keys[0]; | |
$item = $keys[1]; | |
// we need the type of this item, which is only stored in that type's | |
// serialized content | |
$type_ref = &$custom_fields_raw->$type; | |
$type_info = unserialize($type_ref[0]); | |
// now we need to create an array for that type | |
if (!$sections->$type) $sections->$type = array(); | |
$object = &$sections->$type; | |
// add the object's type, if we have one. | |
if ($type_info[$index]) $object[$index]->type = $type_info[$index]; | |
// check if we've got an integer or serialized data, and massage to the | |
// right type accordingly | |
$content = north_cast_api_data($content[0]); | |
// now add the content to the data array | |
$object[$index]->data->$item = $content; | |
} | |
// if we didn't match that format, this may be an additional custom field | |
// that needs to be included. | |
else { | |
$custom_fields_additional[$fieldName] = north_cast_api_data($content[0]); | |
} | |
} | |
$data->meta->sections = $sections; | |
$data->meta->custom_fields = (object) $custom_fields_additional; | |
} | |
return $data; | |
}, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment