MDL-24531 Extend filter manager API so that options can be passed to the filters
The only option passed at the moment is the original format in which the user inserted the text. Other options can be added later if/when needed.
This commit is contained in:
@@ -35,7 +35,7 @@ class filter_activitynames extends moodle_text_filter {
|
||||
static $activitylist = null;
|
||||
static $cachedcourseid;
|
||||
|
||||
function filter($text) {
|
||||
function filter($text, array $options = array()) {
|
||||
global $CFG, $COURSE, $DB;
|
||||
|
||||
if (!$courseid = get_courseid_from_context($this->context)) {
|
||||
|
||||
@@ -92,7 +92,7 @@ function filter_algebra_image($imagefile, $tex= "", $height="", $width="", $alig
|
||||
}
|
||||
|
||||
class filter_algebra extends moodle_text_filter {
|
||||
function filter($text){
|
||||
function filter($text, array $options = array()){
|
||||
global $CFG, $DB;
|
||||
|
||||
/// Do a quick check using stripos to avoid unnecessary wor
|
||||
|
||||
@@ -52,7 +52,7 @@ class filter_censor extends moodle_text_filter {
|
||||
return $cap;
|
||||
}
|
||||
|
||||
function filter($text){
|
||||
function filter($text, array $options = array()){
|
||||
static $words;
|
||||
global $CFG;
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* hides them using the Moodle obfuscate_text function.
|
||||
*/
|
||||
class filter_emailprotect extends moodle_text_filter {
|
||||
function filter($text) {
|
||||
function filter($text, array $options = array()) {
|
||||
/// Do a quick check using stripos to avoid unnecessary work
|
||||
if (strpos($text, '@') === false) {
|
||||
return $text;
|
||||
|
||||
@@ -33,7 +33,7 @@ require_once($CFG->libdir.'/filelib.php');
|
||||
|
||||
class filter_mediaplugin extends moodle_text_filter {
|
||||
private $eolas_fix_applied = false;
|
||||
function filter($text) {
|
||||
function filter($text, array $options = array()) {
|
||||
global $CFG, $PAGE;
|
||||
// You should never modify parameters passed to a method or function, it's BAD practice. Create a copy instead.
|
||||
// The reason is that you must always be able to refer to the original parameter that was passed.
|
||||
|
||||
@@ -40,7 +40,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
// <span lang="XX" class="multilang">one lang</span><span lang="YY" class="multilang">another language</span>
|
||||
|
||||
class filter_multilang extends moodle_text_filter {
|
||||
function filter($text) {
|
||||
function filter($text, array $options = array()) {
|
||||
global $CFG;
|
||||
|
||||
// [pj] I don't know about you but I find this new implementation funny :P
|
||||
|
||||
@@ -105,7 +105,7 @@ function filter_text_image($imagefile, $tex= "", $height="", $width="", $align="
|
||||
}
|
||||
|
||||
class filter_tex extends moodle_text_filter {
|
||||
function filter ($text) {
|
||||
function filter ($text, array $options = array()) {
|
||||
|
||||
global $CFG, $DB;
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
// values are, see http://php.net/manual/en/function.tidy-get-config.php.
|
||||
|
||||
class filter_tidy extends moodle_text_filter {
|
||||
function filter($text) {
|
||||
function filter($text, array $options = array()) {
|
||||
|
||||
/// Configuration for tidy. Feel free to tune for your needs, e.g. to allow
|
||||
/// proprietary markup.
|
||||
|
||||
+15
-10
@@ -148,11 +148,12 @@ class filter_manager {
|
||||
* @todo Document this function
|
||||
* @param string $text
|
||||
* @param array $filterchain
|
||||
* @param array $options options passed to the filters
|
||||
* @return string $text
|
||||
*/
|
||||
protected function apply_filter_chain($text, $filterchain) {
|
||||
protected function apply_filter_chain($text, $filterchain, array $options = array()) {
|
||||
foreach ($filterchain as $filter) {
|
||||
$text = $filter->filter($text);
|
||||
$text = $filter->filter($text, $options);
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
@@ -186,10 +187,11 @@ class filter_manager {
|
||||
*
|
||||
* @param string $text The text to filter
|
||||
* @param object $context
|
||||
* @param array $options options passed to the filters
|
||||
* @return string resulting text
|
||||
*/
|
||||
public function filter_text($text, $context) {
|
||||
$text = $this->apply_filter_chain($text, $this->get_text_filters($context));
|
||||
public function filter_text($text, $context, array $options = array()) {
|
||||
$text = $this->apply_filter_chain($text, $this->get_text_filters($context), $options);
|
||||
/// <nolink> tags removed for XHTML compatibility
|
||||
$text = str_replace(array('<nolink>', '</nolink>'), '', $text);
|
||||
return $text;
|
||||
@@ -236,7 +238,7 @@ class null_filter_manager {
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function filter_text($text, $context) {
|
||||
public function filter_text($text, $context, $options) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
@@ -285,11 +287,12 @@ class performance_measuring_filter_manager extends filter_manager {
|
||||
/**
|
||||
* @param string $text
|
||||
* @param object $context
|
||||
* @param array $options options passed to the filters
|
||||
* @return mixed
|
||||
*/
|
||||
public function filter_text($text, $context) {
|
||||
public function filter_text($text, $context, array $options = array()) {
|
||||
$this->textsfiltered++;
|
||||
return parent::filter_text($text, $context);
|
||||
return parent::filter_text($text, $context, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,7 +335,7 @@ class performance_measuring_filter_manager extends filter_manager {
|
||||
abstract class moodle_text_filter {
|
||||
/** @var object The context we are in. */
|
||||
protected $context;
|
||||
/** @var object Any local configuration for this filter in this context. */
|
||||
/** @var array Any local configuration for this filter in this context. */
|
||||
protected $localconfig;
|
||||
|
||||
/**
|
||||
@@ -357,9 +360,10 @@ abstract class moodle_text_filter {
|
||||
* Override this function to actually implement the filtering.
|
||||
*
|
||||
* @param $text some HTML content.
|
||||
* @param array $options options passed to the filters
|
||||
* @return the HTML content after the filtering has been applied.
|
||||
*/
|
||||
public abstract function filter($text);
|
||||
public abstract function filter($text, array $options = array());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -391,9 +395,10 @@ class legacy_filter extends moodle_text_filter {
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
* @param array $options options - not supported for legacy filters
|
||||
* @return mixed
|
||||
*/
|
||||
public function filter($text) {
|
||||
public function filter($text, array $options = array()) {
|
||||
if ($this->courseid) {
|
||||
// old filters are called only when inside courses
|
||||
return call_user_func($this->filterfunction, $this->courseid, $text);
|
||||
|
||||
+3
-3
@@ -1036,7 +1036,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_
|
||||
if (!$options['noclean']) {
|
||||
$text = clean_text($text, FORMAT_HTML);
|
||||
}
|
||||
$text = $filtermanager->filter_text($text, $context);
|
||||
$text = $filtermanager->filter_text($text, $context, array('originalformat' => FORMAT_HTML));
|
||||
break;
|
||||
|
||||
case FORMAT_PLAIN:
|
||||
@@ -1062,7 +1062,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_
|
||||
if (!$options['noclean']) {
|
||||
$text = clean_text($text, FORMAT_HTML);
|
||||
}
|
||||
$text = $filtermanager->filter_text($text, $context);
|
||||
$text = $filtermanager->filter_text($text, $context, array('originalformat' => FORMAT_MARKDOWN));
|
||||
break;
|
||||
|
||||
default: // FORMAT_MOODLE or anything else
|
||||
@@ -1070,7 +1070,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_
|
||||
if (!$options['noclean']) {
|
||||
$text = clean_text($text, FORMAT_HTML);
|
||||
}
|
||||
$text = $filtermanager->filter_text($text, $context);
|
||||
$text = $filtermanager->filter_text($text, $context, array('originalformat' => $format));
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user