Skip to content

Commit f335756

Browse files
committed
Add Moodle 4.5 support (see MDL-82427)
1 parent ddd8419 commit f335756

File tree

2 files changed

+104
-71
lines changed

2 files changed

+104
-71
lines changed

classes/text_filter.php

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
// This file is part of Moodle-oembed-Filter
3+
//
4+
// Moodle-oembed-Filter 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-oembed-Filter 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-oembed-Filter. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace filter_oembed;
18+
19+
/**
20+
* Filter for component 'filter_oembed'
21+
*
22+
* @package filter_oembed
23+
* @copyright Erich M. Wappis / Guy Thomas 2016
24+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
* @author Mat Cannings
26+
* @author James McQuillan
27+
* @author Vin Bhalerao
28+
* @author Erich M. Wappis <[email protected]>
29+
* @author Guy Thomas <[email protected]>
30+
* @author Mike Churchward <[email protected]>
31+
*/
32+
33+
defined('MOODLE_INTERNAL') || die();
34+
35+
use filter_oembed\service\oembed;
36+
37+
require_once($CFG->libdir.'/filelib.php');
38+
/**
39+
* Main filter class for embedded remote content.
40+
*
41+
* @package filter_oembed
42+
* @copyright Erich M. Wappis / Guy Thomas 2016
43+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44+
*/
45+
class text_filter extends \moodle_text_filter {
46+
47+
/**
48+
* content gets filtered, links either wrapped in an <a> tag or in a <div> tag with class="oembed"
49+
* will be replaced by embeded content
50+
*
51+
* @param $text HTML to be processed.
52+
* @param $options
53+
* @return string String containing processed HTML.
54+
*/
55+
public function filter($text, array $options = array()) {
56+
global $PAGE;
57+
58+
static $initialised = false;
59+
60+
if (!$initialised) {
61+
$PAGE->requires->js_call_amd('filter_oembed/oembed', 'init');
62+
$initialised = true;
63+
}
64+
65+
$targettag = get_config('filter_oembed', 'targettag');
66+
67+
if ($targettag == 'atag' && stripos($text, '</a>') === false) {
68+
// Performance shortcut - all regexes below end with the </a> tag.
69+
// If not present nothing can match.
70+
return $text;
71+
}
72+
73+
$filtered = $text; // We need to return the original value if regex fails!
74+
if ($targettag == 'divtag') {
75+
$search = '/\<div\s[^\>]*data-oembed-href="(.*?)"(.*?)>(.*?)\<\/div\>/';
76+
} else { // Using 'atag'.
77+
$search = '/\<a\s[^\>]*href="(.*?)"(?:.*?)>(?:.*?)\<\/a\>/is';
78+
}
79+
80+
$filtered = preg_replace_callback($search, [self::class, 'find_oembeds_callback'], $filtered);
81+
if (empty($filtered)) {
82+
// If $filtered is emtpy return original $text.
83+
return $text;
84+
} else {
85+
return $filtered;
86+
}
87+
}
88+
89+
/**
90+
* Callback function to be used by the main filter
91+
*
92+
* @param $match array An array of matched groups, where [1] is the URL matched.
93+
*
94+
*/
95+
private static function find_oembeds_callback($match) {
96+
$instance = oembed::get_instance();
97+
$result = $instance->html_output($match[1]);
98+
if (empty($result)) {
99+
$result = $match[0];
100+
}
101+
return $result;
102+
}
103+
}

filter.php

+1-71
Original file line numberDiff line numberDiff line change
@@ -28,74 +28,4 @@
2828
* @author Mike Churchward <[email protected]>
2929
*/
3030

31-
defined('MOODLE_INTERNAL') || die();
32-
33-
use filter_oembed\service\oembed;
34-
35-
require_once($CFG->libdir.'/filelib.php');
36-
/**
37-
* Main filter class for embedded remote content.
38-
*
39-
* @package filter_oembed
40-
* @copyright Erich M. Wappis / Guy Thomas 2016
41-
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42-
*/
43-
class filter_oembed extends moodle_text_filter {
44-
45-
/**
46-
* content gets filtered, links either wrapped in an <a> tag or in a <div> tag with class="oembed"
47-
* will be replaced by embeded content
48-
*
49-
* @param $text HTML to be processed.
50-
* @param $options
51-
* @return string String containing processed HTML.
52-
*/
53-
public function filter($text, array $options = array()) {
54-
global $PAGE;
55-
56-
static $initialised = false;
57-
58-
if (!$initialised) {
59-
$PAGE->requires->js_call_amd('filter_oembed/oembed', 'init');
60-
$initialised = true;
61-
}
62-
63-
$targettag = get_config('filter_oembed', 'targettag');
64-
65-
if ($targettag == 'atag' && stripos($text, '</a>') === false) {
66-
// Performance shortcut - all regexes below end with the </a> tag.
67-
// If not present nothing can match.
68-
return $text;
69-
}
70-
71-
$filtered = $text; // We need to return the original value if regex fails!
72-
if ($targettag == 'divtag') {
73-
$search = '/\<div\s[^\>]*data-oembed-href="(.*?)"(.*?)>(.*?)\<\/div\>/';
74-
} else { // Using 'atag'.
75-
$search = '/\<a\s[^\>]*href="(.*?)"(?:.*?)>(?:.*?)\<\/a\>/is';
76-
}
77-
78-
$filtered = preg_replace_callback($search, [self::class, 'find_oembeds_callback'], $filtered);
79-
if (empty($filtered)) {
80-
// If $filtered is emtpy return original $text.
81-
return $text;
82-
} else {
83-
return $filtered;
84-
}
85-
}
86-
87-
/**
88-
* Callback function to be used by the main filter
89-
*
90-
* @param $match array An array of matched groups, where [1] is the URL matched.
91-
*
92-
*/
93-
private static function find_oembeds_callback($match) {
94-
$instance = oembed::get_instance();
95-
$result = $instance->html_output($match[1]);
96-
if (empty($result)) {
97-
$result = $match[0];
98-
}
99-
return $result;
100-
}
101-
}
31+
class_alias(\filter_oembed\text_filter::class, \filter_oembed::class);

0 commit comments

Comments
 (0)