Created
April 14, 2013 18:39
-
-
Save mshmsh5000/5383736 to your computer and use it in GitHub Desktop.
Topps_Content_Helper_Data::populateObjectWithCustomFields()
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 | |
/** | |
* Populate an existing object with the results of a WP JSON API response, | |
* which includes a custom_fields object. These can be the same object. In | |
* this case, leave the second parameter blank or as null. | |
* | |
* @param stdClass $object | |
* @param stdClass $response_object JSON response. | |
* @param bool $load_relations Optional; pass false to disable | |
* loading all related content. | |
* @return stdClass | |
* @throws Exception | |
*/ | |
public function populateObjectWithCustomFields( stdClass $object, $response_object = null, $load_relations = true ) | |
{ | |
if ( empty( $response_object ) ) | |
{ | |
$response_object = $object; | |
} | |
if ( empty( $response_object->custom_fields ) ) | |
{ | |
throw new Exception( "Can't find custom fields to populate in object" ); | |
} | |
$custom_fields = array(); | |
if ( !empty( $response_object->type ) ) | |
{ | |
$custom_fields = $this->getCustomFieldsForPostType( $response_object->type ); | |
} | |
// Mage::log( sprintf( '%s: Response object: %s', __METHOD__, print_r( $response_object, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
foreach( $response_object->custom_fields as $key => $field ) | |
{ | |
if ( is_array( $field ) && !empty( $field[ 0 ] ) ) | |
{ | |
// Mage::log( sprintf( '%s: Processing key %s', __METHOD__, $key ), null, self::TOPPS_CONTENT_LOG ); | |
$unserialized = @unserialize( $field[ 0 ] ); | |
$object->$key = ( false === $unserialized ) ? $field[ 0 ] : $unserialized; | |
if ( !is_array( $object->$key) ) | |
{ | |
if ( !empty( $custom_fields[ 'fields' ][ $key ] ) && 'boolean' == $custom_fields[ 'fields' ][ $key ] ) | |
{ | |
$object->$key = (boolean) $object->$key; | |
} | |
} | |
// If this attribute is an array of arrays, it's most likely | |
// an array of attachment IDs. E.g.: | |
// | |
// [css_file] => Array | |
// ( | |
// [114] => Array | |
// ( | |
// [id] => 114 | |
// ) | |
// ) | |
// | |
// A simple array is most likely a post ID: | |
// | |
// [article] => Array | |
// ( | |
// [0] => 101 | |
// ) | |
if ( $load_relations ) | |
{ | |
$data = $object->$key; | |
// Mage::log( sprintf( '%s: Loading relations for key %s', __METHOD__, $key ), null, self::TOPPS_CONTENT_LOG ); | |
if ( is_array( $data ) ) | |
{ | |
$attachments = array(); | |
// Mage::log( sprintf( '%s: Key: %s || data: %s', __LINE__, $key, print_r( $data, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
// Case 1: Attachment (only non-zero keys | |
// corresponding to WP IDs). | |
if ( !isset( $data[ 0 ] ) ) | |
{ | |
// Mage::log( sprintf( '%s: Loading attachment for key %s', __LINE__, $key ), null, self::TOPPS_CONTENT_LOG ); | |
foreach ( $data as $attachment ) | |
{ | |
$attached_object = new stdClass(); | |
foreach ( $attachment as $field_name => $row ) | |
{ | |
if ( 'id' == $field_name ) | |
{ | |
$media = $this->loadMedia( $row ); | |
// Mage::log( sprintf( '%s: %s', __LINE__, print_r( $media, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
// Test the existence of the "full" variant, just to identify | |
// that we have an image attachment here. | |
if ( !empty( $media->full ) ) | |
{ | |
foreach( $media as $variant_key => $image_type ) | |
{ | |
$variant = $media->$variant_key; | |
$attached_object->$variant_key = new stdClass(); | |
$attached_object->$variant_key->date_created = $variant->attachment->post_date; | |
$attached_object->$variant_key->date_modified = $variant->attachment->post_modified; | |
$attached_object->$variant_key->title_plain = $variant->attachment->post_title; | |
$attached_object->$variant_key->width = $variant->width; | |
$attached_object->$variant_key->height = $variant->height; | |
$attached_object->$variant_key->url = $variant->url; | |
} | |
} | |
elseif ( !empty( $media->file ) && !empty( $media->file->url ) ) | |
{ | |
$attached_object->url = $media->file->url; | |
} | |
else | |
{ | |
Mage::log( sprintf( '%s: WARNING: empty media file for ID = %d: %s', __LINE__, $row, print_r( $media, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
} | |
} | |
else | |
{ | |
$attached_object->$field_name = $row; | |
} | |
} | |
$attachments[] = $attached_object; | |
} | |
} | |
// Case 2: Post | |
else | |
{ | |
// Mage::log( sprintf( '%s: Loading post for key %s', __LINE__, $key ), null, self::TOPPS_CONTENT_LOG ); | |
foreach ( $data as $id ) | |
{ | |
if ( empty( $id ) ) | |
{ | |
Mage::log( sprintf( '%s: Skipping empty ID for key %s', __LINE__, $key ), null, self::TOPPS_CONTENT_LOG ); | |
continue; | |
} | |
// Mage::log( sprintf( '%s: Loading post: ID=%d, key=%s, for %s', __LINE__, $id, $key, $response_object->type ), null, self::TOPPS_CONTENT_LOG ); | |
$raw = $this->loadPost( $id, $this->determineChildDataType( $response_object->type, $key ) ); | |
$attached_object = $raw; | |
try | |
{ | |
// Mage::log( sprintf( '%s: POPULATING CHILD POST FOR KEY %s', __METHOD__, $key ), null, self::TOPPS_CONTENT_LOG ); | |
if ( !empty( $raw->custom_fields ) ) | |
{ | |
$attached_object = $this->populateObjectWithCustomFields( $attached_object, $raw ); | |
} | |
} | |
catch ( Exception $e ) | |
{ | |
// Not all post types have custom fields. | |
} | |
$attachments[] = ( !empty( $attached_object ) ) ? $attached_object : $raw; | |
// Mage::log( sprintf( '%s: Attached object: %s: %s', __METHOD__, $key, print_r( $attached_object, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
// Mage::log( sprintf( '%s: Original object: %s: %s', __METHOD__, $key, print_r( $raw, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
} | |
} | |
// Clean excerpt's "Continue reading" link, which we may in the future choose to remove in the WP theme. | |
for ( $i = 0; $i < count( $attachments ); $i++ ) | |
{ | |
if ( !empty( $attachments[ $i ]->excerpt ) ) { | |
$attachments[ $i ]->excerpt = preg_replace( '/<a[^>]+>Continue reading.*?<\/a>/i', '', $attachments[ $i ]->excerpt ); | |
} | |
} | |
$object->$key = $attachments; | |
// Mage::log( sprintf( '%s: Added attachments to key %s: %s', __METHOD__, $key, print_r( $attachments, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
} | |
} | |
// Mage::log( sprintf( '%s: Finished loading relations for key %s', __METHOD__, $key ), null, self::TOPPS_CONTENT_LOG ); | |
} | |
} | |
// Mage::log( sprintf( '%s: Final object: %s', __METHOD__, print_r( $object, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
unset( $object->custom_fields ); | |
if ( !empty( $object->slug ) && !empty( $object->type ) ) | |
{ | |
$object->css_unique_class = str_replace( '_', '-', $object->type ) . '-' . $object->slug; | |
} | |
return $object; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment