Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Forked from panoslyrakis/bannedwords.csv.zip
Last active November 15, 2022 15:39
Show Gist options
  • Save wpmudev-sls/02eff5cf209f3f8b46c10d99b250ea6d to your computer and use it in GitHub Desktop.
Save wpmudev-sls/02eff5cf209f3f8b46c10d99b250ea6d to your computer and use it in GitHub Desktop.
[Forminator] - Banned words
<?php
/**
* Plugin Name: [Forminator] - Banned words
* Plugin URI: https://premium.wpmudev.org/
* Description: Banned words check for Forminator.
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: SLS-2801
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( ! class_exists( 'WPMUDEV_Forminator_Banned_Words' ) ) {
class WPMUDEV_Forminator_Banned_Words {
private $error_message = 'There were some banned words found in your submission';
private static $_instance = null;
private $filter_error_message = false;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct() {
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'control_words' ), 10, 3 );
add_filter( 'forminator_custom_form_invalid_form_message', array( $this, 'filter_error_message' ) );
}
public function control_words( $submit_errors, $form_id, $field_data_array ) {
$csv_file = WP_CONTENT_DIR . '/mu-plugins/bannedwords.csv';
foreach( $field_data_array as $key => $value ) {
if( isset ( $value[ 'form_field_obj' ] ) ) {
unset( $field_data_array[ $key ][ 'form_field_obj' ] );
}
}
if ( file_exists( $csv_file ) && is_readable( $csv_file ) ) {
$fields_words = implode(
' ',
array_map(
function( $a ) {
return implode( ' ', $a );
},
$field_data_array
)
);
$banned_words = array_map(
function( $csv ) {
return $csv[0];
},
array_map( 'str_getcsv', file( $csv_file ) )
);
$banned_words = implode( '|', $banned_words );
$banned_words = preg_replace( '/[^\w_|]+/u', '', $banned_words );
$matches = array();
$match_found = preg_match(
'(' . $banned_words . ')',
$fields_words,
$matches
);
if ( $match_found && ! empty( $matches ) ) {
// We need somehting here so that the `$submit_errors` array is not empty.
$submit_errors[] = array( $matches );
$this->filter_error_message = true;
}
}
return $submit_errors;
}
public function filter_error_message( $invalid_form_message ) {
if ( $this->filter_error_message ) {
$invalid_form_message = __( $this->error_message );
}
return $invalid_form_message;
}
}
if ( ! function_exists( 'wpmudev_forminator_banned_words' ) ) {
function wpmudev_forminator_banned_words() {
return WPMUDEV_Forminator_Banned_Words::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_forminator_banned_words', 10 );
}
}
@DrFunkenbreakz
Copy link

Thanks for the snippet, but I think i've found a limitation or issue here as banned words that are part of acceptable words would also cause the "Banned words" submit error. E.g if the word compass was to be used. It contains a banned word. Is there anyway it could be updated to not include these part of word issue? Also having a way to debug to see what banned word was hit would be good. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment