Skip to content

Commit

Permalink
Block hCaptcha in admin for CF7 Stripe.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Jul 21, 2024
1 parent db3d237 commit ea851be
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/php/CF7/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,23 @@ private function insert_live_form( string $output ): string {
}

$form_shortcode = htmlspecialchars_decode( $m[1] );
$live_form = do_shortcode( $form_shortcode );
$stripe_message = '';

if ( $this->has_stripe_element( $live_form ) ) {
$stripe_message =
'<h4><em>' .
__( 'The Stripe payment element already contains an invisible hCaptcha. No need to add it to the form.', 'hcaptcha-for-forms-and-more' ) .
'</em></h4>';
}

$live_container =
"\n" .
'<div id="postbox-container-live" class="postbox-container">' .
'<div id="form-live">' .
'<h3>Live Form</h3>' .
do_shortcode( $form_shortcode ) .
'<h3>' . __( 'Live Form', 'hcaptcha-for-forms-and-more' ) . '</h3>' .
$stripe_message .
$live_form .
'</div>' .
'</div>' .
"\n";
Expand Down
11 changes: 11 additions & 0 deletions src/php/CF7/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ public function init_hooks(): void {
$this->mode_auto = hcaptcha()->settings()->is( 'cf7_status', 'form' );
$this->mode_embed = hcaptcha()->settings()->is( 'cf7_status', 'embed' );
}

/**
* Whether the form contains a Stripe element.
*
* @param string $output Output.
*
* @return bool
*/
protected function has_stripe_element( string $output ): bool {
return false !== strpos( $output, '<div class="wpcf7-stripe">' );
}
}
11 changes: 6 additions & 5 deletions src/php/CF7/CF7.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,24 @@ public function init_hooks(): void {
* @param array|string $attr Shortcode attributes array or empty string.
* @param array $m Regular expression match array.
*
* @return string|mixed
* @return string
* @noinspection PhpUnusedParameterInspection
*/
public function wpcf7_shortcode( $output, string $tag, $attr, array $m ) {
public function wpcf7_shortcode( $output, string $tag, $attr, array $m ): string {
$output = (string) $output;

if ( 'contact-form-7' !== $tag ) {
return $output;
}

if ( false !== strpos( $output, '<div class="wpcf7-stripe">' ) ) {
if ( $this->has_stripe_element( $output ) ) {
/**
* Do not show hCaptcha in the CF7 form having Stripe field.
* Stripe payment form has its own hidden hCaptcha field.
*/
return $output;
return preg_replace( '/\[cf7-hcaptcha.*?]/', '', $output );
}

$output = (string) $output;
$form_id = isset( $attr['id'] ) ? (int) $attr['id'] : 0;
$cf7_hcap_shortcode = $this->get_cf7_hcap_shortcode( $output );

Expand Down

0 comments on commit ea851be

Please sign in to comment.