Skip to content

Instantly share code, notes, and snippets.

@moimikey moimikey/custom.php
Created Dec 30, 2015

Embed
What would you like to do?
thermal, pull custom & acf fields
<?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;
// Initialize objects if not already created
if ( !is_object($object[$index]) ) $object[$index] = new StdClass;
if ( !is_object($object[$index]->data) ) $object[$index]->data = new StdClass;
// 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);
add_filter( 'thermal_post_entity', function( $data, &$post, $state ) {
if ( $state === 'read' ) {
$post_format = get_post_format( $post->ID );
$data = (array)$data;
$data['page_format'] = $post_format ? $post_format : 'standard';
}
return $data;
}, 100, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.