Skip to content

Commit

Permalink
MDL-24777 replace_smilies() replaced with the new standard filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mudrd8mz committed Oct 23, 2010
1 parent 84a8bed commit 015ba71
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 65 deletions.
154 changes: 154 additions & 0 deletions filter/emoticon/filter.php
@@ -0,0 +1,154 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 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. If not, see <http://www.gnu.org/licenses/>.

/**
* Filter converting emoticon texts into images
*
* This filter uses the emoticon settings in Site admin > Appearance > HTML settings
* and replaces emoticon texts with images.
*
* @package filter
* @subpackage emoticon
* @see emoticon_manager
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

class filter_emoticon extends moodle_text_filter {

/**
* @var array global configuration for this filter
*
* This might be eventually moved into parent class if we found it
* useful for other filters, too.
*/
protected static $globalconfig;

/**
* Apply the filter to the text
*
* @see filter_manager::apply_filter_chain()
* @param string $text to be processed by the text
* @param array $options filter options
* @return string text after processing
*/
public function filter($text, array $options = array()) {

if (!isset($options['originalformat'])) {
// if the format is not specified, we are probably called by {@see format_string()}
// in that case, it would be dangerous to replace text with the image because it could
// be stripped. therefore, we do nothing
return $text;
}
if (in_array($options['originalformat'], explode(',', $this->get_global_config('formats')))) {
$this->replace_emoticons($text);
}
return $text;
}

////////////////////////////////////////////////////////////////////////////
// internal implementation starts here
////////////////////////////////////////////////////////////////////////////

/**
* Returns the global filter setting
*
* If the $name is provided, returns single value. Otherwise returns all
* global settings in object. Returns null if the named setting is not
* found.
*
* @param mixed $name optional config variable name, defaults to null for all
* @return string|object|null
*/
protected function get_global_config($name=null) {
$this->load_global_config();
if (is_null($name)) {
return self::$globalconfig;

} elseif (array_key_exists($name, self::$globalconfig)) {
return self::$globalconfig->{$name};

} else {
return null;
}
}

/**
* Makes sure that the global config is loaded in $this->globalconfig
*
* @return void
*/
protected function load_global_config() {
if (is_null(self::$globalconfig)) {
self::$globalconfig = get_config('filter_emoticon');
}
}

/**
* Replace emoticons found in the text with their images
*
* @param string $text to modify
* @return void
*/
protected function replace_emoticons(&$text) {
global $CFG, $OUTPUT, $PAGE;
static $emoticontexts = array(); // internal cache used for replacing
static $emoticonimgs = array(); // internal cache used for replacing

$lang = current_language();
$theme = $PAGE->theme->name;

if (!isset($emoticontexts[$lang][$theme]) or !isset($emoticonimgs[$lang][$theme])) {
// prepare internal caches
$manager = get_emoticon_manager();
$emoticons = $manager->get_emoticons();
$emoticontexts[$lang][$theme] = array();
$emoticonimgs[$lang][$theme] = array();
foreach ($emoticons as $emoticon) {
$emoticontexts[$lang][$theme][] = $emoticon->text;
$emoticonimgs[$lang][$theme][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
}
unset($emoticons);
}

if (empty($emoticontexts[$lang][$theme])) { // No emoticons defined, nothing to process here
return;
}

// detect all the <script> zones to take out
$excludes = array();
preg_match_all('/<script language(.+?)<\/script>/is', $text, $listofexcludes);

// take out all the <script> zones from text
foreach (array_unique($listofexcludes[0]) as $key => $value) {
$excludes['<+'.$key.'+>'] = $value;
}
if ($excludes) {
$text = str_replace($excludes, array_keys($excludes), $text);
}

// this is the meat of the code - this is run every time
$text = str_replace($emoticontexts[$lang][$theme], $emoticonimgs[$lang][$theme], $text);

// Recover all the <script> zones to text
if ($excludes) {
$text = str_replace(array_keys($excludes), $excludes, $text);
}
}
}
33 changes: 33 additions & 0 deletions filter/emoticon/filtersettings.php
@@ -0,0 +1,33 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 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. If not, see <http://www.gnu.org/licenses/>.

/**
* @package filter
* @subpackage emoticon
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

if ($ADMIN->fulltree) {

$settings->add(new admin_setting_configmulticheckbox('filter_emoticon/formats',
get_string('settingformats', 'filter_emoticon'),
get_string('settingformats_desc', 'filter_emoticon'),
array(FORMAT_HTML => 1, FORMAT_MARKDOWN => 1, FORMAT_MOODLE => 1), format_text_menu()));
}
31 changes: 31 additions & 0 deletions filter/emoticon/lang/en/filter_emoticon.php
@@ -0,0 +1,31 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 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. If not, see <http://www.gnu.org/licenses/>.

/**
* Strings for filter_emoticon
*
* @package filter
* @subpackage emoticon
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$string['filtername'] = 'Display emoticons as images';
$string['settingformats'] = 'Apply to formats';
$string['settingformats_desc'] = 'The filter will be applied only if the original text was inserted in one of the selected formats.';
27 changes: 27 additions & 0 deletions filter/emoticon/version.php
@@ -0,0 +1,27 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 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. If not, see <http://www.gnu.org/licenses/>.

/**
* @package filter
* @subpackage emoticon
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2010102300;
1 change: 1 addition & 0 deletions lib/adminlib.php
Expand Up @@ -6092,6 +6092,7 @@ function print_plugin_tables() {
'algebra',
'censor',
'emailprotect',
'emoticon',
'filter',
'mediaplugin',
'multilang',
Expand Down
9 changes: 9 additions & 0 deletions lib/deprecatedlib.php
Expand Up @@ -545,6 +545,15 @@ function get_emoticons_list_for_help_file() {
return '';
}

/**
* Was used to replace all known smileys in the text with image equivalents
*
* This core function has been replaced with filter_emoticon since Moodle 2.0
*/
function replace_smilies(&$text) {
debugging('replace_smilies() has been deprecated and replaced with the new filter_emoticon');
}

/**
* deprecated - use clean_param($string, PARAM_FILE); instead
* Check for bad characters ?
Expand Down
65 changes: 0 additions & 65 deletions lib/weblib.php
Expand Up @@ -933,7 +933,6 @@ function format_text_menu() {
*/
function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_do_not_use = NULL) {
global $CFG, $COURSE, $DB, $PAGE;

static $croncache = array();

if ($text === '') {
Expand All @@ -942,7 +941,6 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_

$options = (array)$options; // detach object, we can not modify it


if (!isset($options['trusted'])) {
$options['trusted'] = false;
}
Expand Down Expand Up @@ -1608,69 +1606,6 @@ function cleanAttributes2($htmlArray){
return '<'. $slash . $elem . $attStr . $xhtml_slash .'>';
}

/**
* Replaces all known smileys in the text with image equivalents
*
* @global object
* @staticvar array $e
* @staticvar array $img
* @staticvar array $emoticons
* @param string $text Passed by reference. The string to search for smiley strings.
* @return string
*/
function replace_smilies(&$text) {
global $CFG, $OUTPUT;
static $emoticons = null;
static $e = array(); // array of emoticon texts like ':-)'
static $img = array(); // array of HTML to replace emoticon text with

$manager = get_emoticon_manager();

if (is_null($emoticons)) {
$emoticons = $manager->get_emoticons();
}

if (empty($emoticons)) { /// No emoticons defined, nothing to process here
return;
}

$lang = current_language();

if (empty($img[$lang])) { /// After the first time this is not run again
$e[$lang] = array();
$img[$lang] = array();
foreach ($emoticons as $emoticon) {
$e[$lang][] = $emoticon->text;
$img[$lang][] = $OUTPUT->render($manager->prepare_renderable_emoticon($emoticon));
}
}

// Exclude from transformations all the code inside <script> tags
// Needed to solve Bug 1185. Thanks to jouse 2001 detecting it. :-)
// Based on code from glossary filter by Williams Castillo.
// - Eloy

// Detect all the <script> zones to take out
$excludes = array();
preg_match_all('/<script language(.+?)<\/script>/is',$text,$list_of_excludes);

// Take out all the <script> zones from text
foreach (array_unique($list_of_excludes[0]) as $key=>$value) {
$excludes['<+'.$key.'+>'] = $value;
}
if ($excludes) {
$text = str_replace($excludes,array_keys($excludes),$text);
}

/// this is the meat of the code - this is run every time
$text = str_replace($e[$lang], $img[$lang], $text);

// Recover all the <script> zones to text
if ($excludes) {
$text = str_replace(array_keys($excludes),$excludes,$text);
}
}

/**
* Given plain text, makes it into HTML as nicely as possible.
* May contain HTML tags already
Expand Down

0 comments on commit 015ba71

Please sign in to comment.