Skip to content

Instantly share code, notes, and snippets.

@wararyo
Created July 14, 2021 12:17
Show Gist options
  • Save wararyo/5e21bf8effa9941e36b281cd3427d4fd to your computer and use it in GitHub Desktop.
Save wararyo/5e21bf8effa9941e36b281cd3427d4fd to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: MW WP Form Forbidden Word
Plugin URI:
Description: MW WP Formのバリデーションに「禁止ワード」を追加
Version: 1.0.0
Author: wararyo
Author URI: https://github.com/wararyo
License: GPLv2
*/
$config_class = WP_PLUGIN_DIR . '/mw-wp-form/classes/config.php';
$validation_rule_class = WP_PLUGIN_DIR . '/mw-wp-form/classes/abstract/class.validation-rule.php';
if (file_exists($config_class) && file_exists($validation_rule_class)) {
include_once $config_class;
include_once $validation_rule_class;
}
include_once(ABSPATH . 'wp-admin/includes/plugin.php');
if (is_plugin_active('mw-wp-form/mw-wp-form.php')) {
class MW_WP_Form_ForbiddenWord_Validation extends MW_WP_Form_Abstract_Validation_Rule
{
/**
* Validation rule name.
*
* @var string
*/
protected $name = 'forbidden_word';
/**
* Validation process.
*
* @param string $name Validation name.
* @param array $options Validation options.
* @return string
*/
public function rule($name, array $options = array())
{
$value = $this->Data->get($name);
$value = MWF_Functions::convert_eol($value);
if (MWF_Functions::is_empty($value)) {
return;
}
$defaults = array(
'words' => 0,
'message' => '禁止ワードが含まれています。'
);
$options = array_merge($defaults, $options);
$words = explode(",", $options['words']);
foreach ($words as $word) {
if (strpos($value, $word) !== false) {
return $options['message'];
}
}
}
/**
* Add setting field to validation rule setting panel.
*
* @param numeric $key ID of validation rule.
* @param array $value Content of validation rule.
* @return void
*/
public function admin($key, $value)
{
$min = '';
if (is_array($value[$this->getName()]) && isset($value[$this->getName()]['words'])) {
$min = $value[$this->getName()]['words'];
}
?>
<table>
<tr>
<td>禁止ワード</td>
<td><input type="text" value="<?php echo esc_attr($min); ?>" size="48" name="<?php echo MWF_Config::NAME; ?>[validation][<?php echo $key; ?>][<?php echo esc_attr($this->getName()); ?>][words]" /></td>
</tr>
</table>
<?php
}
}
function add_validation_forbidden_word_rules($validation_rules)
{
$instance = new MW_WP_Form_ForbiddenWord_Validation();
$validation_rules[$instance->get_name()] = $instance;
return $validation_rules;
}
add_filter('mwform_validation_rules', 'add_validation_forbidden_word_rules');
} //is_plugin_active
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment