File size: 2,148 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import page from '@automattic/calypso-router';
import { canCurrentUser } from 'calypso/state/selectors/can-current-user';
import canCurrentUserStartSiteOwnerTransfer from 'calypso/state/selectors/can-current-user-start-site-owner-transfer';
import isSiteAutomatedTransfer from 'calypso/state/selectors/is-site-automated-transfer';
import isSiteWpcomStaging from 'calypso/state/selectors/is-site-wpcom-staging';
import isVipSite from 'calypso/state/selectors/is-vip-site';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { getSelectedSiteId, getSelectedSiteSlug } from 'calypso/state/ui/selectors';
import type { Context as PageJSContext } from '@automattic/calypso-router';
import type { AppState } from 'calypso/types';

function canDeleteSite( state: AppState, siteId: number | null ) {
	const canManageOptions = canCurrentUser( state, siteId, 'manage_options' );

	if ( ! siteId || ! canManageOptions ) {
		// Current user doesn't have manage options to delete the site
		return false;
	}

	if ( isSiteWpcomStaging( state, siteId ) ) {
		return false;
	}

	if ( isJetpackSite( state, siteId ) && ! isSiteAutomatedTransfer( state, siteId ) ) {
		// Current user can't delete a Jetpack site, but can request to delete an Atomic site
		return false;
	}

	if ( isVipSite( state, siteId ) ) {
		// Current user can't delete a VIP site
		return false;
	}

	return true;
}

export function redirectIfCantDeleteSite( context: PageJSContext, next: () => void ) {
	const state = context.store.getState();
	if ( ! canDeleteSite( state, getSelectedSiteId( state ) ) ) {
		return redirectToAdministration( context, getSelectedSiteSlug( state ) );
	}

	next();
}

export function redirectIfCantStartSiteOwnerTransfer( context: PageJSContext, next: () => void ) {
	const state = context.store.getState();
	if ( ! canCurrentUserStartSiteOwnerTransfer( state, getSelectedSiteId( state ) ) ) {
		return redirectToAdministration( context, getSelectedSiteSlug( state ) );
	}
	next();
}

async function redirectToAdministration( context: PageJSContext, siteSlug: string | null ) {
	return page.redirect( '/sites/settings/site/' + siteSlug );
}