Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions cleantalk.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,6 @@ function apbct_write_js_errors($data)

// Public actions
if ( ! is_admin() && ! apbct_is_ajax() && ! apbct_is_customize_preview() ) {
// Default search
add_filter('get_search_query', 'apbct_forms__search__testSpam');
add_action('wp_head', 'apbct_search_add_noindex', 1);

if (apbct_is_plugin_active('fluentformpro/fluentformpro.php') && apbct_is_in_uri('ff_landing=')) {
add_action('wp_head', function () {
echo '<script data-pagespeed-no-defer="" src="'
Expand Down
5 changes: 5 additions & 0 deletions inc/cleantalk-integrations-by-class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
'plugin_path' => 'woocommerce/woocommerce.php',
'plugin_class' => 'WooCommerce',
),
'WPSearchForm' => array(
'plugin_path' => '',
'plugin_class' => '',
'wp_includes' => true,
),
);

add_action('plugins_loaded', function () use ($apbct_integrations_by_class) {
Expand Down
111 changes: 0 additions & 111 deletions inc/cleantalk-public-integrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,70 +206,6 @@ function ct_woocommerce_wishlist_check($args)
return $args;
}


/**
* Test default search string for spam
*
* @param $search string
*
* @return string
*/
function apbct_forms__search__testSpam($search)
{
global $apbct, $cleantalk_executed;

if (
empty($search) ||
$cleantalk_executed ||
$apbct->settings['forms__search_test'] == 0 ||
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
) {
do_action('apbct_skipped_request', __FILE__ . ' -> ' . __FUNCTION__ . '():' . __LINE__, $_POST);

return $search;
}

$user = apbct_is_user_logged_in() ? wp_get_current_user() : null;

$base_call_result = apbct_base_call(
array(
'message' => $search,
'sender_email' => $user !== null ? $user->user_email : null,
'sender_nickname' => $user !== null ? $user->user_login : null,
'post_info' => array('comment_type' => 'site_search_wordpress'),
'exception_action' => 0,
)
);

if ( isset($base_call_result['ct_result']) ) {
$ct_result = $base_call_result['ct_result'];

$cleantalk_executed = true;

if ( $ct_result->allow == 0 ) {
die($ct_result->comment);
}
}

return $search;
}

function apbct_search_add_noindex()
{
global $apbct;

if (
! is_search() || // If it is search results
$apbct->settings['forms__search_test'] == 0 ||
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
) {
return;
}

echo '<!-- meta by CleanTalk Anti-Spam Protection plugin -->' . "\n";
echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
}

/**
* Public function - Tests for Pirate contact forms
* return NULL
Expand Down Expand Up @@ -3446,53 +3382,6 @@ function apbct_form_happyforms_test_spam($is_valid, $request, $_form)
return $is_valid;
}

/**
* Prepare data to add honeypot to the WordPress default search form.
* Fires ct_add_honeypot_field() on hook get_search_form when:
* - method of the form is post
* - spam test of search form is enabled
*
* @param string $form_html
* @return string
*/
function apbct_form_search__add_fields($form_html)
{
global $apbct;

if ( !empty($form_html) && is_string($form_html) && $apbct->settings['forms__search_test'] == 1 ) {
// extract method of the form with DOMDocument
if ( class_exists('DOMDocument') ) {
libxml_use_internal_errors(true);
$dom = new DOMDocument();
if ( @$dom->loadHTML($form_html) ) {
$search_form_dom = $dom->getElementById('searchform');
if ( !empty($search_form_dom) ) {
$method = empty($search_form_dom->getAttribute('method'))
//default method is get for any form if no method specified
? 'get'
: $search_form_dom->getAttribute('method');
}
}
libxml_clear_errors();
unset($dom);
}

// retry extract method of the form with regex
if ( empty($method) ) {
preg_match('/form.*method="(.*?)"/', $form_html, $matches);
$method = empty($matches[1])
? 'get'
: trim($matches[1]);
}

$form_method = strtolower($method);

return str_replace('</form>', Honeypot::generateHoneypotField('search_form', $form_method) . '</form>', $form_html);
}

return $form_html;
}

/**
* Advanced Classifieds & Directory Pro
*
Expand Down
5 changes: 0 additions & 5 deletions inc/cleantalk-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ function apbct_init()
add_filter('rocket_delay_js_exclusions', 'apbct_rocket_delay_js_exclusions');
}

//Search form hook init
if ( $apbct->settings['forms__search_test'] ) {
add_filter('get_search_form', 'apbct_form_search__add_fields', 999);
}

//fix for EPM registration form
if ( Post::get('reg_email') && shortcode_exists('epm_registration_form') ) {
unset($_POST['ct_checkjs_register_form']);
Expand Down
19 changes: 13 additions & 6 deletions lib/Cleantalk/Antispam/IntegrationsByClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Cleantalk\Antispam;

use Cleantalk\Antispam\IntegrationsByClass\IntegrationByClassBase;

class IntegrationsByClass
{
/**
Expand All @@ -27,20 +29,25 @@ public function __construct($integrations)

foreach ($this->integrations as $integration_name => $integration_info) {
// pre-check to skip integration by plugin path
if ( isset($integration_info['plugin_path']) && !$this->isPluginActive($integration_info['plugin_path']) ) {
continue;
}
if (!isset($integration_info['wp_includes'])) {
if ( isset($integration_info['plugin_path']) && !$this->isPluginActive($integration_info['plugin_path']) ) {
continue;
}

// pre-check to skip integration by plugin class
if ( isset($integration_info['plugin_class']) && !class_exists($integration_info['plugin_class']) ) {
continue;
// pre-check to skip integration by plugin class
if ( isset($integration_info['plugin_class']) && !class_exists($integration_info['plugin_class']) ) {
continue;
}
}

$class = '\\Cleantalk\\Antispam\\IntegrationsByClass\\' . $integration_name;
if (!class_exists($class)) {
continue;
}

/**
* @var IntegrationByClassBase $integration
*/
$integration = new $class();

// Ajax work
Expand Down
165 changes: 165 additions & 0 deletions lib/Cleantalk/Antispam/IntegrationsByClass/WPSearchForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Cleantalk\Antispam\IntegrationsByClass;

use Cleantalk\ApbctWP\Escape;
use Cleantalk\ApbctWP\Variables\Post;
use Cleantalk\ApbctWP\Variables\Server;
use Cleantalk\Common\TT;
use Cleantalk\ApbctWP\Sanitize;
use Cleantalk\ApbctWP\Variables\Cookie;
use Cleantalk\ApbctWP\State;
use Cleantalk\ApbctWP\Honeypot;
use DOMDocument;

/**
* @psalm-suppress UnusedClass
*/
class WPSearchForm extends IntegrationByClassBase
{
/**
* @return void
* @psalm-suppress PossiblyUnusedMethod
*/
public function doAjaxWork()
{
}

/**
* @return void
* @psalm-suppress PossiblyUnusedMethod
*/
public function doPublicWork()
{
global $apbct;
if ( $apbct->settings['forms__search_test'] ) {
add_filter('get_search_form', array($this, 'apbctFormSearchAddFields'), 999);
}
if ( ! is_admin() && ! apbct_is_ajax() && ! apbct_is_customize_preview() ) {
// Default search
add_filter('get_search_query', array($this, 'testSpam'));
add_action('wp_head', array($this, 'addNoindex'), 1);
}
}

/**
* @return void
* @psalm-suppress PossiblyUnusedMethod
*/
public function doAdminWork()
{
}

/**
* Prepare data to add honeypot to the WordPress default search form.
* Fires ct_add_honeypot_field() on hook get_search_form when:
* - method of the form is post
* - spam test of search form is enabled
*
* @param string $form_html
* @return string
*/
public function apbctFormSearchAddFields($form_html)
{
global $apbct;

if ( !empty($form_html) && is_string($form_html) && $apbct->settings['forms__search_test'] == 1 ) {
// extract method of the form with DOMDocument
if ( class_exists('DOMDocument') ) {
libxml_use_internal_errors(true);
$dom = new DOMDocument();
if ( @$dom->loadHTML($form_html) ) {
$search_form_dom = $dom->getElementById('searchform');
if ( !empty($search_form_dom) ) {
$method = empty($search_form_dom->getAttribute('method'))
//default method is get for any form if no method specified
? 'get'
: $search_form_dom->getAttribute('method');
}
}
libxml_clear_errors();
unset($dom);
}

// retry extract method of the form with regex
if ( empty($method) ) {
preg_match('/form.*method="(.*?)"/', $form_html, $matches);
$method = empty($matches[1])
? 'get'
: trim($matches[1]);
}
$form_method = strtolower($method);

$resalt = str_replace('</form>', Honeypot::generateHoneypotField('search_form', $form_method) . '</form>', $form_html);
return $resalt;
}

return $form_html;
}

/**
* Test default search string for spam
*
* @param string $search
*
* @return string
*/
public function testSpam($search)
{
global $apbct, $cleantalk_executed;

if (
empty($search) ||
$cleantalk_executed ||
$apbct->settings['forms__search_test'] == 0 ||
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
) {
do_action('apbct_skipped_request', __FILE__ . ' -> ' . __FUNCTION__ . '():' . __LINE__, $_POST);
return $search;
}

$user = apbct_is_user_logged_in() ? wp_get_current_user() : null;

$data = array(
'message' => $search,
'sender_email' => $user !== null ? $user->user_email : null,
'sender_nickname' => $user !== null ? $user->user_login : null,
'post_info' => array('comment_type' => 'site_search_wordpress'),
'exception_action' => 0,
);

$base_call_result = apbct_base_call($data);

if ( isset($base_call_result['ct_result']) ) {
$ct_result = $base_call_result['ct_result'];

$cleantalk_executed = true;

if ( $ct_result->allow == 0 ) {
die($ct_result->comment);
}
}

return $search;
}

/**
* Add no-index meta to the page of search results
* @return void
*/
public function addNoindex()
{
global $apbct;

if (
! is_search() || // If it is search results
$apbct->settings['forms__search_test'] == 0 ||
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
) {
return;
}

echo '<!-- meta by CleanTalk Anti-Spam Protection plugin -->' . "\n";
echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
}
}
6 changes: 5 additions & 1 deletion lib/Cleantalk/ApbctWP/Honeypot.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ private static function getHoneypotFilledFields()

// AltSessions way to collect search forms honeypot
if ( $apbct->settings['forms__search_test'] ) {
$honeypot_potential_values['apbct__email_id__search_form'] = AltSessions::get("apbct_search_form__honeypot_value");
$alt_session_data = AltSessions::get("apbct_search_form__honeypot_value");
if (!empty($alt_session_data)) {
$honeypot_potential_values['apbct__email_id__search_form'] = $alt_session_data;
$hp_exists = true;
}
}

// if source is filled then pass them to params as additional fields
Expand Down
Loading