public static function more($content, $post, $properties = array())
{
// If the post requested is the post under consideration, always return the full post
if ($post->slug == Controller::get_var('slug')) {
return $content;
} elseif (is_string($properties)) {
$args = func_get_args();
$more_text = $properties;
$max_words = isset($args[3]) ? $args[3] : null;
$max_paragraphs = isset($args[4]) ? $args[4] : null;
$inside_last = isset($args[5]) ? $args[5] : true;
$paramstring = "";
} else {
$paramstring = "";
$paramarray = Utils::get_params($properties);
$more_text = isset($paramarray['more_text']) ? $paramarray['more_text'] : 'Read More';
$max_words = isset($paramarray['max_words']) ? $paramarray['max_words'] : null;
$max_paragraphs = isset($paramarray['max_paragraphs']) ? $paramarray['max_paragraphs'] : null;
$inside_last = isset($paramarray['inside_last']) ? $paramarray['inside_last'] : true;
if (isset($paramarray['title:before']) || isset($paramarray['title']) || isset($paramarray['title:after'])) {
$paramstring .= 'title="';
if (isset($paramarray['title:before'])) {
$paramstring .= $paramarray['title:before'];
}
if (isset($paramarray['title'])) {
$paramstring .= $post->title;
}
if (isset($paramarray['title:after'])) {
$paramstring .= $paramarray['title:after'];
}
$paramstring .= '" ';
}
if (isset($paramarray['class'])) {
$paramstring .= 'class="' . $paramarray['class'] . '" ';
}
}
$link_text = '<a ' . $paramstring . ' href="' . $post->permalink . '">' . $more_text . '</a>';
// if we want it inside the last element, make sure there's a space before the link
if ($inside_last) {
$link_text = ' ' . $link_text;
}
// check for a <!--more--> link, which sets exactly where we should split
$matches = preg_split('/<!--\\s*more\\s*-->/isu', $content, 2, PREG_SPLIT_NO_EMPTY);
if (count($matches) > 1) {
$summary = reset($matches);
} else {
// otherwise, we need to summarize it automagically
$max_words = empty($max_words) ? 9999999 : intval($max_words);
$max_paragraphs = empty($max_paragraphs) ? 9999999 : intval($max_paragraphs);
$summary = Format::summarize($content, $max_words, $max_paragraphs);
}
// if the summary is equal to the length of the content (or somehow greater??), there's no need to add a link, just return the content
if (MultiByte::strlen($summary) >= MultiByte::strlen($content)) {
return $content;
} else {
// make sure there's actually text to append before we waste our time
if (strlen($more_text)) {
// parse out the summary and stick in our linky goodness
// tokenize the summary
$ht = new HTMLTokenizer($summary);
$summary_set = $ht->parse();
// tokenize the link we're adding
$ht = new HTMLTokenizer($link_text);
$link_set = $ht->parse();
// find out where to put the link by bumping the iterator to the last element
$end = $summary_set->end();
// and what index is that?
$key = $summary_set->key();
// if we want it inside the last element, we're good to go - if we want it outside, we need to add it as the *next* element
if ($inside_last == false) {
$key++;
}
// if the element is a text node, there were no tags; probably not autop'ed yet, just add link as new line
if ($end['type'] == HTMLTokenizer::NODE_TYPE_TEXT) {
$summary_set->insert($link_set, $key + 1);
} else {
// inject it, whereever we decided it should go
$summary_set->insert($link_set, $key);
}
// and return a stringified version
return (string) $summary_set;
} else {
// no text to append? just return the summary
return $summary;
}
}
return $content;
}