Skip to content

Instantly share code, notes, and snippets.

@mshmsh5000
Created April 14, 2013 19:01
Show Gist options
  • Save mshmsh5000/5383803 to your computer and use it in GitHub Desktop.
Save mshmsh5000/5383803 to your computer and use it in GitHub Desktop.
Topps_Content_Model_Observer
<?php
/**
* Where appropriate, we have injected some customized content rendering to
* substitute WordPress post URLs with their actual content.
*/
class Topps_Content_Model_Observer
{
const TOPPS_CONTENT_LOG = 'content.log';
/**
* If page content contains a bare WordPress URL reference, load that
* content instead.
*
* @param Varien_Event_Observer $observer
*/
public function cmsPageLoadAfter( Varien_Event_Observer $observer )
{
$page = $observer->getEvent()->getData( 'page' );
if ( !empty( $page ) && is_a( $page, 'Mage_Cms_Model_Page' ) && 'no-route' !== $page->getData( 'identifier' ) )
{
// Mage::log( sprintf( '%s: %s: %s', __METHOD__, $page->getData( 'identifier' ), print_r( $page->debug(), true ) ), null, self::TOPPS_CONTENT_LOG );
// Stripping tags that may have been injected by the WP WYSIWYG.
$content = strip_tags( trim( $page->getData( 'content' ) ) );
if ( !empty( $content ) && filter_var( $content, FILTER_VALIDATE_URL ) )
{
$response_raw = false;
try
{
$custom_fields = Mage::helper( 'topps_content' )->getCustomFieldsForPostType( 'article' );
$custom_fields_string = ( empty( $custom_fields ) || empty( $custom_fields[ 'stringified' ] ) ) ? '' : '&custom_fields=' . $custom_fields[ 'stringified' ];
$response_raw = Mage::helper( 'topps_content' )->curlData( $content . '?json=1&' . $custom_fields_string );
Mage::log( sprintf( '%s: %s', __METHOD__, print_r( $response_raw, true ) ), null, self::TOPPS_CONTENT_LOG );
}
catch( Exception $e )
{
// Load failed, move on.
}
if ( !empty( $response_raw ) )
{
$wp_data = json_decode( $response_raw );
// Mage::log( sprintf( '%s: Final page content: %s', __METHOD__, print_r( $wp_data, true ) ), null, self::TOPPS_CONTENT_LOG );
if ( !empty( $wp_data->page->content ) )
{
$page->setContent( $wp_data->page->content );
// Mage::log( sprintf( '%s: Final page content: %s', __METHOD__, print_r( $page->getContent, true ) ), null, self::TOPPS_CONTENT_LOG );
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment