Last active
July 21, 2022 17:52
-
-
Save panoslyrakis/3af8aa3b223249c7754ffd6b76eb6c51 to your computer and use it in GitHub Desktop.
[Forminator] - Banned words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
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 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just tried uploading both of these files to the wp-content/mu-plugins/ directory on a few different Wordpress sites, and I thought it was working, but it was stopping all form submissions and spitting back this error, "An error occurred while processing the form. Please try again".