Skip to content
Open
11 changes: 10 additions & 1 deletion classes/controllers/FrmFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,16 @@ public static function destroy() {
check_ajax_referer( 'frm_ajax', 'nonce' );

$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
FrmField::destroy( $field_id );

if ( FrmField::getOne( $field_id ) && false === FrmField::destroy( $field_id ) ) {
wp_send_json_error(
array(
'message' => __( 'This field cannot be deleted.', 'formidable' ),
),
403
);
}

wp_die();
}

Expand Down
40 changes: 26 additions & 14 deletions classes/controllers/FrmFormsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,12 @@ public static function update_settings() {

// 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 );
$captcha_updated = self::handle_captcha_field( $id, $include_captcha );
$gdpr_updated = self::maybe_handle_gdpr_field( $id );

self::maybe_handle_gdpr_field( $id );
if ( ! $captcha_updated || ! $gdpr_updated ) {
$warnings[] = __( 'Some fields could not be added or deleted.', 'formidable' );
}

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

Expand All @@ -314,10 +317,10 @@ private static function antispam_was_on( $form_id ) {
* @param int $form_id Form ID.
* @param bool $include_captcha Whether to include captcha field.
*
* @return void
* @return bool
*/
private static function handle_captcha_field( $form_id, $include_captcha ) {
self::toggle_field_in_form( $form_id, 'captcha', $include_captcha, __( 'Captcha', 'formidable' ) );
return self::toggle_field_in_form( $form_id, 'captcha', $include_captcha, __( 'Captcha', 'formidable' ) );
}

/**
Expand All @@ -328,16 +331,16 @@ private static function handle_captcha_field( $form_id, $include_captcha ) {
*
* @param int $form_id Form ID.
*
* @return void
* @return bool
*/
private static function maybe_handle_gdpr_field( $form_id ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['frm_include_gdpr'] ) || FrmFieldGdprHelper::hide_gdpr_field() ) {
return;
return true;
}

$include_gdpr = '1' === $_POST['frm_include_gdpr']; // phpcs:ignore WordPress.Security.NonceVerification.Missing
self::toggle_field_in_form( $form_id, FrmFieldGdprHelper::FIELD_TYPE, $include_gdpr, __( 'GDPR', 'formidable' ) );
return self::toggle_field_in_form( $form_id, FrmFieldGdprHelper::FIELD_TYPE, $include_gdpr, __( 'GDPR', 'formidable' ) );
}

/**
Expand All @@ -350,7 +353,7 @@ private static function maybe_handle_gdpr_field( $form_id ) {
* @param bool $include Whether the form should include the field.
* @param string $field_name The name to give a newly created field.
*
* @return void
* @return bool
*/
private static function toggle_field_in_form( $form_id, $field_type, $include, $field_name ) {
$form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude' );
Expand All @@ -376,10 +379,14 @@ private static function toggle_field_in_form( $form_id, $field_type, $include, $
$field_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
$field_values['name'] = $field_name;
$field_values['field_order'] = $submit_field_order > 0 ? $submit_field_order - 1 : $last_field_order + 1;
FrmField::create( $field_values );
} elseif ( ! $include && $existing_field_id ) {
FrmField::destroy( $existing_field_id );
return false !== FrmField::create( $field_values );
}

if ( ! $include && $existing_field_id ) {
return false !== FrmField::destroy( $existing_field_id );
}

return true;
}

/**
Expand Down Expand Up @@ -2736,16 +2743,21 @@ public static function show_form( $id = '', $key = '', $title = false, $descript
$form = do_shortcode( $frm_settings->login_msg );
} else {
do_action( 'frm_pre_get_form', $form );
$form = self::get_form( $form, $title, $description, $atts );
$form_object = $form;
$form = self::get_form( $form, $title, $description, $atts );

/**
* Use this shortcode to check for external shortcodes that may span
* across multiple fields in the customizable HTML
*
* @since 2.0.8
* @since x.x The form object is passed as the second argument.
*
* @param string $form Rendered form HTML.
* @param object $form_object Form being rendered.
*/
$form = apply_filters( 'frm_filter_final_form', $form );
}
$form = apply_filters( 'frm_filter_final_form', $form, $form_object );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

}//end if

return $form;
}
Expand Down
20 changes: 15 additions & 5 deletions classes/helpers/FrmXMLHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ public static function import_xml_forms( $forms, $imported ) {

self::import_xml_fields( $item->field, $form_id, $this_form, $form_fields, $imported );

self::delete_removed_fields( $form_fields );
if ( ! self::delete_removed_fields( $form_fields ) ) {
$imported['warnings'][] = __( 'Some fields could not be deleted during import.', 'formidable' );
}

// Update field ids/keys to new ones.
do_action( 'frm_after_duplicate_form', $form_id, $form, array( 'old_id' => $old_id ) );
Expand Down Expand Up @@ -417,19 +419,23 @@ private static function get_form_fields( $form_id ) {
*
* @param array $form_fields
*
* @return void
* @return bool
*/
private static function delete_removed_fields( $form_fields ) {
if ( ! $form_fields ) {
return;
return true;
}

$deleted = true;

foreach ( $form_fields as $field ) {
if ( is_object( $field ) ) {
FrmField::destroy( $field->id );
if ( is_object( $field ) && false === FrmField::destroy( $field->id ) ) {
$deleted = false;
}
unset( $field );
}

return $deleted;
}

/**
Expand Down Expand Up @@ -1637,6 +1643,10 @@ public static function parse_message( $result, &$message, &$errors ) {
return;
}

if ( ! empty( $result['warnings'] ) ) {
$errors = array_merge( $errors, array_unique( $result['warnings'] ) );
}

$t_strings = array(
'imported' => __( 'Imported', 'formidable' ),
'updated' => __( 'Updated', 'formidable' ),
Expand Down
78 changes: 78 additions & 0 deletions classes/models/FrmEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,76 @@ public static function create( $values ) {
return self::create_entry( $values, 'standard' );
}

/**
* Allow integrations to prevent entry storage and its associated actions.
*
* This runs before the frm_pre_create_entry filter, so a callback on that filter that
* changes the form ID is not checked again.
*
* @since x.x
*
* @param array $values Entry values. On create this is the full set of posted values. On
* update it is the values being saved, with the entry's form ID added
* when they do not include one.
* @param int|string $entry_id The entry being updated, or 0 when a new entry is created.
*
* @return bool
*/
public static function allows_save( $values, $entry_id = 0 ) {
foreach ( array( 'form_id', 'parent_form_id' ) as $key ) {
if ( empty( $values[ $key ] ) ) {
continue;
}

$form = FrmForm::getOne( $values[ $key ] );

if ( $form && ! empty( $form->options['prevent_entry_storage'] ) ) {
return false;
}
}

/**
* Filter whether an entry may be saved.
*
* @since x.x
*
* @param bool $allow Whether the entry may be saved.
* @param array $values Entry values, including the form ID.
* @param int|string $entry_id The entry being updated, or 0 when a new entry is created.
*/
return apply_filters( 'frm_allow_entry_save', true, $values, (int) $entry_id );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 4 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

}

/**
* Check whether an existing entry may be updated. The entry's own form is always
* checked, and so is the form in the new values when it moves the entry.
*
* @since x.x
*
* @param int $id Entry ID.
* @param array $values Values being saved.
*
* @return bool
*/
private static function allows_update( $id, $values ) {
$entry = self::getOne( $id );
$check_values = $values;

if ( $entry && empty( $check_values['form_id'] ) ) {
$check_values['form_id'] = $entry->form_id;
}

if ( ! self::allows_save( $check_values, $id ) ) {
return false;
}

if ( ! $entry || (int) $entry->form_id === (int) $check_values['form_id'] ) {
return true;
}

return self::allows_save( array( 'form_id' => $entry->form_id ), $id );
}

/**
* Create a new entry with some differences depending on type
*
Expand All @@ -32,6 +102,10 @@ public static function create( $values ) {
* @return bool|int Entry ID.
*/
private static function create_entry( $values, $type ) {
if ( ! self::allows_save( $values ) ) {
return false;
}

$new_values = self::before_insert_entry_in_database( $values, $type );

// Don't check XML entries for duplicates
Expand Down Expand Up @@ -346,6 +420,10 @@ public static function update( $id, $values ) {
private static function update_entry( $id, $values, $update_type ) {
global $wpdb;

if ( ! self::allows_update( $id, $values ) ) {
return false;
}

$update = self::before_update_entry( $id, $values, $update_type );

if ( ! $update ) {
Expand Down
27 changes: 27 additions & 0 deletions classes/models/FrmField.php
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,16 @@ public static function update( $id, $values ) {
$values['required'] = (int) $values['required'];
}

/**
* Filter field values before any update is persisted.
*
* @since x.x
*
* @param array $values Field values to save.
* @param int $id Existing field ID.
*/
$values = apply_filters( 'frm_field_values_before_update', $values, $id );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.


self::preserve_format_option_backslashes( $values );

if ( isset( $values['type'] ) ) {
Expand Down Expand Up @@ -912,6 +922,10 @@ private static function preserve_format_option_backslashes( &$values ) {
public static function destroy( $id ) {
global $wpdb;

if ( ! self::can_destroy( $id ) ) {
return false;
}

do_action( 'frm_before_destroy_field', $id );

wp_cache_delete( $id, 'frm_field' );
Expand Down Expand Up @@ -941,6 +955,19 @@ public static function destroy( $id ) {
return $result;
}

/**
* Whether a field may be deleted.
*
* @since x.x
*
* @param int|string $id Field ID.
*
* @return bool
*/
public static function can_destroy( $id ) {
return (bool) apply_filters( 'frm_can_destroy_field', true, $id );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function apply_filters invoked with 3 parameters, 2 required


The function call is not valid, which will result in a fatal runtime error.

}

/**
* @param int|string $form_id
*
Expand Down
2 changes: 1 addition & 1 deletion js/formidable_admin.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions js/src/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6521,6 +6521,12 @@ window.frmAdminBuildJS = function() {
field_id: fieldId,
nonce: frmGlobal.nonce
},
error( xhr ) {
const message = xhr.responseJSON?.data?.message;
if ( message ) {
infoModal( message );
}
},
success() {
forgetFieldSettings( fieldId );
forgetFieldPreview( fieldId );
Expand Down
Loading
Loading