Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 62 additions & 6 deletions classes/controllers/FrmFormsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ public static function update_settings() {
FrmAntiSpam::clear_caches();
}

// Handle captcha field inclusion
$include_captcha = isset( $_POST['frm_include_captcha'] ) && '1' === $_POST['frm_include_captcha']; // phpcs:ignore WordPress.Security.NonceVerification.Missing
self::handle_captcha_field( $id, $include_captcha );

$message = __( 'Settings Successfully Updated', 'formidable' );

self::get_settings_vars( $id, array(), compact( 'message', 'warnings' ) );
Expand All @@ -299,6 +303,44 @@ private static function antispam_was_on( $form_id ) {
return ! empty( $form->options['antispam'] );
}

/**
* Handle captcha field inclusion in form
*
* @since x.x
*
* @param int $form_id Form ID.
* @param bool $include_captcha Whether to include captcha field.
*
* @return void
*/
private static function handle_captcha_field( $form_id, $include_captcha ) {
$form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude' );
$captcha_field_id = 0;
$submit_field_order = 0;

foreach ( $form_fields as $field ) {
if ( 'captcha' === $field->type ) {
$captcha_field_id = $field->id;
break;
}

if ( 'submit' === $field->type ) {
$submit_field_order = $field->field_order;
}
}

if ( $include_captcha && ! $captcha_field_id ) {
// Create captcha field just before submit button
$field_values = FrmFieldsHelper::setup_new_vars( 'captcha', $form_id );
$field_values['name'] = __( 'Captcha', 'formidable' );
$field_values['field_order'] = $submit_field_order > 0 ? $submit_field_order - 1 : 0;
FrmField::create( $field_values );
} elseif ( ! $include_captcha && $captcha_field_id ) {
// Delete captcha field
FrmField::destroy( $captcha_field_id );
}
}

Comment thread
coderabbitai[bot] marked this conversation as resolved.
/**
* @param array $values
*
Expand Down Expand Up @@ -1546,6 +1588,11 @@ private static function get_settings_tabs( $values ) {
'id' => 'frm_notification_settings',
'icon' => 'frmfont frm_notification_check_icon',
),
'spam' => array(
'name' => __( 'Spam', 'formidable' ),
'function' => array( self::class, 'spam_settings' ),
'icon' => 'frmfont frm_shield_check2_icon',
),
'permissions' => array(
'name' => __( 'Form Permissions', 'formidable' ),
'icon' => 'frmfont frm_lock_closed2_icon',
Expand Down Expand Up @@ -1665,16 +1712,14 @@ public static function advanced_settings( $values ) {
}

/**
* @since x.x
*
* @param array $values
*
* @return void
*/
public static function render_spam_settings( $values ) {
if ( function_exists( 'akismet_http_post' ) ) {
include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/spam-settings/akismet.php';
}
include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/spam-settings/stopforumspam.php';
include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/spam-settings/antispam.php';
public static function spam_settings( $values ) {
include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/spam-settings.php';
}

/**
Expand Down Expand Up @@ -3757,4 +3802,15 @@ public static function create( $values = array() ) {
_deprecated_function( __METHOD__, '4.0', 'FrmFormsController::update' );
self::update( $values );
}

/**
* @deprecated x.x
*
* @param array $values
*
* @return void
*/
public static function render_spam_settings( $values ) {
_deprecated_function( __METHOD__, 'x.x' );
}
}
22 changes: 22 additions & 0 deletions classes/helpers/FrmHtmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ function () use ( $id, $name, $args ) {
);
}

/**
* Show readonly setting icon
*
* @since x.x
*
* @param bool $is_enabled Whether the setting is enabled.
*
* @return void
*/
public static function show_readonly_setting_icon( $is_enabled ) {
if ( $is_enabled ) {
echo '<span class="frm-success-icon">';
FrmAppHelper::icon_by_class( 'frmfont frm_checkmark_icon', array( 'aria-hidden' => 'true' ) );
echo '</span>';
return;
}

echo '<span class="frm-x-icon">';
FrmAppHelper::icon_by_class( 'frmfont frm_x_icon', array( 'aria-hidden' => 'true' ) );
echo '</span>';
}

/**
* Echo a dropdown option.
* This is useful to avoid closing and opening PHP to echo <option> tags which leads to extra whitespace.
Expand Down
8 changes: 5 additions & 3 deletions classes/views/frm-forms/settings-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@
</label>
</p>

<div class="frm_note_style">
<?php esc_html_e( 'Spam settings have been moved to a new section.', 'formidable' ); ?>
<a href="#spam_settings"><?php esc_html_e( 'Click here to jump to Spam settings.', 'formidable' ); ?></a>
</div>

<?php
if ( is_callable( 'FrmFormsController::render_spam_settings' ) ) {
FrmFormsController::render_spam_settings( $values );
}
FrmTipsHelper::pro_tip( 'get_form_settings_tip', 'p' );
?>

Expand Down
120 changes: 120 additions & 0 deletions classes/views/frm-forms/spam-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Spam settings for form
*
* @since x.x
*
* @package Formidable
*
* @var array $values Form values.
*/

if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}

$frm_settings = FrmAppHelper::get_settings();
$view_dir_path = FrmAppHelper::plugin_path() . '/classes/views/frm-forms/spam-settings/';

?>
<div class="frm-spam-settings">
<p class="howto">
<?php esc_html_e( 'Prevent spam submissions with anti-spam tools.', 'formidable' ); ?>
</p>

<?php
if ( function_exists( 'akismet_http_post' ) ) {
include $view_dir_path . 'akismet.php';
}

include $view_dir_path . 'stopforumspam.php';
include $view_dir_path . 'antispam.php';
?>

<?php
// Check if captcha is configured
$captcha_settings = FrmCaptchaFactory::get_settings_object();
$captcha_configured = $captcha_settings->has_pubkey();

// Check if form has a captcha field
$form_fields = FrmField::get_all_for_form( $values['id'], '', 'exclude' );
$has_captcha_field = false;
$captcha_field_id = 0;

foreach ( $form_fields as $field ) {
if ( 'captcha' === $field->type ) {
$has_captcha_field = true;
$captcha_field_id = $field->id;
break;
}
}
?>

<p class="frm8 frm_form_field">
<input type="hidden" name="frm_include_captcha" value="0" />
<label for="frm_include_captcha" class="frm_inline_block">
<input type="checkbox" id="frm_include_captcha" name="frm_include_captcha" value="1" <?php disabled( $captcha_configured, false ); ?> <?php checked( $has_captcha_field, true ); ?> />
<?php esc_html_e( 'Include Captcha in this form', 'formidable' ); ?>
</label>
</p>

<?php if ( ! $captcha_configured ) : ?>
<div class="frm_warning_style frm_force_visible_warning">
<span>
<?php
printf(
/* translators: %1$s: Opening anchor tag, %2$s: Closing anchor tag */
esc_html__( 'To enable Captcha, first set up a Captcha service in %1$sGlobal Spam Settings%2$s.', 'formidable' ),
'<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings&t=captcha_settings' ) ) . '">',
'</a>'
);
?>
</span>
</div>
<?php else : ?>
<div id="frm_captcha_add_warning" class="frm_warning_style" style="display: none;">
<span><?php esc_html_e( 'A Captcha field will be added to this form when you save settings.', 'formidable' ); ?></span>
</div>
<div id="frm_captcha_remove_warning" class="frm_warning_style" style="display: none;">
<span><?php esc_html_e( 'The Captcha field will be removed from this form when you save settings.', 'formidable' ); ?></span>
</div>
<?php endif; ?>

<h3><?php esc_html_e( 'Global Spam Settings', 'formidable' ); ?></h3>

<p class="howto">
<?php esc_html_e( 'Managed in Global Settings and applied to every form.', 'formidable' ); ?>
</p>

<table class="form-table frm-fields frm-global-spam-table">
<tr>
<td>
<?php FrmHtmlHelper::show_readonly_setting_icon( $frm_settings->honeypot ); ?>
<?php esc_html_e( 'Use honeypot to check entries for spam', 'formidable' ); ?>
</td>
</tr>
<tr>
<td>
<?php FrmHtmlHelper::show_readonly_setting_icon( $frm_settings->wp_spam_check ); ?>
<?php esc_html_e( 'Use WordPress spam comments to check entries for spam', 'formidable' ); ?>
</td>
</tr>
<tr>
<td>
<?php FrmHtmlHelper::show_readonly_setting_icon( $frm_settings->denylist_check ); ?>
<?php esc_html_e( 'Check denylist data to validate for spam', 'formidable' ); ?>
</td>
</tr>
</table>

<p>
<?php
printf(
/* translators: %1$s: Opening anchor tag, %2$s: Closing anchor tag */
esc_html__( 'To change these values %1$svisit Global Spam Settings%2$s', 'formidable' ),
'<a href="' . esc_url( admin_url( 'admin.php?page=formidable-settings&t=captcha_settings' ) ) . '" target="_blank">',
'</a>'
);
?>
</p>
</div>
2 changes: 1 addition & 1 deletion css/frm_admin.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/formidable_admin.js

Large diffs are not rendered by default.

47 changes: 46 additions & 1 deletion js/src/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ window.frmAdminBuildJS = function() {
if ( auto !== 'auto' ) {
// Hide success message on tab change.
jQuery( '.frm_updated_message' ).hide();
jQuery( '.frm_warning_style' ).hide();
jQuery( '.frm_warning_style:not(.frm_force_visible_warning)' ).hide();
}

if ( jQuery( link ).closest( '#frm_adv_info' ).length ) {
Expand Down Expand Up @@ -11053,6 +11053,51 @@ window.frmAdminBuildJS = function() {
}
} );

// Handle spam settings redirect link
const spamRedirectLinks = document.querySelectorAll( 'a[href="#spam_settings"]' );
spamRedirectLinks.forEach( function( link ) {
link.addEventListener( 'click', function( e ) {
e.preventDefault();
const spamTab = document.querySelector( '.frm-category-tabs a[href="#spam_settings"]' );
if ( spamTab ) {
clickTab( spamTab );
// Scroll to top after tab switch
setTimeout( function() {
const categoryDiv = document.getElementById( 'frm-categorydiv' );
if ( categoryDiv ) {
categoryDiv.scrollIntoView( { block: 'start' } );
}
}, 50 );
}
} );
} );

// Handle Captcha checkbox toggle to show/hide warnings
const captchaCheckbox = document.getElementById( 'frm_include_captcha' );
if ( captchaCheckbox ) {
const initialState = captchaCheckbox.checked;
captchaCheckbox.addEventListener( 'change', function() {
const addWarning = document.getElementById( 'frm_captcha_add_warning' );
const removeWarning = document.getElementById( 'frm_captcha_remove_warning' );
if ( addWarning && removeWarning ) {
// Only show warning if current state differs from initial state
if ( this.checked !== initialState ) {
if ( this.checked ) {
addWarning.style.display = 'block';
removeWarning.style.display = 'none';
} else {
addWarning.style.display = 'none';
removeWarning.style.display = 'block';
}
} else {
// Hide warnings if toggled back to initial state
addWarning.style.display = 'none';
removeWarning.style.display = 'none';
}
}
} );
}

jQuery( 'select[name="options[edit_action]"]' ).on( 'change', showSuccessOpt );

const $loggedIn = document.getElementById( 'logged_in' );
Expand Down
35 changes: 35 additions & 0 deletions resources/scss/admin/components/settings/_form-settings.scss
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,38 @@ h2.frm-h2,
.frm_select_with_label {
margin: 7px 0 0;
}

/**
* Spam Settings styles
*/
.frm-spam-settings h3 {
margin-bottom: 0.5em;
}

.frm-spam-settings h3 + .howto {
margin-top: 0;
}

.frm-spam-settings .frm-global-spam-table {
width: auto;
}

.frm-spam-settings .frm-global-spam-table td {
border-bottom: 1px solid var(--grey-200);
padding-bottom: 12px;
}

.frm-spam-settings .frm-global-spam-table tr:last-child td {
border-bottom: none;
}

.frm-spam-settings .frm-success-icon {
color: rgba(3, 152, 85, 1);
font-size: 16px;
margin-right: 8px;
}

.frm-spam-settings .frm-x-icon {
font-size: 16px;
margin-right: 8px;
}
Loading