Skip to content

Commit 493f5b4

Browse files
committed
Prepare anti-spam entry processing for CF7.
1 parent 5d2f4a8 commit 493f5b4

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/php/CF7/CF7.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ public function cf7_hcaptcha_shortcode( $attr = [] ): string {
170170
/**
171171
* Check rest nonce and remove it for not logged-in users.
172172
*
173-
* @param WP_Error|mixed $result Error from another authentication handler,
174-
* null if we should handle it, or another value if not.
173+
* @param WP_Error|mixed $result Error from another authentication handler.
174+
* It is null if we should handle it, or another value if not.
175175
*
176176
* @return WP_Error|mixed
177177
*/
@@ -226,8 +226,7 @@ public function verify_hcaptcha( $result, $tag ) {
226226
}
227227

228228
$data = $submission->get_posted_data();
229-
$response = $data['h-captcha-response'] ?? '';
230-
$captcha_result = API::verify_request( $response );
229+
$captcha_result = API::verify( $this->get_entry( $data ) );
231230

232231
if ( null !== $captcha_result ) {
233232
return $this->get_invalidated_result( $result, $captcha_result );
@@ -401,4 +400,43 @@ static function ( &$value, $key ) {
401400

402401
return str_replace( $cf7_hcap_shortcode, "[$updated_cf_hcap_sc]", $output );
403402
}
403+
404+
/**
405+
* Get entry.
406+
*
407+
* @param array $data Entry data.
408+
*
409+
* @return array
410+
*/
411+
private function get_entry( array $data ): array {
412+
$entry = [
413+
'data' => [],
414+
];
415+
416+
$submission = WPCF7_Submission::get_instance();
417+
418+
if ( ! $submission ) {
419+
return $entry;
420+
}
421+
422+
$contact_form = $submission->get_contact_form();
423+
$form_id = $contact_form->id();
424+
$post = get_post( $form_id );
425+
$entry['form_date_gmt'] = $post->post_modified_gmt ?? null;
426+
$form_tags = $contact_form->scan_form_tags();
427+
428+
foreach ( $form_tags as $form_tag ) {
429+
$type = $form_tag->type;
430+
$name = $form_tag->name;
431+
$value = $data[ $name ] ?? '';
432+
433+
if ( 'email' === $type ) {
434+
$entry['data']['email'] = $value;
435+
}
436+
437+
$entry['data'][ $name ] = $value;
438+
}
439+
440+
return $entry;
441+
}
404442
}

tests/php/integration/CF7/CF7Test.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ public function test_verify_hcaptcha(): void {
395395
$form_fields = [ $field ];
396396

397397
$contact_form = Mockery::mock( WPCF7_ContactForm::class );
398+
$contact_form->shouldReceive( 'id' )->andReturn( $wpcf7_id );
398399
$contact_form->shouldReceive( 'scan_form_tags' )->andReturn( $form_fields );
399400

400401
$submission = Mockery::mock( WPCF7_Submission::class );
@@ -515,12 +516,14 @@ public function test_verify_hcaptcha_without_mode_set(): void {
515516
*/
516517
public function test_verify_hcaptcha_without_posted_data(): void {
517518
$data = [];
519+
$wpcf7_id = 23;
518520
$hcaptcha_site_key = 'some site key';
519521
$field = Mockery::mock( WPCF7_FormTag::class );
520522
$field->type = 'some';
521523
$form_fields = [ $field ];
522524

523525
$contact_form = Mockery::mock( WPCF7_ContactForm::class );
526+
$contact_form->shouldReceive( 'id' )->andReturn( $wpcf7_id );
524527
$contact_form->shouldReceive( 'scan_form_tags' )->andReturn( $form_fields );
525528

526529
$submission = Mockery::mock( WPCF7_Submission::class );
@@ -561,11 +564,13 @@ public function test_verify_hcaptcha_without_posted_data(): void {
561564
*/
562565
public function test_verify_hcaptcha_without_site_key(): void {
563566
$data = [];
567+
$wpcf7_id = 23;
564568
$field = Mockery::mock( WPCF7_FormTag::class );
565569
$field->type = 'some';
566570
$form_fields = [ $field ];
567571

568572
$contact_form = Mockery::mock( WPCF7_ContactForm::class );
573+
$contact_form->shouldReceive( 'id' )->andReturn( $wpcf7_id );
569574
$contact_form->shouldReceive( 'scan_form_tags' )->andReturn( $form_fields );
570575

571576
$submission = Mockery::mock( WPCF7_Submission::class );
@@ -619,6 +624,7 @@ public function test_verify_hcaptcha_without_response(): void {
619624
$form_fields = [ $field ];
620625

621626
$contact_form = Mockery::mock( WPCF7_ContactForm::class );
627+
$contact_form->shouldReceive( 'id' )->andReturn( $wpcf7_id );
622628
$contact_form->shouldReceive( 'scan_form_tags' )->andReturn( $form_fields );
623629

624630
$submission = Mockery::mock( WPCF7_Submission::class );
@@ -687,6 +693,7 @@ public function test_verify_hcaptcha_not_verified(): void {
687693
$form_fields = [ $field ];
688694

689695
$contact_form = Mockery::mock( WPCF7_ContactForm::class );
696+
$contact_form->shouldReceive( 'id' )->andReturn( $wpcf7_id );
690697
$contact_form->shouldReceive( 'scan_form_tags' )->andReturn( $form_fields );
691698

692699
$submission = Mockery::mock( WPCF7_Submission::class );

0 commit comments

Comments
 (0)