From 4cd13f020092e5e2352672a09de8922893025231 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 25 Nov 2026 10:44:19 -0400 Subject: [PATCH 1/3] feat(stripe): warn when the site URL no longer matches the connection Save the site URL when Stripe is connected and compare it to the current site URL on the Stripe settings page. When they differ, show an error suggesting the user disconnect and reconnect, since this usually means the database was copied from another site. A trailing slash difference is ignored. Connections made before this change save the current URL the first time the settings load. Co-Authored-By: Claude Opus 5.5 --- .../controllers/FrmStrpLiteAppController.php | 2 +- stripe/helpers/FrmStrpLiteConnectHelper.php | 84 ++++++++++++++++--- stripe/views/settings/connect.php | 12 +++ 3 files changed, 87 insertions(+), 11 deletions(-) diff --git a/stripe/controllers/FrmStrpLiteAppController.php b/stripe/controllers/FrmStrpLiteAppController.php index 3f5652a106..6d633e63e9 100644 --- a/stripe/controllers/FrmStrpLiteAppController.php +++ b/stripe/controllers/FrmStrpLiteAppController.php @@ -44,7 +44,7 @@ public static function uninstall() { ); $modes = array( 'test', 'live' ); - $option_name_keys = array( 'account_id', 'client_password', 'server_password', 'details_submitted' ); + $option_name_keys = array( 'account_id', 'client_password', 'server_password', 'details_submitted', 'site_url' ); foreach ( $modes as $mode ) { foreach ( $option_name_keys as $key ) { diff --git a/stripe/helpers/FrmStrpLiteConnectHelper.php b/stripe/helpers/FrmStrpLiteConnectHelper.php index 25e0a9bd5c..4b3809ab65 100644 --- a/stripe/helpers/FrmStrpLiteConnectHelper.php +++ b/stripe/helpers/FrmStrpLiteConnectHelper.php @@ -193,6 +193,55 @@ private static function get_stripe_details_submitted_option_name( $mode = 'auto' return self::get_strp_connect_option_name( 'details_submitted', $mode ); } + /** + * @since x.x + * + * @param string $mode either 'auto', 'live', or 'test'. + * + * @return string + */ + private static function get_site_url_option_name( $mode = 'auto' ) { + return self::get_strp_connect_option_name( 'site_url', $mode ); + } + + /** + * Save the current site URL as the URL this Stripe connection belongs to. + * + * @since x.x + * + * @param string $mode 'live' or 'test'. + * + * @return void + */ + private static function save_connected_site_url( $mode ) { + update_option( self::get_site_url_option_name( $mode ), self::get_site_url_for_connect_server(), false ); + } + + /** + * Get the site URL saved on connect when it no longer matches, usually because the database was copied from another site. + * + * @since x.x + * + * @param string $mode 'live' or 'test'. + * + * @return false|string The site URL the account was connected on, or false when it matches. A trailing slash (WPML can add one) is ignored. + */ + public static function get_mismatched_site_url( $mode ) { + if ( ! self::get_account_id( $mode ) ) { + return false; + } + + $connected_url = get_option( self::get_site_url_option_name( $mode ) ); + + if ( ! $connected_url || ! is_string( $connected_url ) ) { + // Connections made before the site URL was tracked have nothing saved yet. + self::save_connected_site_url( $mode ); + return false; + } + + return untrailingslashit( $connected_url ) === untrailingslashit( self::get_site_url_for_connect_server() ) ? false : $connected_url; + } + /** * @return string */ @@ -221,6 +270,7 @@ public static function reset_stripe_connect_integration() { delete_option( self::get_server_side_token_option_name( $mode ) ); delete_option( self::get_client_side_token_option_name( $mode ) ); delete_option( self::get_stripe_details_submitted_option_name( $mode ) ); + delete_option( self::get_site_url_option_name( $mode ) ); } /** @@ -331,6 +381,7 @@ private static function check_server_for_oauth_account_id() { if ( is_object( $data ) && ! empty( $data->account_id ) ) { update_option( self::get_account_id_option_name( $mode ), $data->account_id, false ); + self::save_connected_site_url( $mode ); if ( ! empty( $data->details_submitted ) ) { self::set_stripe_details_as_submitted( $mode ); @@ -473,18 +524,29 @@ private static function build_headers_for_post() { $password = 'lite_' . self::get_uuid(); } + $site_url = self::get_site_url_for_connect_server(); + + // $password is either a Pro license or a uuid (See FrmUsage::uuid). + return array( + 'Authorization' => 'Basic ' . base64_encode( $site_url . ':' . $password ), + ); + } + + /** + * Get the site URL the way the connect server identifies this site. + * + * @since x.x + * + * @return string + */ + private static function get_site_url_for_connect_server() { $site_url = home_url(); $site_url = self::maybe_fix_wpml_url( $site_url ); // Remove protocol from url (our url cannot include the colon). $site_url = preg_replace( '#^https?://#', '', $site_url ); // Remove port from url (mostly helpful in development). $site_url = preg_replace( '/:[0-9]+/', '', $site_url ); - $site_url = self::strip_lang_from_url( $site_url ); - - // $password is either a Pro license or a uuid (See FrmUsage::uuid). - return array( - 'Authorization' => 'Basic ' . base64_encode( $site_url . ':' . $password ), - ); + return self::strip_lang_from_url( $site_url ); } /** @@ -579,10 +641,12 @@ private static function render_settings() { $html = ''; foreach ( $modes as $mode ) { - $account_id = self::get_account_id( $mode ); - $connected = self::stripe_connect_is_setup( $mode ); - $test = 'test' === $mode; - $title = $test ? __( 'TEST', 'formidable' ) : __( 'LIVE', 'formidable' ); + $account_id = self::get_account_id( $mode ); + $connected = self::stripe_connect_is_setup( $mode ); + $test = 'test' === $mode; + $title = $test ? __( 'TEST', 'formidable' ) : __( 'LIVE', 'formidable' ); + $mismatched_url = self::get_mismatched_site_url( $mode ); + $current_url = self::get_site_url_for_connect_server(); ob_start(); require FrmStrpLiteAppHelper::plugin_path() . '/views/settings/connect.php'; diff --git a/stripe/views/settings/connect.php b/stripe/views/settings/connect.php index b21434ec35..6432e48e88 100644 --- a/stripe/views/settings/connect.php +++ b/stripe/views/settings/connect.php @@ -25,6 +25,18 @@ or + + +

From 6a37eea0fa266c17aa10c3efdc4a06df35159b74 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 25 Nov 2026 15:20:17 -0400 Subject: [PATCH 2/3] Save the Stripe connection site URL in the database upgrade Move the backfill for connections made before the site URL was tracked out of get_mismatched_site_url() and into a new db migration (108), so every site records its own URL when it updates instead of the first time an admin opens the Stripe settings. The getter no longer writes to the database during a page render. Add PHPUnit coverage for get_mismatched_site_url(), the backfill, the trailing slash handling, and the site_url cleanup on disconnect and uninstall. Co-Authored-By: Claude Opus 5.5 --- classes/helpers/FrmAppHelper.php | 2 +- classes/models/FrmMigrate.php | 13 +- stripe/helpers/FrmStrpLiteConnectHelper.php | 38 ++--- tests/phpunit/database/test_FrmMigrate.php | 16 +++ .../stripe/test_FrmStrpLiteConnectHelper.php | 134 ++++++++++++++++++ 5 files changed, 182 insertions(+), 21 deletions(-) create mode 100644 tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index 28a8b6f7ec..1b4cb3e80f 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -10,7 +10,7 @@ class FrmAppHelper { * * @var int */ - public static $db_version = 107; + public static $db_version = 108; /** * Used by the API add-on. diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index e622f780af..4845b23679 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -440,7 +440,7 @@ private function migrate_data( $old_db_version ) { return; } - $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98, 101, 104, 105, 107 ); + $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98, 101, 104, 105, 107, 108 ); foreach ( $migrations as $migration ) { if ( FrmAppHelper::$db_version < $migration || $old_db_version >= $migration ) { @@ -554,6 +554,17 @@ private function migrate_to_107() { } } + /** + * Save the site URL for Stripe connections made before it was tracked, so a copied database can be detected. + * + * @since x.x + * + * @return void + */ + private function migrate_to_108() { + FrmStrpLiteConnectHelper::save_missing_connected_site_urls(); + } + /** * Add new wp_options row for custom setting. * diff --git a/stripe/helpers/FrmStrpLiteConnectHelper.php b/stripe/helpers/FrmStrpLiteConnectHelper.php index 4b3809ab65..75bb8da89a 100644 --- a/stripe/helpers/FrmStrpLiteConnectHelper.php +++ b/stripe/helpers/FrmStrpLiteConnectHelper.php @@ -148,7 +148,7 @@ private static function get_active_mode_option_name_suffix( $mode = 'auto' ) { } /** - * @param string $key 'account_id', 'client_password', 'server_password', 'details_submitted'. + * @param string $key 'account_id', 'client_password', 'server_password', 'details_submitted', 'site_url'. * @param string $mode either 'auto', 'live', or 'test'. * * @return string @@ -194,27 +194,32 @@ private static function get_stripe_details_submitted_option_name( $mode = 'auto' } /** + * Save the current site URL as the URL this Stripe connection belongs to. + * * @since x.x * - * @param string $mode either 'auto', 'live', or 'test'. + * @param string $mode 'live' or 'test'. * - * @return string + * @return void */ - private static function get_site_url_option_name( $mode = 'auto' ) { - return self::get_strp_connect_option_name( 'site_url', $mode ); + private static function save_connected_site_url( $mode ) { + update_option( self::get_strp_connect_option_name( 'site_url', $mode ), self::get_site_url_for_connect_server(), false ); } /** - * Save the current site URL as the URL this Stripe connection belongs to. + * Save the current site URL for connections made before it was tracked. This runs from the database upgrade. + * It is best-effort, since a database copied before the update records the URL of the copy instead. * * @since x.x * - * @param string $mode 'live' or 'test'. - * * @return void */ - private static function save_connected_site_url( $mode ) { - update_option( self::get_site_url_option_name( $mode ), self::get_site_url_for_connect_server(), false ); + public static function save_missing_connected_site_urls() { + foreach ( array( 'test', 'live' ) as $mode ) { + if ( self::get_account_id( $mode ) && ! get_option( self::get_strp_connect_option_name( 'site_url', $mode ) ) ) { + self::save_connected_site_url( $mode ); + } + } } /** @@ -224,18 +229,13 @@ private static function save_connected_site_url( $mode ) { * * @param string $mode 'live' or 'test'. * - * @return false|string The site URL the account was connected on, or false when it matches. A trailing slash (WPML can add one) is ignored. + * @return false|string The site URL the account was connected on, or false when it matches or no URL is saved. A trailing slash (WPML can add one) is ignored. */ public static function get_mismatched_site_url( $mode ) { - if ( ! self::get_account_id( $mode ) ) { - return false; - } - - $connected_url = get_option( self::get_site_url_option_name( $mode ) ); + $connected_url = self::get_account_id( $mode ) ? get_option( self::get_strp_connect_option_name( 'site_url', $mode ) ) : false; + // The database upgrade saves a URL for older connections. Without one there is nothing to compare. if ( ! $connected_url || ! is_string( $connected_url ) ) { - // Connections made before the site URL was tracked have nothing saved yet. - self::save_connected_site_url( $mode ); return false; } @@ -270,7 +270,7 @@ public static function reset_stripe_connect_integration() { delete_option( self::get_server_side_token_option_name( $mode ) ); delete_option( self::get_client_side_token_option_name( $mode ) ); delete_option( self::get_stripe_details_submitted_option_name( $mode ) ); - delete_option( self::get_site_url_option_name( $mode ) ); + delete_option( self::get_strp_connect_option_name( 'site_url', $mode ) ); } /** diff --git a/tests/phpunit/database/test_FrmMigrate.php b/tests/phpunit/database/test_FrmMigrate.php index cf12265c56..6e6f8aec8c 100644 --- a/tests/phpunit/database/test_FrmMigrate.php +++ b/tests/phpunit/database/test_FrmMigrate.php @@ -292,6 +292,22 @@ public function test_migrate_to_97() { } } + /** + * @covers FrmMigrate::migrate_to_108 + */ + public function test_migrate_to_108() { + update_option( 'frm_strp_connect_account_id_live', 'acct_live' ); + delete_option( 'frm_strp_connect_site_url_live' ); + delete_option( 'frm_strp_connect_account_id_test' ); + delete_option( 'frm_strp_connect_site_url_test' ); + + $frmdb = new FrmMigrate(); + $this->run_private_method( array( $frmdb, 'migrate_to_108' ), array() ); + + $this->assertNotEmpty( get_option( 'frm_strp_connect_site_url_live' ), 'An existing Stripe connection should get the current site URL.' ); + $this->assertFalse( get_option( 'frm_strp_connect_site_url_test' ), 'A mode with no Stripe connection should not get a site URL.' ); + } + public function test_collation() { global $wpdb; diff --git a/tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php b/tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php new file mode 100644 index 0000000000..f2566f93d1 --- /dev/null +++ b/tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php @@ -0,0 +1,134 @@ +current_url = $this->run_private_method( array( 'FrmStrpLiteConnectHelper', 'get_site_url_for_connect_server' ) ); + $this->delete_connect_options(); + } + + public function tearDown(): void { + $this->delete_connect_options(); + $_POST = array(); + parent::tearDown(); + } + + /** + * @return void + */ + private function delete_connect_options() { + foreach ( array( 'test', 'live' ) as $mode ) { + foreach ( array( 'account_id', 'client_password', 'server_password', 'details_submitted', 'site_url' ) as $key ) { + delete_option( 'frm_strp_connect_' . $key . '_' . $mode ); + } + } + } + + /** + * @covers FrmStrpLiteConnectHelper::get_mismatched_site_url + */ + public function test_get_mismatched_site_url_without_account() { + update_option( 'frm_strp_connect_site_url_test', 'old-site.example.com' ); + $this->assertFalse( FrmStrpLiteConnectHelper::get_mismatched_site_url( 'test' ) ); + } + + /** + * @covers FrmStrpLiteConnectHelper::get_mismatched_site_url + */ + public function test_get_mismatched_site_url_without_saved_url() { + update_option( 'frm_strp_connect_account_id_test', 'acct_test' ); + + $this->assertFalse( FrmStrpLiteConnectHelper::get_mismatched_site_url( 'test' ) ); + $this->assertFalse( get_option( 'frm_strp_connect_site_url_test' ), 'Reading the mismatch should not save a URL.' ); + } + + /** + * @covers FrmStrpLiteConnectHelper::get_mismatched_site_url + */ + public function test_get_mismatched_site_url_with_matching_url() { + update_option( 'frm_strp_connect_account_id_test', 'acct_test' ); + + update_option( 'frm_strp_connect_site_url_test', $this->current_url ); + $this->assertFalse( FrmStrpLiteConnectHelper::get_mismatched_site_url( 'test' ) ); + + update_option( 'frm_strp_connect_site_url_test', trailingslashit( $this->current_url ) ); + $this->assertFalse( FrmStrpLiteConnectHelper::get_mismatched_site_url( 'test' ), 'A trailing slash on the saved URL should be ignored.' ); + + add_filter( 'home_url', 'trailingslashit' ); + update_option( 'frm_strp_connect_site_url_test', untrailingslashit( $this->current_url ) ); + $mismatched_url = FrmStrpLiteConnectHelper::get_mismatched_site_url( 'test' ); + remove_filter( 'home_url', 'trailingslashit' ); + $this->assertFalse( $mismatched_url, 'A trailing slash on the current URL should be ignored.' ); + } + + /** + * @covers FrmStrpLiteConnectHelper::get_mismatched_site_url + */ + public function test_get_mismatched_site_url_with_different_url() { + update_option( 'frm_strp_connect_account_id_test', 'acct_test' ); + update_option( 'frm_strp_connect_site_url_test', 'old-site.example.com' ); + + $this->assertSame( 'old-site.example.com', FrmStrpLiteConnectHelper::get_mismatched_site_url( 'test' ) ); + $this->assertFalse( FrmStrpLiteConnectHelper::get_mismatched_site_url( 'live' ), 'Live mode has no account, so it should not warn.' ); + } + + /** + * @covers FrmStrpLiteConnectHelper::save_missing_connected_site_urls + */ + public function test_save_missing_connected_site_urls() { + update_option( 'frm_strp_connect_account_id_test', 'acct_test' ); + update_option( 'frm_strp_connect_account_id_live', 'acct_live' ); + update_option( 'frm_strp_connect_site_url_live', 'old-site.example.com' ); + + FrmStrpLiteConnectHelper::save_missing_connected_site_urls(); + + $this->assertSame( $this->current_url, get_option( 'frm_strp_connect_site_url_test' ), 'A connection with no saved URL should get the current one.' ); + $this->assertSame( 'old-site.example.com', get_option( 'frm_strp_connect_site_url_live' ), 'A saved URL should not be overwritten.' ); + + delete_option( 'frm_strp_connect_account_id_test' ); + delete_option( 'frm_strp_connect_site_url_test' ); + FrmStrpLiteConnectHelper::save_missing_connected_site_urls(); + $this->assertFalse( get_option( 'frm_strp_connect_site_url_test' ), 'A mode with no account should not get a URL.' ); + } + + /** + * @covers FrmStrpLiteConnectHelper::reset_stripe_connect_integration + */ + public function test_reset_stripe_connect_integration_deletes_site_url() { + update_option( 'frm_strp_connect_account_id_test', 'acct_test' ); + update_option( 'frm_strp_connect_site_url_test', 'old-site.example.com' ); + $_POST = array( 'testMode' => 1 ); + + FrmStrpLiteConnectHelper::reset_stripe_connect_integration(); + + $this->assertFalse( get_option( 'frm_strp_connect_account_id_test' ) ); + $this->assertFalse( get_option( 'frm_strp_connect_site_url_test' ) ); + } + + /** + * @covers FrmStrpLiteAppController::uninstall + */ + public function test_uninstall_deletes_site_url() { + $this->set_user_by_role( 'administrator' ); + update_option( 'frm_strp_connect_site_url_test', 'old-site.example.com' ); + update_option( 'frm_strp_connect_site_url_live', 'old-site.example.com' ); + + FrmStrpLiteAppController::uninstall(); + + $this->assertFalse( get_option( 'frm_strp_connect_site_url_test' ) ); + $this->assertFalse( get_option( 'frm_strp_connect_site_url_live' ) ); + } +} From f47ec2bfa9cdeda1be202a852055f77f990a1177 Mon Sep 17 00:00:00 2001 From: Mike Letellier Date: Wed, 25 Nov 2026 15:58:58 -0400 Subject: [PATCH 3/3] Test that a new Stripe connection saves the site URL Stub the connect server's oauth_account_status response and check that check_server_for_oauth_account_id() saves the current site URL along with the account ID. Co-Authored-By: Claude Opus 5.5 --- .../stripe/test_FrmStrpLiteConnectHelper.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php b/tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php index f2566f93d1..52fa58dfbf 100644 --- a/tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php +++ b/tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php @@ -104,6 +104,61 @@ public function test_save_missing_connected_site_urls() { $this->assertFalse( get_option( 'frm_strp_connect_site_url_test' ), 'A mode with no account should not get a URL.' ); } + /** + * @covers FrmStrpLiteConnectHelper::check_server_for_oauth_account_id + */ + public function test_check_server_for_oauth_account_id_saves_site_url() { + $_GET = array( 'mode' => 'test' ); + add_filter( 'pre_http_request', array( $this, 'mock_oauth_account_status' ), 10, 3 ); + + $connected = $this->run_private_method( array( 'FrmStrpLiteConnectHelper', 'check_server_for_oauth_account_id' ) ); + + remove_filter( 'pre_http_request', array( $this, 'mock_oauth_account_status' ), 10 ); + $_GET = array(); + + $this->assertTrue( $connected ); + $this->assertSame( 'acct_test', get_option( 'frm_strp_connect_account_id_test' ) ); + $this->assertSame( $this->current_url, get_option( 'frm_strp_connect_site_url_test' ), 'A new connection should save the current site URL.' ); + } + + /** + * Answer the connect server's oauth_account_status request with a connected account. + * + * @param array|false $response The response supplied by another filter. + * @param array $args The request options. + * @param string $url The request URL. + * + * @return array|false + */ + public function mock_oauth_account_status( $response, $args, $url ) { + if ( ! is_array( $args['body'] ) || ! isset( $args['body']['frm_strp_connect_action'] ) || 'oauth_account_status' !== $args['body']['frm_strp_connect_action'] ) { + return $response; + } + + $requests_response = class_exists( 'WpOrg\Requests\Response' ) ? new WpOrg\Requests\Response() : new Requests_Response(); + $requests_response->body = wp_json_encode( + array( + 'success' => true, + 'data' => array( 'account_id' => 'acct_test' ), + ) + ); + $requests_response->status_code = 200; + $requests_response->success = true; + $requests_response->url = $url; + + return array( + 'headers' => array(), + 'body' => $requests_response->body, + 'response' => array( + 'code' => 200, + 'message' => 'OK', + ), + 'cookies' => array(), + 'filename' => null, + 'http_response' => new WP_HTTP_Requests_Response( $requests_response ), + ); + } + /** * @covers FrmStrpLiteConnectHelper::reset_stripe_connect_integration */