forked from catalyst/moodle-filter_oembed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.php
101 lines (89 loc) · 3.35 KB
/
filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
// This file is part of Moodle-oembed-Filter
//
// Moodle-oembed-Filter is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle-oembed-Filter is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle-oembed-Filter. If not, see <http://www.gnu.org/licenses/>.
/**
* Filter for component 'filter_oembed'
*
* @package filter_oembed
* @copyright Erich M. Wappis / Guy Thomas 2016
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Mat Cannings
* @author James McQuillan
* @author Vin Bhalerao
* @author Erich M. Wappis <[email protected]>
* @author Guy Thomas <[email protected]>
* @author Mike Churchward <[email protected]>
*/
defined('MOODLE_INTERNAL') || die();
use filter_oembed\service\oembed;
require_once($CFG->libdir.'/filelib.php');
/**
* Main filter class for embedded remote content.
*
* @package filter_oembed
* @copyright Erich M. Wappis / Guy Thomas 2016
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter_oembed extends moodle_text_filter {
/**
* content gets filtered, links either wrapped in an <a> tag or in a <div> tag with class="oembed"
* will be replaced by embeded content
*
* @param $text HTML to be processed.
* @param $options
* @return string String containing processed HTML.
*/
public function filter($text, array $options = array()) {
global $PAGE;
static $initialised = false;
if (!$initialised) {
$PAGE->requires->js_call_amd('filter_oembed/oembed', 'init');
$initialised = true;
}
$targettag = get_config('filter_oembed', 'targettag');
if ($targettag == 'atag' && stripos($text, '</a>') === false) {
// Performance shortcut - all regexes below end with the </a> tag.
// If not present nothing can match.
return $text;
}
$filtered = $text; // We need to return the original value if regex fails!
if ($targettag == 'divtag') {
$search = '/\<div\s[^\>]*data-oembed-href="(.*?)"(.*?)>(.*?)\<\/div\>/';
} else { // Using 'atag'.
$search = '/\<a\s[^\>]*href="(.*?)"(?:.*?)>(?:.*?)\<\/a\>/is';
}
$filtered = preg_replace_callback($search, 'self::find_oembeds_callback', $filtered);
if (empty($filtered)) {
// If $filtered is emtpy return original $text.
return $text;
} else {
return $filtered;
}
}
/**
* Callback function to be used by the main filter
*
* @param $match array An array of matched groups, where [1] is the URL matched.
*
*/
private static function find_oembeds_callback($match) {
$instance = oembed::get_instance();
$result = $instance->html_output($match[1]);
if (empty($result)) {
$result = $match[0];
}
return $result;
}
}