diff --git a/classes/controllers/FrmAbilitiesFieldsController.php b/classes/controllers/FrmAbilitiesFieldsController.php index c715f9d734..b805c3b6e0 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,30 @@ 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 ); + + /** + * 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. + */ + $placed = apply_filters( + 'frm_ability_update_field_placement', + $field_data, + array( + 'field' => $field, + 'input' => $input, + ) + ); + + 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 ); @@ -444,6 +466,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 ) ); @@ -489,6 +516,15 @@ public static function execute_delete_field( $input ) { $data = self::prepare_field_for_response( $field ); $form_id = $field->form_id; + /** + * 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 ); } @@ -497,6 +533,15 @@ public static function execute_delete_field( $input ) { return self::get_invalid_field_error(); } + /** + * 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(); @@ -542,7 +587,12 @@ 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::place_new_ability_field( $prepared, $field, $form ); + + if ( is_wp_error( $prepared ) ) { + return $prepared; + } + $prepared = self::maybe_enable_separate_values( $prepared, $field ); if ( empty( $field['field_key'] ) && ! empty( $field['name'] ) ) { @@ -678,56 +728,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..601f5fb79b 100644 --- a/classes/controllers/FrmAbilitiesFormsController.php +++ b/classes/controllers/FrmAbilitiesFormsController.php @@ -603,8 +603,30 @@ public static function execute_create_form( $input ) { FrmFormsController::create_default_email_action( $new_form ); } - if ( ! empty( $input['fields'] ) && is_array( $input['fields'] ) ) { - foreach ( $input['fields'] as $field ) { + $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. Any other value is treated + * like null, so the fields are only skipped on an explicit true. + * + * @since x.x + * + * @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. + */ + $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 ( 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. $prepared = FrmAbilitiesFieldsController::prepare_new_field( $field, $new_form ); @@ -613,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/classes/helpers/FrmFieldPlacementHelper.php b/classes/helpers/FrmFieldPlacementHelper.php index 03c0ff7b11..693c838649 100644 --- a/classes/helpers/FrmFieldPlacementHelper.php +++ b/classes/helpers/FrmFieldPlacementHelper.php @@ -85,4 +85,115 @@ public static function is_allowed( $type, $form_id, $section_id = 0 ) { return FrmFieldFactory::get_field_type( $type )->allows_in_repeater(); } + + /** + * Check whether Pro places ability-created fields in sections and repeaters. + * + * 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 + * + * @return bool + */ + public static function has_ability_section_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 new file mode 100644 index 0000000000..4a417dea4a --- /dev/null +++ b/tests/phpunit/fields/test_FrmAbilitiesFieldPlacement.php @@ -0,0 +1,453 @@ +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(); + $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 ); + } + + /** + * 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_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( + 'form_id' => $layout['child'], + 'type' => 'text', + 'name' => 'Added', + ) + ); + + $this->assertNotWPError( $created ); + $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_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 + ); + + $this->assertTrue( FrmFieldPlacementHelper::has_ability_section_placement() ); + + $created = $this->execute( + 'create-field', + array( + 'form_id' => $form, + 'type' => 'text', + 'field_order' => 5, + ) + ); + + $this->assertNotWPError( $created ); + $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_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', + ) + ); + + $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.' ); + } + + /** + * @return void + */ + 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 + ); + + $updated = $this->execute( + 'update-field', + array( + 'id' => $field_id, + 'name' => 'Renamed', + ) + ); + + $this->assertNotWPError( $updated ); + $this->assertSame( $field_id, (int) $checked['field']->id ); + $this->assertSame( 'Renamed', $checked['input']['name'] ); + + $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_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 ) ); + } + ); + + $result = $this->execute( + 'update-field', + array( + 'id' => $field_id, + 'name' => 'Renamed', + ) + ); + + $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.' ); + } + + /** + * 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 + */ + 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 ) ); + } + ); + + $this->assertNotWPError( $this->execute( 'delete-field', array( 'id' => $field_id ) ) ); + $this->assertSame( + array( + array( 'before', $field_id, true ), + array( 'after', $field_id, false ), + ), + $calls + ); + } + + /** + * @return void + */ + public function test_create_form_creates_inline_fields_itself_without_a_hook() { + $created = $this->execute( 'create-form', $this->inline_form_input() ); + + $this->assertNotWPError( $created ); + $this->assertCount( 2, FrmField::get_all_for_form( $created['id'] ) ); + } + + /** + * @return void + */ + 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 + ); + + $created = $this->execute( 'create-form', $this->inline_form_input() ); + + $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.' ); + } + + /** + * 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 + */ + 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 ) ); + } + ); + + $result = $this->execute( 'create-form', $this->inline_form_input() ); + + $this->assertWPError( $result ); + $this->assertSame( 'frm_invalid_section', $result->get_error_code() ); + } + + /** + * @return array + */ + private function inline_form_input() { + return array( + 'name' => 'Inline fields', + 'fields' => array( + array( + 'type' => 'text', + 'name' => 'First', + ), + array( + 'type' => 'text', + 'name' => 'Second', + ), + ), + ); + } + + /** + * 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; + } +}