Skip to content

Commit 819caec

Browse files
Merge pull request #602 from CleanTalk/ref_wp_search_av
Form Search Refactoring
2 parents 05c1b43 + 0c875bc commit 819caec

8 files changed

Lines changed: 199 additions & 136 deletions

File tree

cleantalk.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,6 @@ function apbct_write_js_errors($data)
600600

601601
// Public actions
602602
if ( ! is_admin() && ! apbct_is_ajax() && ! apbct_is_customize_preview() ) {
603-
// Default search
604-
add_filter('get_search_query', 'apbct_forms__search__testSpam');
605-
add_action('wp_head', 'apbct_search_add_noindex', 1);
606-
607603
if (apbct_is_plugin_active('fluentformpro/fluentformpro.php') && apbct_is_in_uri('ff_landing=')) {
608604
add_action('wp_head', function () {
609605
echo '<script data-pagespeed-no-defer="" src="'

inc/cleantalk-integrations-by-class.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
'plugin_path' => 'woocommerce/woocommerce.php',
1010
'plugin_class' => 'WooCommerce',
1111
),
12+
'WPSearchForm' => array(
13+
'plugin_path' => '',
14+
'plugin_class' => '',
15+
'wp_includes' => true,
16+
),
1217
);
1318

1419
add_action('plugins_loaded', function () use ($apbct_integrations_by_class) {

inc/cleantalk-public-integrations.php

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -206,70 +206,6 @@ function ct_woocommerce_wishlist_check($args)
206206
return $args;
207207
}
208208

209-
210-
/**
211-
* Test default search string for spam
212-
*
213-
* @param $search string
214-
*
215-
* @return string
216-
*/
217-
function apbct_forms__search__testSpam($search)
218-
{
219-
global $apbct, $cleantalk_executed;
220-
221-
if (
222-
empty($search) ||
223-
$cleantalk_executed ||
224-
$apbct->settings['forms__search_test'] == 0 ||
225-
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
226-
) {
227-
do_action('apbct_skipped_request', __FILE__ . ' -> ' . __FUNCTION__ . '():' . __LINE__, $_POST);
228-
229-
return $search;
230-
}
231-
232-
$user = apbct_is_user_logged_in() ? wp_get_current_user() : null;
233-
234-
$base_call_result = apbct_base_call(
235-
array(
236-
'message' => $search,
237-
'sender_email' => $user !== null ? $user->user_email : null,
238-
'sender_nickname' => $user !== null ? $user->user_login : null,
239-
'post_info' => array('comment_type' => 'site_search_wordpress'),
240-
'exception_action' => 0,
241-
)
242-
);
243-
244-
if ( isset($base_call_result['ct_result']) ) {
245-
$ct_result = $base_call_result['ct_result'];
246-
247-
$cleantalk_executed = true;
248-
249-
if ( $ct_result->allow == 0 ) {
250-
die($ct_result->comment);
251-
}
252-
}
253-
254-
return $search;
255-
}
256-
257-
function apbct_search_add_noindex()
258-
{
259-
global $apbct;
260-
261-
if (
262-
! is_search() || // If it is search results
263-
$apbct->settings['forms__search_test'] == 0 ||
264-
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
265-
) {
266-
return;
267-
}
268-
269-
echo '<!-- meta by CleanTalk Anti-Spam Protection plugin -->' . "\n";
270-
echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
271-
}
272-
273209
/**
274210
* Public function - Tests for Pirate contact forms
275211
* return NULL
@@ -3446,53 +3382,6 @@ function apbct_form_happyforms_test_spam($is_valid, $request, $_form)
34463382
return $is_valid;
34473383
}
34483384

3449-
/**
3450-
* Prepare data to add honeypot to the WordPress default search form.
3451-
* Fires ct_add_honeypot_field() on hook get_search_form when:
3452-
* - method of the form is post
3453-
* - spam test of search form is enabled
3454-
*
3455-
* @param string $form_html
3456-
* @return string
3457-
*/
3458-
function apbct_form_search__add_fields($form_html)
3459-
{
3460-
global $apbct;
3461-
3462-
if ( !empty($form_html) && is_string($form_html) && $apbct->settings['forms__search_test'] == 1 ) {
3463-
// extract method of the form with DOMDocument
3464-
if ( class_exists('DOMDocument') ) {
3465-
libxml_use_internal_errors(true);
3466-
$dom = new DOMDocument();
3467-
if ( @$dom->loadHTML($form_html) ) {
3468-
$search_form_dom = $dom->getElementById('searchform');
3469-
if ( !empty($search_form_dom) ) {
3470-
$method = empty($search_form_dom->getAttribute('method'))
3471-
//default method is get for any form if no method specified
3472-
? 'get'
3473-
: $search_form_dom->getAttribute('method');
3474-
}
3475-
}
3476-
libxml_clear_errors();
3477-
unset($dom);
3478-
}
3479-
3480-
// retry extract method of the form with regex
3481-
if ( empty($method) ) {
3482-
preg_match('/form.*method="(.*?)"/', $form_html, $matches);
3483-
$method = empty($matches[1])
3484-
? 'get'
3485-
: trim($matches[1]);
3486-
}
3487-
3488-
$form_method = strtolower($method);
3489-
3490-
return str_replace('</form>', Honeypot::generateHoneypotField('search_form', $form_method) . '</form>', $form_html);
3491-
}
3492-
3493-
return $form_html;
3494-
}
3495-
34963385
/**
34973386
* Advanced Classifieds & Directory Pro
34983387
*

inc/cleantalk-public.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ function apbct_init()
4747
add_filter('rocket_delay_js_exclusions', 'apbct_rocket_delay_js_exclusions');
4848
}
4949

50-
//Search form hook init
51-
if ( $apbct->settings['forms__search_test'] ) {
52-
add_filter('get_search_form', 'apbct_form_search__add_fields', 999);
53-
}
54-
5550
//fix for EPM registration form
5651
if ( Post::get('reg_email') && shortcode_exists('epm_registration_form') ) {
5752
unset($_POST['ct_checkjs_register_form']);

lib/Cleantalk/Antispam/IntegrationsByClass.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Cleantalk\Antispam;
44

5+
use Cleantalk\Antispam\IntegrationsByClass\IntegrationByClassBase;
6+
57
class IntegrationsByClass
68
{
79
/**
@@ -27,20 +29,25 @@ public function __construct($integrations)
2729

2830
foreach ($this->integrations as $integration_name => $integration_info) {
2931
// pre-check to skip integration by plugin path
30-
if ( isset($integration_info['plugin_path']) && !$this->isPluginActive($integration_info['plugin_path']) ) {
31-
continue;
32-
}
32+
if (!isset($integration_info['wp_includes'])) {
33+
if ( isset($integration_info['plugin_path']) && !$this->isPluginActive($integration_info['plugin_path']) ) {
34+
continue;
35+
}
3336

34-
// pre-check to skip integration by plugin class
35-
if ( isset($integration_info['plugin_class']) && !class_exists($integration_info['plugin_class']) ) {
36-
continue;
37+
// pre-check to skip integration by plugin class
38+
if ( isset($integration_info['plugin_class']) && !class_exists($integration_info['plugin_class']) ) {
39+
continue;
40+
}
3741
}
3842

3943
$class = '\\Cleantalk\\Antispam\\IntegrationsByClass\\' . $integration_name;
4044
if (!class_exists($class)) {
4145
continue;
4246
}
4347

48+
/**
49+
* @var IntegrationByClassBase $integration
50+
*/
4451
$integration = new $class();
4552

4653
// Ajax work
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
namespace Cleantalk\Antispam\IntegrationsByClass;
4+
5+
use Cleantalk\ApbctWP\Escape;
6+
use Cleantalk\ApbctWP\Variables\Post;
7+
use Cleantalk\ApbctWP\Variables\Server;
8+
use Cleantalk\Common\TT;
9+
use Cleantalk\ApbctWP\Sanitize;
10+
use Cleantalk\ApbctWP\Variables\Cookie;
11+
use Cleantalk\ApbctWP\State;
12+
use Cleantalk\ApbctWP\Honeypot;
13+
use DOMDocument;
14+
15+
/**
16+
* @psalm-suppress UnusedClass
17+
*/
18+
class WPSearchForm extends IntegrationByClassBase
19+
{
20+
/**
21+
* @return void
22+
* @psalm-suppress PossiblyUnusedMethod
23+
*/
24+
public function doAjaxWork()
25+
{
26+
}
27+
28+
/**
29+
* @return void
30+
* @psalm-suppress PossiblyUnusedMethod
31+
*/
32+
public function doPublicWork()
33+
{
34+
global $apbct;
35+
if ( $apbct->settings['forms__search_test'] ) {
36+
add_filter('get_search_form', array($this, 'apbctFormSearchAddFields'), 999);
37+
}
38+
if ( ! is_admin() && ! apbct_is_ajax() && ! apbct_is_customize_preview() ) {
39+
// Default search
40+
add_filter('get_search_query', array($this, 'testSpam'));
41+
add_action('wp_head', array($this, 'addNoindex'), 1);
42+
}
43+
}
44+
45+
/**
46+
* @return void
47+
* @psalm-suppress PossiblyUnusedMethod
48+
*/
49+
public function doAdminWork()
50+
{
51+
}
52+
53+
/**
54+
* Prepare data to add honeypot to the WordPress default search form.
55+
* Fires ct_add_honeypot_field() on hook get_search_form when:
56+
* - method of the form is post
57+
* - spam test of search form is enabled
58+
*
59+
* @param string $form_html
60+
* @return string
61+
*/
62+
public function apbctFormSearchAddFields($form_html)
63+
{
64+
global $apbct;
65+
66+
if ( !empty($form_html) && is_string($form_html) && $apbct->settings['forms__search_test'] == 1 ) {
67+
// extract method of the form with DOMDocument
68+
if ( class_exists('DOMDocument') ) {
69+
libxml_use_internal_errors(true);
70+
$dom = new DOMDocument();
71+
if ( @$dom->loadHTML($form_html) ) {
72+
$search_form_dom = $dom->getElementById('searchform');
73+
if ( !empty($search_form_dom) ) {
74+
$method = empty($search_form_dom->getAttribute('method'))
75+
//default method is get for any form if no method specified
76+
? 'get'
77+
: $search_form_dom->getAttribute('method');
78+
}
79+
}
80+
libxml_clear_errors();
81+
unset($dom);
82+
}
83+
84+
// retry extract method of the form with regex
85+
if ( empty($method) ) {
86+
preg_match('/form.*method="(.*?)"/', $form_html, $matches);
87+
$method = empty($matches[1])
88+
? 'get'
89+
: trim($matches[1]);
90+
}
91+
$form_method = strtolower($method);
92+
93+
$resalt = str_replace('</form>', Honeypot::generateHoneypotField('search_form', $form_method) . '</form>', $form_html);
94+
return $resalt;
95+
}
96+
97+
return $form_html;
98+
}
99+
100+
/**
101+
* Test default search string for spam
102+
*
103+
* @param string $search
104+
*
105+
* @return string
106+
*/
107+
public function testSpam($search)
108+
{
109+
global $apbct, $cleantalk_executed;
110+
111+
if (
112+
empty($search) ||
113+
$cleantalk_executed ||
114+
$apbct->settings['forms__search_test'] == 0 ||
115+
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
116+
) {
117+
do_action('apbct_skipped_request', __FILE__ . ' -> ' . __FUNCTION__ . '():' . __LINE__, $_POST);
118+
return $search;
119+
}
120+
121+
$user = apbct_is_user_logged_in() ? wp_get_current_user() : null;
122+
123+
$data = array(
124+
'message' => $search,
125+
'sender_email' => $user !== null ? $user->user_email : null,
126+
'sender_nickname' => $user !== null ? $user->user_login : null,
127+
'post_info' => array('comment_type' => 'site_search_wordpress'),
128+
'exception_action' => 0,
129+
);
130+
131+
$base_call_result = apbct_base_call($data);
132+
133+
if ( isset($base_call_result['ct_result']) ) {
134+
$ct_result = $base_call_result['ct_result'];
135+
136+
$cleantalk_executed = true;
137+
138+
if ( $ct_result->allow == 0 ) {
139+
die($ct_result->comment);
140+
}
141+
}
142+
143+
return $search;
144+
}
145+
146+
/**
147+
* Add no-index meta to the page of search results
148+
* @return void
149+
*/
150+
public function addNoindex()
151+
{
152+
global $apbct;
153+
154+
if (
155+
! is_search() || // If it is search results
156+
$apbct->settings['forms__search_test'] == 0 ||
157+
($apbct->settings['data__protect_logged_in'] != 1 && is_user_logged_in()) // Skip processing for logged in users.
158+
) {
159+
return;
160+
}
161+
162+
echo '<!-- meta by CleanTalk Anti-Spam Protection plugin -->' . "\n";
163+
echo '<meta name="robots" content="noindex,nofollow" />' . "\n";
164+
}
165+
}

lib/Cleantalk/ApbctWP/Honeypot.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ private static function getHoneypotFilledFields()
117117

118118
// AltSessions way to collect search forms honeypot
119119
if ( $apbct->settings['forms__search_test'] ) {
120-
$honeypot_potential_values['apbct__email_id__search_form'] = AltSessions::get("apbct_search_form__honeypot_value");
120+
$alt_session_data = AltSessions::get("apbct_search_form__honeypot_value");
121+
if (!empty($alt_session_data)) {
122+
$honeypot_potential_values['apbct__email_id__search_form'] = $alt_session_data;
123+
$hp_exists = true;
124+
}
121125
}
122126

123127
// if source is filled then pass them to params as additional fields

0 commit comments

Comments
 (0)