Onboarding: Add redirect URL option to jetpack connection URL (https://github.com/woocommerce/woocommerce-admin/pull/3052)

* Add redirect_url as rest param to jetpack connection

* Add query param to jetpack connection URL in wc-api

* Add redirect URL to connection and plugin steps
This commit is contained in:
Joshua T Flowers 2019-10-17 06:00:47 +08:00 committed by GitHub
parent 96c633ed32
commit 181c02bbc8
5 changed files with 44 additions and 43 deletions

View File

@ -13,7 +13,7 @@ import { withDispatch } from '@wordpress/data';
* WooCommerce depdencies
*/
import { H, Stepper, Card } from '@woocommerce/components';
import { updateQueryString } from '@woocommerce/navigation';
import { getNewPath, updateQueryString } from '@woocommerce/navigation';
import { getSetting } from '@woocommerce/wc-admin-settings';
/**
@ -169,11 +169,14 @@ export default compose(
const installationErrors = getPluginInstallationErrors( plugins );
const installedPlugins = Object.keys( getPluginInstallations( plugins ) );
const isJetpackConnectUrlRequesting = isGetJetpackConnectUrlRequesting();
const jetpackConnectUrlError = getJetpackConnectUrlError();
const queryArgs = {
redirect_url: getNewPath( { step: 'store-details' } ),
};
const isJetpackConnectUrlRequesting = isGetJetpackConnectUrlRequesting( queryArgs );
const jetpackConnectUrlError = getJetpackConnectUrlError( queryArgs );
let jetpackConnectUrl = null;
if ( activatedPlugins.includes( 'jetpack' ) ) {
jetpackConnectUrl = getJetpackConnectUrl();
jetpackConnectUrl = getJetpackConnectUrl( queryArgs );
}
const errors = [];

View File

@ -61,9 +61,12 @@ export default compose(
getJetpackConnectUrlError,
} = select( 'wc-api' );
const isRequesting = isGetJetpackConnectUrlRequesting();
const error = getJetpackConnectUrlError();
const jetpackConnectUrl = getJetpackConnectUrl();
const queryArgs = {
redirect_url: window.location.href,
};
const isRequesting = isGetJetpackConnectUrlRequesting( queryArgs );
const error = getJetpackConnectUrlError( queryArgs );
const jetpackConnectUrl = getJetpackConnectUrl( queryArgs );
return {
error,

View File

@ -4,12 +4,13 @@
* External dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import { getResourceName } from '../utils';
import { getResourceIdentifier, getResourceName } from '../utils';
import { JETPACK_NAMESPACE, WC_ADMIN_NAMESPACE } from '../constants';
import { pluginNames } from './constants';
@ -195,26 +196,25 @@ function readJetpackStatus( resourceNames, fetch ) {
}
function readJetpackConnectUrl( resourceNames, fetch ) {
const resourceName = 'jetpack-connect-url';
const filteredNames = resourceNames.filter( name => {
return name.startsWith( 'jetpack-connect-url' );
} );
if ( resourceNames.includes( resourceName ) ) {
const url = WC_ADMIN_NAMESPACE + '/onboarding/plugins/connect-jetpack';
return filteredNames.map( async resourceName => {
const query = getResourceIdentifier( resourceName );
const url = addQueryArgs( WC_ADMIN_NAMESPACE + '/onboarding/plugins/connect-jetpack', query );
return [
fetch( {
path: url,
return fetch( {
path: url,
} )
.then( response => {
return { [ resourceName ]: { data: response.connectAction } };
} )
.then( response => {
return { [ resourceName ]: { data: response.connectAction } };
} )
.catch( error => {
error.message = getPluginErrorMessage( 'connect', 'jetpack' );
return { [ resourceName ]: { error } };
} ),
];
}
return [];
.catch( error => {
error.message = getPluginErrorMessage( 'connect', 'jetpack' );
return { [ resourceName ]: { error } };
} );
} );
}
function getPluginErrorMessage( action, plugin ) {

View File

@ -50,17 +50,21 @@ const isGetProfileItemsRequesting = getResource => () => {
};
const getJetpackConnectUrl = ( getResource, requireResource ) => (
query = {},
requirement = DEFAULT_REQUIREMENT
) => {
return requireResource( requirement, 'jetpack-connect-url' ).data;
const resourceName = getResourceName( 'jetpack-connect-url', query );
return requireResource( requirement, resourceName ).data;
};
const getJetpackConnectUrlError = getResource => () => {
return getResource( 'jetpack-connect-url' ).error;
const getJetpackConnectUrlError = getResource => ( query = {} ) => {
const resourceName = getResourceName( 'jetpack-connect-url', query );
return getResource( resourceName ).error;
};
const isGetJetpackConnectUrlRequesting = getResource => () => {
const { lastReceived, lastRequested } = getResource( 'jetpack-connect-url' );
const isGetJetpackConnectUrlRequesting = getResource => ( query = {} ) => {
const resourceName = getResourceName( 'jetpack-connect-url', query );
const { lastReceived, lastRequested } = getResource( resourceName );
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
return true;

View File

@ -273,25 +273,16 @@ class OnboardingPlugins extends \WC_REST_Data_Controller {
/**
* Generates a Jetpack Connect URL.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Connection URL for Jetpack
*/
public function connect_jetpack() {
public function connect_jetpack( $request ) {
if ( ! class_exists( '\Jetpack' ) ) {
return new \WP_Error( 'woocommerce_rest_jetpack_not_active', __( 'Jetpack is not installed or active.', 'woocommerce-admin' ), 404 );
}
$next_step_slug = apply_filters( 'woocommerce_onboarding_after_jetpack_step', 'store-details' );
$redirect_url = esc_url_raw(
add_query_arg(
array(
'page' => 'wc-admin',
'step' => $next_step_slug,
),
admin_url( 'admin.php' )
)
);
$connect_url = \Jetpack::init()->build_connect_url( true, $redirect_url, 'woocommerce-setup-wizard' );
$redirect_url = apply_filters( 'woocommerce_onboarding_jetpack_connect_redirect_url', esc_url_raw( $request['redirect_url'] ) );
$connect_url = \Jetpack::init()->build_connect_url( true, $redirect_url, 'woocommerce-setup-wizard' );
if ( defined( 'WOOCOMMERCE_CALYPSO_ENVIRONMENT' ) && in_array( WOOCOMMERCE_CALYPSO_ENVIRONMENT, array( 'development', 'wpcalypso', 'horizon', 'stage' ) ) ) {
$connect_url = add_query_arg(