Repository navigation
Add generic entry storage and field protection APIs #3665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
325c715
6b901b5
c5da052
f207a0d
f3d5263
fad0eaa
fe661c9
2f43596
d69c425
26f4b3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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' ); | ||
|
|
||
|
|
@@ -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' ) ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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' ) ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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' ); | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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 ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }//end if | ||
|
|
||
| return $form; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
| * | ||
|
|
@@ -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 | ||
|
|
@@ -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 ) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| self::preserve_format_option_backslashes( $values ); | ||
|
|
||
| if ( isset( $values['type'] ) ) { | ||
|
|
@@ -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' ); | ||
|
|
@@ -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 ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** | ||
| * @param int|string $form_id | ||
| * | ||
|
|
||
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function call is not valid, which will result in a fatal runtime error.