. * * @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))) . '…'; } return $string; } }