Skip to content

Commit

Permalink
add WordFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
skywalker512 committed Jul 7, 2018
1 parent 6ace7d6 commit c551a37
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 0 deletions.
3 changes: 3 additions & 0 deletions addons/languages/Chinese/definitions.WordFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
$definitions["Word filters"] = "文字替换";
$definitions["message.wordFilterInstructions"] = "每一个字符为一行,格式为 <strong>要替换的文字|替换的文字</strong> ; <br>若没有输入要替换的文字则由 ** 替代。<br>英文不区分大小写,可以使用正则";
37 changes: 37 additions & 0 deletions addons/plugins/WordFilter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.buildpath
.project
.settings
.htaccess

# Compiled source
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases
*.log
*.sql
*.sqlite

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
*~
19 changes: 19 additions & 0 deletions addons/plugins/WordFilter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Word Filter Plugin

Perform find and replace on post content when posts are displayed.

## Installation

[Download](https://github.com/esotalk/WordFilter/archive/master.zip) or clone the Word Filter plugin repo into your esoTalk plugin directory:

cd ESOTALK_DIR/addons/plugins/
git clone [email protected]:esotalk/WordFilter.git WordFilter

Navigate to the the admin/plugins page and activate the Word Filter plugin.

## Translation

Create `definitions.WordFilter.php` in your language pack with the following definitions:

$definitions["Word filters"] = "Word filters";
$definitions["message.wordFilterInstructions"] = "Enter each word on a new line. Optionally specify a replacement after a vertical bar (|) character; otherwise, the word will be replaced with asterisks (*). Words are case-insensitive. Regular expressions are allowed.";
Binary file added addons/plugins/WordFilter/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
112 changes: 112 additions & 0 deletions addons/plugins/WordFilter/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
// Copyright 2011 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.

if (!defined("IN_ESOTALK")) exit;

ET::$pluginInfo["WordFilter"] = array(
"name" => "文字替换",
"description" => "替换特殊字符",
"version" => ESOTALK_VERSION,
"author" => "esoTalk Team",
"authorEmail" => "[email protected]",
"authorURL" => "http://esotalk.org",
"license" => "GPLv2",
"dependencies" => array(
"esoTalk" => "1.0.0g4"
)
);


class ETPlugin_WordFilter extends ETPlugin {


public function handler_format_format($sender)
{
$filters = C("plugin.WordFilter.filters", array());

if (!count($filters)) return;

// Pass each instance of any filtered word to our callback.
$words = array_keys($filters);
$sender->content = preg_replace_callback('#\b('.implode('|', $words).')\b#i', array($this, "filterCallback"), $sender->content);
}


public function filterCallback($matches)
{
$filters = C("plugin.WordFilter.filters", array());

// Construct a mapping of lowercase words to their normal case in the filters array.
$keys = array_keys($filters);
$map = array();
foreach ($keys as $key) {
$map[strtolower($key)] = $key;
}

$match = $matches[1];

// If there's a replacement for this particular casing of the word, use that.
if (!empty($filters[$match])) $replacement = $filters[$match];

// If there's a replacement for a lowercased version of this word, use that.
elseif (!empty($filters[$map[strtolower($match)]])) $replacement = $filters[$map[strtolower($match)]];

// Otherwise, use asterisks.
else $replacement = str_repeat("*", strlen($match));

return $replacement;
}


// Construct and process the settings form.
public function settings($sender)
{
// Expand the filters array into a string that will go in the textarea.
$filters = C("plugin.WordFilter.filters", array());
$filterText = "";
foreach ($filters as $word => $replacement) {
$filterText .= $word.($replacement ? "|$replacement" : "")."\n";
}
$filterText = trim($filterText);

// Set up the settings form.
$form = ETFactory::make("form");
$form->action = URL("admin/plugins/settings/WordFilter");
$form->setValue("filters", $filterText);

// If the form was submitted...
if ($form->validPostBack("wordFilterSave")) {

// Create an array of word filters from the contents of the textarea.
// Each line is a new element in the array; keys and values are separated by a | character.
$filters = array();
$lines = explode("\n", strtr($form->getValue("filters"), array("\r\n" => "\n", "\r" => "\n")));
foreach ($lines as $line) {
if (!$line) continue;
$parts = explode("|", $line, 2);
if (!$parts[0]) continue;
$filters[$parts[0]] = @$parts[1];
}

// Construct an array of config options to write.
$config = array();
$config["plugin.WordFilter.filters"] = $filters;

if (!$form->errorCount()) {

// Write the config file.
ET::writeConfig($config);

$sender->message(T("message.changesSaved"), "success autoDismiss");
$sender->redirect(URL("admin/plugins"));

}
}

$sender->data("wordFilterSettingsForm", $form);
return $this->view("settings");
}


}
29 changes: 29 additions & 0 deletions addons/plugins/WordFilter/views/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
// Copyright 2013 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.

if (!defined("IN_ESOTALK")) exit;

$form = $data["wordFilterSettingsForm"];
?>
<?php echo $form->open(); ?>

<div class='section'>

<ul class='form'>

<li>
<label><?php echo T("Word filters"); ?></label>
<?php echo $form->input("filters", "textarea", array("style" => "height:200px; width:350px")); ?>
<small><?php echo T("message.wordFilterInstructions", "Enter each word on a new line. Optionally specify a replacement after a vertical bar (|) character; otherwise, the word will be replaced with asterisks (*). Words are case-insensitive. Regular expressions are allowed."); ?></small>
</li>

</ul>

</div>

<div class='buttons'>
<?php echo $form->saveButton("wordFilterSave"); ?>
</div>

<?php echo $form->close(); ?>

0 comments on commit c551a37

Please sign in to comment.