Pico/plugins/02-PicoExcerpt.php
Daniel Rudolf 70316eca87 Add PicoDeprecated, PicoParsePagesContent, PicoExcerpt
These plugins are crucial for backward compatibility
2015-08-28 18:30:19 +02:00

79 lines
2.3 KiB
PHP

<?php
/**
* Creates a excerpt for the contents of each page (as of Pico v0.9 and older)
*
* This plugin exists for backward compatibility and is disabled by default.
* It gets automatically enabled when {@link PicoDeprecated} is enabled. You
* can avoid this by calling {@link PicoExcerpt::setEnabled()}.
*
* This plugin doesn't do its job very well and depends on
* {@link PicoParsePagesContent}, what heavily impacts Picos performance. You
* should either use the Description meta header field or write something own.
* Best solution seems to be a filter for twig, see e.g.
* <https://gist.github.com/james2doyle/6629712>.
*
* @author Daniel Rudolf
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT
* @version 1.0
*/
class PicoExcerpt extends AbstractPicoPlugin
{
/**
* This plugin is disabled by default
*
* @see AbstractPicoPlugin::$enabled
*/
protected $enabled = false;
/**
* This plugin depends on {@link PicoParsePagesContent}
*
* @see AbstractPicoPlugin::$dependsOn
*/
protected $dependsOn = array('PicoParsePagesContent');
/**
* Adds the default excerpt length of 50 words to the config
*
* @see DummyPlugin::onConfigLoaded()
*/
public function onConfigLoaded(&$config)
{
if (!isset($config['excerpt_length'])) {
$config['excerpt_length'] = 50;
}
}
/**
* Creates a excerpt for the contents of each page
*
* @see DummyPlugin::onSinglePageLoaded()
*/
public function onSinglePageLoaded(&$pageData)
{
if (!isset($pageData['excerpt'])) {
$pageData['excerpt'] = $this->createExcerpt(
strip_tags($pageData['content']),
$this->getConfig('excerpt_length')
);
}
}
/**
* Helper function to create a excerpt of a string
*
* @param string $string the string to create a excerpt from
* @param int $wordLimit the maximum number of words the excerpt should be long
* @return string excerpt of $string
*/
protected function createExcerpt($string, $wordLimit)
{
$words = explode(' ', $string);
if (count($words) > $wordLimit) {
return trim(implode(' ', array_slice($words, 0, $wordLimit))) . '&hellip;';
}
return $string;
}
}