Last active
December 16, 2015 05:19
-
-
Save mshmsh5000/5383710 to your computer and use it in GitHub Desktop.
Topps_Content_Helper_Data::getWorld()
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 | |
/** | |
* Load a world and its child channels. Typically detects world from | |
* current category, but can also be passed a category slug. | |
* | |
* This is a heavy method. It loads related media, content, etc., | |
* sufficient to populate all content on the page. | |
* | |
* @param string $for_slug Identifier for the world to load. | |
* @param bool $load_children Optional; pass false to defeat loading child | |
* sub-channel data. | |
* @throws Exception | |
* @return object | |
*/ | |
public function getWorld( $for_slug = '', $load_children = true ) | |
{ | |
$world_slug = ( empty( $for_slug ) ) ? $this->getWorldSlug() : $for_slug; | |
if ( empty( $world_slug ) ) | |
{ | |
throw new Exception( "Can't find a world to load: no category loaded on this page, and nothing passed to the method."); | |
} | |
$world = $this->getWorldFromCache( $world_slug ); | |
if ( !empty( $world ) ) | |
{ | |
Mage::log( sprintf( '%s: Loaded world from cache: %s', __METHOD__, $world_slug ), null, self::TOPPS_CONTENT_LOG ); | |
return $world; | |
} | |
Mage::log( sprintf( '%s: Loading world: %s', __METHOD__, $world_slug ), null, self::TOPPS_CONTENT_LOG ); | |
$categories = $this->getCategories(); | |
$worlds = $categories[ 'cats' ]; | |
foreach ( $worlds as $world ) | |
{ | |
if ( $world->slug == $world_slug ) | |
{ | |
break; | |
} | |
} | |
if ( $world->slug !== $world_slug ) | |
{ | |
throw new Exception( sprintf( "Can't find world for slug '%s'", $world_slug ) ); | |
} | |
$custom_fields = $this->getCustomFieldsForPostType( 'world' ); | |
$data = $this->curlData( $this->getWPBaseDir() . 'api/get_category_posts/?slug=' . $world_slug . | |
'&post_type=worlds&custom_fields=' . $custom_fields[ 'stringified' ] ); | |
$response = json_decode( $data ); | |
// This should have been populated from the original getCategories(). | |
$world->children = ( !isset( $world->children ) ) ? array() : $world->children; | |
$world->ideas_json_url = '/content/index/ideas?world=' . $world_slug; | |
if ( !empty( $response->posts ) ) | |
{ | |
// Mage::log( sprintf( '%s: Loaded: %s', __METHOD__, print_r( $response->posts[ 0 ], true) ), null, self::TOPPS_CONTENT_LOG ); | |
$world_response = $response->posts[ 0 ]; | |
$world->id = $world_response->id; | |
$world = $this->populateObjectWithCustomFields( $world, $world_response ); | |
} | |
else | |
{ | |
Mage::log( sprintf( '%s: NOT LOADED: %s', __METHOD__, print_r( $response, true) ), null, self::TOPPS_CONTENT_LOG ); | |
} | |
// Populate sub-channels with everything possible. | |
if ( $load_children && !empty( $world->children ) ) | |
{ | |
foreach ( $world->children as $subchannel_id => $child ) | |
{ | |
Mage::log( sprintf( '%s: Loading sub-channel data for %s', __METHOD__, $child->slug ), null, self::TOPPS_CONTENT_LOG ); | |
$custom_fields = $this->getCustomFieldsForPostType( 'subchannel' ); | |
$data = $this->curlData( $this->getWPBaseDir() . 'api/get_category_posts/?slug=' . $child->slug . | |
'&post_type=subchannels&custom_fields=' . $custom_fields[ 'stringified' ] ); | |
$response = json_decode( $data ); | |
if ( !empty( $response->posts ) ) | |
{ | |
// Mage::log( sprintf( '%s: Loaded: %s', __METHOD__, print_r( $response->posts[ 0 ], true) ), null, self::TOPPS_CONTENT_LOG ); | |
$subchannel_response = $response->posts[ 0 ]; | |
$subchannel = $this->populateObjectWithCustomFields( $subchannel_response, $subchannel_response ); | |
// Each subchannel is categorized in its eponymous WP | |
// category, which contains the correct category slug. | |
$subchannel->category_slug = $child->slug; | |
$subchannel->images = ( !empty( $subchannel->curated_images ) ) ? $subchannel->curated_images : array(); | |
unset( $subchannel->curated_images ); | |
$all_subchannel_images = $this->getSubchannelImages( $subchannel ); | |
$subchannel->images_json = $all_subchannel_images[ 'json' ]; | |
$this->cacheSubchannelMedia( $child->id, 'images', $subchannel->images_json ); | |
$subchannel->images_json_url = '/content/index/media?subchannel=' . $child->id . '&type=images'; | |
$subchannel_videos = $this->getSubchannelVideos( $subchannel, 1 ); | |
$subchannel->videos = $subchannel_videos[ 'display' ]; | |
$subchannel->videos_json = $subchannel_videos[ 'json' ]; | |
$this->cacheSubchannelMedia( $child->id, 'video', $subchannel->videos_json ); | |
$subchannel->video_json_url = '/content/index/media?subchannel=' . $child->id . '&type=video'; | |
$subchannel_elements = $this->getSubchannelDesignElements( $subchannel, 5 ); | |
$subchannel->design_elements = $subchannel_elements[ 'display' ]; | |
// JS-safe CSS wrapper. | |
$subchannel->css_wrapper_id_safe = str_replace( '-', '_', $subchannel->css_wrapper_id ); | |
$world->children[ $subchannel->slug ] = $subchannel; | |
unset( $world->children[ $subchannel_id ] ); | |
} | |
else | |
{ | |
Mage::log( sprintf( '%s: NOT LOADED: %s', __METHOD__, print_r( $response, true) ), null, self::TOPPS_CONTENT_LOG ); | |
} | |
} | |
// Order sub-channels by their 'subchannel_order' attribute. | |
usort( $world->children, array( 'Topps_Content_Helper_Data', 'compareSubchannelOrder' ) ); | |
Mage::log( sprintf( '%s: Loaded subchannels', __METHOD__ ), null, self::TOPPS_CONTENT_LOG ); | |
} | |
Mage::log( sprintf( '%s: Final world: %s', __METHOD__, print_r( $world, true ) ), null, self::TOPPS_CONTENT_LOG ); | |
$this->cacheWorld( $world, $world_slug ); | |
return $world; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment