|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * The ReadingTime Plugin |
| 5 | + * |
| 6 | + * This plugin estimates and displays the reading time for FAQ articles. |
| 7 | + * |
| 8 | + * Add |
| 9 | + * |
| 10 | + * {{ phpMyFAQPlugin('reading.time', answer) | raw }} |
| 11 | + * |
| 12 | + * into the FAQ template. |
| 13 | + * |
| 14 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 15 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 16 | + * obtain one at https://mozilla.org/MPL/2.0/. |
| 17 | + * |
| 18 | + * @package phpMyFAQ |
| 19 | + * @author Thorsten Rinne <[email protected]> |
| 20 | + * @copyright 2024-2025 phpMyFAQ Team |
| 21 | + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 22 | + * @link https://www.phpmyfaq.de |
| 23 | + * @since 2024-07-10 |
| 24 | + */ |
| 25 | + |
| 26 | +declare(strict_types=1); |
| 27 | + |
| 28 | +namespace phpMyFAQ\Plugin\ReadingTime; |
| 29 | + |
| 30 | +use phpMyFAQ\Plugin\PluginInterface; |
| 31 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 32 | + |
| 33 | +require_once __DIR__ . '/ReadingTimePluginConfiguration.php'; |
| 34 | + |
| 35 | +class ReadingTimePlugin implements PluginInterface |
| 36 | +{ |
| 37 | + private ReadingTimePluginConfiguration $config; |
| 38 | + |
| 39 | + public function __construct() |
| 40 | + { |
| 41 | + $this->config = new ReadingTimePluginConfiguration(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @inheritDoc |
| 46 | + */ |
| 47 | + public function getName(): string |
| 48 | + { |
| 49 | + return 'ReadingTimePlugin'; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @inheritDoc |
| 54 | + */ |
| 55 | + public function getVersion(): string |
| 56 | + { |
| 57 | + return '0.2.0'; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @inheritDoc |
| 62 | + */ |
| 63 | + public function getDescription(): string |
| 64 | + { |
| 65 | + return 'Shows estimated reading time for FAQ articles'; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @inheritDoc |
| 70 | + */ |
| 71 | + public function getAuthor(): string |
| 72 | + { |
| 73 | + return 'phpMyFAQ Team'; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @inheritDoc |
| 78 | + */ |
| 79 | + public function getDependencies(): array |
| 80 | + { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @inheritDoc |
| 86 | + */ |
| 87 | + public function getConfig(): ?ReadingTimePluginConfiguration |
| 88 | + { |
| 89 | + return $this->config; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @inheritDoc |
| 94 | + */ |
| 95 | + public function registerEvents(EventDispatcherInterface $eventDispatcher): void |
| 96 | + { |
| 97 | + $eventDispatcher->addListener('reading.time', [$this, 'addReadingTime']); |
| 98 | + } |
| 99 | + |
| 100 | + public function addReadingTime($event): void |
| 101 | + { |
| 102 | + $content = $event->getData(); |
| 103 | + $readingTime = $this->calculateReadingTime($content); |
| 104 | + $badge = $this->generateBadge($readingTime); |
| 105 | + |
| 106 | + $event->setOutput($badge); |
| 107 | + } |
| 108 | + |
| 109 | + private function calculateReadingTime(string $content): int |
| 110 | + { |
| 111 | + $plainText = strip_tags($content); |
| 112 | + |
| 113 | + $wordCount = str_word_count($plainText); |
| 114 | + |
| 115 | + return max(1, (int) ceil($wordCount / $this->config->wordsPerMinute)); |
| 116 | + } |
| 117 | + |
| 118 | + private function generateBadge(int $minutes): string |
| 119 | + { |
| 120 | + $showIcon = $this->config->showIcon; |
| 121 | + |
| 122 | + $icon = $showIcon ? '<i class="bi bi-clock"></i> ' : ''; |
| 123 | + $pluralSuffix = $minutes === 1 ? '' : 'n'; |
| 124 | + |
| 125 | + return sprintf('%s ~ %d min %s', $icon, $minutes, $pluralSuffix); |
| 126 | + } |
| 127 | +} |
0 commit comments