Skip to content

Commit ea4bb9e

Browse files
authored
Merge pull request #32 from erikyo/0.4.4
version 0.4.4
2 parents 04a98b4 + 7d3812c commit ea4bb9e

18 files changed

+2620
-831
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ root = true
88

99
[*]
1010
charset = utf-8
11-
end_of_line = lf
11+
end_of_line = crlf
1212
insert_final_newline = true
1313
trim_trailing_whitespace = true
1414
indent_style = tab

.github/workflows/build.yml

-23
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,3 @@ jobs:
3232
with:
3333
reporter: github-check
3434
eslint_flags: -c .eslintrc.js --ext .js --fix admin/src/ includes/src/
35-
36-
37-
e2e:
38-
name: E2E Tests
39-
runs-on: ubuntu-latest
40-
41-
steps:
42-
- uses: actions/checkout@v3
43-
44-
- name: Setup environment to use the desired version of NodeJS
45-
uses: actions/setup-node@v3
46-
with:
47-
node-version: 16
48-
cache: npm
49-
50-
- name: Installing NPM dependencies
51-
run: npm install
52-
53-
- name: Starting the WordPress Environment
54-
run: npm run wp-env:start
55-
56-
- name: Running the tests
57-
run: npm run test:e2e

.github/workflows/tests.yml

+42
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,45 @@ jobs:
4242

4343
- name: Run PHP Unit tests
4444
run: composer run test
45+
46+
47+
test-e2e:
48+
runs-on: ubuntu-latest
49+
50+
strategy:
51+
matrix:
52+
node-version: [14.x, 16.x, 18.x]
53+
wp: [ '6.0', 'latest' ]
54+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
55+
56+
steps:
57+
- uses: actions/checkout@v3
58+
59+
- name: Setup PHP
60+
uses: shivammathur/setup-php@v2
61+
with:
62+
php-version: 7.4
63+
tools: composer
64+
extensions: mysql
65+
coverage: none
66+
67+
- name: Install dependencies
68+
run: composer install --no-interaction --no-progress
69+
70+
- name: Use Node.js ${{ matrix.node-version }}
71+
uses: actions/setup-node@v3
72+
with:
73+
node-version: ${{ matrix.node-version }}
74+
cache: 'npm'
75+
76+
- name: Installing NPM dependencies
77+
run: npm install
78+
79+
- name: Build
80+
run: npm run build
81+
82+
- name: Starting the WordPress Environment
83+
run: npm run wp-env:start
84+
85+
- name: Running e2e tests
86+
run: npm run test:e2e

.wp-env.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"phpVersion": "7.4",
32
"core": "WordPress/WordPress",
43
"plugins": [
54
".",

admin/admin-customizations.php

+40-20
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function cf7a_options_init() {
273273
/* Settings check_bad_ip */
274274
add_settings_field(
275275
'check_bad_ip',
276-
__( 'IP Address', 'cf7-antispam' ),
276+
__( 'Check Bad IP Address', 'cf7-antispam' ),
277277
array( $this, 'cf7a_check_bad_ip_callback' ),
278278
'cf7a-settings',
279279
'cf7a_bad_ip'
@@ -920,6 +920,43 @@ public function cf7a_enable_geo( $enabled ) {
920920
}
921921
}
922922

923+
/**
924+
* Handles WP-cron task registrations
925+
*
926+
* @param array $input - The post input values.
927+
* @param string $input_name - The value of the input field.
928+
* @param string $cron_task - The slug of the Post value.
929+
* @param array $schedule - The schedules list obtained with wp_get_schedules().
930+
*
931+
* @return array|false the new value that the user has selected
932+
*/
933+
private function cf7a_input_cron_schedule( $input, $input_name, $cron_task, $schedule ) {
934+
$new_value = false;
935+
936+
if ( ! empty( $input[$input_name] ) && in_array( $input[$input_name], array_keys( $schedule ), true ) ) {
937+
if ( $this->options[$input_name] !== $input[$input_name] ) {
938+
$new_value = $input[$input_name];
939+
/* delete previous scheduled events */
940+
$timestamp = wp_next_scheduled( $cron_task );
941+
if ( $timestamp ) {
942+
wp_clear_scheduled_hook( $cron_task );
943+
}
944+
945+
/* add the new scheduled event */
946+
wp_schedule_event( time() + $schedule[ $new_value ]['interval'], $new_value, $cron_task );
947+
}
948+
} else {
949+
/* Get the timestamp for the next event. */
950+
$timestamp = wp_next_scheduled( $cron_task );
951+
if ( $timestamp ) {
952+
wp_clear_scheduled_hook( $cron_task );
953+
}
954+
$new_value[$input_name] = 'disabled';
955+
}
956+
return $new_value;
957+
}
958+
959+
923960
/**
924961
* Sanitize each setting field as needed
925962
*
@@ -975,26 +1012,9 @@ public function cf7a_sanitize_options( $input ) {
9751012

9761013
/* auto-unban delay */
9771014
$schedule = wp_get_schedules();
978-
if ( ! empty( $input['unban_after'] ) && in_array( $input['unban_after'], array_keys( $schedule ), true ) ) {
979-
if ( $this->options['unban_after'] !== $input['unban_after'] ) {
980-
$new_input['unban_after'] = $input['unban_after'];
981-
/* delete previous scheduled events */
982-
$timestamp = wp_next_scheduled( 'cf7a_cron' );
983-
if ( $timestamp ) {
984-
wp_clear_scheduled_hook( 'cf7a_cron' );
985-
}
9861015

987-
/* add the new scheduled event */
988-
wp_schedule_event( time() + $schedule[ $new_input['unban_after'] ]['interval'], $new_input['unban_after'], 'cf7a_cron' );
989-
}
990-
} else {
991-
/* Get the timestamp for the next event. */
992-
$timestamp = wp_next_scheduled( 'cf7a_cron' );
993-
if ( $timestamp ) {
994-
wp_clear_scheduled_hook( 'cf7a_cron' );
995-
}
996-
$new_input['unban_after'] = 'disabled';
997-
}
1016+
/* unban after */
1017+
$new_input['unban_after'] = $this->cf7a_input_cron_schedule( $input, 'unban_after', 'cf7a_cron', $schedule );
9981018

9991019
/* bad ip */
10001020
$new_input['check_refer'] = isset( $input['check_refer'] ) ? 1 : 0;

admin/admin-tools.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,17 @@ public function cf7a_handle_actions() {
7575

7676
$filter = new CF7_AntiSpam_Filters();
7777

78+
$plugin_options = CF7_AntiSpam::get_options();
79+
7880
$ban_id = intval( substr( $action, 12 ) );
7981
$ban_ip = $filter->cf7a_blacklist_get_id( $ban_id );
8082

81-
if ( $ban_ip ) {
82-
if ( CF7_AntiSpam::update_plugin_option( 'bad_ip_list', array( $ban_ip->ip ) ) ) {
83+
if ( $ban_ip && !empty($plugin_options) ) {
84+
85+
if ( CF7_AntiSpam::update_plugin_option( 'bad_ip_list', array_merge( $plugin_options['bad_ip_list'], array( $ban_ip->ip ) ) ) ) {
8386
$filter->cf7a_unban_by_id( $ban_id );
8487
}
88+
8589
self::cf7a_push_notice(
8690
sprintf(
8791
/* translators: the %1$s is the user id and %2$s is the ip address. */
@@ -90,6 +94,7 @@ public function cf7a_handle_actions() {
9094
! empty( $ban_ip->ip ) ? $ban_ip->ip : 'not available'
9195
)
9296
);
97+
9398
} else {
9499
self::cf7a_push_notice(
95100
sprintf(

admin/dist/admin-scripts.asset.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array(), 'version' => '31f79903e33e38d0926a');
1+
<?php return array('dependencies' => array(), 'version' => '55a0a782bc47ae12f770');

babel.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = ( api ) => {
2+
api.cache( true );
3+
4+
return {
5+
presets: [ '@wordpress/babel-preset-default' ],
6+
};
7+
};

cf7-antispam.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Author: Codekraft
66
* Text Domain: cf7-antispam
77
* Domain Path: /languages/
8-
* Version: 0.4.3
8+
* Version: 0.4.4
99
*
1010
* @package cf7-antispam
1111
*/

composer.json

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,11 @@
1515
"php": ">=7.2",
1616
"friendsofphp/php-cs-fixer": "3.*",
1717
"squizlabs/php_codesniffer": "^3",
18-
"phpmd/phpmd" : "@stable",
19-
"phpstan/phpstan": "^1.8",
2018
"phpcompatibility/php-compatibility": "*",
2119
"wp-coding-standards/wpcs": "^2.3",
2220
"automattic/vipwpcs": "^2.3",
2321
"phpcompatibility/phpcompatibility-wp": "*",
24-
"slevomat/coding-standard": "^7.2",
25-
"phpstan/extension-installer": "^1.2",
26-
"szepeviktor/phpstan-wordpress": "^1.1",
27-
"vimeo/psalm": "^4.29",
28-
"phpunit/phpunit": "^7",
22+
"phpunit/phpunit": "^8",
2923
"wp-phpunit/wp-phpunit": "^5.9.5",
3024
"yoast/phpunit-polyfills": "*",
3125
"dealerdirect/phpcodesniffer-composer-installer": "^0.7"

includes/cf7a-antispam-filters.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ public function cf7a_spam_filter( $spam ) {
328328

329329
/* client referer */
330330
$cf7a_referer = isset( $_POST[ $prefix . 'referer' ] ) ? cf7a_decrypt( sanitize_text_field( wp_unslash( $_POST[ $prefix . 'referer' ] ) ), $options['cf7a_cipher'] ) : false;
331+
$cf7a_protocol = isset( $_POST[ $prefix . 'protocol' ] ) ? cf7a_decrypt( sanitize_text_field( wp_unslash( $_POST[ $prefix . 'protocol' ] ) ), $options['cf7a_cipher'] ) : false;
331332

332333
/* CF7 user agent */
333334
$user_agent = sanitize_text_field( $submission->get_meta( 'user_agent' ) );
@@ -490,6 +491,16 @@ public function cf7a_spam_filter( $spam ) {
490491
}
491492
}
492493

494+
if ( $cf7a_protocol ) {
495+
if ( in_array($cf7a_protocol, array('HTTP/1.0', 'HTTP/1.1','HTTP/1.2') ) ) {
496+
497+
$spam_score += $score_warn;
498+
$reason['no_protocol'] = 'client has a bot-like connection protocol';
499+
500+
cf7a_log( "the $remote_ip has a bot-like connection protocol (HTTP/1.X)", 1 );
501+
}
502+
}
503+
493504
/**
494505
* Check the CF7 AntiSpam version field
495506
*/
@@ -854,22 +865,23 @@ public function cf7a_spam_filter( $spam ) {
854865
*/
855866
if ( $options['check_honeypot'] ) {
856867

857-
/* we need only the text tags of the form */
868+
/* collect the input "name" value of the type="text" tags of the submitted form */
858869
foreach ( $mail_tags as $mail_tag ) {
859870
if ( 'text' === $mail_tag['type'] || 'text*' === $mail_tag['type'] ) {
860871
$mail_tag_text[] = $mail_tag['name'];
861872
}
862873
}
863874

864-
if ( isset( $mail_tag_text ) ) {
875+
if ( ! empty( $mail_tag_text ) ) {
865876

866-
/* faked input name used into honeypots */
877+
/* get the collection of the generated (fake) input name used as honeypots name value */
867878
$input_names = get_honeypot_input_names( $options['honeypot_input_names'] );
868879

869880
$mail_tag_count = count( $input_names );
870881

871882
for ( $i = 0; $i < $mail_tag_count; $i ++ ) {
872883

884+
/* check if any posted input name value has a name from the honeypot names array, if yes the bot has fallen into the trap and filled the input */
873885
$has_honeypot = ! empty( $_POST[ $input_names[ $i ] ] );
874886

875887
/* check only if it's set and if it is different from "" */

includes/cf7a-frontend.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function cf7a_honeypot_add( $form_elements ) {
9595
*
9696
* @since 0.4.3
9797
*/
98-
$max_replacements = intval( apply_filters( 'cf7a_additional_max_honeypots', 5 ) );
98+
$max_replacements = min( intval( apply_filters( 'cf7a_additional_max_honeypots', 5 ) ), count( $input_names ) );
9999

100100
/* get the inputs data */
101101
if ( $inputs && $inputs->length > 0 ) {
@@ -280,12 +280,14 @@ public function cf7a_add_hidden_fields( $fields ) {
280280

281281
/* add the default hidden fields */
282282
$referrer = ! empty( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : false;
283+
$protocol = ! empty( $_SERVER['SERVER_PROTOCOL'] ) ? esc_url_raw( wp_unslash( $_SERVER['SERVER_PROTOCOL'] ) ) : false;
283284
return array_merge(
284285
$fields,
285286
array(
286287
$prefix . 'version' => '1.0',
287288
$prefix . 'address' => cf7a_crypt( cf7a_get_real_ip(), $this->options['cf7a_cipher'] ),
288289
$prefix . 'referer' => cf7a_crypt( $referrer ? $referrer : 'no referer', $this->options['cf7a_cipher'] ),
290+
$prefix . 'protocol' => cf7a_crypt( $protocol ? $protocol : 'protocol missing', $this->options['cf7a_cipher'] ),
289291
)
290292
);
291293
}

includes/cf7a-functions.php

+22-23
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,28 @@ function ( $res, $el ) {
109109
);
110110
}
111111

112+
/**
113+
* It adds two new cron schedules to WordPress
114+
*
115+
* @param array $schedules This is the name of the hook that we're adding a schedule to.
116+
*/
117+
function cf7a_add_cron_steps( $schedules ) {
118+
return array_merge(
119+
$schedules,
120+
array(
121+
'5min' => array(
122+
'interval' => 300,
123+
'display' => __( 'Every 5 Minutes', 'cf7-antispam' ),
124+
),
125+
'60sec' => array(
126+
'interval' => 60,
127+
'display' => __( 'Every 60 seconds', 'cf7-antispam' ),
128+
),
129+
)
130+
);
131+
}
132+
add_filter( 'cron_schedules', 'cf7a_add_cron_steps' );
133+
112134
/**
113135
* It adds a bunch of common honeypot input names to the list of honeypot input names
114136
*
@@ -140,29 +162,6 @@ function get_honeypot_input_names( $custom_names = array() ) {
140162
);
141163
}
142164

143-
144-
/**
145-
* It adds two new cron schedules to WordPress
146-
*
147-
* @param array $schedules This is the name of the hook that we're adding a schedule to.
148-
*/
149-
function cf7a_add_cron_steps( $schedules ) {
150-
return array_merge(
151-
$schedules,
152-
array(
153-
'5min' => array(
154-
'interval' => 300,
155-
'display' => __( 'Every 5 Minutes', 'cf7-antispam' ),
156-
),
157-
'60sec' => array(
158-
'interval' => 60,
159-
'display' => __( 'Every 60 seconds', 'cf7-antispam' ),
160-
),
161-
)
162-
);
163-
}
164-
add_filter( 'cron_schedules', 'cf7a_add_cron_steps' );
165-
166165
/**
167166
* It encrypts a string using the WordPress salt as the key
168167
*

includes/dist/script.asset.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array(), 'version' => 'f679f15b63f8cb923e61');
1+
<?php return array('dependencies' => array(), 'version' => '269fbb9de6b1349e0e67');

0 commit comments

Comments
 (0)