Repository navigation
Include the previous form ID in field update hooks #3723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,8 +8,9 @@ class FrmField { | |
| /** | ||
| * Version of the persistent field write hooks available to derived indexes. | ||
| * | ||
| * Version 1 covers frm_field_created and frm_field_updated, plus frm_field_order_updated for | ||
| * Covers frm_field_created and frm_field_updated, plus frm_field_order_updated for | ||
| * builder saves that write the field order of many fields in one query without frm_field_updated. | ||
| * Includes the previous form ID when frm_field_updated moves a field between forms. | ||
| * | ||
| * @since x.x | ||
| */ | ||
|
|
@@ -852,6 +853,12 @@ public static function update( $id, $values ) { | |
| $values['default_value'] = json_encode( $values['default_value'] ); | ||
| } | ||
|
|
||
| $previous_form_id = 0; | ||
|
|
||
| if ( isset( $values['form_id'] ) ) { | ||
| $previous_form_id = (int) self::get_type( $id, 'form_id' ); | ||
| } | ||
|
|
||
| $query_results = $wpdb->update( $wpdb->prefix . 'frm_fields', $values, array( 'id' => $id ) ); | ||
| $form_id = 0; | ||
|
|
||
|
|
@@ -882,8 +889,9 @@ public static function update( $id, $values ) { | |
| * @since x.x | ||
| * | ||
| * @param int $id Updated field ID. | ||
| * @param int $previous_form_id Previous form ID when the update includes form_id, otherwise zero. | ||
| */ | ||
| do_action( 'frm_field_updated', $id ); | ||
| do_action( 'frm_field_updated', $id, $previous_form_id ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking, and this isn't new in this PR. When Only the lookup is new, so I read this from source and did not reproduce it live. If you want to close it here, now that the previous ID is known, add this after that call: if ( $previous_form_id && $previous_form_id !== $form_id ) {
self::delete_form_transient( $previous_form_id );
}It's fine as a follow-up too, since the Pro index refresh is driven by the hook argument and not by this cache. |
||
| } | ||
|
|
||
| return $query_results; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @covers FrmField::update | ||
| */ | ||
| class test_FrmFieldUpdateHooks extends FrmUnitTest { | ||
|
|
||
| /** | ||
| * @var array<string> | ||
| */ | ||
| private $field_reads = array(); | ||
|
|
||
| /** | ||
| * @var array<int> | ||
| */ | ||
| private $updated_field = array(); | ||
|
|
||
| public function tearDown(): void { | ||
| remove_filter( 'query', array( $this, 'record_field_read' ) ); | ||
| remove_action( 'frm_field_updated', array( $this, 'record_updated_field' ) ); | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider update_cases | ||
| * | ||
| * @param bool $warm_cache Whether the field is cached before the update. | ||
| * @param bool $move Whether the update moves the field to another form. | ||
| */ | ||
| #[\PHPUnit\Framework\Attributes\DataProvider( 'update_cases' )] | ||
| public function test_previous_form_id_reuses_the_placement_lookup( $warm_cache, $move ) { | ||
| $form_id = (int) $this->factory->form->create(); | ||
| $target = $move ? (int) $this->factory->form->create() : $form_id; | ||
| $field_id = (int) $this->factory->field->create( | ||
| array( | ||
| 'form_id' => $form_id, | ||
| 'type' => 'text', | ||
| ) | ||
| ); | ||
| FrmDb::cache_delete_group( 'frm_field' ); | ||
|
|
||
| if ( $warm_cache ) { | ||
| FrmField::getOne( $field_id ); | ||
| } | ||
|
|
||
| add_filter( 'query', array( $this, 'record_field_read' ) ); | ||
| add_action( 'frm_field_updated', array( $this, 'record_updated_field' ), 10, 2 ); | ||
| $result = FrmField::update( $field_id, array( 'form_id' => $target ) ); | ||
| remove_filter( 'query', array( $this, 'record_field_read' ) ); | ||
|
|
||
| $this->assertNotFalse( $result, 'The field update should succeed.' ); | ||
| $this->assertCount( $warm_cache ? 0 : 1, $this->field_reads, 'Reading the previous form ID should add no query beyond placement validation.' ); | ||
| $this->assertSame( array( $field_id, $form_id ), $this->updated_field, 'The hook should receive the original form ID for both moves and ordinary updates.' ); | ||
| $this->assertSame( $target, (int) FrmField::getOne( $field_id )->form_id, 'The field should belong to the requested destination after the update.' ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public static function update_cases() { | ||
| return array( | ||
| 'cached ordinary update' => array( true, false ), | ||
| 'uncached ordinary update' => array( false, false ), | ||
| 'cached move' => array( true, true ), | ||
| 'uncached move' => array( false, true ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Record field table reads while an update is running. | ||
| * | ||
| * @param string $query SQL statement. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function record_field_read( $query ) { | ||
| global $wpdb; | ||
|
|
||
| if ( str_starts_with( $query, 'SELECT' ) && str_contains( $query, $wpdb->prefix . 'frm_fields' ) ) { | ||
| $this->field_reads[] = $query; | ||
| } | ||
|
|
||
| return $query; | ||
| } | ||
|
|
||
| /** | ||
| * Capture the field ID and its original form ID from the update hook. | ||
| * | ||
| * @param int $field_id Updated field ID. | ||
| * @param int $previous_form_id Original form ID. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function record_updated_field( $field_id, $previous_form_id ) { | ||
| $this->updated_field = array( $field_id, $previous_form_id ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear the previous form's field cache when a field moves.
When a field moves to another form,
update()clears only the destination form's field-list transients. The source form's transients still contain the moved field.get_all_for_form()returns those cached rows without a database check. A refresh of the source form that uses this method can still return the moved field. Clear the previous form's transients when the form ID changes.Suggested fix
if ( $query_results ) { wp_cache_delete( $id, 'frm_field' ); if ( $form_id ) { self::delete_form_transient( $form_id ); } + + if ( $previous_form_id && $previous_form_id !== $form_id ) { + self::delete_form_transient( $previous_form_id ); + } }