Skip to content

Commit

Permalink
Fixed shortcode processing in the Contact Form 7 form when Auto-Add w…
Browse files Browse the repository at this point in the history
…as off.
  • Loading branch information
kagg-design committed Feb 19, 2025
1 parent bd91ef4 commit c86ade2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .tests/php/integration/CF7/CF7Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,11 @@ public function test_has_field(): void {
$field_type = 'hcaptcha';
$field->type = $field_type;
$form_fields = [ $field ];
$form_html = 'some cf7 html';

$submission->shouldReceive( 'get_contact_form' )->andReturn( $contact_form );
$contact_form->shouldReceive( 'scan_form_tags' )->andReturn( $form_fields );
$contact_form->shouldReceive( 'form_html' )->andReturn( $form_html );

$subject = Mockery::mock( CF7::class )->makePartial();

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ Instructions for popular native integrations are below:
* Fixed ASC ordering by date on the Events page.
* Fixed selection of a time interval on the Events page when site local time was not GMT.
* Fixed losing options during plugin update in rare cases.
* Fixed shortcode processing in the Contact Form 7 form when Auto-Add was off.

= 4.10.0 =
* Added support for wp_login_form() function and LoginOut block.
Expand Down
17 changes: 14 additions & 3 deletions src/php/CF7/CF7.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class CF7 extends Base {
*/
private const DATA_NAME = 'hcap-cf7';

/**
* Field type.
*/
private const FIELD_TYPE = 'hcaptcha';

/**
* Init hooks.
*
Expand Down Expand Up @@ -202,7 +207,7 @@ public function verify_hcaptcha( $result, $tag ) {

if (
! $this->mode_auto &&
! ( $this->mode_embed && $this->has_field( $submission, 'hcaptcha' ) )
! ( $this->mode_embed && $this->has_field( $submission, self::FIELD_TYPE ) )
) {
return $result;
}
Expand All @@ -227,7 +232,13 @@ public function verify_hcaptcha( $result, $tag ) {
* @return bool
*/
protected function has_field( WPCF7_Submission $submission, string $type ): bool {
$form_fields = $submission->get_contact_form()->scan_form_tags();
$contact_form = $submission->get_contact_form();

if ( self::FIELD_TYPE === $type && has_shortcode( $contact_form->form_html(), 'cf7-hcaptcha' ) ) {
return true;
}

$form_fields = $contact_form->scan_form_tags();

foreach ( $form_fields as $form_field ) {
if ( $type === $form_field->type ) {
Expand All @@ -254,7 +265,7 @@ private function get_invalidated_result( $result, $captcha_result = '' ) {

$result->invalidate(
[
'type' => 'hcaptcha',
'type' => self::FIELD_TYPE,
'name' => self::DATA_NAME,
],
$captcha_result
Expand Down

0 comments on commit c86ade2

Please sign in to comment.