|
Server : Apache System : Linux s1230 5.15.0-139-generic #149~20.04.1 SMP Tue Jul 14 11:21:49 UTC 2026 x86_64 User : p141464 ( 418825) PHP Version : 7.4.33.12 Disable Function : NONE Directory : /html/relaunch-kmu/wp-content/plugins/wpforms-lite/src/Admin/Education/Builder/ |
Upload File : |
<?php
namespace WPForms\Admin\Education\Builder;
use WPForms\Admin\Education\EducationInterface;
use WPForms\Education\ActiveLayer\Helper as ActiveLayer;
/**
* Builder/ReCaptcha Education feature.
*
* @since 1.6.6
*/
class Captcha implements EducationInterface {
/**
* Indicate if current Education feature is allowed to load.
*
* @since 1.6.6
*/
public function allow_load() {
return wp_doing_ajax();
}
/**
* Init.
*
* @since 1.6.6
*/
public function init() {
if ( ! $this->allow_load() ) {
return;
}
// Define hooks.
$this->hooks();
}
/**
* Hooks.
*
* @since 1.6.6
*/
public function hooks() {
add_action( 'wp_ajax_wpforms_update_field_captcha', [ $this, 'captcha_field_callback' ] );
}
/**
* Targeting on hCaptcha/reCAPTCHA field button in the builder.
*
* @since 1.6.6
*/
public function captcha_field_callback() {
// Run a security check.
check_ajax_referer( 'wpforms-builder', 'nonce' );
// Check for form ID.
if ( empty( $_POST['id'] ) ) {
wp_send_json_error( esc_html__( 'No form ID found.', 'wpforms-lite' ) );
}
$form_id = absint( $_POST['id'] );
// Check for permissions.
if ( ! wpforms_current_user_can( 'edit_form_single', $form_id ) ) {
wp_send_json_error( esc_html__( 'You do not have permission.', 'wpforms-lite' ) );
}
// Get an actual form data.
$form_data = wpforms()->obj( 'form' )->get( $form_id, [ 'content_only' => true ] );
// Check that CAPTCHA is configured in the settings.
$captcha_settings = wpforms_get_captcha_settings();
$captcha_name = $this->get_captcha_name( $captcha_settings );
if ( empty( $form_data ) || empty( $captcha_name ) ) {
wp_send_json_error( esc_html__( 'Something wrong. Please try again later.', 'wpforms-lite' ) );
}
// Prepare a result array.
$data = $this->get_captcha_result_mockup( $captcha_settings );
if ( empty( $captcha_settings['site_key'] ) || empty( $captcha_settings['secret_key'] ) ) {
// If CAPTCHA is not configured in the WPForms plugin settings.
$data['current'] = 'not_configured';
} elseif ( ! isset( $form_data['settings']['recaptcha'] ) || $form_data['settings']['recaptcha'] !== '1' ) {
// If CAPTCHA is configured in WPForms plugin settings, but wasn't set in form settings.
$data['current'] = 'configured_not_enabled';
} else {
// If CAPTCHA is configured in WPForms plugin and form settings.
$data['current'] = 'configured_enabled';
}
wp_send_json_success( $data );
}
/**
* Retrieve the CAPTCHA name.
*
* @since 1.6.6
*
* @param array $settings The CAPTCHA settings.
*
* @return string
*/
private function get_captcha_name( $settings ) {
if ( empty( $settings['provider'] ) ) {
return '';
}
if ( empty( $settings['site_key'] ) && empty( $settings['secret_key'] ) ) {
return esc_html__( 'CAPTCHA', 'wpforms-lite' );
}
if ( $settings['provider'] === 'hcaptcha' ) {
return esc_html__( 'hCaptcha', 'wpforms-lite' );
}
if ( $settings['provider'] === 'turnstile' ) {
return esc_html__( 'Cloudflare Turnstile', 'wpforms-lite' );
}
$recaptcha_names = [
'v2' => esc_html__( 'Google Checkbox v2 reCAPTCHA', 'wpforms-lite' ),
'invisible' => esc_html__( 'Google Invisible v2 reCAPTCHA', 'wpforms-lite' ),
'v3' => esc_html__( 'Google v3 reCAPTCHA', 'wpforms-lite' ),
];
return isset( $recaptcha_names[ $settings['recaptcha_type'] ] ) ? $recaptcha_names[ $settings['recaptcha_type'] ] : '';
}
/**
* Get CAPTCHA callback result mockup.
*
* @since 1.6.6
*
* @param array $settings The CAPTCHA settings.
*
* @return array
*/
private function get_captcha_result_mockup( $settings ) {
$captcha_name = $this->get_captcha_name( $settings );
if ( empty( $captcha_name ) ) {
wp_send_json_error( esc_html__( 'Something wrong. Please, try again later.', 'wpforms-lite' ) );
}
$not_configured_content = sprintf(
wp_kses( /* translators: %1$s - CAPTCHA settings page URL, %2$s - WPForms.com doc URL. */
__( 'Please complete the setup in your <a href="%1$s" target="_blank">WPForms Settings</a>, and check out <a href="%2$s" target="_blank" rel="noopener noreferrer">our guide</a> to learn about available CAPTCHA solutions.', 'wpforms-lite' ),
[
'a' => [
'href' => true,
'rel' => true,
'target' => true,
],
]
),
esc_url( admin_url( 'admin.php?page=wpforms-settings&view=captcha' ) ),
esc_url( wpforms_utm_link( 'https://wpforms.com/docs/setup-captcha-wpforms/', 'builder-modal', 'Captcha Documentation' ) )
);
// The success modal is only reachable through the footer's install CTA,
// so only ship its data when the footer actually renders.
$footer = $this->get_activelayer_footer_html();
return [
'current' => false,
'cases' => [
'not_configured' => [
'title' => esc_html__( 'Heads up!', 'wpforms-lite' ),
'content' => $not_configured_content,
'footer' => $footer,
'success_modal' => $footer === '' ? null : $this->get_activelayer_success_modal_data(),
],
'configured_not_enabled' => [
'title' => false,
/* translators: %s - CAPTCHA name. */
'content' => sprintf( esc_html__( '%s has been enabled for this form. Don\'t forget to save your form!', 'wpforms-lite' ), $captcha_name ),
],
'configured_enabled' => [
'title' => false,
/* translators: %s - CAPTCHA name. */
'content' => sprintf( esc_html__( 'Are you sure you want to disable %s for this form?', 'wpforms-lite' ), $captcha_name ),
'cancel' => true,
],
],
'provider' => $settings['provider'],
];
}
/**
* ActiveLayer cross-promo footer for the captcha not_configured modal.
* Empty when ActiveLayer is already active or the user cannot install plugins.
*
* @since 2.0.0
*
* @return string
*/
private function get_activelayer_footer_html(): string {
if ( ActiveLayer::is_activated() || ! wpforms_can_install( 'plugin' ) ) {
return '';
}
$modal = ActiveLayer::get_modal_data();
$attrs_html = '';
foreach ( $modal['attrs'] as $key => $value ) {
$attrs_html .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) );
}
// Body copy with "ActiveLayer" linking to the marketing site in a new tab.
$body = sprintf(
wp_kses( /* translators: %1$s - ActiveLayer marketing site URL. */
__( 'Captchas can reduce form completions by up to 40%%. <a href="%1$s" target="_blank" rel="noopener noreferrer">ActiveLayer</a> catches bots invisibly, without asking your visitors to prove they\'re human.', 'wpforms-lite' ),
[
'a' => [
'href' => true,
'target' => true,
'rel' => true,
],
]
),
esc_url( wpforms_utm_link( 'https://activelayer.com/', 'builder-modal', 'ActiveLayer Captcha Footer' ) )
);
// The install CTA drives the bespoke flow in captcha.js (direct install,
// no confirm modal); it deliberately does NOT use the `education-modal` class.
return sprintf(
'<div class="wpforms-captcha-activelayer-footer"><p>%1$s <a href="#" class="wpforms-captcha-activelayer-install"%2$s>%3$s</a></p></div>',
$body, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped via wp_kses and esc_url.
$attrs_html, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- attrs escaped above.
esc_html__( 'Install Now', 'wpforms-lite' )
);
}
/**
* Copy and target for the "Installation Successful" confirmation modal
* shown by captcha.js after ActiveLayer is installed from the footer CTA.
*
* @since 2.0.0
*
* @return array
*/
private function get_activelayer_success_modal_data(): array {
return [
'title' => esc_html__( 'Installation Successful', 'wpforms-lite' ),
'content' => esc_html__( 'ActiveLayer has been installed and is ready to be configured. Would you like to set it up now?', 'wpforms-lite' ),
'setup_text' => esc_html__( 'Yes, Go to Setup', 'wpforms-lite' ),
'setup_url' => ActiveLayer::get_dashboard_url(),
];
}
}