|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +/** |
| 18 | + * Filter file. |
| 19 | + * |
| 20 | + * @package filter_shortcodes |
| 21 | + * @copyright 2024 Frédéric Massart |
| 22 | + * @author Frédéric Massart <fred@branchup.tech> |
| 23 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 24 | + */ |
| 25 | + |
| 26 | +namespace filter_shortcodes; |
| 27 | + |
| 28 | +defined('MOODLE_INTERNAL') || die(); |
| 29 | + |
| 30 | +use filter_shortcodes\local\processor\standard_processor; |
| 31 | +use filter_shortcodes\local\registry\plugin_registry; |
| 32 | + |
| 33 | +require_once($CFG->dirroot . '/filter/shortcodes/lib/helpers.php'); |
| 34 | + |
| 35 | +if (class_exists(\core_filters\text_filter::class)) { |
| 36 | + /** |
| 37 | + * Parent class. |
| 38 | + */ |
| 39 | + abstract class core_text_filter extends \core_filters\text_filter {} |
| 40 | +} else { |
| 41 | + require_once($CFG->libdir . '/filterlib.php'); |
| 42 | + |
| 43 | + /** |
| 44 | + * Parent class. |
| 45 | + */ |
| 46 | + abstract class core_text_filter extends \moodle_text_filter {} |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Filter class. |
| 51 | + * |
| 52 | + * @package filter_shortcodes |
| 53 | + * @copyright 2024 Frédéric Massart |
| 54 | + * @author Frédéric Massart <fred@branchup.tech> |
| 55 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 56 | + */ |
| 57 | +class text_filter extends core_text_filter { |
| 58 | + |
| 59 | + /** @var processor The processor. */ |
| 60 | + private $processor; |
| 61 | + |
| 62 | + /** |
| 63 | + * The filtering occurs here. |
| 64 | + * |
| 65 | + * @param string $text HTML content. |
| 66 | + * @param array $options Options passed to the filter. |
| 67 | + * @return string The new content. |
| 68 | + */ |
| 69 | + public function filter($text, array $options = []) { |
| 70 | + $env = filter_shortcodes_make_env($this->context, $options); |
| 71 | + $processor = $this->get_processor(); |
| 72 | + $processor->set_env($env); |
| 73 | + return $processor->process($text); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the processor. |
| 78 | + * |
| 79 | + * @return standard_processor |
| 80 | + */ |
| 81 | + private function get_processor() { |
| 82 | + if ($this->processor === null) { |
| 83 | + $this->processor = new standard_processor(new plugin_registry()); |
| 84 | + } |
| 85 | + return $this->processor; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments