diff --git a/cleantalk.php b/cleantalk.php index 0293d247..909158b7 100644 --- a/cleantalk.php +++ b/cleantalk.php @@ -11,6 +11,7 @@ Domain Path: /i18n */ +use Cleantalk\Antispam\ProtectByShortcode; use Cleantalk\ApbctWP\Activator; use Cleantalk\ApbctWP\AdminNotices; use Cleantalk\ApbctWP\Antispam\EmailEncoder; @@ -366,6 +367,9 @@ function apbct_alt_session__save__WP_AJAX() require_once(CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-integrations-by-hook.php'); require_once(CLEANTALK_PLUGIN_DIR . 'inc/cleantalk-integrations-by-class.php'); +// Form protection by shortcode +new ProtectByShortcode(); + // WP Delicious integration add_filter('delicious_recipes_process_registration_errors', 'apbct_wp_delicious', 10, 4); diff --git a/lib/Cleantalk/Antispam/ProtectByShortcode.php b/lib/Cleantalk/Antispam/ProtectByShortcode.php new file mode 100644 index 00000000..46de7de1 --- /dev/null +++ b/lib/Cleantalk/Antispam/ProtectByShortcode.php @@ -0,0 +1,64 @@ +settings['forms__contact_forms_test'] === 1) { + add_filter('ct_wordpress_protect_from_spam', array($this, 'protectByShortcode'), 10, 2); + } + } + + /** + * A function to protect the data of $_POST, $_GET custom forms by the ct_wordpress_protect_from_spam hook. + * Returns an array with the result of the check. Also, if $options['redirect_to_block_page'] = 1 is passed, + * a redirect will be made to the blocking page. + * @param array $data + * @param array $options + * @return array + * @psalm-suppress PossiblyUnusedReturnValue + */ + public function protectByShortcode($data, $options = []) + { + $output = [ + 'is_spam' => false, + 'message' => '', + ]; + + $input_array = apply_filters('apbct__filter_post', $data); + $data = ct_gfa($input_array); + + $base_call_data = array( + 'message' => ! empty($data['message']) ? json_encode($data['message']) : '', + 'sender_email' => ! empty($data['email']) ? $data['email'] : '', + 'sender_nickname' => ! empty($data['nickname']) ? $data['nickname'] : '', + 'event_token' => ! empty($data['event_token']) ? $data['event_token'] : '', + 'post_info' => array( + 'post_url' => Server::get('HTTP_REFERER'), + ), + ); + $result = apbct_base_call($base_call_data, false); + $ct_result = isset($result['ct_result']) ? $result['ct_result'] : null; + + $is_spam = $ct_result !== null && $ct_result->allow !== 1; + + if (isset($options['redirect_to_block_page']) && $options['redirect_to_block_page'] && $is_spam) { + wp_die(isset($ct_result->comment) ? $ct_result->comment : __('Blocked by CleanTalk'), __('Forbidden'), array('response' => 403)); + } + + if ($is_spam) { + $output = [ + 'is_spam' => true, + 'message' => $ct_result->comment, + ]; + } + + return $output; + } +}