. /** * 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 * @author Guy Thomas * @author Mike Churchward */ 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 tag or in a
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, '') === false) { // Performance shortcut - all regexes below end with the 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 = '/\]*data-oembed-href="(.*?)"(.*?)>(.*?)\<\/div\>/'; } else { // Using 'atag'. $search = '/\]*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; } }