diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index 1ce76b393c..4f74ac8bae 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -10,7 +10,7 @@ class FrmAppHelper { * * @var int */ - public static $db_version = 108; + public static $db_version = 109; /** * Used by the API add-on. diff --git a/classes/models/FrmMigrate.php b/classes/models/FrmMigrate.php index a4987bf467..7cb3b32948 100644 --- a/classes/models/FrmMigrate.php +++ b/classes/models/FrmMigrate.php @@ -549,6 +549,7 @@ private function migrate_to_108() { if ( is_string( $css ) && '' !== $css ) { add_option( 'frm_saved_css_hash', hash( 'sha256', $css ), '', false ); } + delete_option( 'frmpro_css' ); } @@ -569,6 +570,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_109() { + FrmStrpLiteConnectHelper::save_missing_connected_site_urls(); + } + /** * Add new wp_options row for custom setting. * diff --git a/stripe/controllers/FrmStrpLiteAppController.php b/stripe/controllers/FrmStrpLiteAppController.php index 3820b5fed3..bfaf67d51a 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 0ca7b155e4..0b512a0501 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 @@ -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 ); } + /** + * 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_strp_connect_option_name( 'site_url', $mode ), self::get_site_url_for_connect_server(), false ); + } + + /** + * 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 + * + * @return void + */ + 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 ); + } + } + } + + /** + * 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 or no URL is saved. A trailing slash (WPML can add one) is ignored. + */ + public static function get_mismatched_site_url( $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 ) ) { + 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_strp_connect_option_name( 'site_url', $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 + + +

diff --git a/tests/phpunit/database/test_FrmMigrate.php b/tests/phpunit/database/test_FrmMigrate.php index c1ac880b65..b76e6abd22 100644 --- a/tests/phpunit/database/test_FrmMigrate.php +++ b/tests/phpunit/database/test_FrmMigrate.php @@ -317,6 +317,22 @@ public function test_migrate_to_108() { $this->assertFalse( get_option( 'frm_saved_css_hash' ) ); } + /** + * @covers FrmMigrate::migrate_to_109 + */ + public function test_migrate_to_109() { + 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_109' ), 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..52fa58dfbf --- /dev/null +++ b/tests/phpunit/stripe/test_FrmStrpLiteConnectHelper.php @@ -0,0 +1,189 @@ +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::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 + */ + 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' ) ); + } +}