Skip to content

Instantly share code, notes, and snippets.

@loulou2u
Last active July 13, 2016 20:04
Show Gist options
  • Save loulou2u/a5a3b7cd91168ad0c2c9250daaed2798 to your computer and use it in GitHub Desktop.
Save loulou2u/a5a3b7cd91168ad0c2c9250daaed2798 to your computer and use it in GitHub Desktop.
<?php
$_LW->REGISTERED_APPS['resize_summary'] = array(
'title' => 'Resize Summary',
'handlers' => array('onOutputRSS'),
'custom' => array(
'always_truncate'=>false, // always truncate, or require /truncate/1?
'fields_to_shorten'=>array( // fields to shorten, and the maximum number of words for that field
'description'=>30
)
)
);
class LiveWhaleApplicationResizeSummary {
public function onOutputRSS($buffer) { // post-processes RSS output
global $_LW, $LIVE_URL;
if (!empty($_LW->REGISTERED_APPS['resize_summary']['custom']['always_truncate']) || in_array('truncate', @$LIVE_URL['REQUEST'])) { // if truncating should occur
$fields_to_shorten=&$_LW->REGISTERED_APPS['resize_summary']['custom']['fields_to_shorten']; // get fields to shorten
if (!empty($fields_to_shorten)) { // if there are fields to shorten
foreach($fields_to_shorten as $field=>$max_words) { // for each one
if (!empty($max_words)) { // if there is a valid maximum word length
$find=array();
$replace=array();
$matches=array();
$field=preg_quote($field, '~');
preg_match_all('~<'.$field.'><\!\[CDATA\[(.+?)\]\]></'.$field.'>~s', $buffer, $matches); // find all instances of this field
if (!empty($matches[1])) {
foreach($matches[1] as $field_name) {
if (str_word_count($field_name)>$max_words) { // swap out abbreviated value where necessary
$find[]='<'.$field.'><![CDATA['.$field_name.']]></'.$field.'>';
$replace[]='<'.$field.'><![CDATA['.$_LW->setFormatSummarize($field_name, $max_words).']]></'.$field.'>';
};
};
if (!empty($find)) { // perform final find/replace of all swapped values
$buffer=str_replace($find, $replace, $buffer);
};
};
};
};
};
};
return $buffer;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment