forms_utm_link( 'https://wpforms.com/docs/setup-captcha-wpforms/', 'Settings - Captcha', 'Captcha Comparison Documentation' ) )
),
],
];
// Add settings fields for each of available captcha types.
foreach ( $this->captchas as $captcha ) {
$settings[ self::VIEW ] = array_merge( $settings[ self::VIEW ], $captcha->get_settings_fields() );
}
$settings[ self::VIEW ] = array_merge(
$settings[ self::VIEW ],
[
'recaptcha-noconflict' => [
'id' => 'recaptcha-noconflict',
'name' => esc_html__( 'No-Conflict Mode', 'wpforms-lite' ),
'desc' => esc_html__( 'Forcefully remove other CAPTCHA occurrences in order to prevent conflicts. Only enable this option if your site is having compatibility issues or instructed by support.', 'wpforms-lite' ),
'type' => 'toggle',
'status' => true,
],
self::VIEW . '-preview' =>
[
'id' => self::VIEW . '-preview',
'name' => esc_html__( 'Preview', 'wpforms-lite' ),
'content' => '
' . esc_html__( 'Please save settings to generate a preview of your CAPTCHA here.', 'wpforms-lite' ) . '
',
'type' => 'content',
'class' => [ 'wpforms-hidden' ],
],
]
);
if (
$this->settings['provider'] === 'hcaptcha' ||
$this->settings['provider'] === 'turnstile' ||
( $this->settings['provider'] === 'recaptcha' && $this->settings['recaptcha_type'] === 'v2' )
) {
// phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName
/**
* Modify captcha settings data.
*
* @since 1.6.4
*
* @param array $data Array of settings.
*/
$data = apply_filters(
'wpforms_admin_pages_settings_captcha_data',
[
'sitekey' => $this->settings['site_key'],
'theme' => $this->settings['theme'],
]
);
// phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName
// Prepare HTML for CAPTCHA preview.
$placeholder_description = $settings[ self::VIEW ][ self::VIEW . '-preview' ]['content'];
$captcha_description = esc_html__( 'This CAPTCHA is generated using your site and secret keys. If an error is displayed, please double-check your keys.', 'wpforms-lite' );
$captcha_preview = sprintf(
'',
wpforms_html_attributes( '', [ 'wpforms-captcha', 'wpforms-captcha-' . $this->settings['provider'] ], $data )
);
$settings[ self::VIEW ][ self::VIEW . '-preview' ]['content'] = sprintf(
'
%3$s
',
$captcha_preview,
$captcha_description,
$placeholder_description
);
$settings[ self::VIEW ][ self::VIEW . '-preview' ]['class'] = [];
}
return $settings;
}
/**
* Re-init CAPTCHA settings when plugin settings were updated.
*
* @since 1.8.0
*/
public function updated() {
$this->init_settings();
$this->notice();
}
/**
* Display notice about the CAPTCHA preview.
*
* @since 1.8.0
*/
private function notice() {
if ( ! wpforms_is_admin_page( 'settings', self::VIEW ) || ! $this->is_captcha_preview_ready() ) {
return;
}
Notice::info( esc_html__( 'A preview of your CAPTCHA is displayed below. Please view to verify the CAPTCHA settings are correct.', 'wpforms-lite' ) );
}
/**
* Check if CAPTCHA config is ready to display a preview.
*
* @since 1.8.0
*
* @return bool
*/
private function is_captcha_preview_ready() {
$current_captcha = $this->get_current_captcha();
if ( ! $current_captcha ) {
return false;
}
return $current_captcha->is_captcha_preview_ready();
}
/**
* Enqueue assets for the CAPTCHA settings page.
*
* @since 1.8.0
*/
public function enqueues() {
$current_captcha = $this->get_current_captcha();
if ( ! $current_captcha ) {
return;
}
$current_captcha->enqueues();
}
/**
* Get current active captcha object.
*
* @since 1.8.0
*
* @return object|string
*/
private function get_current_captcha() {
return ! empty( $this->captchas[ $this->settings['provider'] ] ) ? $this->captchas[ $this->settings['provider'] ] : '';
}
/**
* Use the CAPTCHA no-conflict mode.
*
* When enabled in the WPForms settings, forcefully remove all other
* CAPTCHA enqueues to prevent conflicts. Filter can be used to target
* specific pages, etc.
*
* @since 1.6.4
*/
public function apply_noconflict() {
if (
! wpforms_is_admin_page( 'settings', self::VIEW ) ||
empty( wpforms_setting( 'recaptcha-noconflict' ) ) ||
/**
* Allow/disallow applying non-conflict mode for captcha scripts.
*
* @since 1.6.4
*
* @param boolean $allow True/false. Default: true.
*/
! apply_filters( 'wpforms_admin_settings_captcha_apply_noconflict', true ) // phpcs:ignore WPForms.PHP.ValidateHooks.InvalidHookName
) {
return;
}
$scripts = wp_scripts();
$urls = [ 'google.com/recaptcha', 'gstatic.com/recaptcha', 'hcaptcha.com/1', 'challenges.cloudflare.com/turnstile' ];
foreach ( $scripts->queue as $handle ) {
// Skip the WPForms JavaScript assets.
if (
! isset( $scripts->registered[ $handle ] ) ||
false !== strpos( $scripts->registered[ $handle ]->handle, 'wpforms' )
) {
return;
}
foreach ( $urls as $url ) {
if ( false !== strpos( $scripts->registered[ $handle ]->src, $url ) ) {
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
break;
}
}
}
}
}