From 27f7ab1cdac17f4adc28f57167d7ade798a832b5 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 27 Nov 2026 13:15:10 -0400 Subject: [PATCH 1/5] fix(fields): group section fields like the frontend The frontend repeats the fields of a repeater's child form whatever their field_order, and groups other fields into sections by order. The builder grouped everything by order, and the field abilities let form_id, in_section and field_order disagree, so fields showed inside a repeater in one place and outside it in the other. Sort builder fields by the frontend rules, and make create-field, update-field, delete-field and create-form inline fields keep the three in agreement, rejecting sections that cannot hold the field. Co-Authored-By: Claude Opus 5.5 --- .../FrmAbilitiesFieldsController.php | 84 +-- .../FrmAbilitiesFormsController.php | 17 +- classes/controllers/FrmHooksController.php | 1 + .../FrmAbilitiesInlineFieldsHelper.php | 214 +++++++ classes/helpers/FrmFieldPlacementHelper.php | 603 ++++++++++++++++++ stubs.php | 7 + .../test_FrmAbilitiesFieldPlacement.php | 514 +++++++++++++++ .../fields/test_FrmSectionStructure.php | 268 ++++++++ 8 files changed, 1640 insertions(+), 68 deletions(-) create mode 100644 classes/helpers/FrmAbilitiesInlineFieldsHelper.php create mode 100644 tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php create mode 100644 tests/phpunit/fields/test_FrmSectionStructure.php diff --git a/classes/controllers/FrmAbilitiesFieldsController.php b/classes/controllers/FrmAbilitiesFieldsController.php index c715f9d734..25b84dc34f 100644 --- a/classes/controllers/FrmAbilitiesFieldsController.php +++ b/classes/controllers/FrmAbilitiesFieldsController.php @@ -379,14 +379,13 @@ public static function execute_create_field( $input ) { return FrmAbilitiesHelper::flatten_error( $prepared ); } - $prepared['form_id'] = (int) $form->id; - $field_id = FrmField::create( $prepared ); + $field_id = FrmField::create( $prepared ); if ( ! $field_id ) { return new WP_Error( 'frm_create_field', __( 'Field creation failed.', 'formidable' ), array( 'status' => 409 ) ); } - FrmField::delete_form_transient( $form->id ); + FrmField::delete_form_transient( $prepared['form_id'] ); FrmForm::clear_form_cache(); return self::prepare_field_for_response( FrmField::getOne( $field_id ) ); @@ -430,7 +429,13 @@ public static function execute_update_field( $input ) { return new WP_Error( 'frm_no_update_data', __( 'No data provided to update.', 'formidable' ), array( 'status' => 400 ) ); } - $field_data = self::add_stored_values_for_cleaning( $field, $field_data ); + $field_data = self::add_stored_values_for_cleaning( $field, $field_data ); + $field_data = FrmFieldPlacementHelper::prepare_update_placement( $field, $field_data, $input ); + + if ( is_wp_error( $field_data ) ) { + return $field_data; + } + $new_type = ! empty( $field_data['type'] ) ? $field_data['type'] : $field->type; $field_options = isset( $field_data['field_options'] ) && is_array( $field_data['field_options'] ) ? $field_data['field_options'] : (array) $field->field_options; $checked = self::check_form_select( $new_type, $field_options ); @@ -444,6 +449,11 @@ public static function execute_update_field( $input ) { } FrmField::delete_form_transient( $field->form_id ); + + if ( isset( $field_data['form_id'] ) ) { + FrmField::delete_form_transient( $field_data['form_id'] ); + } + FrmForm::clear_form_cache(); return self::prepare_field_for_response( FrmField::getOne( $field->id ) ); @@ -488,6 +498,7 @@ public static function execute_delete_field( $input ) { // Read the field before it is gone, so the caller gets back what it deleted. $data = self::prepare_field_for_response( $field ); $form_id = $field->form_id; + $parts = FrmFieldPlacementHelper::is_container_type( $field->type ) ? FrmFieldPlacementHelper::get_container_parts( $field ) : false; if ( FrmField::is_repeating_field( $field ) && ! empty( $field->field_options['form_select'] ) ) { self::destroy_fields_in_repeater( $field ); @@ -497,6 +508,10 @@ public static function execute_delete_field( $input ) { return self::get_invalid_field_error(); } + if ( $parts ) { + FrmFieldPlacementHelper::clean_up_deleted_container( $field, $parts ); + } + FrmField::delete_form_transient( $form_id ); FrmForm::clear_form_cache(); @@ -542,7 +557,16 @@ public static function prepare_new_field( $field, $form ) { $prepared['form_id'] = $form_id; $prepared = self::maybe_create_repeat_form( $prepared, $form_id ); - $prepared = self::maybe_position_in_repeater( $prepared, $field, $form ); + $prepared = FrmFieldPlacementHelper::prepare_new_placement( $prepared, isset( $field['field_order'] ) ); + + if ( is_wp_error( $prepared ) ) { + return $prepared; + } + + if ( ! FrmFieldPlacementHelper::is_allowed( $prepared['type'], (int) $prepared['form_id'], (int) ( $prepared['field_options']['in_section'] ?? 0 ) ) ) { + return new WP_Error( 'frm_invalid_field_placement', __( 'This field type cannot be placed in this section or repeater.', 'formidable' ), array( 'status' => 400 ) ); + } + $prepared = self::maybe_enable_separate_values( $prepared, $field ); if ( empty( $field['field_key'] ) && ! empty( $field['name'] ) ) { @@ -678,56 +702,6 @@ private static function maybe_create_repeat_form( $prepared, $form_id ) { return $prepared; } - /** - * Slot a new repeater child field after the last field of its section. - * - * Repeater child fields sort into the parent form's field list purely by - * field_order, so an order computed against the child form alone can land - * before the repeater's own divider and render outside the section in the - * builder. When the caller gives no explicit order, the field goes right - * after the last field of its section and the fields that follow shift down. - * - * @since 7.0 - * - * @param array $prepared Prepared field data. - * @param array $field Raw field input. - * @param stdClass $form The form the field is created on. - * - * @return array - */ - private static function maybe_position_in_repeater( $prepared, $field, $form ) { - if ( isset( $field['field_order'] ) || empty( $form->parent_form_id ) ) { - return $prepared; - } - - $parent_fields = FrmField::get_all_for_form( $form->parent_form_id, '', 'include' ); - $section_order = 0; - - foreach ( $parent_fields as $parent_field ) { - $form_select = isset( $parent_field->field_options['form_select'] ) ? (int) $parent_field->field_options['form_select'] : 0; - $starts_this_section = 'divider' === $parent_field->type && $form_select === (int) $form->id; - $in_this_section = (int) $parent_field->form_id === (int) $form->id; - - if ( $starts_this_section || $in_this_section ) { - $section_order = max( $section_order, (int) $parent_field->field_order ); - } - } - - if ( ! $section_order ) { - return $prepared; - } - - $prepared['field_order'] = $section_order + 1; - - foreach ( $parent_fields as $parent_field ) { - if ( (int) $parent_field->field_order > $section_order ) { - FrmField::update( $parent_field->id, array( 'field_order' => (int) $parent_field->field_order + 1 ) ); - } - } - - return $prepared; - } - /** * Reject a repeater or embedded form field with no usable child form. * diff --git a/classes/controllers/FrmAbilitiesFormsController.php b/classes/controllers/FrmAbilitiesFormsController.php index af244d092b..8b1fedd160 100644 --- a/classes/controllers/FrmAbilitiesFormsController.php +++ b/classes/controllers/FrmAbilitiesFormsController.php @@ -280,7 +280,7 @@ private static function get_create_form_input_schema() { 'fields' => array( 'type' => 'array', 'description' => __( - 'Array of field objects to create in the form. Each field represents a form input like text, dropdown, checkbox, etc.', + 'Fields to create, in display order. Fields after a section or repeater and before its end_divider go inside it.', 'formidable' ), 'items' => self::get_inline_field_schema(), @@ -604,19 +604,10 @@ public static function execute_create_form( $input ) { } if ( ! empty( $input['fields'] ) && is_array( $input['fields'] ) ) { - foreach ( $input['fields'] as $field ) { - // Share create-field's pipeline so inline fields get the same - // separate-value detection, repeater child form, and validation. - $prepared = FrmAbilitiesFieldsController::prepare_new_field( $field, $new_form ); + $created = ( new FrmAbilitiesInlineFieldsHelper( $new_form ) )->create( $input['fields'] ); - if ( is_wp_error( $prepared ) ) { - return FrmAbilitiesHelper::flatten_error( $prepared ); - } - - $prepared['form_id'] = $form_id; - - FrmField::create( $prepared ); - unset( $prepared, $field ); + if ( is_wp_error( $created ) ) { + return FrmAbilitiesHelper::flatten_error( $created ); } } diff --git a/classes/controllers/FrmHooksController.php b/classes/controllers/FrmHooksController.php index 09d40e4df2..04f495db11 100644 --- a/classes/controllers/FrmHooksController.php +++ b/classes/controllers/FrmHooksController.php @@ -193,6 +193,7 @@ public static function load_admin_hooks() { add_action( 'frm_after_field_options', 'FrmFormsController::logic_tip' ); add_action( 'frm_field_code_tab', 'FrmFormsController::field_part_shortcodes' ); add_filter( 'frm_fields_in_form_builder', 'FrmFormsController::update_form_builder_fields' ); + add_filter( 'frm_fields_in_form_builder', 'FrmFieldPlacementHelper::sort_for_builder', 5, 2 ); add_filter( 'set-screen-option', 'FrmFormsController::save_per_page', 10, 3 ); add_action( 'admin_footer', 'FrmFormsController::insert_form_popup' ); diff --git a/classes/helpers/FrmAbilitiesInlineFieldsHelper.php b/classes/helpers/FrmAbilitiesInlineFieldsHelper.php new file mode 100644 index 0000000000..eac72456ab --- /dev/null +++ b/classes/helpers/FrmAbilitiesInlineFieldsHelper.php @@ -0,0 +1,214 @@ +form = $form; + } + + /** + * Create the inline fields in order. + * + * @since x.x + * + * @param array $fields Inline field input, in order. + * + * @return true|WP_Error + */ + public function create( $fields ) { + foreach ( $fields as $field ) { + if ( ! is_array( $field ) ) { + continue; + } + + $field = FrmAbilitiesFieldsController::normalize_field_type( $field ); + $field = $this->place_in_open_sections( $field ); + + if ( is_wp_error( $field ) ) { + return $field; + } + + if ( ! $field ) { + // An end field with nothing open to close. + continue; + } + + $created = $this->create_field( $field ); + + if ( is_wp_error( $created ) ) { + return $created; + } + + if ( $created && FrmFieldPlacementHelper::is_container_type( $created->type ) ) { + $this->open[] = $created; + } + }//end foreach + + return $this->close_containers( 0 ); + } + + /** + * Close the sections a field is outside of, and give it the open section otherwise. + * + * @param array $field Inline field input. + * + * @return array|false|WP_Error The field to create, false to skip an end field that closes nothing. + */ + private function place_in_open_sections( $field ) { + $type = explode( '|', (string) ( $field['type'] ?? '' ) )[0]; + $top = $this->open ? end( $this->open ) : null; + + if ( $top && 'divider' === $top->type && in_array( $type, array( 'divider', 'break' ), true ) ) { + // The next section or page break closes a section, as on the frontend. + $closed = $this->close_containers( count( $this->open ) - 1 ); + + if ( is_wp_error( $closed ) ) { + return $closed; + } + } + + if ( FrmFieldPlacementHelper::is_end_type( $type ) ) { + return $this->close_for_end_field( $field, $type ); + } + + if ( ! $this->open ) { + return $field; + } + + if ( ! isset( $field['field_options']['in_section'] ) ) { + $field['field_options'] = isset( $field['field_options'] ) && is_array( $field['field_options'] ) ? $field['field_options'] : array(); + $field['field_options']['in_section'] = (int) end( $this->open )->id; + + return $field; + } + + // A field that names its own section is outside every open section nested deeper than that one. + $closed = $this->close_containers( $this->count_open_through( (int) $field['field_options']['in_section'] ) ); + + return is_wp_error( $closed ) ? $closed : $field; + } + + /** + * Close what an end field closes, so it is created as the last of them. + * + * @param array $field Inline end field input. + * @param string $type The end field type. + * + * @return array|false|WP_Error The end field, or false when it closes nothing. + */ + private function close_for_end_field( $field, $type ) { + for ( $i = count( $this->open ) - 1; $i >= 0; $i-- ) { + if ( FrmFieldPlacementHelper::get_end_type( $this->open[ $i ]->type ) !== $type ) { + continue; + } + + // Close anything still open inside it first, so each container gets its own end field. + $closed = $this->close_containers( $i + 1 ); + + if ( is_wp_error( $closed ) ) { + return $closed; + } + + array_pop( $this->open ); + + return $field; + } + + return false; + } + + /** + * Count the open containers up to and including the one a field names as its section. + * + * @param int $section_id The field's in_section. + * + * @return int How many containers stay open, 0 when the section is not open. + */ + private function count_open_through( $section_id ) { + foreach ( $this->open as $index => $container ) { + if ( (int) $container->id === $section_id ) { + return $index + 1; + } + } + + return 0; + } + + /** + * Create the end fields for open containers, innermost first. + * + * @param int $keep How many of the outermost containers stay open. + * + * @return true|WP_Error + */ + private function close_containers( $keep ) { + for ( $i = count( $this->open ); $i > $keep; $i-- ) { + $container = array_pop( $this->open ); + $created = $this->create_field( array( 'type' => FrmFieldPlacementHelper::get_end_type( $container->type ) ) ); + + if ( is_wp_error( $created ) ) { + return $created; + } + } + + return true; + } + + /** + * Create one inline field through create-field's pipeline. + * + * Sharing it gives inline fields the same separate-value detection, + * repeater child form, placement, and validation as create-field. + * + * @param array $field Inline field input. + * + * @return false|object|WP_Error The created field, false if it could not be saved. + */ + private function create_field( $field ) { + $prepared = FrmAbilitiesFieldsController::prepare_new_field( $field, $this->form ); + + if ( is_wp_error( $prepared ) ) { + return $prepared; + } + + $field_id = FrmField::create( $prepared ); + $created = $field_id ? FrmField::getOne( $field_id ) : null; + + return is_object( $created ) ? $created : false; + } +} diff --git a/classes/helpers/FrmFieldPlacementHelper.php b/classes/helpers/FrmFieldPlacementHelper.php index 03c0ff7b11..8a7c50f02d 100644 --- a/classes/helpers/FrmFieldPlacementHelper.php +++ b/classes/helpers/FrmFieldPlacementHelper.php @@ -85,4 +85,607 @@ public static function is_allowed( $type, $form_id, $section_id = 0 ) { return FrmFieldFactory::get_field_type( $type )->allows_in_repeater(); } + + /** + * Order builder fields the way the frontend groups them. + * + * The frontend renders a repeater's rows from the fields of its child form, + * whatever their field_order, and renders every parent form field once. The + * builder nests a field by where its field_order falls between a divider and + * its end_divider. So a child field ordered past the end_divider showed below + * the repeater in the builder, and a parent field ordered inside the repeater + * looked repeated. Listing each child form right after its repeater, and + * parent form fields from inside a repeater right after its end_divider, makes + * the builder show what the form renders. Saving the builder then stores the + * matching field_order values. + * + * @since x.x + * + * @param array $fields Builder field objects sorted by field_order. + * @param array $args The filter arguments. Contains `form`. + * + * @return array + */ + public static function sort_for_builder( $fields, $args = array() ) { + $form = $args['form'] ?? null; + + if ( ! is_array( $fields ) || ! is_object( $form ) || empty( $form->id ) ) { + return $fields; + } + + $form_id = (int) $form->id; + $top_level = array(); + $child_forms = array(); + + foreach ( $fields as $field ) { + if ( ! is_object( $field ) || ! isset( $field->form_id, $field->type ) ) { + // Leave a list changed by another filter alone. + return $fields; + } + + if ( (int) $field->form_id === $form_id ) { + $top_level[] = $field; + } else { + $child_forms[ (int) $field->form_id ][] = $field; + } + } + + $sorted = array(); + $deferred = array(); + $open_repeater = false; + + foreach ( $top_level as $field ) { + if ( $open_repeater ) { + if ( 'end_divider' === $field->type ) { + $sorted[] = $field; + $sorted = array_merge( $sorted, $deferred ); + $deferred = array(); + $open_repeater = false; + continue; + } + + if ( ! in_array( $field->type, array( 'divider', 'break' ), true ) ) { + // Rendered once on the frontend, so it is shown after the repeater. + $deferred[] = $field; + continue; + } + + // A section or page break before the end_divider closes the repeater, like the frontend. + $sorted = array_merge( $sorted, $deferred ); + $deferred = array(); + $open_repeater = false; + } + + $sorted[] = $field; + + if ( ! FrmField::is_repeating_field( $field ) ) { + continue; + } + + $child_form_id = (int) FrmField::get_option( $field, 'form_select' ); + + if ( isset( $child_forms[ $child_form_id ] ) ) { + $sorted = array_merge( $sorted, $child_forms[ $child_form_id ] ); + unset( $child_forms[ $child_form_id ] ); + } + + $open_repeater = true; + }//end foreach + + $sorted = array_merge( $sorted, $deferred ); + + if ( ! $child_forms ) { + return $sorted; + } + + // No repeater renders these, so they never show on the frontend. Keep them editable, before the submit button. + $submit = $sorted && FrmSubmitHelper::FIELD_TYPE === end( $sorted )->type ? array_pop( $sorted ) : null; + + foreach ( $child_forms as $unrendered ) { + $sorted = array_merge( $sorted, $unrendered ); + } + + if ( $submit ) { + $sorted[] = $submit; + } + + return $sorted; + } + + /** + * Find the repeater that renders a child form's fields on the frontend. + * + * @since x.x + * + * @param int $child_form_id Repeater child form ID. + * + * @return false|object The repeater field, or false when no repeater uses the form. + */ + public static function get_repeater_for_child_form( $child_form_id ) { + $child_form = $child_form_id ? FrmForm::getOne( $child_form_id ) : false; + + if ( ! $child_form || empty( $child_form->parent_form_id ) ) { + return false; + } + + foreach ( FrmField::get_all_types_in_form( $child_form->parent_form_id, 'divider' ) as $divider ) { + if ( is_object( $divider ) && FrmField::is_repeating_field( $divider ) && (int) FrmField::get_option( $divider, 'form_select' ) === (int) $child_form->id ) { + return $divider; + } + } + + return false; + } + + /** + * Settle where a new field goes, using the rules the frontend renders with. + * + * The frontend repeats the fields of a repeater's child form and groups the + * other fields into sections by field_order. So in_section and form_id are + * made to agree with each other before the field is saved: a field placed in + * a repeater is stored in its child form, a field created on a child form + * gets that repeater as its section, and a field given a field_order inside + * a section or repeater joins it. A field with no field_order is put at the + * end of its section or repeater rather than at the end of the form. + * + * @since x.x + * + * @param array $values Prepared field values, including form_id and field_options. + * @param bool $has_order True when the caller chose the field_order. + * + * @return array|WP_Error + */ + public static function prepare_new_placement( $values, $has_order ) { + $form = FrmForm::getOne( $values['form_id'] ?? 0 ); + + if ( ! $form ) { + return $values; + } + + $top_form_id = $form->parent_form_id ? (int) $form->parent_form_id : (int) $form->id; + $type = explode( '|', $values['type'] ?? '' )[0]; + $ends = self::get_container_ends(); + + if ( in_array( $type, $ends, true ) ) { + // Closing fields only render in the top-level form. + $values['form_id'] = $top_form_id; + + if ( ! $has_order ) { + $values['field_order'] = self::get_next_order( $top_form_id ); + } + + return $values; + } + + $section = self::get_requested_section( $values, $top_form_id, $form ); + + if ( is_wp_error( $section ) ) { + return $section; + } + + if ( $has_order && (int) $form->id === $top_form_id ) { + // The frontend groups top-level fields by order, so the order decides the section. + $at_order = self::get_container_at_order( $top_form_id, (int) $values['field_order'] ); + + if ( ! $section ) { + // A field that cannot go in that section, like a page break, keeps its top-level placement. + $section = $at_order && self::is_allowed( $type, $top_form_id, (int) $at_order->id ) ? $at_order : false; + } elseif ( ! FrmField::is_repeating_field( $section ) && ( ! $at_order || (int) $at_order->id !== (int) $section->id ) ) { + // Repeaters render their child form whatever the order, but a section is only the fields ordered inside it. + return new WP_Error( + 'frm_field_order_outside_section', + __( 'field_order is outside the section set in field_options.in_section. Leave field_order out to add the field at the end of the section.', 'formidable' ), + array( 'status' => 400 ) + ); + } + } + + $values['field_options']['in_section'] = $section ? (int) $section->id : 0; + $values['form_id'] = $top_form_id; + + if ( $section && FrmField::is_repeating_field( $section ) ) { + $values['form_id'] = (int) FrmField::get_option( $section, 'form_select' ); + } elseif ( ! $section && (int) $form->id !== $top_form_id ) { + // A child form no repeater points to. Keep the field where it was asked for. + $values['form_id'] = (int) $form->id; + } + + if ( ! $has_order ) { + $values['field_order'] = $section ? self::get_order_at_section_end( $section ) : self::get_next_order( $top_form_id ); + } + + return $values; + } + + /** + * Settle where an updated field goes when the update changes its section. + * + * Uses the same rules as prepare_new_placement(), so moving a field into a + * repeater stores it in the child form, and moving it out stores it in the + * top-level form, like dragging it in the builder. Updates that keep the + * stored in_section are left alone, even for a legacy field whose form_id + * disagrees with it, because callers often resend every stored option and + * moving the field would hide its saved entry values. + * + * @since x.x + * + * @param stdClass $field The stored field. + * @param array $field_data Field data to update, with field_options merged over the stored options. + * @param array $input The update input as sent, before stored options were merged in. + * + * @return array|WP_Error + */ + public static function prepare_update_placement( $field, $field_data, $input ) { + if ( ! isset( $input['field_options']['in_section'], $field_data['field_options']['in_section'] ) || ! is_numeric( $input['field_options']['in_section'] ) ) { + return $field_data; + } + + $section_id = (int) $field_data['field_options']['in_section']; + $type = $field_data['type'] ?? $field->type; + + if ( $section_id === (int) FrmField::get_option( $field, 'in_section' ) || self::is_end_type( $type ) ) { + return $field_data; + } + + $has_order = isset( $input['field_order'] ); + $values = array( + 'type' => $type, + 'form_id' => $field->form_id, + 'field_order' => $field_data['field_order'] ?? $field->field_order, + 'field_options' => $field_data['field_options'], + ); + + if ( ! $section_id ) { + // Moving out of a section or repeater. The top-level form is the destination. + $form = FrmForm::getOne( $field->form_id ); + + if ( $form && $form->parent_form_id ) { + $values['form_id'] = (int) $form->parent_form_id; + } + } + + $values = self::prepare_new_placement( $values, $has_order ); + + if ( is_wp_error( $values ) ) { + return $values; + } + + $field_data['field_options']['in_section'] = $values['field_options']['in_section']; + + if ( (int) $values['form_id'] !== (int) $field->form_id ) { + $field_data['form_id'] = (int) $values['form_id']; + } + + if ( ! $has_order ) { + $field_data['field_order'] = (int) $values['field_order']; + } + + return $field_data; + } + + /** + * Get the fields that close a deleted section, and the ones inside it. + * + * @since x.x + * + * @param stdClass $container Section or other container field. + * + * @return array{end: false|object, children: array} The closing field, if any, and the + * top-level fields with in_section set to the container. + */ + public static function get_container_parts( $container ) { + $map = self::map_containers( (int) $container->form_id ); + $end = $map['ends'][ (int) $container->id ] ?? false; + $children = array(); + + foreach ( $map['fields'] as $field ) { + if ( (int) FrmField::get_option( $field, 'in_section' ) === (int) $container->id ) { + $children[] = $field; + } + } + + return array( + 'end' => is_object( $end ) && self::is_end_type( $end->type ) ? $end : false, + 'children' => $children, + ); + } + + /** + * Remove what a deleted section or repeater leaves behind. + * + * The builder deletes a section's end field and its fields with it, and Pro + * deletes a repeater's child form, but only in the admin. Without this a + * deleted section left an end field that closes nothing, fields pointing at + * a section that is gone, and an empty child form. Fields of a plain section + * are kept and move up to the section's own container, since deleting a + * heading should not delete what was under it. + * + * @since x.x + * + * @param stdClass $field The deleted container field. + * @param array $parts Its closing field and child fields, from get_container_parts(). + * + * @return void + */ + public static function clean_up_deleted_container( $field, $parts ) { + if ( $parts['end'] ) { + FrmField::destroy( $parts['end']->id ); + } + + $parent_section = (int) FrmField::get_option( $field, 'in_section' ); + + foreach ( $parts['children'] as $child ) { + $options = (array) $child->field_options; + $options['in_section'] = $parent_section; + FrmField::update( $child->id, array( 'field_options' => $options ) ); + } + + $child_form = FrmField::is_repeating_field( $field ) ? FrmForm::getOne( (int) FrmField::get_option( $field, 'form_select' ) ) : false; + + if ( $child_form && (int) $child_form->parent_form_id === (int) $field->form_id ) { + FrmForm::destroy( $child_form->id ); + } + } + + /** + * Check whether a field type opens a builder container, like a section. + * + * @since x.x + * + * @param string $type Field type. + * + * @return bool + */ + public static function is_container_type( $type ) { + return isset( self::get_container_ends()[ $type ] ); + } + + /** + * Check whether a field type closes a builder container, like an end_divider. + * + * @since x.x + * + * @param string $type Field type. + * + * @return bool + */ + public static function is_end_type( $type ) { + return in_array( $type, self::get_container_ends(), true ); + } + + /** + * Get the field type that closes a container type. + * + * @since x.x + * + * @param string $type Container field type, like divider. + * + * @return string The end field type, or an empty string when the type is not a container. + */ + public static function get_end_type( $type ) { + return self::get_container_ends()[ $type ] ?? ''; + } + + /** + * Get the section a new field asks for, after checking it can hold fields. + * + * @param array $values Prepared field values. + * @param int $top_form_id The top-level form. + * @param stdClass $form The form the field was created on. + * + * @return false|object|WP_Error + */ + private static function get_requested_section( $values, $top_form_id, $form ) { + $section_id = (int) ( $values['field_options']['in_section'] ?? 0 ); + + if ( ! $section_id ) { + // A field created on a child form belongs to the repeater that renders it. + return (int) $form->id === $top_form_id ? false : self::get_repeater_for_child_form( $form->id ); + } + + $section = FrmField::getOne( $section_id ); + + if ( ! $section || (int) $section->form_id !== $top_form_id || ! self::is_container_type( $section->type ) ) { + return new WP_Error( + 'frm_invalid_section', + __( 'field_options.in_section must be the ID of a section or repeater on the same form.', 'formidable' ), + array( 'status' => 400 ) + ); + } + + if ( FrmField::is_repeating_field( $section ) ) { + $child_form = FrmForm::getOne( (int) FrmField::get_option( $section, 'form_select' ) ); + + if ( ! $child_form || (int) $child_form->parent_form_id !== $top_form_id ) { + return new WP_Error( + 'frm_invalid_section', + __( 'That repeater has no child form, so fields cannot be added to it. Set its field_options.form_select first.', 'formidable' ), + array( 'status' => 400 ) + ); + } + } + + return $section; + } + + /** + * Get the section or repeater a field_order falls in, as the frontend groups fields. + * + * @param int $form_id Top-level form ID. + * @param int $order The field_order. A field already at this order is pushed after it. + * + * @return false|object + */ + private static function get_container_at_order( $form_id, $order ) { + $map = self::map_containers( $form_id ); + $container = 0; + + foreach ( $map['fields'] as $field ) { + if ( (int) $field->field_order >= $order ) { + break; + } + + $container = $map['after'][ (int) $field->id ]; + } + + $field = $container ? FrmField::getOne( $container ) : null; + + return is_object( $field ) ? $field : false; + } + + /** + * Get the field_order at the end of a section or repeater, and make room there. + * + * @param stdClass $section Section or repeater field. + * + * @return int + */ + private static function get_order_at_section_end( $section ) { + $map = self::map_containers( (int) $section->form_id ); + $end = $map['ends'][ (int) $section->id ] ?? false; + $order = (int) $section->field_order; + + foreach ( $map['fields'] as $field ) { + if ( $end && (int) $field->field_order >= (int) $end->field_order ) { + break; + } + + if ( (int) $field->field_order > $order && $map['after'][ (int) $field->id ] === (int) $section->id ) { + $order = (int) $field->field_order; + } + } + + if ( FrmField::is_repeating_field( $section ) ) { + foreach ( FrmField::get_all_for_form( (int) FrmField::get_option( $section, 'form_select' ), '', 'exclude', 'exclude' ) as $child ) { + if ( ! $end || (int) $child->field_order < (int) $end->field_order ) { + $order = max( $order, (int) $child->field_order ); + } + } + } + + if ( ! $end ) { + // Nothing closes it, so the end of the form is the end of the section. + return max( $order + 1, self::get_next_order( (int) $section->form_id ) ); + } + + ++$order; + self::shift_orders( (int) $section->form_id, $order ); + + return $order; + } + + /** + * Get the field_order after every field of a form and its repeaters. + * + * @param int $form_id Top-level form ID. + * + * @return int + */ + private static function get_next_order( $form_id ) { + $max = 0; + + foreach ( FrmField::get_all_for_form( $form_id, '', 'exclude', 'include' ) as $field ) { + $max = max( $max, (int) $field->field_order ); + } + + return $max + 1; + } + + /** + * Move every field of a form and its repeaters at or after a field_order down by one. + * + * @param int $form_id Top-level form ID. + * @param int $order The first field_order to move. + * + * @return void + */ + private static function shift_orders( $form_id, $order ) { + foreach ( FrmField::get_all_for_form( $form_id, '', 'exclude', 'include' ) as $field ) { + if ( (int) $field->field_order >= $order ) { + FrmField::update( $field->id, array( 'field_order' => (int) $field->field_order + 1 ) ); + } + } + } + + /** + * Walk a top-level form's fields and record which container each one leaves open. + * + * Mirrors the frontend: a container opens at its field and closes at its end + * field. A section is also closed by the next section or page break, as + * FrmFormsHelper::auto_add_end_section_fields() does. + * + * @param int $form_id Top-level form ID. + * + * @return array{fields: array, after: array, ends: array} The fields + * in order, the container open after each field, and the field closing each container. + */ + private static function map_containers( $form_id ) { + $ends = self::get_container_ends(); + $fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' ); + $open = array(); + $after = array(); + $closed = array(); + + foreach ( $fields as $field ) { + $top = $open ? end( $open ) : null; + + if ( $top && 'divider' === $top->type && in_array( $field->type, array( 'divider', 'break' ), true ) ) { + $closed[ (int) $top->id ] = $field; + array_pop( $open ); + } + + if ( isset( $ends[ $field->type ] ) ) { + $open[] = $field; + } elseif ( in_array( $field->type, $ends, true ) ) { + self::close_container( $open, $closed, $field, $ends ); + } + + $after[ (int) $field->id ] = $open ? (int) end( $open )->id : 0; + } + + return array( + 'fields' => array_values( $fields ), + 'after' => $after, + 'ends' => $closed, + ); + } + + /** + * Close the innermost open container that the end field belongs to. + * + * @param stdClass[] $open Open containers, innermost last. + * @param stdClass[] $closed The field closing each container, keyed by container ID. + * @param stdClass $end The end field. + * @param string[] $ends End field type for each container type. + * + * @return void + */ + private static function close_container( &$open, &$closed, $end, $ends ) { + for ( $i = count( $open ) - 1; $i >= 0; $i-- ) { + if ( $ends[ $open[ $i ]->type ] === $end->type ) { + $closed[ (int) $open[ $i ]->id ] = $end; + array_splice( $open, $i ); + return; + } + } + } + + /** + * Get the end field type for each container type the builder supports. + * + * @return array End field type keyed by container field type. + */ + private static function get_container_ends() { + $ends = array( 'divider' => 'end_divider' ); + + if ( ! is_callable( 'FrmProBuilderContainersHelper::get_containers' ) ) { + return $ends; + } + + foreach ( FrmProBuilderContainersHelper::get_containers() as $type => $container ) { + $ends[ $type ] = $container['end']; + } + + return $ends; + } } diff --git a/stubs.php b/stubs.php index 137a9897a5..2720c13520 100644 --- a/stubs.php +++ b/stubs.php @@ -351,6 +351,13 @@ class FrmProField { public static function create_repeat_form( $field_id, $args = array() ) { } } + class FrmProBuilderContainersHelper { + /** + * @return array}> Closing field and permitted parent types, keyed by container type. + */ + public static function get_containers() { + } + } class FrmProEntriesHelper { public static function get_search_str( $where_clause, $search_str, $form_id = 0, $fid = '' ) { } diff --git a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php new file mode 100644 index 0000000000..2e5fa53fb0 --- /dev/null +++ b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php @@ -0,0 +1,514 @@ +markTestSkipped( 'The Abilities API is not available in this WordPress install.' ); + } + + $this->set_current_user_to_1(); + + $frm_settings = FrmAppHelper::get_settings(); + $frm_settings->mcp = 1; + $frm_settings->store(); + FrmMcpController::reset(); + + WP_Ability_Categories_Registry::get_instance(); + WP_Abilities_Registry::get_instance(); + + remove_all_actions( 'wp_abilities_api_categories_init' ); + remove_all_actions( 'wp_abilities_api_init' ); + + add_action( 'wp_abilities_api_categories_init', array( 'FrmAbilitiesController', 'register_categories' ) ); + add_action( 'wp_abilities_api_init', array( 'FrmAbilitiesController', 'register_abilities' ) ); + + do_action( 'wp_abilities_api_categories_init' ); + do_action( 'wp_abilities_api_init' ); + } + + /** + * @param string $slug Ability slug, without the formidable-forms prefix. + * @param array $input Ability input parameters. + * + * @return mixed + */ + private function execute( $slug, $input = array() ) { + $ability = wp_get_ability( 'formidable-forms/' . $slug ); + + $this->assertInstanceOf( \WP_Ability::class, $ability, 'The ' . $slug . ' ability should be registered.' ); + + return $ability->execute( $input ); + } + + /** + * A field put in a repeater from the parent form must be stored in the child form, + * or the frontend renders it once while the builder shows it in the repeater. + * + * @return void + */ + public function test_create_field_in_a_repeater_is_stored_in_its_child_form() { + $layout = $this->create_repeater_layout(); + $created = $this->execute( + 'create-field', + array( + 'form_id' => $layout['form'], + 'type' => 'text', + 'name' => 'Item', + 'field_options' => array( 'in_section' => $layout['repeater'] ), + ) + ); + + $this->assertNotWPError( $created ); + $this->assertSame( $layout['child'], $created['form_id'], 'The frontend only repeats fields of the child form.' ); + $this->assert_between( $created['id'], $layout['repeater'], $layout['end'] ); + } + + /** + * @return void + */ + public function test_create_field_on_a_child_form_joins_its_repeater_before_the_end_divider() { + $layout = $this->create_repeater_layout(); + // A child field already ordered past the end_divider must not pull new fields out with it. + $this->store_field( 'text', $layout['child'], 50, array( 'in_section' => $layout['repeater'] ) ); + + $created = $this->execute( + 'create-field', + array( + 'form_id' => $layout['child'], + 'type' => 'text', + 'name' => 'Added', + ) + ); + + $this->assertNotWPError( $created ); + $this->assertSame( $layout['repeater'], (int) $created['field_options']['in_section'], 'A child form field belongs to the repeater that renders it.' ); + $this->assert_between( $created['id'], $layout['repeater'], $layout['end'] ); + $end_order = (int) FrmField::getOne( $layout['end'] )->field_order; + $this->assertLessThan( (int) FrmField::getOne( $layout['after'] )->field_order, $end_order, 'Fields after the repeater move down to make room.' ); + } + + /** + * @return void + */ + public function test_create_field_rejects_an_in_section_that_is_not_a_section_on_the_form() { + $layout = $this->create_repeater_layout(); + $other = $this->create_repeater_layout(); + $missing = FrmForm::create( $this->empty_form_values( $layout['form'] ) ); + $no_child = $this->store_field( + 'divider', + $layout['form'], + 20, + array( + 'repeat' => 1, + 'form_select' => $missing, + ) + ); + $candidates = array( + 'a missing field' => 999999, + 'another form section' => $other['repeater'], + 'a field that is not a container' => $layout['after'], + ); + FrmForm::destroy( $missing ); + $candidates['a repeater whose child form is gone'] = $no_child; + + foreach ( $candidates as $label => $section_id ) { + $result = $this->execute( + 'create-field', + array( + 'form_id' => $layout['form'], + 'type' => 'text', + 'field_options' => array( 'in_section' => $section_id ), + ) + ); + + $this->assertWPError( $result, 'in_section set to ' . $label . ' must be rejected.' ); + } + } + + /** + * @return void + */ + public function test_create_field_in_a_section_without_an_order_is_added_before_its_end_divider() { + $form = $this->create_empty_form(); + $section = $this->store_field( 'divider', $form, 1 ); + $inside = $this->store_field( 'text', $form, 2, array( 'in_section' => $section ) ); + $end = $this->store_field( 'end_divider', $form, 3 ); + $after = $this->store_field( 'text', $form, 4 ); + + $created = $this->execute( + 'create-field', + array( + 'form_id' => $form, + 'type' => 'text', + 'field_options' => array( 'in_section' => $section ), + ) + ); + + $this->assertNotWPError( $created ); + $this->assertSame( 3, $created['field_order'], 'The field goes at the end of the section.' ); + $this->assertSame( 2, (int) FrmField::getOne( $inside )->field_order ); + $this->assertSame( 4, (int) FrmField::getOne( $end )->field_order, 'The end_divider moves down so the field stays inside.' ); + $this->assertSame( 5, (int) FrmField::getOne( $after )->field_order ); + } + + /** + * @return void + */ + public function test_create_field_with_an_order_outside_its_section_is_rejected() { + $form = $this->create_empty_form(); + $section = $this->store_field( 'divider', $form, 1 ); + $this->store_field( 'end_divider', $form, 2 ); + $this->store_field( 'text', $form, 3 ); + + $result = $this->execute( + 'create-field', + array( + 'form_id' => $form, + 'type' => 'text', + 'field_order' => 4, + 'field_options' => array( 'in_section' => $section ), + ) + ); + + $this->assertWPError( $result, 'The frontend would show the field outside the section it names.' ); + } + + /** + * The frontend groups top-level fields by order, so an order inside a section or repeater joins it. + * + * @return void + */ + public function test_create_field_with_an_order_inside_a_section_or_repeater_joins_it() { + $layout = $this->create_repeater_layout(); + + $in_repeater = $this->execute( + 'create-field', + array( + 'form_id' => $layout['form'], + 'type' => 'text', + 'field_order' => (int) FrmField::getOne( $layout['end'] )->field_order, + ) + ); + + $this->assertNotWPError( $in_repeater ); + $this->assertSame( $layout['child'], $in_repeater['form_id'] ); + $this->assertSame( $layout['repeater'], (int) $in_repeater['field_options']['in_section'] ); + + $form = $this->create_empty_form(); + $section = $this->store_field( 'divider', $form, 10 ); + $this->store_field( 'end_divider', $form, 20 ); + + $in_section = $this->execute( + 'create-field', + array( + 'form_id' => $form, + 'type' => 'text', + 'field_order' => 15, + ) + ); + $this->assertNotWPError( $in_section ); + $this->assertSame( $section, (int) $in_section['field_options']['in_section'] ); + + // Page breaks are a Pro field type, so check the shared placement step directly. + $page_break = FrmFieldPlacementHelper::prepare_new_placement( + array( + 'type' => 'break', + 'form_id' => $form, + 'field_order' => 16, + 'field_options' => array(), + ), + true + ); + $this->assertNotWPError( $page_break, 'A type sections cannot hold keeps its old top-level placement.' ); + $this->assertSame( 0, (int) $page_break['field_options']['in_section'] ); + } + + /** + * @return void + */ + public function test_create_end_divider_on_a_child_form_is_stored_after_every_field() { + $layout = $this->create_repeater_layout(); + $late = $this->store_field( 'text', $layout['child'], 70, array( 'in_section' => $layout['repeater'] ) ); + // end_divider is a Pro field type, so check the shared placement step directly. + $created = FrmFieldPlacementHelper::prepare_new_placement( + array( + 'type' => 'end_divider', + 'form_id' => $layout['child'], + 'field_order' => 1, + 'field_options' => array(), + ), + false + ); + + $this->assertNotWPError( $created ); + $this->assertSame( $layout['form'], $created['form_id'], 'End fields only render in the top-level form.' ); + $this->assertGreaterThan( (int) FrmField::getOne( $late )->field_order, $created['field_order'], 'A new end field must not land on a child field order.' ); + } + + /** + * @return void + */ + public function test_update_field_moves_a_field_into_and_out_of_a_repeater() { + $layout = $this->create_repeater_layout(); + + $moved_in = $this->execute( + 'update-field', + array( + 'id' => $layout['after'], + 'field_options' => array( 'in_section' => $layout['repeater'] ), + ) + ); + + $this->assertNotWPError( $moved_in ); + $this->assertSame( $layout['child'], $moved_in['form_id'], 'Moving into a repeater stores the field in the child form, like a builder drag.' ); + $this->assert_between( $layout['after'], $layout['repeater'], $layout['end'] ); + + $moved_out = $this->execute( + 'update-field', + array( + 'id' => $layout['after'], + 'field_options' => array( 'in_section' => 0 ), + ) + ); + + $this->assertNotWPError( $moved_out ); + $this->assertSame( $layout['form'], $moved_out['form_id'], 'Moving out of a repeater stores the field in the top-level form.' ); + $this->assertGreaterThan( (int) FrmField::getOne( $layout['end'] )->field_order, $moved_out['field_order'], 'With no order given, the field goes after the repeater.' ); + } + + /** + * Callers often resend every stored option, so an unchanged in_section must not move a legacy field. + * + * @return void + */ + public function test_update_field_keeps_a_legacy_placement_when_in_section_is_unchanged() { + $layout = $this->create_repeater_layout(); + $legacy = $this->store_field( 'text', $layout['form'], 2, array( 'in_section' => $layout['repeater'] ) ); + $stored = FrmField::getOne( $legacy ); + + $updated = $this->execute( + 'update-field', + array( + 'id' => $legacy, + 'name' => 'Renamed', + 'field_options' => (array) $stored->field_options, + ) + ); + + $this->assertNotWPError( $updated ); + $this->assertSame( $layout['form'], $updated['form_id'], 'Its saved entry values stay with the form they were saved on.' ); + $this->assertSame( (int) $stored->field_order, $updated['field_order'] ); + } + + /** + * @return void + */ + public function test_delete_section_removes_its_end_and_keeps_its_fields() { + $form = $this->create_empty_form(); + $section = $this->store_field( 'divider', $form, 1 ); + $inside = $this->store_field( 'text', $form, 2, array( 'in_section' => $section ) ); + $end = $this->store_field( 'end_divider', $form, 3 ); + + $this->assertNotWPError( $this->execute( 'delete-field', array( 'id' => $section ) ) ); + + $this->assertNull( FrmField::getOne( $end ), 'An end_divider with nothing to close must go with its section.' ); + $this->assertNotNull( FrmField::getOne( $inside ), 'Deleting a heading must not delete the fields under it.' ); + $this->assertSame( 0, (int) FrmField::get_option( FrmField::getOne( $inside ), 'in_section' ), 'The kept field must not point at a deleted section.' ); + } + + /** + * @return void + */ + public function test_delete_repeater_removes_its_child_form_and_end() { + $layout = $this->create_repeater_layout(); + $legacy = $this->store_field( 'text', $layout['form'], 2, array( 'in_section' => $layout['repeater'] ) ); + + $this->assertNotWPError( $this->execute( 'delete-field', array( 'id' => $layout['repeater'] ) ) ); + + $this->assertNull( FrmField::getOne( $layout['end'] ) ); + $this->assertNull( FrmField::getOne( $layout['item'] ), 'The child form fields go with the repeater.' ); + $this->assertEmpty( FrmForm::getOne( $layout['child'] ), 'An empty child form must not be left linked to the parent.' ); + $this->assertSame( 0, (int) FrmField::get_option( FrmField::getOne( $legacy ), 'in_section' ) ); + $this->assertNotNull( FrmField::getOne( $layout['after'] ), 'Fields after the repeater are untouched.' ); + } + + /** + * Inline fields cannot name their section by ID, so their order places them, and open sections get closed. + * + * @return void + */ + public function test_create_form_places_inline_fields_in_their_sections() { + $created = $this->execute( + 'create-form', + array( + 'name' => 'Inline sections', + // Without Pro, end_divider is not an inline type, so every section here is closed automatically. + 'fields' => array( + array( + 'type' => 'text', + 'name' => 'Before', + ), + array( + 'type' => 'section', + 'name' => 'First', + ), + array( + 'type' => 'text', + 'name' => 'In first', + ), + array( + 'type' => 'section', + 'name' => 'Second', + ), + array( + 'type' => 'text', + 'name' => 'In second', + ), + array( + 'type' => 'text', + 'name' => 'Also in second', + ), + array( + 'type' => 'text', + 'name' => 'After', + 'field_options' => array( 'in_section' => 0 ), + ), + ), + ) + ); + + $this->assertNotWPError( $created ); + + $fields = FrmField::get_all_for_form( $created['id'] ); + $names = array(); + $ids = array(); + $section = array(); + + foreach ( $fields as $field ) { + $names[] = $field->type . ':' . $field->name; + $ids[ $field->name ] = (int) $field->id; + $section[ $field->name ] = (int) FrmField::get_option( $field, 'in_section' ); + } + + $end = FrmFieldsHelper::setup_new_vars( 'end_divider' )['name']; + $this->assertSame( + array( + 'text:Before', + 'divider:First', + 'text:In first', + 'end_divider:' . $end, + 'divider:Second', + 'text:In second', + 'text:Also in second', + 'end_divider:' . $end, + 'text:After', + ), + $names, + 'Every section must be closed exactly once, in order.' + ); + $this->assertSame( $ids['First'], $section['In first'] ); + $this->assertSame( $ids['Second'], $section['In second'] ); + $this->assertSame( $ids['Second'], $section['Also in second'] ); + $this->assertSame( 0, $section['After'], 'An explicit in_section of 0 takes the field out of the open section.' ); + } + + /** + * Build a form with a repeater whose child form has one field, plus a field after it. + * + * @return array + */ + private function create_repeater_layout() { + $form = $this->create_empty_form(); + $child = (int) FrmForm::create( $this->empty_form_values( $form ) ); + $repeater = $this->store_field( + 'divider', + $form, + 1, + array( + 'repeat' => 1, + 'form_select' => $child, + ) + ); + $item = $this->store_field( 'text', $child, 2, array( 'in_section' => $repeater ) ); + $end = $this->store_field( 'end_divider', $form, 3 ); + $after = $this->store_field( 'text', $form, 4 ); + + return compact( 'form', 'child', 'repeater', 'item', 'end', 'after' ); + } + + /** + * The form factory adds a text field to every form, which these layouts must not have. + * + * @return int + */ + private function create_empty_form() { + return (int) FrmForm::create( $this->empty_form_values() ); + } + + /** + * @param int $parent_form_id Parent form, 0 for a top-level form. + * + * @return array + */ + private function empty_form_values( $parent_form_id = 0 ) { + return array( + 'form_key' => '', + 'name' => 'Placement', + 'description' => '', + 'parent_form_id' => $parent_form_id, + ); + } + + /** + * Store a field exactly as given, the way legacy data and imports can hold it. + * + * @param string $type Field type. + * @param int $form_id Form ID. + * @param int $order Field order. + * @param array $options Field options to merge over the defaults. + * + * @return int + */ + private function store_field( $type, $form_id, $order, $options = array() ) { + $values = FrmFieldsHelper::setup_new_vars( $type, $form_id ); + $values['field_order'] = $order; + $values['field_options'] = array_merge( $values['field_options'], $options ); + $field_id = FrmField::create( $values ); + + $this->assertNotFalse( $field_id, 'The ' . $type . ' test field should be created.' ); + + return (int) $field_id; + } + + /** + * @param int $field_id Field that must sit inside the window. + * @param int $start_id Field that opens the window. + * @param int $end_id Field that closes the window. + * + * @return void + */ + private function assert_between( $field_id, $start_id, $end_id ) { + $order = (int) FrmField::getOne( $field_id )->field_order; + + $this->assertGreaterThan( (int) FrmField::getOne( $start_id )->field_order, $order, 'The field must be ordered after the section opens.' ); + $this->assertLessThan( (int) FrmField::getOne( $end_id )->field_order, $order, 'The field must be ordered before the section closes.' ); + } +} diff --git a/tests/phpunit/fields/test_FrmSectionStructure.php b/tests/phpunit/fields/test_FrmSectionStructure.php new file mode 100644 index 0000000000..48aa45c1e8 --- /dev/null +++ b/tests/phpunit/fields/test_FrmSectionStructure.php @@ -0,0 +1,268 @@ +form_id = $this->create_empty_form(); + } + + /** + * @return void + */ + public function test_builder_order_is_unchanged_without_repeaters() { + $ids = array( + $this->add_field( 'text', 1 ), + $this->add_field( 'divider', 2 ), + $this->add_field( 'text', 3 ), + $this->add_field( 'end_divider', 4 ), + $this->add_field( 'text', 5 ), + ); + + $this->assertSame( $ids, $this->get_builder_order(), 'A form with no repeater must keep its stored order.' ); + } + + /** + * @return void + */ + public function test_builder_shows_child_fields_ordered_after_the_end_divider_inside_the_repeater() { + $child = $this->create_child_form(); + $before = $this->add_field( 'text', 1 ); + $repeater = $this->add_repeater( 2, $child ); + $end = $this->add_field( 'end_divider', 3 ); + // Same orders as a real broken form: the children sort after the end_divider. + $first = $this->add_field( 'text', 4, array( 'in_section' => $repeater ), $child ); + $second = $this->add_field( 'text', 5, array( 'in_section' => $repeater ), $child ); + + $this->assertSame( array( $before, $repeater, $first, $second, $end ), $this->get_builder_order() ); + } + + /** + * @return void + */ + public function test_builder_shows_child_fields_ordered_before_the_divider_inside_the_repeater() { + $child = $this->create_child_form(); + $orphaned = $this->add_field( 'text', 1, array(), $child ); + $top = $this->add_field( 'text', 5 ); + $repeater = $this->add_repeater( 10, $child ); + $end = $this->add_field( 'end_divider', 11 ); + + $this->assertSame( array( $top, $repeater, $orphaned, $end ), $this->get_builder_order() ); + } + + /** + * @return void + */ + public function test_builder_moves_parent_fields_inside_a_repeater_after_it() { + $child = $this->create_child_form(); + $repeater = $this->add_repeater( 1, $child ); + $in_child = $this->add_field( 'text', 2, array( 'in_section' => $repeater ), $child ); + $in_parent = $this->add_field( 'text', 3, array( 'in_section' => $repeater ) ); + $end = $this->add_field( 'end_divider', 4 ); + $after = $this->add_field( 'text', 5 ); + + $this->assertSame( + array( $repeater, $in_child, $end, $in_parent, $after ), + $this->get_builder_order(), + 'A parent form field is rendered once on the frontend, so the builder must not show it as repeated.' + ); + } + + /** + * @return void + */ + public function test_builder_closes_a_repeater_at_a_section_or_page_break_without_an_end_divider() { + $child = $this->create_child_form(); + $repeater = $this->add_repeater( 1, $child ); + $in_parent = $this->add_field( 'text', 2 ); + $break = $this->add_field( 'break', 3 ); + $after = $this->add_field( 'text', 4 ); + $in_child = $this->add_field( 'text', 9, array( 'in_section' => $repeater ), $child ); + + $this->assertSame( array( $repeater, $in_child, $in_parent, $break, $after ), $this->get_builder_order() ); + } + + /** + * @return void + */ + public function test_builder_keeps_each_child_form_with_its_own_repeater() { + $child_a = $this->create_child_form(); + $child_b = $this->create_child_form(); + $repeater_a = $this->add_repeater( 1, $child_a ); + $end_a = $this->add_field( 'end_divider', 2 ); + $repeater_b = $this->add_repeater( 3, $child_b ); + $end_b = $this->add_field( 'end_divider', 4 ); + // Swapped orders: each child field sits in the other repeater's window. + $in_b = $this->add_field( 'text', 2, array( 'in_section' => $repeater_b ), $child_b ); + $in_a = $this->add_field( 'text', 4, array( 'in_section' => $repeater_a ), $child_a ); + + $this->assertSame( array( $repeater_a, $in_a, $end_a, $repeater_b, $in_b, $end_b ), $this->get_builder_order() ); + } + + /** + * @return void + */ + public function test_builder_keeps_fields_of_an_unused_child_form_before_the_submit_button() { + $unused = $this->create_child_form(); + $top = $this->add_field( 'text', 1 ); + $missing = $this->add_repeater( 2, 999999 ); + $orphan = $this->add_field( 'text', 3, array(), $unused ); + $end = $this->add_field( 'end_divider', 4 ); + $submit = $this->add_field( 'submit', 5 ); + + $this->assertSame( + array( $top, $missing, $end, $orphan, $submit ), + $this->get_builder_order(), + 'Fields no repeater renders stay editable, and a repeater with a missing child form keeps its place.' + ); + } + + /** + * Other builder filters must see the list in the order the builder renders it. + * + * @return void + */ + public function test_builder_sort_runs_before_other_builder_filters() { + $this->assertSame( 5, has_filter( 'frm_fields_in_form_builder', 'FrmFieldPlacementHelper::sort_for_builder' ) ); + } + + /** + * @return void + */ + public function test_builder_ignores_a_list_changed_into_another_shape() { + $fields = array( 'not a field' ); + $this->assertSame( $fields, FrmFieldPlacementHelper::sort_for_builder( $fields, array( 'form' => FrmForm::getOne( $this->form_id ) ) ) ); + $this->assertSame( $fields, FrmFieldPlacementHelper::sort_for_builder( $fields, array() ) ); + } + + /** + * @return void + */ + public function test_repeater_for_child_form_is_found_from_form_select() { + $child = $this->create_child_form(); + $repeater = $this->add_repeater( 1, $child ); + + $this->assertSame( $repeater, (int) FrmFieldPlacementHelper::get_repeater_for_child_form( $child )->id ); + $this->assertFalse( FrmFieldPlacementHelper::get_repeater_for_child_form( $this->create_child_form() ), 'A child form no repeater uses has no repeater.' ); + $this->assertFalse( FrmFieldPlacementHelper::get_repeater_for_child_form( $this->form_id ), 'A top-level form is not a child form.' ); + $this->assertFalse( FrmFieldPlacementHelper::get_repeater_for_child_form( 0 ) ); + } + + /** + * @return void + */ + public function test_container_parts_find_the_end_field_and_children() { + $section = $this->add_field( 'divider', 1 ); + $inside = $this->add_field( 'text', 2, array( 'in_section' => $section ) ); + $end = $this->add_field( 'end_divider', 3 ); + $this->add_field( 'text', 4 ); + + $parts = FrmFieldPlacementHelper::get_container_parts( FrmField::getOne( $section ) ); + + $this->assertSame( $end, (int) $parts['end']->id ); + $this->assertSame( array( $inside ), array_map( 'intval', wp_list_pluck( $parts['children'], 'id' ) ) ); + } + + /** + * A section closed by the next section has no end field to delete. + * + * @return void + */ + public function test_container_parts_skip_an_implicit_close() { + $section = $this->add_field( 'divider', 1 ); + $this->add_field( 'divider', 2 ); + + $this->assertFalse( FrmFieldPlacementHelper::get_container_parts( FrmField::getOne( $section ) )['end'] ); + } + + /** + * @return int[] + */ + private function get_builder_order() { + $fields = FrmField::get_all_for_form( $this->form_id ); + /** This filter is documented in classes/controllers/FrmFormsController.php */ + $fields = apply_filters( 'frm_fields_in_form_builder', $fields, array( 'form' => FrmForm::getOne( $this->form_id ) ) ); + + return array_map( 'intval', wp_list_pluck( $fields, 'id' ) ); + } + + /** + * @return int + */ + private function create_child_form() { + return $this->create_empty_form( $this->form_id ); + } + + /** + * The form factory adds a text field to every form, which these tests must not have. + * + * @param int $parent_form_id Parent form, 0 for a top-level form. + * + * @return int + */ + private function create_empty_form( $parent_form_id = 0 ) { + return (int) FrmForm::create( + array( + 'form_key' => '', + 'name' => 'Section structure', + 'description' => '', + 'parent_form_id' => $parent_form_id, + ) + ); + } + + /** + * @param int $order Field order. + * @param int $child_form_id Child form for form_select. + * + * @return int + */ + private function add_repeater( $order, $child_form_id ) { + return $this->add_field( + 'divider', + $order, + array( + 'repeat' => 1, + 'form_select' => $child_form_id, + ) + ); + } + + /** + * Store a field exactly as given, the way legacy data and imports can hold it. + * + * @param string $type Field type. + * @param int $order Field order. + * @param array $options Field options to merge over the defaults. + * @param int|null $form_id Form, the test form by default. + * + * @return int + */ + private function add_field( $type, $order, $options = array(), $form_id = null ) { + $values = FrmFieldsHelper::setup_new_vars( $type, $form_id ?? $this->form_id ); + $values['field_order'] = $order; + $values['field_options'] = array_merge( $values['field_options'], $options ); + $field_id = FrmField::create( $values ); + + $this->assertNotFalse( $field_id, 'The ' . $type . ' test field should be created.' ); + + return (int) $field_id; + } +} From 1dc9fdf15e8841978178a1cbb76f6461187c72cd Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 27 Nov 2026 13:46:01 -0400 Subject: [PATCH 2/5] refactor(fields): move section placement to Pro behind ability hooks Sections and repeaters are Pro fields, so the placement rules, the builder sort and the inline section handling move to Pro. Lite keeps hooks for Pro to attach to, and falls back to the earlier repeater placement when no Pro version hooks in. Co-Authored-By: Claude Opus 5.5 --- .../FrmAbilitiesFieldsController.php | 148 ++++- .../FrmAbilitiesFormsController.php | 40 +- classes/controllers/FrmHooksController.php | 1 - .../FrmAbilitiesInlineFieldsHelper.php | 214 ------- classes/helpers/FrmFieldPlacementHelper.php | 600 +----------------- stubs.php | 7 - .../test_FrmAbilitiesFieldPlacement.php | 460 +++++--------- .../fields/test_FrmSectionStructure.php | 268 -------- 8 files changed, 347 insertions(+), 1391 deletions(-) delete mode 100644 classes/helpers/FrmAbilitiesInlineFieldsHelper.php delete mode 100644 tests/phpunit/fields/test_FrmSectionStructure.php diff --git a/classes/controllers/FrmAbilitiesFieldsController.php b/classes/controllers/FrmAbilitiesFieldsController.php index 25b84dc34f..194467e4d8 100644 --- a/classes/controllers/FrmAbilitiesFieldsController.php +++ b/classes/controllers/FrmAbilitiesFieldsController.php @@ -379,6 +379,7 @@ public static function execute_create_field( $input ) { return FrmAbilitiesHelper::flatten_error( $prepared ); } + // prepare_new_field() may have moved the field into a repeater's child form. $field_id = FrmField::create( $prepared ); if ( ! $field_id ) { @@ -430,7 +431,28 @@ public static function execute_update_field( $input ) { } $field_data = self::add_stored_values_for_cleaning( $field, $field_data ); - $field_data = FrmFieldPlacementHelper::prepare_update_placement( $field, $field_data, $input ); + + /** + * Filters an update-field ability call before it is saved, so Pro can move the field when its section changes. + * + * Return a WP_Error to reject the update. + * + * @since x.x + * + * @param array $field_data Field data to save, with field_options merged over the stored options. + * @param array $args { + * @type stdClass $field The stored field. + * @type array $input The update input as sent, before stored options were merged in. + * } + */ + $field_data = apply_filters( + 'frm_ability_update_field_placement', + $field_data, + array( + 'field' => $field, + 'input' => $input, + ) + ); if ( is_wp_error( $field_data ) ) { return $field_data; @@ -451,6 +473,7 @@ public static function execute_update_field( $input ) { FrmField::delete_form_transient( $field->form_id ); if ( isset( $field_data['form_id'] ) ) { + // The field moved to another form. FrmField::delete_form_transient( $field_data['form_id'] ); } @@ -498,7 +521,15 @@ public static function execute_delete_field( $input ) { // Read the field before it is gone, so the caller gets back what it deleted. $data = self::prepare_field_for_response( $field ); $form_id = $field->form_id; - $parts = FrmFieldPlacementHelper::is_container_type( $field->type ) ? FrmFieldPlacementHelper::get_container_parts( $field ) : false; + + /** + * Fires before a delete-field ability call deletes a field, while its section is still intact. + * + * @since x.x + * + * @param stdClass $field The field about to be deleted. + */ + do_action( 'frm_ability_before_delete_field', $field ); if ( FrmField::is_repeating_field( $field ) && ! empty( $field->field_options['form_select'] ) ) { self::destroy_fields_in_repeater( $field ); @@ -508,9 +539,14 @@ public static function execute_delete_field( $input ) { return self::get_invalid_field_error(); } - if ( $parts ) { - FrmFieldPlacementHelper::clean_up_deleted_container( $field, $parts ); - } + /** + * Fires after a delete-field ability call deletes a field, so Pro can remove what a section leaves behind. + * + * @since x.x + * + * @param stdClass $field The deleted field. + */ + do_action( 'frm_ability_after_delete_field', $field ); FrmField::delete_form_transient( $form_id ); FrmForm::clear_form_cache(); @@ -557,16 +593,12 @@ public static function prepare_new_field( $field, $form ) { $prepared['form_id'] = $form_id; $prepared = self::maybe_create_repeat_form( $prepared, $form_id ); - $prepared = FrmFieldPlacementHelper::prepare_new_placement( $prepared, isset( $field['field_order'] ) ); + $prepared = self::place_new_field( $prepared, $field, $form ); if ( is_wp_error( $prepared ) ) { return $prepared; } - if ( ! FrmFieldPlacementHelper::is_allowed( $prepared['type'], (int) $prepared['form_id'], (int) ( $prepared['field_options']['in_section'] ?? 0 ) ) ) { - return new WP_Error( 'frm_invalid_field_placement', __( 'This field type cannot be placed in this section or repeater.', 'formidable' ), array( 'status' => 400 ) ); - } - $prepared = self::maybe_enable_separate_values( $prepared, $field ); if ( empty( $field['field_key'] ) && ! empty( $field['name'] ) ) { @@ -702,6 +734,102 @@ private static function maybe_create_repeat_form( $prepared, $form_id ) { return $prepared; } + /** + * Decide which form, section and field_order a new field gets. + * + * Sections and repeaters are Pro fields, so Pro places fields in them + * through the frm_ability_new_field_placement filter. With a Pro version + * that does not, the earlier repeater placement is kept. + * + * @since x.x + * + * @param array $prepared Prepared field data. + * @param array $field Raw field input. + * @param stdClass $form The form the field is created on. + * + * @return array|WP_Error + */ + private static function place_new_field( $prepared, $field, $form ) { + if ( ! FrmFieldPlacementHelper::has_ability_section_placement() ) { + return self::maybe_position_in_repeater( $prepared, $field, $form ); + } + + /** + * Filters where a field created by an ability is stored. + * + * A callback may change form_id, field_order and field_options.in_section, + * or return a WP_Error to reject the field. + * + * @since x.x + * + * @param array $prepared Prepared field data. + * @param array $args { + * @type array $field The field input as sent. + * @type stdClass $form The form the field is created on. + * @type bool $has_order True when the caller chose the field_order. + * } + */ + return apply_filters( + 'frm_ability_new_field_placement', + $prepared, + array( + 'field' => $field, + 'form' => $form, + 'has_order' => isset( $field['field_order'] ), + ) + ); + } + + /** + * Slot a new repeater child field after the last field of its section. + * + * Repeater child fields sort into the parent form's field list purely by + * field_order, so an order computed against the child form alone can land + * before the repeater's own divider and render outside the section in the + * builder. When the caller gives no explicit order, the field goes right + * after the last field of its section and the fields that follow shift down. + * + * @since 7.0 + * + * @param array $prepared Prepared field data. + * @param array $field Raw field input. + * @param stdClass $form The form the field is created on. + * + * @return array + */ + private static function maybe_position_in_repeater( $prepared, $field, $form ) { + if ( isset( $field['field_order'] ) || empty( $form->parent_form_id ) ) { + return $prepared; + } + + $parent_fields = FrmField::get_all_for_form( $form->parent_form_id, '', 'include' ); + $section_order = 0; + + foreach ( $parent_fields as $parent_field ) { + $form_select = isset( $parent_field->field_options['form_select'] ) ? (int) $parent_field->field_options['form_select'] : 0; + $starts_this_section = 'divider' === $parent_field->type && $form_select === (int) $form->id; + $in_this_section = (int) $parent_field->form_id === (int) $form->id; + + if ( $starts_this_section || $in_this_section ) { + $section_order = max( $section_order, (int) $parent_field->field_order ); + } + } + + if ( ! $section_order ) { + return $prepared; + } + + $prepared['field_order'] = $section_order + 1; + + foreach ( $parent_fields as $parent_field ) { + if ( (int) $parent_field->field_order > $section_order ) { + FrmField::update( $parent_field->id, array( 'field_order' => (int) $parent_field->field_order + 1 ) ); + } + } + + return $prepared; + } + /** * Reject a repeater or embedded form field with no usable child form. * diff --git a/classes/controllers/FrmAbilitiesFormsController.php b/classes/controllers/FrmAbilitiesFormsController.php index 8b1fedd160..0d21b3290f 100644 --- a/classes/controllers/FrmAbilitiesFormsController.php +++ b/classes/controllers/FrmAbilitiesFormsController.php @@ -280,7 +280,7 @@ private static function get_create_form_input_schema() { 'fields' => array( 'type' => 'array', 'description' => __( - 'Fields to create, in display order. Fields after a section or repeater and before its end_divider go inside it.', + 'Array of field objects to create in the form. Each field represents a form input like text, dropdown, checkbox, etc.', 'formidable' ), 'items' => self::get_inline_field_schema(), @@ -603,11 +603,41 @@ public static function execute_create_form( $input ) { FrmFormsController::create_default_email_action( $new_form ); } - if ( ! empty( $input['fields'] ) && is_array( $input['fields'] ) ) { - $created = ( new FrmAbilitiesInlineFieldsHelper( $new_form ) )->create( $input['fields'] ); + $fields = ! empty( $input['fields'] ) && is_array( $input['fields'] ) ? $input['fields'] : array(); + + /** + * Short-circuits creating the inline fields of a create-form ability call. + * + * Pro uses it to put the fields listed between a section and its end_divider + * inside that section. Return true when the fields were created, a WP_Error to + * stop, or null to create them one at a time here. + * + * @since x.x + * + * @param null|true|WP_Error $created Null to let Formidable create the fields. + * @param array $fields The inline field input, in order. + * @param stdClass $form The new form. + */ + $created = $fields ? apply_filters( 'frm_ability_create_inline_fields', null, $fields, $new_form ) : true; + + if ( is_wp_error( $created ) ) { + return FrmAbilitiesHelper::flatten_error( $created ); + } + + if ( null === $created ) { + foreach ( $fields as $field ) { + // Share create-field's pipeline so inline fields get the same + // separate-value detection, repeater child form, and validation. + $prepared = FrmAbilitiesFieldsController::prepare_new_field( $field, $new_form ); + + if ( is_wp_error( $prepared ) ) { + return FrmAbilitiesHelper::flatten_error( $prepared ); + } + + $prepared['form_id'] = $form_id; - if ( is_wp_error( $created ) ) { - return FrmAbilitiesHelper::flatten_error( $created ); + FrmField::create( $prepared ); + unset( $prepared, $field ); } } diff --git a/classes/controllers/FrmHooksController.php b/classes/controllers/FrmHooksController.php index 04f495db11..09d40e4df2 100644 --- a/classes/controllers/FrmHooksController.php +++ b/classes/controllers/FrmHooksController.php @@ -193,7 +193,6 @@ public static function load_admin_hooks() { add_action( 'frm_after_field_options', 'FrmFormsController::logic_tip' ); add_action( 'frm_field_code_tab', 'FrmFormsController::field_part_shortcodes' ); add_filter( 'frm_fields_in_form_builder', 'FrmFormsController::update_form_builder_fields' ); - add_filter( 'frm_fields_in_form_builder', 'FrmFieldPlacementHelper::sort_for_builder', 5, 2 ); add_filter( 'set-screen-option', 'FrmFormsController::save_per_page', 10, 3 ); add_action( 'admin_footer', 'FrmFormsController::insert_form_popup' ); diff --git a/classes/helpers/FrmAbilitiesInlineFieldsHelper.php b/classes/helpers/FrmAbilitiesInlineFieldsHelper.php deleted file mode 100644 index eac72456ab..0000000000 --- a/classes/helpers/FrmAbilitiesInlineFieldsHelper.php +++ /dev/null @@ -1,214 +0,0 @@ -form = $form; - } - - /** - * Create the inline fields in order. - * - * @since x.x - * - * @param array $fields Inline field input, in order. - * - * @return true|WP_Error - */ - public function create( $fields ) { - foreach ( $fields as $field ) { - if ( ! is_array( $field ) ) { - continue; - } - - $field = FrmAbilitiesFieldsController::normalize_field_type( $field ); - $field = $this->place_in_open_sections( $field ); - - if ( is_wp_error( $field ) ) { - return $field; - } - - if ( ! $field ) { - // An end field with nothing open to close. - continue; - } - - $created = $this->create_field( $field ); - - if ( is_wp_error( $created ) ) { - return $created; - } - - if ( $created && FrmFieldPlacementHelper::is_container_type( $created->type ) ) { - $this->open[] = $created; - } - }//end foreach - - return $this->close_containers( 0 ); - } - - /** - * Close the sections a field is outside of, and give it the open section otherwise. - * - * @param array $field Inline field input. - * - * @return array|false|WP_Error The field to create, false to skip an end field that closes nothing. - */ - private function place_in_open_sections( $field ) { - $type = explode( '|', (string) ( $field['type'] ?? '' ) )[0]; - $top = $this->open ? end( $this->open ) : null; - - if ( $top && 'divider' === $top->type && in_array( $type, array( 'divider', 'break' ), true ) ) { - // The next section or page break closes a section, as on the frontend. - $closed = $this->close_containers( count( $this->open ) - 1 ); - - if ( is_wp_error( $closed ) ) { - return $closed; - } - } - - if ( FrmFieldPlacementHelper::is_end_type( $type ) ) { - return $this->close_for_end_field( $field, $type ); - } - - if ( ! $this->open ) { - return $field; - } - - if ( ! isset( $field['field_options']['in_section'] ) ) { - $field['field_options'] = isset( $field['field_options'] ) && is_array( $field['field_options'] ) ? $field['field_options'] : array(); - $field['field_options']['in_section'] = (int) end( $this->open )->id; - - return $field; - } - - // A field that names its own section is outside every open section nested deeper than that one. - $closed = $this->close_containers( $this->count_open_through( (int) $field['field_options']['in_section'] ) ); - - return is_wp_error( $closed ) ? $closed : $field; - } - - /** - * Close what an end field closes, so it is created as the last of them. - * - * @param array $field Inline end field input. - * @param string $type The end field type. - * - * @return array|false|WP_Error The end field, or false when it closes nothing. - */ - private function close_for_end_field( $field, $type ) { - for ( $i = count( $this->open ) - 1; $i >= 0; $i-- ) { - if ( FrmFieldPlacementHelper::get_end_type( $this->open[ $i ]->type ) !== $type ) { - continue; - } - - // Close anything still open inside it first, so each container gets its own end field. - $closed = $this->close_containers( $i + 1 ); - - if ( is_wp_error( $closed ) ) { - return $closed; - } - - array_pop( $this->open ); - - return $field; - } - - return false; - } - - /** - * Count the open containers up to and including the one a field names as its section. - * - * @param int $section_id The field's in_section. - * - * @return int How many containers stay open, 0 when the section is not open. - */ - private function count_open_through( $section_id ) { - foreach ( $this->open as $index => $container ) { - if ( (int) $container->id === $section_id ) { - return $index + 1; - } - } - - return 0; - } - - /** - * Create the end fields for open containers, innermost first. - * - * @param int $keep How many of the outermost containers stay open. - * - * @return true|WP_Error - */ - private function close_containers( $keep ) { - for ( $i = count( $this->open ); $i > $keep; $i-- ) { - $container = array_pop( $this->open ); - $created = $this->create_field( array( 'type' => FrmFieldPlacementHelper::get_end_type( $container->type ) ) ); - - if ( is_wp_error( $created ) ) { - return $created; - } - } - - return true; - } - - /** - * Create one inline field through create-field's pipeline. - * - * Sharing it gives inline fields the same separate-value detection, - * repeater child form, placement, and validation as create-field. - * - * @param array $field Inline field input. - * - * @return false|object|WP_Error The created field, false if it could not be saved. - */ - private function create_field( $field ) { - $prepared = FrmAbilitiesFieldsController::prepare_new_field( $field, $this->form ); - - if ( is_wp_error( $prepared ) ) { - return $prepared; - } - - $field_id = FrmField::create( $prepared ); - $created = $field_id ? FrmField::getOne( $field_id ) : null; - - return is_object( $created ) ? $created : false; - } -} diff --git a/classes/helpers/FrmFieldPlacementHelper.php b/classes/helpers/FrmFieldPlacementHelper.php index 8a7c50f02d..f4bbffe2c2 100644 --- a/classes/helpers/FrmFieldPlacementHelper.php +++ b/classes/helpers/FrmFieldPlacementHelper.php @@ -87,605 +87,17 @@ public static function is_allowed( $type, $form_id, $section_id = 0 ) { } /** - * Order builder fields the way the frontend groups them. + * Check whether Pro places ability-created fields in sections and repeaters. * - * The frontend renders a repeater's rows from the fields of its child form, - * whatever their field_order, and renders every parent form field once. The - * builder nests a field by where its field_order falls between a divider and - * its end_divider. So a child field ordered past the end_divider showed below - * the repeater in the builder, and a parent field ordered inside the repeater - * looked repeated. Listing each child form right after its repeater, and - * parent form fields from inside a repeater right after its end_divider, makes - * the builder show what the form renders. Saving the builder then stores the - * matching field_order values. + * When it does not, the abilities keep their earlier placement. Pro also + * checks that this method exists before it changes how sections are grouped, + * so a mismatched Lite and Pro both keep the earlier behavior. * * @since x.x * - * @param array $fields Builder field objects sorted by field_order. - * @param array $args The filter arguments. Contains `form`. - * - * @return array - */ - public static function sort_for_builder( $fields, $args = array() ) { - $form = $args['form'] ?? null; - - if ( ! is_array( $fields ) || ! is_object( $form ) || empty( $form->id ) ) { - return $fields; - } - - $form_id = (int) $form->id; - $top_level = array(); - $child_forms = array(); - - foreach ( $fields as $field ) { - if ( ! is_object( $field ) || ! isset( $field->form_id, $field->type ) ) { - // Leave a list changed by another filter alone. - return $fields; - } - - if ( (int) $field->form_id === $form_id ) { - $top_level[] = $field; - } else { - $child_forms[ (int) $field->form_id ][] = $field; - } - } - - $sorted = array(); - $deferred = array(); - $open_repeater = false; - - foreach ( $top_level as $field ) { - if ( $open_repeater ) { - if ( 'end_divider' === $field->type ) { - $sorted[] = $field; - $sorted = array_merge( $sorted, $deferred ); - $deferred = array(); - $open_repeater = false; - continue; - } - - if ( ! in_array( $field->type, array( 'divider', 'break' ), true ) ) { - // Rendered once on the frontend, so it is shown after the repeater. - $deferred[] = $field; - continue; - } - - // A section or page break before the end_divider closes the repeater, like the frontend. - $sorted = array_merge( $sorted, $deferred ); - $deferred = array(); - $open_repeater = false; - } - - $sorted[] = $field; - - if ( ! FrmField::is_repeating_field( $field ) ) { - continue; - } - - $child_form_id = (int) FrmField::get_option( $field, 'form_select' ); - - if ( isset( $child_forms[ $child_form_id ] ) ) { - $sorted = array_merge( $sorted, $child_forms[ $child_form_id ] ); - unset( $child_forms[ $child_form_id ] ); - } - - $open_repeater = true; - }//end foreach - - $sorted = array_merge( $sorted, $deferred ); - - if ( ! $child_forms ) { - return $sorted; - } - - // No repeater renders these, so they never show on the frontend. Keep them editable, before the submit button. - $submit = $sorted && FrmSubmitHelper::FIELD_TYPE === end( $sorted )->type ? array_pop( $sorted ) : null; - - foreach ( $child_forms as $unrendered ) { - $sorted = array_merge( $sorted, $unrendered ); - } - - if ( $submit ) { - $sorted[] = $submit; - } - - return $sorted; - } - - /** - * Find the repeater that renders a child form's fields on the frontend. - * - * @since x.x - * - * @param int $child_form_id Repeater child form ID. - * - * @return false|object The repeater field, or false when no repeater uses the form. - */ - public static function get_repeater_for_child_form( $child_form_id ) { - $child_form = $child_form_id ? FrmForm::getOne( $child_form_id ) : false; - - if ( ! $child_form || empty( $child_form->parent_form_id ) ) { - return false; - } - - foreach ( FrmField::get_all_types_in_form( $child_form->parent_form_id, 'divider' ) as $divider ) { - if ( is_object( $divider ) && FrmField::is_repeating_field( $divider ) && (int) FrmField::get_option( $divider, 'form_select' ) === (int) $child_form->id ) { - return $divider; - } - } - - return false; - } - - /** - * Settle where a new field goes, using the rules the frontend renders with. - * - * The frontend repeats the fields of a repeater's child form and groups the - * other fields into sections by field_order. So in_section and form_id are - * made to agree with each other before the field is saved: a field placed in - * a repeater is stored in its child form, a field created on a child form - * gets that repeater as its section, and a field given a field_order inside - * a section or repeater joins it. A field with no field_order is put at the - * end of its section or repeater rather than at the end of the form. - * - * @since x.x - * - * @param array $values Prepared field values, including form_id and field_options. - * @param bool $has_order True when the caller chose the field_order. - * - * @return array|WP_Error - */ - public static function prepare_new_placement( $values, $has_order ) { - $form = FrmForm::getOne( $values['form_id'] ?? 0 ); - - if ( ! $form ) { - return $values; - } - - $top_form_id = $form->parent_form_id ? (int) $form->parent_form_id : (int) $form->id; - $type = explode( '|', $values['type'] ?? '' )[0]; - $ends = self::get_container_ends(); - - if ( in_array( $type, $ends, true ) ) { - // Closing fields only render in the top-level form. - $values['form_id'] = $top_form_id; - - if ( ! $has_order ) { - $values['field_order'] = self::get_next_order( $top_form_id ); - } - - return $values; - } - - $section = self::get_requested_section( $values, $top_form_id, $form ); - - if ( is_wp_error( $section ) ) { - return $section; - } - - if ( $has_order && (int) $form->id === $top_form_id ) { - // The frontend groups top-level fields by order, so the order decides the section. - $at_order = self::get_container_at_order( $top_form_id, (int) $values['field_order'] ); - - if ( ! $section ) { - // A field that cannot go in that section, like a page break, keeps its top-level placement. - $section = $at_order && self::is_allowed( $type, $top_form_id, (int) $at_order->id ) ? $at_order : false; - } elseif ( ! FrmField::is_repeating_field( $section ) && ( ! $at_order || (int) $at_order->id !== (int) $section->id ) ) { - // Repeaters render their child form whatever the order, but a section is only the fields ordered inside it. - return new WP_Error( - 'frm_field_order_outside_section', - __( 'field_order is outside the section set in field_options.in_section. Leave field_order out to add the field at the end of the section.', 'formidable' ), - array( 'status' => 400 ) - ); - } - } - - $values['field_options']['in_section'] = $section ? (int) $section->id : 0; - $values['form_id'] = $top_form_id; - - if ( $section && FrmField::is_repeating_field( $section ) ) { - $values['form_id'] = (int) FrmField::get_option( $section, 'form_select' ); - } elseif ( ! $section && (int) $form->id !== $top_form_id ) { - // A child form no repeater points to. Keep the field where it was asked for. - $values['form_id'] = (int) $form->id; - } - - if ( ! $has_order ) { - $values['field_order'] = $section ? self::get_order_at_section_end( $section ) : self::get_next_order( $top_form_id ); - } - - return $values; - } - - /** - * Settle where an updated field goes when the update changes its section. - * - * Uses the same rules as prepare_new_placement(), so moving a field into a - * repeater stores it in the child form, and moving it out stores it in the - * top-level form, like dragging it in the builder. Updates that keep the - * stored in_section are left alone, even for a legacy field whose form_id - * disagrees with it, because callers often resend every stored option and - * moving the field would hide its saved entry values. - * - * @since x.x - * - * @param stdClass $field The stored field. - * @param array $field_data Field data to update, with field_options merged over the stored options. - * @param array $input The update input as sent, before stored options were merged in. - * - * @return array|WP_Error - */ - public static function prepare_update_placement( $field, $field_data, $input ) { - if ( ! isset( $input['field_options']['in_section'], $field_data['field_options']['in_section'] ) || ! is_numeric( $input['field_options']['in_section'] ) ) { - return $field_data; - } - - $section_id = (int) $field_data['field_options']['in_section']; - $type = $field_data['type'] ?? $field->type; - - if ( $section_id === (int) FrmField::get_option( $field, 'in_section' ) || self::is_end_type( $type ) ) { - return $field_data; - } - - $has_order = isset( $input['field_order'] ); - $values = array( - 'type' => $type, - 'form_id' => $field->form_id, - 'field_order' => $field_data['field_order'] ?? $field->field_order, - 'field_options' => $field_data['field_options'], - ); - - if ( ! $section_id ) { - // Moving out of a section or repeater. The top-level form is the destination. - $form = FrmForm::getOne( $field->form_id ); - - if ( $form && $form->parent_form_id ) { - $values['form_id'] = (int) $form->parent_form_id; - } - } - - $values = self::prepare_new_placement( $values, $has_order ); - - if ( is_wp_error( $values ) ) { - return $values; - } - - $field_data['field_options']['in_section'] = $values['field_options']['in_section']; - - if ( (int) $values['form_id'] !== (int) $field->form_id ) { - $field_data['form_id'] = (int) $values['form_id']; - } - - if ( ! $has_order ) { - $field_data['field_order'] = (int) $values['field_order']; - } - - return $field_data; - } - - /** - * Get the fields that close a deleted section, and the ones inside it. - * - * @since x.x - * - * @param stdClass $container Section or other container field. - * - * @return array{end: false|object, children: array} The closing field, if any, and the - * top-level fields with in_section set to the container. - */ - public static function get_container_parts( $container ) { - $map = self::map_containers( (int) $container->form_id ); - $end = $map['ends'][ (int) $container->id ] ?? false; - $children = array(); - - foreach ( $map['fields'] as $field ) { - if ( (int) FrmField::get_option( $field, 'in_section' ) === (int) $container->id ) { - $children[] = $field; - } - } - - return array( - 'end' => is_object( $end ) && self::is_end_type( $end->type ) ? $end : false, - 'children' => $children, - ); - } - - /** - * Remove what a deleted section or repeater leaves behind. - * - * The builder deletes a section's end field and its fields with it, and Pro - * deletes a repeater's child form, but only in the admin. Without this a - * deleted section left an end field that closes nothing, fields pointing at - * a section that is gone, and an empty child form. Fields of a plain section - * are kept and move up to the section's own container, since deleting a - * heading should not delete what was under it. - * - * @since x.x - * - * @param stdClass $field The deleted container field. - * @param array $parts Its closing field and child fields, from get_container_parts(). - * - * @return void - */ - public static function clean_up_deleted_container( $field, $parts ) { - if ( $parts['end'] ) { - FrmField::destroy( $parts['end']->id ); - } - - $parent_section = (int) FrmField::get_option( $field, 'in_section' ); - - foreach ( $parts['children'] as $child ) { - $options = (array) $child->field_options; - $options['in_section'] = $parent_section; - FrmField::update( $child->id, array( 'field_options' => $options ) ); - } - - $child_form = FrmField::is_repeating_field( $field ) ? FrmForm::getOne( (int) FrmField::get_option( $field, 'form_select' ) ) : false; - - if ( $child_form && (int) $child_form->parent_form_id === (int) $field->form_id ) { - FrmForm::destroy( $child_form->id ); - } - } - - /** - * Check whether a field type opens a builder container, like a section. - * - * @since x.x - * - * @param string $type Field type. - * - * @return bool - */ - public static function is_container_type( $type ) { - return isset( self::get_container_ends()[ $type ] ); - } - - /** - * Check whether a field type closes a builder container, like an end_divider. - * - * @since x.x - * - * @param string $type Field type. - * * @return bool */ - public static function is_end_type( $type ) { - return in_array( $type, self::get_container_ends(), true ); - } - - /** - * Get the field type that closes a container type. - * - * @since x.x - * - * @param string $type Container field type, like divider. - * - * @return string The end field type, or an empty string when the type is not a container. - */ - public static function get_end_type( $type ) { - return self::get_container_ends()[ $type ] ?? ''; - } - - /** - * Get the section a new field asks for, after checking it can hold fields. - * - * @param array $values Prepared field values. - * @param int $top_form_id The top-level form. - * @param stdClass $form The form the field was created on. - * - * @return false|object|WP_Error - */ - private static function get_requested_section( $values, $top_form_id, $form ) { - $section_id = (int) ( $values['field_options']['in_section'] ?? 0 ); - - if ( ! $section_id ) { - // A field created on a child form belongs to the repeater that renders it. - return (int) $form->id === $top_form_id ? false : self::get_repeater_for_child_form( $form->id ); - } - - $section = FrmField::getOne( $section_id ); - - if ( ! $section || (int) $section->form_id !== $top_form_id || ! self::is_container_type( $section->type ) ) { - return new WP_Error( - 'frm_invalid_section', - __( 'field_options.in_section must be the ID of a section or repeater on the same form.', 'formidable' ), - array( 'status' => 400 ) - ); - } - - if ( FrmField::is_repeating_field( $section ) ) { - $child_form = FrmForm::getOne( (int) FrmField::get_option( $section, 'form_select' ) ); - - if ( ! $child_form || (int) $child_form->parent_form_id !== $top_form_id ) { - return new WP_Error( - 'frm_invalid_section', - __( 'That repeater has no child form, so fields cannot be added to it. Set its field_options.form_select first.', 'formidable' ), - array( 'status' => 400 ) - ); - } - } - - return $section; - } - - /** - * Get the section or repeater a field_order falls in, as the frontend groups fields. - * - * @param int $form_id Top-level form ID. - * @param int $order The field_order. A field already at this order is pushed after it. - * - * @return false|object - */ - private static function get_container_at_order( $form_id, $order ) { - $map = self::map_containers( $form_id ); - $container = 0; - - foreach ( $map['fields'] as $field ) { - if ( (int) $field->field_order >= $order ) { - break; - } - - $container = $map['after'][ (int) $field->id ]; - } - - $field = $container ? FrmField::getOne( $container ) : null; - - return is_object( $field ) ? $field : false; - } - - /** - * Get the field_order at the end of a section or repeater, and make room there. - * - * @param stdClass $section Section or repeater field. - * - * @return int - */ - private static function get_order_at_section_end( $section ) { - $map = self::map_containers( (int) $section->form_id ); - $end = $map['ends'][ (int) $section->id ] ?? false; - $order = (int) $section->field_order; - - foreach ( $map['fields'] as $field ) { - if ( $end && (int) $field->field_order >= (int) $end->field_order ) { - break; - } - - if ( (int) $field->field_order > $order && $map['after'][ (int) $field->id ] === (int) $section->id ) { - $order = (int) $field->field_order; - } - } - - if ( FrmField::is_repeating_field( $section ) ) { - foreach ( FrmField::get_all_for_form( (int) FrmField::get_option( $section, 'form_select' ), '', 'exclude', 'exclude' ) as $child ) { - if ( ! $end || (int) $child->field_order < (int) $end->field_order ) { - $order = max( $order, (int) $child->field_order ); - } - } - } - - if ( ! $end ) { - // Nothing closes it, so the end of the form is the end of the section. - return max( $order + 1, self::get_next_order( (int) $section->form_id ) ); - } - - ++$order; - self::shift_orders( (int) $section->form_id, $order ); - - return $order; - } - - /** - * Get the field_order after every field of a form and its repeaters. - * - * @param int $form_id Top-level form ID. - * - * @return int - */ - private static function get_next_order( $form_id ) { - $max = 0; - - foreach ( FrmField::get_all_for_form( $form_id, '', 'exclude', 'include' ) as $field ) { - $max = max( $max, (int) $field->field_order ); - } - - return $max + 1; - } - - /** - * Move every field of a form and its repeaters at or after a field_order down by one. - * - * @param int $form_id Top-level form ID. - * @param int $order The first field_order to move. - * - * @return void - */ - private static function shift_orders( $form_id, $order ) { - foreach ( FrmField::get_all_for_form( $form_id, '', 'exclude', 'include' ) as $field ) { - if ( (int) $field->field_order >= $order ) { - FrmField::update( $field->id, array( 'field_order' => (int) $field->field_order + 1 ) ); - } - } - } - - /** - * Walk a top-level form's fields and record which container each one leaves open. - * - * Mirrors the frontend: a container opens at its field and closes at its end - * field. A section is also closed by the next section or page break, as - * FrmFormsHelper::auto_add_end_section_fields() does. - * - * @param int $form_id Top-level form ID. - * - * @return array{fields: array, after: array, ends: array} The fields - * in order, the container open after each field, and the field closing each container. - */ - private static function map_containers( $form_id ) { - $ends = self::get_container_ends(); - $fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' ); - $open = array(); - $after = array(); - $closed = array(); - - foreach ( $fields as $field ) { - $top = $open ? end( $open ) : null; - - if ( $top && 'divider' === $top->type && in_array( $field->type, array( 'divider', 'break' ), true ) ) { - $closed[ (int) $top->id ] = $field; - array_pop( $open ); - } - - if ( isset( $ends[ $field->type ] ) ) { - $open[] = $field; - } elseif ( in_array( $field->type, $ends, true ) ) { - self::close_container( $open, $closed, $field, $ends ); - } - - $after[ (int) $field->id ] = $open ? (int) end( $open )->id : 0; - } - - return array( - 'fields' => array_values( $fields ), - 'after' => $after, - 'ends' => $closed, - ); - } - - /** - * Close the innermost open container that the end field belongs to. - * - * @param stdClass[] $open Open containers, innermost last. - * @param stdClass[] $closed The field closing each container, keyed by container ID. - * @param stdClass $end The end field. - * @param string[] $ends End field type for each container type. - * - * @return void - */ - private static function close_container( &$open, &$closed, $end, $ends ) { - for ( $i = count( $open ) - 1; $i >= 0; $i-- ) { - if ( $ends[ $open[ $i ]->type ] === $end->type ) { - $closed[ (int) $open[ $i ]->id ] = $end; - array_splice( $open, $i ); - return; - } - } - } - - /** - * Get the end field type for each container type the builder supports. - * - * @return array End field type keyed by container field type. - */ - private static function get_container_ends() { - $ends = array( 'divider' => 'end_divider' ); - - if ( ! is_callable( 'FrmProBuilderContainersHelper::get_containers' ) ) { - return $ends; - } - - foreach ( FrmProBuilderContainersHelper::get_containers() as $type => $container ) { - $ends[ $type ] = $container['end']; - } - - return $ends; + public static function has_ability_section_placement() { + return (bool) has_filter( 'frm_ability_new_field_placement' ); } } diff --git a/stubs.php b/stubs.php index 2720c13520..137a9897a5 100644 --- a/stubs.php +++ b/stubs.php @@ -351,13 +351,6 @@ class FrmProField { public static function create_repeat_form( $field_id, $args = array() ) { } } - class FrmProBuilderContainersHelper { - /** - * @return array}> Closing field and permitted parent types, keyed by container type. - */ - public static function get_containers() { - } - } class FrmProEntriesHelper { public static function get_search_str( $where_clause, $search_str, $form_id = 0, $fid = '' ) { } diff --git a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php index 2e5fa53fb0..dea75b42f6 100644 --- a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php +++ b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php @@ -1,16 +1,17 @@ markTestSkipped( 'The Abilities API is not available in this WordPress install.' ); } + // Start each test as if no Pro version places fields, whether or not Pro is active. + remove_all_filters( 'frm_ability_new_field_placement' ); + remove_all_filters( 'frm_ability_update_field_placement' ); + remove_all_filters( 'frm_ability_create_inline_fields' ); + remove_all_actions( 'frm_ability_before_delete_field' ); + remove_all_actions( 'frm_ability_after_delete_field' ); + $this->set_current_user_to_1(); $frm_settings = FrmAppHelper::get_settings(); @@ -59,36 +67,14 @@ private function execute( $slug, $input = array() ) { } /** - * A field put in a repeater from the parent form must be stored in the child form, - * or the frontend renders it once while the builder shows it in the repeater. + * With a Pro version that does not place fields, a new child form field still goes before the end_divider. * * @return void */ - public function test_create_field_in_a_repeater_is_stored_in_its_child_form() { - $layout = $this->create_repeater_layout(); - $created = $this->execute( - 'create-field', - array( - 'form_id' => $layout['form'], - 'type' => 'text', - 'name' => 'Item', - 'field_options' => array( 'in_section' => $layout['repeater'] ), - ) - ); - - $this->assertNotWPError( $created ); - $this->assertSame( $layout['child'], $created['form_id'], 'The frontend only repeats fields of the child form.' ); - $this->assert_between( $created['id'], $layout['repeater'], $layout['end'] ); - } - - /** - * @return void - */ - public function test_create_field_on_a_child_form_joins_its_repeater_before_the_end_divider() { - $layout = $this->create_repeater_layout(); - // A child field already ordered past the end_divider must not pull new fields out with it. - $this->store_field( 'text', $layout['child'], 50, array( 'in_section' => $layout['repeater'] ) ); + public function test_create_field_keeps_the_earlier_repeater_placement_without_a_placement_hook() { + $this->assertFalse( FrmFieldPlacementHelper::has_ability_section_placement() ); + $layout = $this->create_repeater_layout(); $created = $this->execute( 'create-field', array( @@ -99,335 +85,239 @@ public function test_create_field_on_a_child_form_joins_its_repeater_before_the_ ); $this->assertNotWPError( $created ); - $this->assertSame( $layout['repeater'], (int) $created['field_options']['in_section'], 'A child form field belongs to the repeater that renders it.' ); - $this->assert_between( $created['id'], $layout['repeater'], $layout['end'] ); - $end_order = (int) FrmField::getOne( $layout['end'] )->field_order; - $this->assertLessThan( (int) FrmField::getOne( $layout['after'] )->field_order, $end_order, 'Fields after the repeater move down to make room.' ); + $this->assertSame( $layout['child'], (int) $created['form_id'] ); + $this->assertSame( 3, (int) $created['field_order'], 'The field goes right after the last field of the repeater.' ); + $this->assertSame( 4, (int) FrmField::getOne( $layout['end'] )->field_order, 'Fields after it move down.' ); + $this->assertSame( 5, (int) FrmField::getOne( $layout['after'] )->field_order ); } /** * @return void */ - public function test_create_field_rejects_an_in_section_that_is_not_a_section_on_the_form() { - $layout = $this->create_repeater_layout(); - $other = $this->create_repeater_layout(); - $missing = FrmForm::create( $this->empty_form_values( $layout['form'] ) ); - $no_child = $this->store_field( - 'divider', - $layout['form'], - 20, - array( - 'repeat' => 1, - 'form_select' => $missing, - ) - ); - $candidates = array( - 'a missing field' => 999999, - 'another form section' => $other['repeater'], - 'a field that is not a container' => $layout['after'], + public function test_create_field_stores_the_field_where_the_placement_hook_puts_it() { + $form = $this->create_empty_form(); + $other = (int) FrmForm::create( $this->empty_form_values( $form ) ); + $checked = array(); + + add_filter( + 'frm_ability_new_field_placement', + function ( $prepared, $args ) use ( $other, &$checked ) { + $checked = $args; + $prepared['form_id'] = $other; + $prepared['field_order'] = 77; + return $prepared; + }, + 10, + 2 ); - FrmForm::destroy( $missing ); - $candidates['a repeater whose child form is gone'] = $no_child; - - foreach ( $candidates as $label => $section_id ) { - $result = $this->execute( - 'create-field', - array( - 'form_id' => $layout['form'], - 'type' => 'text', - 'field_options' => array( 'in_section' => $section_id ), - ) - ); - $this->assertWPError( $result, 'in_section set to ' . $label . ' must be rejected.' ); - } - } - - /** - * @return void - */ - public function test_create_field_in_a_section_without_an_order_is_added_before_its_end_divider() { - $form = $this->create_empty_form(); - $section = $this->store_field( 'divider', $form, 1 ); - $inside = $this->store_field( 'text', $form, 2, array( 'in_section' => $section ) ); - $end = $this->store_field( 'end_divider', $form, 3 ); - $after = $this->store_field( 'text', $form, 4 ); + $this->assertTrue( FrmFieldPlacementHelper::has_ability_section_placement() ); $created = $this->execute( 'create-field', array( - 'form_id' => $form, - 'type' => 'text', - 'field_options' => array( 'in_section' => $section ), + 'form_id' => $form, + 'type' => 'text', + 'field_order' => 5, ) ); $this->assertNotWPError( $created ); - $this->assertSame( 3, $created['field_order'], 'The field goes at the end of the section.' ); - $this->assertSame( 2, (int) FrmField::getOne( $inside )->field_order ); - $this->assertSame( 4, (int) FrmField::getOne( $end )->field_order, 'The end_divider moves down so the field stays inside.' ); - $this->assertSame( 5, (int) FrmField::getOne( $after )->field_order ); + $this->assertSame( $other, (int) $created['form_id'], 'The form the hook chose must not be overwritten with the requested form.' ); + $this->assertSame( 77, (int) $created['field_order'] ); + $this->assertSame( $form, (int) $checked['form']->id ); + $this->assertTrue( $checked['has_order'] ); + $this->assertSame( 'text', $checked['field']['type'] ); } /** * @return void */ - public function test_create_field_with_an_order_outside_its_section_is_rejected() { - $form = $this->create_empty_form(); - $section = $this->store_field( 'divider', $form, 1 ); - $this->store_field( 'end_divider', $form, 2 ); - $this->store_field( 'text', $form, 3 ); + public function test_create_field_returns_an_error_from_the_placement_hook() { + $form = $this->create_empty_form(); + + add_filter( + 'frm_ability_new_field_placement', + function () { + return new WP_Error( 'frm_invalid_section', 'Not a section.', array( 'status' => 400 ) ); + } + ); $result = $this->execute( 'create-field', array( - 'form_id' => $form, - 'type' => 'text', - 'field_order' => 4, - 'field_options' => array( 'in_section' => $section ), + 'form_id' => $form, + 'type' => 'text', ) ); - $this->assertWPError( $result, 'The frontend would show the field outside the section it names.' ); + $this->assertWPError( $result ); + $this->assertSame( 'frm_invalid_section', $result->get_error_code() ); + $this->assertEmpty( FrmField::get_all_for_form( $form ), 'A rejected field is not created.' ); } /** - * The frontend groups top-level fields by order, so an order inside a section or repeater joins it. - * * @return void */ - public function test_create_field_with_an_order_inside_a_section_or_repeater_joins_it() { - $layout = $this->create_repeater_layout(); - - $in_repeater = $this->execute( - 'create-field', - array( - 'form_id' => $layout['form'], - 'type' => 'text', - 'field_order' => (int) FrmField::getOne( $layout['end'] )->field_order, - ) + public function test_update_field_saves_what_the_update_hook_returns() { + $form = $this->create_empty_form(); + $other = (int) FrmForm::create( $this->empty_form_values( $form ) ); + $field_id = $this->store_field( 'text', $form, 1 ); + $checked = array(); + + add_filter( + 'frm_ability_update_field_placement', + function ( $field_data, $args ) use ( $other, &$checked ) { + $checked = $args; + $field_data['form_id'] = $other; + $field_data['field_order'] = 55; + return $field_data; + }, + 10, + 2 ); - $this->assertNotWPError( $in_repeater ); - $this->assertSame( $layout['child'], $in_repeater['form_id'] ); - $this->assertSame( $layout['repeater'], (int) $in_repeater['field_options']['in_section'] ); - - $form = $this->create_empty_form(); - $section = $this->store_field( 'divider', $form, 10 ); - $this->store_field( 'end_divider', $form, 20 ); - - $in_section = $this->execute( - 'create-field', + $updated = $this->execute( + 'update-field', array( - 'form_id' => $form, - 'type' => 'text', - 'field_order' => 15, + 'id' => $field_id, + 'name' => 'Renamed', ) ); - $this->assertNotWPError( $in_section ); - $this->assertSame( $section, (int) $in_section['field_options']['in_section'] ); - - // Page breaks are a Pro field type, so check the shared placement step directly. - $page_break = FrmFieldPlacementHelper::prepare_new_placement( - array( - 'type' => 'break', - 'form_id' => $form, - 'field_order' => 16, - 'field_options' => array(), - ), - true - ); - $this->assertNotWPError( $page_break, 'A type sections cannot hold keeps its old top-level placement.' ); - $this->assertSame( 0, (int) $page_break['field_options']['in_section'] ); - } - /** - * @return void - */ - public function test_create_end_divider_on_a_child_form_is_stored_after_every_field() { - $layout = $this->create_repeater_layout(); - $late = $this->store_field( 'text', $layout['child'], 70, array( 'in_section' => $layout['repeater'] ) ); - // end_divider is a Pro field type, so check the shared placement step directly. - $created = FrmFieldPlacementHelper::prepare_new_placement( - array( - 'type' => 'end_divider', - 'form_id' => $layout['child'], - 'field_order' => 1, - 'field_options' => array(), - ), - false - ); + $this->assertNotWPError( $updated ); + $this->assertSame( $field_id, (int) $checked['field']->id ); + $this->assertSame( 'Renamed', $checked['input']['name'] ); - $this->assertNotWPError( $created ); - $this->assertSame( $layout['form'], $created['form_id'], 'End fields only render in the top-level form.' ); - $this->assertGreaterThan( (int) FrmField::getOne( $late )->field_order, $created['field_order'], 'A new end field must not land on a child field order.' ); + $stored = FrmField::getOne( $field_id ); + $this->assertSame( $other, (int) $stored->form_id ); + $this->assertSame( 55, (int) $stored->field_order ); + $this->assertSame( 'Renamed', $stored->name ); } /** * @return void */ - public function test_update_field_moves_a_field_into_and_out_of_a_repeater() { - $layout = $this->create_repeater_layout(); - - $moved_in = $this->execute( - 'update-field', - array( - 'id' => $layout['after'], - 'field_options' => array( 'in_section' => $layout['repeater'] ), - ) + public function test_update_field_returns_an_error_from_the_update_hook() { + $field_id = $this->store_field( 'text', $this->create_empty_form(), 1 ); + + add_filter( + 'frm_ability_update_field_placement', + function () { + return new WP_Error( 'frm_field_order_outside_section', 'Outside.', array( 'status' => 400 ) ); + } ); - $this->assertNotWPError( $moved_in ); - $this->assertSame( $layout['child'], $moved_in['form_id'], 'Moving into a repeater stores the field in the child form, like a builder drag.' ); - $this->assert_between( $layout['after'], $layout['repeater'], $layout['end'] ); - - $moved_out = $this->execute( + $result = $this->execute( 'update-field', array( - 'id' => $layout['after'], - 'field_options' => array( 'in_section' => 0 ), + 'id' => $field_id, + 'name' => 'Renamed', ) ); - $this->assertNotWPError( $moved_out ); - $this->assertSame( $layout['form'], $moved_out['form_id'], 'Moving out of a repeater stores the field in the top-level form.' ); - $this->assertGreaterThan( (int) FrmField::getOne( $layout['end'] )->field_order, $moved_out['field_order'], 'With no order given, the field goes after the repeater.' ); + $this->assertWPError( $result ); + $this->assertSame( 'frm_field_order_outside_section', $result->get_error_code() ); + $this->assertNotSame( 'Renamed', FrmField::getOne( $field_id )->name, 'A rejected update is not saved.' ); } /** - * Callers often resend every stored option, so an unchanged in_section must not move a legacy field. - * * @return void */ - public function test_update_field_keeps_a_legacy_placement_when_in_section_is_unchanged() { - $layout = $this->create_repeater_layout(); - $legacy = $this->store_field( 'text', $layout['form'], 2, array( 'in_section' => $layout['repeater'] ) ); - $stored = FrmField::getOne( $legacy ); + public function test_delete_field_fires_hooks_before_and_after_the_field_is_gone() { + $field_id = $this->store_field( 'text', $this->create_empty_form(), 1 ); + $calls = array(); + + add_action( + 'frm_ability_before_delete_field', + function ( $field ) use ( &$calls ) { + $calls[] = array( 'before', (int) $field->id, (bool) FrmField::getOne( $field->id ) ); + } + ); + add_action( + 'frm_ability_after_delete_field', + function ( $field ) use ( &$calls ) { + $calls[] = array( 'after', (int) $field->id, (bool) FrmField::getOne( $field->id ) ); + } + ); - $updated = $this->execute( - 'update-field', + $this->assertNotWPError( $this->execute( 'delete-field', array( 'id' => $field_id ) ) ); + $this->assertSame( array( - 'id' => $legacy, - 'name' => 'Renamed', - 'field_options' => (array) $stored->field_options, - ) + array( 'before', $field_id, true ), + array( 'after', $field_id, false ), + ), + $calls ); - - $this->assertNotWPError( $updated ); - $this->assertSame( $layout['form'], $updated['form_id'], 'Its saved entry values stay with the form they were saved on.' ); - $this->assertSame( (int) $stored->field_order, $updated['field_order'] ); } /** * @return void */ - public function test_delete_section_removes_its_end_and_keeps_its_fields() { - $form = $this->create_empty_form(); - $section = $this->store_field( 'divider', $form, 1 ); - $inside = $this->store_field( 'text', $form, 2, array( 'in_section' => $section ) ); - $end = $this->store_field( 'end_divider', $form, 3 ); + public function test_create_form_creates_inline_fields_itself_without_a_hook() { + $created = $this->execute( 'create-form', $this->inline_form_input() ); - $this->assertNotWPError( $this->execute( 'delete-field', array( 'id' => $section ) ) ); - - $this->assertNull( FrmField::getOne( $end ), 'An end_divider with nothing to close must go with its section.' ); - $this->assertNotNull( FrmField::getOne( $inside ), 'Deleting a heading must not delete the fields under it.' ); - $this->assertSame( 0, (int) FrmField::get_option( FrmField::getOne( $inside ), 'in_section' ), 'The kept field must not point at a deleted section.' ); + $this->assertNotWPError( $created ); + $this->assertCount( 2, FrmField::get_all_for_form( $created['id'] ) ); } /** * @return void */ - public function test_delete_repeater_removes_its_child_form_and_end() { - $layout = $this->create_repeater_layout(); - $legacy = $this->store_field( 'text', $layout['form'], 2, array( 'in_section' => $layout['repeater'] ) ); + public function test_create_form_skips_inline_fields_a_hook_created() { + $received = array(); + + add_filter( + 'frm_ability_create_inline_fields', + function ( $created, $fields, $form ) use ( &$received ) { + $received = array( count( $fields ), (int) $form->id ); + return true; + }, + 10, + 3 + ); - $this->assertNotWPError( $this->execute( 'delete-field', array( 'id' => $layout['repeater'] ) ) ); + $created = $this->execute( 'create-form', $this->inline_form_input() ); - $this->assertNull( FrmField::getOne( $layout['end'] ) ); - $this->assertNull( FrmField::getOne( $layout['item'] ), 'The child form fields go with the repeater.' ); - $this->assertEmpty( FrmForm::getOne( $layout['child'] ), 'An empty child form must not be left linked to the parent.' ); - $this->assertSame( 0, (int) FrmField::get_option( FrmField::getOne( $legacy ), 'in_section' ) ); - $this->assertNotNull( FrmField::getOne( $layout['after'] ), 'Fields after the repeater are untouched.' ); + $this->assertNotWPError( $created ); + $this->assertSame( array( 2, (int) $created['id'] ), $received ); + $this->assertEmpty( FrmField::get_all_for_form( $created['id'] ), 'Formidable must not create the fields a second time.' ); } /** - * Inline fields cannot name their section by ID, so their order places them, and open sections get closed. - * * @return void */ - public function test_create_form_places_inline_fields_in_their_sections() { - $created = $this->execute( - 'create-form', - array( - 'name' => 'Inline sections', - // Without Pro, end_divider is not an inline type, so every section here is closed automatically. - 'fields' => array( - array( - 'type' => 'text', - 'name' => 'Before', - ), - array( - 'type' => 'section', - 'name' => 'First', - ), - array( - 'type' => 'text', - 'name' => 'In first', - ), - array( - 'type' => 'section', - 'name' => 'Second', - ), - array( - 'type' => 'text', - 'name' => 'In second', - ), - array( - 'type' => 'text', - 'name' => 'Also in second', - ), - array( - 'type' => 'text', - 'name' => 'After', - 'field_options' => array( 'in_section' => 0 ), - ), - ), - ) + public function test_create_form_returns_an_error_from_the_inline_fields_hook() { + add_filter( + 'frm_ability_create_inline_fields', + function () { + return new WP_Error( 'frm_invalid_section', 'Not a section.', array( 'status' => 400 ) ); + } ); - $this->assertNotWPError( $created ); - - $fields = FrmField::get_all_for_form( $created['id'] ); - $names = array(); - $ids = array(); - $section = array(); + $result = $this->execute( 'create-form', $this->inline_form_input() ); - foreach ( $fields as $field ) { - $names[] = $field->type . ':' . $field->name; - $ids[ $field->name ] = (int) $field->id; - $section[ $field->name ] = (int) FrmField::get_option( $field, 'in_section' ); - } + $this->assertWPError( $result ); + $this->assertSame( 'frm_invalid_section', $result->get_error_code() ); + } - $end = FrmFieldsHelper::setup_new_vars( 'end_divider' )['name']; - $this->assertSame( - array( - 'text:Before', - 'divider:First', - 'text:In first', - 'end_divider:' . $end, - 'divider:Second', - 'text:In second', - 'text:Also in second', - 'end_divider:' . $end, - 'text:After', + /** + * @return array + */ + private function inline_form_input() { + return array( + 'name' => 'Inline fields', + 'fields' => array( + array( + 'type' => 'text', + 'name' => 'First', + ), + array( + 'type' => 'text', + 'name' => 'Second', + ), ), - $names, - 'Every section must be closed exactly once, in order.' ); - $this->assertSame( $ids['First'], $section['In first'] ); - $this->assertSame( $ids['Second'], $section['In second'] ); - $this->assertSame( $ids['Second'], $section['Also in second'] ); - $this->assertSame( 0, $section['After'], 'An explicit in_section of 0 takes the field out of the open section.' ); } /** @@ -497,18 +387,4 @@ private function store_field( $type, $form_id, $order, $options = array() ) { return (int) $field_id; } - - /** - * @param int $field_id Field that must sit inside the window. - * @param int $start_id Field that opens the window. - * @param int $end_id Field that closes the window. - * - * @return void - */ - private function assert_between( $field_id, $start_id, $end_id ) { - $order = (int) FrmField::getOne( $field_id )->field_order; - - $this->assertGreaterThan( (int) FrmField::getOne( $start_id )->field_order, $order, 'The field must be ordered after the section opens.' ); - $this->assertLessThan( (int) FrmField::getOne( $end_id )->field_order, $order, 'The field must be ordered before the section closes.' ); - } } diff --git a/tests/phpunit/fields/test_FrmSectionStructure.php b/tests/phpunit/fields/test_FrmSectionStructure.php deleted file mode 100644 index 48aa45c1e8..0000000000 --- a/tests/phpunit/fields/test_FrmSectionStructure.php +++ /dev/null @@ -1,268 +0,0 @@ -form_id = $this->create_empty_form(); - } - - /** - * @return void - */ - public function test_builder_order_is_unchanged_without_repeaters() { - $ids = array( - $this->add_field( 'text', 1 ), - $this->add_field( 'divider', 2 ), - $this->add_field( 'text', 3 ), - $this->add_field( 'end_divider', 4 ), - $this->add_field( 'text', 5 ), - ); - - $this->assertSame( $ids, $this->get_builder_order(), 'A form with no repeater must keep its stored order.' ); - } - - /** - * @return void - */ - public function test_builder_shows_child_fields_ordered_after_the_end_divider_inside_the_repeater() { - $child = $this->create_child_form(); - $before = $this->add_field( 'text', 1 ); - $repeater = $this->add_repeater( 2, $child ); - $end = $this->add_field( 'end_divider', 3 ); - // Same orders as a real broken form: the children sort after the end_divider. - $first = $this->add_field( 'text', 4, array( 'in_section' => $repeater ), $child ); - $second = $this->add_field( 'text', 5, array( 'in_section' => $repeater ), $child ); - - $this->assertSame( array( $before, $repeater, $first, $second, $end ), $this->get_builder_order() ); - } - - /** - * @return void - */ - public function test_builder_shows_child_fields_ordered_before_the_divider_inside_the_repeater() { - $child = $this->create_child_form(); - $orphaned = $this->add_field( 'text', 1, array(), $child ); - $top = $this->add_field( 'text', 5 ); - $repeater = $this->add_repeater( 10, $child ); - $end = $this->add_field( 'end_divider', 11 ); - - $this->assertSame( array( $top, $repeater, $orphaned, $end ), $this->get_builder_order() ); - } - - /** - * @return void - */ - public function test_builder_moves_parent_fields_inside_a_repeater_after_it() { - $child = $this->create_child_form(); - $repeater = $this->add_repeater( 1, $child ); - $in_child = $this->add_field( 'text', 2, array( 'in_section' => $repeater ), $child ); - $in_parent = $this->add_field( 'text', 3, array( 'in_section' => $repeater ) ); - $end = $this->add_field( 'end_divider', 4 ); - $after = $this->add_field( 'text', 5 ); - - $this->assertSame( - array( $repeater, $in_child, $end, $in_parent, $after ), - $this->get_builder_order(), - 'A parent form field is rendered once on the frontend, so the builder must not show it as repeated.' - ); - } - - /** - * @return void - */ - public function test_builder_closes_a_repeater_at_a_section_or_page_break_without_an_end_divider() { - $child = $this->create_child_form(); - $repeater = $this->add_repeater( 1, $child ); - $in_parent = $this->add_field( 'text', 2 ); - $break = $this->add_field( 'break', 3 ); - $after = $this->add_field( 'text', 4 ); - $in_child = $this->add_field( 'text', 9, array( 'in_section' => $repeater ), $child ); - - $this->assertSame( array( $repeater, $in_child, $in_parent, $break, $after ), $this->get_builder_order() ); - } - - /** - * @return void - */ - public function test_builder_keeps_each_child_form_with_its_own_repeater() { - $child_a = $this->create_child_form(); - $child_b = $this->create_child_form(); - $repeater_a = $this->add_repeater( 1, $child_a ); - $end_a = $this->add_field( 'end_divider', 2 ); - $repeater_b = $this->add_repeater( 3, $child_b ); - $end_b = $this->add_field( 'end_divider', 4 ); - // Swapped orders: each child field sits in the other repeater's window. - $in_b = $this->add_field( 'text', 2, array( 'in_section' => $repeater_b ), $child_b ); - $in_a = $this->add_field( 'text', 4, array( 'in_section' => $repeater_a ), $child_a ); - - $this->assertSame( array( $repeater_a, $in_a, $end_a, $repeater_b, $in_b, $end_b ), $this->get_builder_order() ); - } - - /** - * @return void - */ - public function test_builder_keeps_fields_of_an_unused_child_form_before_the_submit_button() { - $unused = $this->create_child_form(); - $top = $this->add_field( 'text', 1 ); - $missing = $this->add_repeater( 2, 999999 ); - $orphan = $this->add_field( 'text', 3, array(), $unused ); - $end = $this->add_field( 'end_divider', 4 ); - $submit = $this->add_field( 'submit', 5 ); - - $this->assertSame( - array( $top, $missing, $end, $orphan, $submit ), - $this->get_builder_order(), - 'Fields no repeater renders stay editable, and a repeater with a missing child form keeps its place.' - ); - } - - /** - * Other builder filters must see the list in the order the builder renders it. - * - * @return void - */ - public function test_builder_sort_runs_before_other_builder_filters() { - $this->assertSame( 5, has_filter( 'frm_fields_in_form_builder', 'FrmFieldPlacementHelper::sort_for_builder' ) ); - } - - /** - * @return void - */ - public function test_builder_ignores_a_list_changed_into_another_shape() { - $fields = array( 'not a field' ); - $this->assertSame( $fields, FrmFieldPlacementHelper::sort_for_builder( $fields, array( 'form' => FrmForm::getOne( $this->form_id ) ) ) ); - $this->assertSame( $fields, FrmFieldPlacementHelper::sort_for_builder( $fields, array() ) ); - } - - /** - * @return void - */ - public function test_repeater_for_child_form_is_found_from_form_select() { - $child = $this->create_child_form(); - $repeater = $this->add_repeater( 1, $child ); - - $this->assertSame( $repeater, (int) FrmFieldPlacementHelper::get_repeater_for_child_form( $child )->id ); - $this->assertFalse( FrmFieldPlacementHelper::get_repeater_for_child_form( $this->create_child_form() ), 'A child form no repeater uses has no repeater.' ); - $this->assertFalse( FrmFieldPlacementHelper::get_repeater_for_child_form( $this->form_id ), 'A top-level form is not a child form.' ); - $this->assertFalse( FrmFieldPlacementHelper::get_repeater_for_child_form( 0 ) ); - } - - /** - * @return void - */ - public function test_container_parts_find_the_end_field_and_children() { - $section = $this->add_field( 'divider', 1 ); - $inside = $this->add_field( 'text', 2, array( 'in_section' => $section ) ); - $end = $this->add_field( 'end_divider', 3 ); - $this->add_field( 'text', 4 ); - - $parts = FrmFieldPlacementHelper::get_container_parts( FrmField::getOne( $section ) ); - - $this->assertSame( $end, (int) $parts['end']->id ); - $this->assertSame( array( $inside ), array_map( 'intval', wp_list_pluck( $parts['children'], 'id' ) ) ); - } - - /** - * A section closed by the next section has no end field to delete. - * - * @return void - */ - public function test_container_parts_skip_an_implicit_close() { - $section = $this->add_field( 'divider', 1 ); - $this->add_field( 'divider', 2 ); - - $this->assertFalse( FrmFieldPlacementHelper::get_container_parts( FrmField::getOne( $section ) )['end'] ); - } - - /** - * @return int[] - */ - private function get_builder_order() { - $fields = FrmField::get_all_for_form( $this->form_id ); - /** This filter is documented in classes/controllers/FrmFormsController.php */ - $fields = apply_filters( 'frm_fields_in_form_builder', $fields, array( 'form' => FrmForm::getOne( $this->form_id ) ) ); - - return array_map( 'intval', wp_list_pluck( $fields, 'id' ) ); - } - - /** - * @return int - */ - private function create_child_form() { - return $this->create_empty_form( $this->form_id ); - } - - /** - * The form factory adds a text field to every form, which these tests must not have. - * - * @param int $parent_form_id Parent form, 0 for a top-level form. - * - * @return int - */ - private function create_empty_form( $parent_form_id = 0 ) { - return (int) FrmForm::create( - array( - 'form_key' => '', - 'name' => 'Section structure', - 'description' => '', - 'parent_form_id' => $parent_form_id, - ) - ); - } - - /** - * @param int $order Field order. - * @param int $child_form_id Child form for form_select. - * - * @return int - */ - private function add_repeater( $order, $child_form_id ) { - return $this->add_field( - 'divider', - $order, - array( - 'repeat' => 1, - 'form_select' => $child_form_id, - ) - ); - } - - /** - * Store a field exactly as given, the way legacy data and imports can hold it. - * - * @param string $type Field type. - * @param int $order Field order. - * @param array $options Field options to merge over the defaults. - * @param int|null $form_id Form, the test form by default. - * - * @return int - */ - private function add_field( $type, $order, $options = array(), $form_id = null ) { - $values = FrmFieldsHelper::setup_new_vars( $type, $form_id ?? $this->form_id ); - $values['field_order'] = $order; - $values['field_options'] = array_merge( $values['field_options'], $options ); - $field_id = FrmField::create( $values ); - - $this->assertNotFalse( $field_id, 'The ' . $type . ' test field should be created.' ); - - return (int) $field_id; - } -} From 14a6e0e7e0f8b2abdb180eee11cbe9434a1222e3 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 27 Nov 2026 13:52:54 -0400 Subject: [PATCH 3/5] test(fields): cover adding to a section with several fields Co-Authored-By: Claude Opus 5.5 --- .../test_FrmAbilitiesFieldPlacement.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php index 2e5fa53fb0..a9753796e5 100644 --- a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php +++ b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php @@ -169,6 +169,38 @@ public function test_create_field_in_a_section_without_an_order_is_added_before_ $this->assertSame( 5, (int) FrmField::getOne( $after )->field_order ); } + /** + * The end of a section is after its last field, not its first. + * + * @return void + */ + public function test_create_field_in_a_section_with_several_fields_is_added_after_the_last_one() { + $form = $this->create_empty_form(); + $section = $this->store_field( 'divider', $form, 1 ); + $first = $this->store_field( 'text', $form, 2, array( 'in_section' => $section ) ); + $second = $this->store_field( 'text', $form, 3, array( 'in_section' => $section ) ); + $last = $this->store_field( 'text', $form, 4, array( 'in_section' => $section ) ); + $end = $this->store_field( 'end_divider', $form, 5 ); + $after = $this->store_field( 'text', $form, 6 ); + + $created = $this->execute( + 'create-field', + array( + 'form_id' => $form, + 'type' => 'text', + 'field_options' => array( 'in_section' => $section ), + ) + ); + + $this->assertNotWPError( $created ); + $this->assertSame( 5, $created['field_order'], 'The field goes after the last field of the section.' ); + $this->assertSame( 2, (int) FrmField::getOne( $first )->field_order ); + $this->assertSame( 3, (int) FrmField::getOne( $second )->field_order ); + $this->assertSame( 4, (int) FrmField::getOne( $last )->field_order, 'Fields already in the section keep their order.' ); + $this->assertSame( 6, (int) FrmField::getOne( $end )->field_order, 'The end_divider moves down so the field stays inside.' ); + $this->assertSame( 7, (int) FrmField::getOne( $after )->field_order ); + } + /** * @return void */ From 46a20b5797fa4716333794774ed3069b53294779 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 27 Nov 2026 14:12:09 -0400 Subject: [PATCH 4/5] refactor(fields): keep placement fallback in the placement helper Moves the placement hook and the earlier repeater fallback into FrmFieldPlacementHelper so the controller stays under the phpcs file length limit, and narrows the filter result to its documented type. Co-Authored-By: Claude Opus 5.5 --- .../FrmAbilitiesFieldsController.php | 109 +----------------- .../FrmAbilitiesFormsController.php | 2 +- classes/helpers/FrmFieldPlacementHelper.php | 98 +++++++++++++++- .../test_FrmAbilitiesFieldPlacement.php | 4 +- 4 files changed, 103 insertions(+), 110 deletions(-) diff --git a/classes/controllers/FrmAbilitiesFieldsController.php b/classes/controllers/FrmAbilitiesFieldsController.php index 194467e4d8..56e421b155 100644 --- a/classes/controllers/FrmAbilitiesFieldsController.php +++ b/classes/controllers/FrmAbilitiesFieldsController.php @@ -379,7 +379,6 @@ public static function execute_create_field( $input ) { return FrmAbilitiesHelper::flatten_error( $prepared ); } - // prepare_new_field() may have moved the field into a repeater's child form. $field_id = FrmField::create( $prepared ); if ( ! $field_id ) { @@ -433,17 +432,12 @@ public static function execute_update_field( $input ) { $field_data = self::add_stored_values_for_cleaning( $field, $field_data ); /** - * Filters an update-field ability call before it is saved, so Pro can move the field when its section changes. - * - * Return a WP_Error to reject the update. + * Filters an update-field ability call before it is saved. Pro moves the field when its section changes, or returns a WP_Error. * * @since x.x * * @param array $field_data Field data to save, with field_options merged over the stored options. - * @param array $args { - * @type stdClass $field The stored field. - * @type array $input The update input as sent, before stored options were merged in. - * } + * @param array $args Contains `field`, the stored field, and `input`, the update as sent. */ $field_data = apply_filters( 'frm_ability_update_field_placement', @@ -473,7 +467,6 @@ public static function execute_update_field( $input ) { FrmField::delete_form_transient( $field->form_id ); if ( isset( $field_data['form_id'] ) ) { - // The field moved to another form. FrmField::delete_form_transient( $field_data['form_id'] ); } @@ -593,7 +586,7 @@ public static function prepare_new_field( $field, $form ) { $prepared['form_id'] = $form_id; $prepared = self::maybe_create_repeat_form( $prepared, $form_id ); - $prepared = self::place_new_field( $prepared, $field, $form ); + $prepared = FrmFieldPlacementHelper::place_new_ability_field( $prepared, $field, $form ); if ( is_wp_error( $prepared ) ) { return $prepared; @@ -734,102 +727,6 @@ private static function maybe_create_repeat_form( $prepared, $form_id ) { return $prepared; } - /** - * Decide which form, section and field_order a new field gets. - * - * Sections and repeaters are Pro fields, so Pro places fields in them - * through the frm_ability_new_field_placement filter. With a Pro version - * that does not, the earlier repeater placement is kept. - * - * @since x.x - * - * @param array $prepared Prepared field data. - * @param array $field Raw field input. - * @param stdClass $form The form the field is created on. - * - * @return array|WP_Error - */ - private static function place_new_field( $prepared, $field, $form ) { - if ( ! FrmFieldPlacementHelper::has_ability_section_placement() ) { - return self::maybe_position_in_repeater( $prepared, $field, $form ); - } - - /** - * Filters where a field created by an ability is stored. - * - * A callback may change form_id, field_order and field_options.in_section, - * or return a WP_Error to reject the field. - * - * @since x.x - * - * @param array $prepared Prepared field data. - * @param array $args { - * @type array $field The field input as sent. - * @type stdClass $form The form the field is created on. - * @type bool $has_order True when the caller chose the field_order. - * } - */ - return apply_filters( - 'frm_ability_new_field_placement', - $prepared, - array( - 'field' => $field, - 'form' => $form, - 'has_order' => isset( $field['field_order'] ), - ) - ); - } - - /** - * Slot a new repeater child field after the last field of its section. - * - * Repeater child fields sort into the parent form's field list purely by - * field_order, so an order computed against the child form alone can land - * before the repeater's own divider and render outside the section in the - * builder. When the caller gives no explicit order, the field goes right - * after the last field of its section and the fields that follow shift down. - * - * @since 7.0 - * - * @param array $prepared Prepared field data. - * @param array $field Raw field input. - * @param stdClass $form The form the field is created on. - * - * @return array - */ - private static function maybe_position_in_repeater( $prepared, $field, $form ) { - if ( isset( $field['field_order'] ) || empty( $form->parent_form_id ) ) { - return $prepared; - } - - $parent_fields = FrmField::get_all_for_form( $form->parent_form_id, '', 'include' ); - $section_order = 0; - - foreach ( $parent_fields as $parent_field ) { - $form_select = isset( $parent_field->field_options['form_select'] ) ? (int) $parent_field->field_options['form_select'] : 0; - $starts_this_section = 'divider' === $parent_field->type && $form_select === (int) $form->id; - $in_this_section = (int) $parent_field->form_id === (int) $form->id; - - if ( $starts_this_section || $in_this_section ) { - $section_order = max( $section_order, (int) $parent_field->field_order ); - } - } - - if ( ! $section_order ) { - return $prepared; - } - - $prepared['field_order'] = $section_order + 1; - - foreach ( $parent_fields as $parent_field ) { - if ( (int) $parent_field->field_order > $section_order ) { - FrmField::update( $parent_field->id, array( 'field_order' => (int) $parent_field->field_order + 1 ) ); - } - } - - return $prepared; - } - /** * Reject a repeater or embedded form field with no usable child form. * diff --git a/classes/controllers/FrmAbilitiesFormsController.php b/classes/controllers/FrmAbilitiesFormsController.php index 0d21b3290f..361d295e6a 100644 --- a/classes/controllers/FrmAbilitiesFormsController.php +++ b/classes/controllers/FrmAbilitiesFormsController.php @@ -614,7 +614,7 @@ public static function execute_create_form( $input ) { * * @since x.x * - * @param null|true|WP_Error $created Null to let Formidable create the fields. + * @param true|WP_Error|null $created Null to let Formidable create the fields. * @param array $fields The inline field input, in order. * @param stdClass $form The new form. */ diff --git a/classes/helpers/FrmFieldPlacementHelper.php b/classes/helpers/FrmFieldPlacementHelper.php index f4bbffe2c2..693c838649 100644 --- a/classes/helpers/FrmFieldPlacementHelper.php +++ b/classes/helpers/FrmFieldPlacementHelper.php @@ -98,6 +98,102 @@ public static function is_allowed( $type, $form_id, $section_id = 0 ) { * @return bool */ public static function has_ability_section_placement() { - return (bool) has_filter( 'frm_ability_new_field_placement' ); + return has_filter( 'frm_ability_new_field_placement' ); + } + + /** + * Decide which form, section and field_order a new field gets. + * + * Sections and repeaters are Pro fields, so Pro places fields in them + * through the frm_ability_new_field_placement filter. With a Pro version + * that does not, the earlier repeater placement is kept. + * + * @since x.x + * + * @param array $prepared Prepared field data. + * @param array $field Raw field input. + * @param stdClass $form The form the field is created on. + * + * @return array|WP_Error + */ + public static function place_new_ability_field( $prepared, $field, $form ) { + if ( ! self::has_ability_section_placement() ) { + return self::maybe_position_in_repeater( $prepared, $field, $form ); + } + + /** + * Filters where a field created by an ability is stored. + * + * A callback may change form_id, field_order and field_options.in_section, + * or return a WP_Error to reject the field. + * + * @since x.x + * + * @param array $prepared Prepared field data. + * @param array $args Contains `field`, the input as sent, `form`, the form the field is created on, + * and `has_order`, true when the caller chose the field_order. + */ + $placed = apply_filters( + 'frm_ability_new_field_placement', + $prepared, + array( + 'field' => $field, + 'form' => $form, + 'has_order' => isset( $field['field_order'] ), + ) + ); + + return is_array( $placed ) || is_wp_error( $placed ) ? $placed : $prepared; + } + + /** + * Slot a new repeater child field after the last field of its section. + * + * Repeater child fields sort into the parent form's field list purely by + * field_order, so an order computed against the child form alone can land + * before the repeater's own divider and render outside the section in the + * builder. When the caller gives no explicit order, the field goes right + * after the last field of its section and the fields that follow shift down. + * + * @since 7.0 + * @since x.x Moved here from FrmAbilitiesFieldsController. + * + * @param array $prepared Prepared field data. + * @param array $field Raw field input. + * @param stdClass $form The form the field is created on. + * + * @return array + */ + private static function maybe_position_in_repeater( $prepared, $field, $form ) { + if ( isset( $field['field_order'] ) || empty( $form->parent_form_id ) ) { + return $prepared; + } + + $parent_fields = FrmField::get_all_for_form( $form->parent_form_id, '', 'include' ); + $section_order = 0; + + foreach ( $parent_fields as $parent_field ) { + $form_select = isset( $parent_field->field_options['form_select'] ) ? (int) $parent_field->field_options['form_select'] : 0; + $starts_this_section = 'divider' === $parent_field->type && $form_select === (int) $form->id; + $in_this_section = (int) $parent_field->form_id === (int) $form->id; + + if ( $starts_this_section || $in_this_section ) { + $section_order = max( $section_order, (int) $parent_field->field_order ); + } + } + + if ( ! $section_order ) { + return $prepared; + } + + $prepared['field_order'] = $section_order + 1; + + foreach ( $parent_fields as $parent_field ) { + if ( (int) $parent_field->field_order > $section_order ) { + FrmField::update( $parent_field->id, array( 'field_order' => (int) $parent_field->field_order + 1 ) ); + } + } + + return $prepared; } } diff --git a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php index dea75b42f6..6163bdd9e8 100644 --- a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php +++ b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php @@ -102,8 +102,8 @@ public function test_create_field_stores_the_field_where_the_placement_hook_puts add_filter( 'frm_ability_new_field_placement', function ( $prepared, $args ) use ( $other, &$checked ) { - $checked = $args; - $prepared['form_id'] = $other; + $checked = $args; + $prepared['form_id'] = $other; $prepared['field_order'] = 77; return $prepared; }, From 352c16583ef492a507ca209ceaca676d441cef72 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Fri, 27 Nov 2026 16:02:04 -0400 Subject: [PATCH 5/5] Tighten ability placement hook return handling Only skip the inline field loop on an explicit true from frm_ability_create_inline_fields, keep the form_id the placement filter picks for inline fields, and ignore a non-array return from frm_ability_update_field_placement instead of failing the update. Co-Authored-By: Claude Opus 5.5 --- .../FrmAbilitiesFieldsController.php | 9 +-- .../FrmAbilitiesFormsController.php | 8 +-- .../test_FrmAbilitiesFieldPlacement.php | 63 +++++++++++++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/classes/controllers/FrmAbilitiesFieldsController.php b/classes/controllers/FrmAbilitiesFieldsController.php index 56e421b155..b805c3b6e0 100644 --- a/classes/controllers/FrmAbilitiesFieldsController.php +++ b/classes/controllers/FrmAbilitiesFieldsController.php @@ -432,14 +432,14 @@ public static function execute_update_field( $input ) { $field_data = self::add_stored_values_for_cleaning( $field, $field_data ); /** - * Filters an update-field ability call before it is saved. Pro moves the field when its section changes, or returns a WP_Error. + * Filters update-field data before it is saved. Pro moves the field when its section changes. Return an array, or a WP_Error to stop. Anything else is ignored. * * @since x.x * * @param array $field_data Field data to save, with field_options merged over the stored options. * @param array $args Contains `field`, the stored field, and `input`, the update as sent. */ - $field_data = apply_filters( + $placed = apply_filters( 'frm_ability_update_field_placement', $field_data, array( @@ -448,10 +448,11 @@ public static function execute_update_field( $input ) { ) ); - if ( is_wp_error( $field_data ) ) { - return $field_data; + if ( is_wp_error( $placed ) ) { + return $placed; } + $field_data = is_array( $placed ) ? $placed : $field_data; $new_type = ! empty( $field_data['type'] ) ? $field_data['type'] : $field->type; $field_options = isset( $field_data['field_options'] ) && is_array( $field_data['field_options'] ) ? $field_data['field_options'] : (array) $field->field_options; $checked = self::check_form_select( $new_type, $field_options ); diff --git a/classes/controllers/FrmAbilitiesFormsController.php b/classes/controllers/FrmAbilitiesFormsController.php index 361d295e6a..601f5fb79b 100644 --- a/classes/controllers/FrmAbilitiesFormsController.php +++ b/classes/controllers/FrmAbilitiesFormsController.php @@ -610,7 +610,8 @@ public static function execute_create_form( $input ) { * * Pro uses it to put the fields listed between a section and its end_divider * inside that section. Return true when the fields were created, a WP_Error to - * stop, or null to create them one at a time here. + * stop, or null to create them one at a time here. Any other value is treated + * like null, so the fields are only skipped on an explicit true. * * @since x.x * @@ -624,7 +625,7 @@ public static function execute_create_form( $input ) { return FrmAbilitiesHelper::flatten_error( $created ); } - if ( null === $created ) { + if ( true !== $created ) { foreach ( $fields as $field ) { // Share create-field's pipeline so inline fields get the same // separate-value detection, repeater child form, and validation. @@ -634,8 +635,7 @@ public static function execute_create_form( $input ) { return FrmAbilitiesHelper::flatten_error( $prepared ); } - $prepared['form_id'] = $form_id; - + // Keep the form_id prepare_new_field() set, so a placement hook can move the field. FrmField::create( $prepared ); unset( $prepared, $field ); } diff --git a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php index 6163bdd9e8..4a417dea4a 100644 --- a/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php +++ b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php @@ -221,6 +221,32 @@ function () { $this->assertNotSame( 'Renamed', FrmField::getOne( $field_id )->name, 'A rejected update is not saved.' ); } + /** + * An update hook that returns nothing must not break the update. + * + * @return void + */ + public function test_update_field_ignores_a_non_array_from_the_update_hook() { + $form = $this->create_empty_form(); + $field_id = $this->store_field( 'text', $form, 1 ); + + add_filter( 'frm_ability_update_field_placement', '__return_null' ); + + $updated = $this->execute( + 'update-field', + array( + 'id' => $field_id, + 'name' => 'Renamed', + ) + ); + + $this->assertNotWPError( $updated ); + + $stored = FrmField::getOne( $field_id ); + $this->assertSame( 'Renamed', $stored->name, 'The unfiltered update is saved.' ); + $this->assertSame( $form, (int) $stored->form_id ); + } + /** * @return void */ @@ -284,6 +310,43 @@ function ( $created, $fields, $form ) use ( &$received ) { $this->assertEmpty( FrmField::get_all_for_form( $created['id'] ), 'Formidable must not create the fields a second time.' ); } + /** + * Only an explicit true means a callback created the fields. + * + * @return void + */ + public function test_create_form_creates_inline_fields_when_the_hook_returns_false() { + add_filter( 'frm_ability_create_inline_fields', '__return_false' ); + + $created = $this->execute( 'create-form', $this->inline_form_input() ); + + $this->assertNotWPError( $created ); + $this->assertCount( 2, FrmField::get_all_for_form( $created['id'] ) ); + } + + /** + * Inline fields go through the placement hook like create-field, and the form it picks is kept. + * + * @return void + */ + public function test_create_form_stores_inline_fields_where_the_placement_hook_puts_them() { + $other = $this->create_empty_form(); + + add_filter( + 'frm_ability_new_field_placement', + function ( $prepared ) use ( $other ) { + $prepared['form_id'] = $other; + return $prepared; + } + ); + + $created = $this->execute( 'create-form', $this->inline_form_input() ); + + $this->assertNotWPError( $created ); + $this->assertEmpty( FrmField::get_all_for_form( $created['id'] ), 'The requested form must not overwrite the form the hook chose.' ); + $this->assertCount( 2, FrmField::get_all_for_form( $other ) ); + } + /** * @return void */