Repository navigation
Warn when the Stripe connection belongs to a different site URL #3737
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
4cd13f0
6a37eea
f47ec2b
a3366e5
e28b50c
96aaecb
b5002eb
f42ee84
101a0a4
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 |
|---|---|---|
|
|
@@ -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 ); | ||
|
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: this line is the only place a new connection gets its saved URL, and no test covers it. Deleting it leaves all 7 new tests green, so a regression here would silently turn off the warning for every new connection. |
||
|
|
||
| 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'; | ||
|
|
||
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.
Non-blocking: this adds no PHPUnit coverage. The logic is plain option handling, so it is cheap to test.
tests/phpunit/stripe/has no test forFrmStrpLiteConnectHelperyet. Worth covering inget_mismatched_site_url():false;falseand saves the current URL (the backfill);false, including when one side has a trailing slash;reset_stripe_connect_integration()deletes thesite_urloption;FrmStrpLiteAppController::uninstall()deletesfrm_strp_connect_site_url_testand_live.