From 2394cc3d759352131138731483e964a2d7e62f5b Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 18 Nov 2026 09:23:56 -0400 Subject: [PATCH 1/2] perf(fields): include the previous form ID --- classes/models/FrmField.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/classes/models/FrmField.php b/classes/models/FrmField.php index 591523b7a5..cca2113f0d 100644 --- a/classes/models/FrmField.php +++ b/classes/models/FrmField.php @@ -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) $wpdb->get_var( $wpdb->prepare( 'SELECT form_id FROM %i WHERE id = %d', $wpdb->prefix . 'frm_fields', $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 ); } return $query_results; From 1220abb8812019662eb056aed3d096f651de6d18 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 18 Nov 2026 09:30:47 -0400 Subject: [PATCH 2/2] perf(fields): reuse cached form IDs on update --- classes/models/FrmField.php | 2 +- .../fields/test_FrmFieldUpdateHooks.php | 97 +++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/fields/test_FrmFieldUpdateHooks.php diff --git a/classes/models/FrmField.php b/classes/models/FrmField.php index cca2113f0d..0316fa7b78 100644 --- a/classes/models/FrmField.php +++ b/classes/models/FrmField.php @@ -856,7 +856,7 @@ public static function update( $id, $values ) { $previous_form_id = 0; if ( isset( $values['form_id'] ) ) { - $previous_form_id = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT form_id FROM %i WHERE id = %d', $wpdb->prefix . 'frm_fields', $id ) ); + $previous_form_id = (int) self::get_type( $id, 'form_id' ); } $query_results = $wpdb->update( $wpdb->prefix . 'frm_fields', $values, array( 'id' => $id ) ); diff --git a/tests/phpunit/fields/test_FrmFieldUpdateHooks.php b/tests/phpunit/fields/test_FrmFieldUpdateHooks.php new file mode 100644 index 0000000000..b57063cc6a --- /dev/null +++ b/tests/phpunit/fields/test_FrmFieldUpdateHooks.php @@ -0,0 +1,97 @@ + + */ + private $field_reads = array(); + + /** + * @var array + */ + 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 ); + } +}