Redirect Woo.com connect request to selected page (#43315)

* Working redirect

* Dynamic connect urls

* Clean up

* Changelog
This commit is contained in:
berislav grgičak 2024-01-10 07:20:15 +01:00 committed by GitHub
parent 2c122ba446
commit c018d7751b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 47 deletions

View File

@ -13,6 +13,7 @@ import './header-account.scss';
import { getAdminSetting } from '../../../utils/admin-settings';
import HeaderAccountModal from './header-account-modal';
import { MARKETPLACE_HOST } from '../constants';
import { connectUrl } from '../../utils/functions';
export default function HeaderAccount(): JSX.Element {
const [ isModalOpen, setIsModalOpen ] = useState( false );
@ -20,7 +21,7 @@ export default function HeaderAccount(): JSX.Element {
const wccomSettings = getAdminSetting( 'wccomHelper', {} );
const isConnected = wccomSettings?.isConnected ?? false;
const connectionURL = wccomSettings?.connectURL ?? '';
const connectionURL = connectUrl();
const userEmail = wccomSettings?.userEmail;
const avatarURL = wccomSettings?.userAvatar ?? commentAuthorAvatar;

View File

@ -24,6 +24,7 @@ import { Subscription } from './types';
import { RefreshButton } from './table/actions/refresh-button';
import Notices from './notices';
import InstallModal from './table/actions/install-modal';
import { connectUrl } from '../../utils/functions';
export default function MySubscriptions(): JSX.Element {
const { subscriptions, isLoading } = useContext( SubscriptionsContext );
@ -70,7 +71,7 @@ export default function MySubscriptions(): JSX.Element {
'woocommerce'
) }
</p>
<Button href={ wccomSettings?.connectURL } variant="primary">
<Button href={ connectUrl() } variant="primary">
{ __( 'Connect Account', 'woocommerce' ) }
</Button>
</div>

View File

@ -7,7 +7,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getAdminSetting } from '../../../../../utils/admin-settings';
import { connectUrl } from '../../../../utils/functions';
interface RenewProps {
variant?: Button.ButtonVariant;
@ -15,9 +15,7 @@ interface RenewProps {
}
export default function ConnectAccountButton( props: RenewProps ) {
const wccomSettings = getAdminSetting( 'wccomHelper', {} );
const url = new URL( wccomSettings?.connectURL ?? '' );
const url = new URL( connectUrl() );
if ( props.install ) {
url.searchParams.set( 'install', props.install );
}

View File

@ -10,7 +10,7 @@ import { Icon } from '@wordpress/components';
/**
* Internal dependencies
*/
import { LOCALE } from '../../utils/admin-settings';
import { LOCALE, getAdminSetting } from '../../utils/admin-settings';
import { CategoryAPIItem } from '../components/category-selector/types';
import {
MARKETPLACE_CART_PATH,
@ -424,6 +424,18 @@ const subscribeUrl = ( subscription: Subscription ): string => {
] );
};
const connectUrl = (): string => {
const wccomSettings = getAdminSetting( 'wccomHelper', {} );
if ( ! wccomSettings.connectURL ) {
return '';
}
return appendURLParams( wccomSettings.connectURL, [
[ 'redirect_admin_url', encodeURIComponent( window.location.href ) ],
] );
};
export {
ProductGroup,
appendURLParams,
@ -441,4 +453,5 @@ export {
renewUrl,
subscribeUrl,
subscriptionToProduct,
connectUrl,
};

View File

@ -0,0 +1,5 @@
Significance: patch
Type: update
Comment: Redirect to correct admin page after connecting to Woo.com.

View File

@ -74,10 +74,6 @@ class WC_Helper_Admin {
$connect_url_args['wc-helper-nonce'] = wp_create_nonce( 'connect' );
}
if ( isset( $current_screen->id ) && 'woocommerce_page_wc-admin' === $current_screen->id ) {
$connect_url_args['redirect-to-wc-admin'] = 1;
}
return add_query_arg(
$connect_url_args,
admin_url( 'admin.php' )

View File

@ -699,40 +699,53 @@ class WC_Helper {
array(
'page' => 'wc-addons',
'section' => 'helper',
),
isset( $_GET['redirect-to-wc-admin'] ),
sanitize_text_field( wp_unslash( $_GET['install'] ) )
)
)
);
// phpcs:enable WordPress.Security.NonceVerification.Recommended
}
/**
* Get helper redirect URL.
*
* @param array $args Query args.
* @param bool $redirect_to_wc_admin Whether to redirect to WC Admin.
* @param string $install_product_key Optional Product key to install.
* @param array $args Query args.
* @return string
*/
private static function get_helper_redirect_url( $args = array(), $redirect_to_wc_admin = false, $install_product_key = '' ) {
private static function get_helper_redirect_url( $args = array() ) {
global $current_screen;
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$redirect_admin_url = isset( $_GET['redirect_admin_url'] )
? esc_url_raw(
urldecode(
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
wp_unslash( $_GET['redirect_admin_url'] )
)
)
: '';
$install_product_key = isset( $_GET['install'] ) ? sanitize_text_field( wp_unslash( $_GET['install'] ) ) : '';
// phpcs:enable WordPress.Security.NonceVerification.Recommended
if (
'woocommerce_page_wc-addons' === $current_screen->id &&
FeaturesUtil::feature_is_enabled( 'marketplace' ) &&
(
true === $redirect_to_wc_admin ||
false === empty( $redirect_admin_url ) ||
false === empty( $install_product_key )
)
) {
$new_url = add_query_arg(
array(
'page' => 'wc-admin',
'tab' => 'my-subscriptions',
'path' => rawurlencode( '/extensions' ),
),
admin_url( 'admin.php' )
);
if ( strpos( $redirect_admin_url, admin_url( 'admin.php' ) ) === 0 ) {
$new_url = $redirect_admin_url;
} else {
$new_url = add_query_arg(
array(
'page' => 'wc-admin',
'tab' => 'my-subscriptions',
'path' => rawurlencode( '/extensions' ),
),
admin_url( 'admin.php' )
);
}
if ( ! empty( $install_product_key ) ) {
$new_url = add_query_arg(
array(
@ -766,10 +779,6 @@ class WC_Helper {
'wc-helper-nonce' => wp_create_nonce( 'connect' ),
);
if ( isset( $_GET['redirect-to-wc-admin'] ) ) {
$redirect_url_args['redirect-to-wc-admin'] = 1;
}
if ( isset( $_GET['install'] ) ) {
$redirect_url_args['install'] = sanitize_text_field( wp_unslash( $_GET['install'] ) );
}
@ -809,9 +818,19 @@ class WC_Helper {
$connect_url = add_query_arg(
array(
'home_url' => rawurlencode( home_url() ),
'redirect_uri' => rawurlencode( $redirect_uri ),
'secret' => rawurlencode( $secret ),
'home_url' => rawurlencode( home_url() ),
'redirect_uri' => rawurlencode( $redirect_uri ),
'secret' => rawurlencode( $secret ),
'redirect_admin_url' => isset( $_GET['redirect_admin_url'] )
? rawurlencode(
esc_url_raw(
urldecode(
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
wp_unslash( $_GET['redirect_admin_url'] )
)
)
)
: '',
),
WC_Helper_API::url( 'oauth/authorize' )
);
@ -841,9 +860,7 @@ class WC_Helper {
array(
'page' => 'wc-addons',
'section' => 'helper',
),
isset( $_GET['redirect-to-wc-admin'] ),
isset( $_GET['install'] ) ? sanitize_text_field( wp_unslash( $_GET['install'] ) ) : ''
)
)
);
die();
@ -905,9 +922,7 @@ class WC_Helper {
'page' => 'wc-addons',
'section' => 'helper',
'wc-helper-status' => 'helper-connected',
),
isset( $_GET['redirect-to-wc-admin'] ),
isset( $_GET['install'] ) ? sanitize_text_field( wp_unslash( $_GET['install'] ) ) : ''
)
)
);
die();
@ -932,9 +947,7 @@ class WC_Helper {
'page' => 'wc-addons',
'section' => 'helper',
'wc-helper-status' => 'helper-disconnected',
),
isset( $_GET['redirect-to-wc-admin'] ),
isset( $_GET['install'] ) ? sanitize_text_field( wp_unslash( $_GET['install'] ) ) : ''
)
);
self::disconnect();
@ -960,9 +973,7 @@ class WC_Helper {
'section' => 'helper',
'filter' => self::get_current_filter(),
'wc-helper-status' => 'helper-refreshed',
),
isset( $_GET['redirect-to-wc-admin'] ),
isset( $_GET['install'] ) ? sanitize_text_field( wp_unslash( $_GET['install'] ) ) : ''
)
);
wp_safe_redirect( $redirect_uri );