|
|
import { isEnabled } from '@automattic/calypso-config'; |
|
|
import { makeLayout, render } from 'calypso/controller'; |
|
|
import { addQueryArgs, getSiteFragment } from 'calypso/lib/route'; |
|
|
import { requestAdminMenu } from 'calypso/state/admin-menu/actions'; |
|
|
import { getAdminMenu, getIsRequestingAdminMenu } from 'calypso/state/admin-menu/selectors'; |
|
|
import { isUserLoggedIn } from 'calypso/state/current-user/selectors'; |
|
|
import { stopEditingPost } from 'calypso/state/editor/actions'; |
|
|
import { requestSelectedEditor } from 'calypso/state/selected-editor/actions'; |
|
|
import getEditorUrl from 'calypso/state/selectors/get-editor-url'; |
|
|
import { getSelectedEditor } from 'calypso/state/selectors/get-selected-editor'; |
|
|
import getSiteEditorUrl from 'calypso/state/selectors/get-site-editor-url'; |
|
|
import isAtomicSite from 'calypso/state/selectors/is-site-automated-transfer'; |
|
|
import shouldLoadGutenframe from 'calypso/state/selectors/should-load-gutenframe'; |
|
|
import { requestSite } from 'calypso/state/sites/actions'; |
|
|
import { |
|
|
getSiteOption, |
|
|
isJetpackSite, |
|
|
isSSOEnabled, |
|
|
getSiteAdminUrl, |
|
|
} from 'calypso/state/sites/selectors'; |
|
|
import { getSelectedSiteId } from 'calypso/state/ui/selectors'; |
|
|
import { Placeholder } from './placeholder'; |
|
|
|
|
|
const noop = () => {}; |
|
|
|
|
|
function determinePostType( context ) { |
|
|
if ( context.path.startsWith( '/post/' ) ) { |
|
|
return 'post'; |
|
|
} |
|
|
if ( context.path.startsWith( '/page/' ) ) { |
|
|
return 'page'; |
|
|
} |
|
|
|
|
|
return context.params.customPostType; |
|
|
} |
|
|
|
|
|
function getPostID( context ) { |
|
|
if ( ! context.params.post || 'new' === context.params.post ) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
if ( 'home' === context.params.post ) { |
|
|
const state = context.store.getState(); |
|
|
const siteId = getSelectedSiteId( state ); |
|
|
|
|
|
return parseInt( getSiteOption( state, siteId, 'page_on_front' ), 10 ); |
|
|
} |
|
|
|
|
|
|
|
|
return parseInt( context.params.post, 10 ); |
|
|
} |
|
|
|
|
|
function waitForSiteIdAndSelectedEditor( context ) { |
|
|
return new Promise( ( resolve ) => { |
|
|
const unsubscribe = context.store.subscribe( () => { |
|
|
const state = context.store.getState(); |
|
|
const siteId = getSelectedSiteId( state ); |
|
|
if ( ! siteId ) { |
|
|
return; |
|
|
} |
|
|
const selectedEditor = getSelectedEditor( state, siteId ); |
|
|
if ( ! selectedEditor ) { |
|
|
return; |
|
|
} |
|
|
unsubscribe(); |
|
|
resolve(); |
|
|
} ); |
|
|
|
|
|
context.store.dispatch( |
|
|
requestSelectedEditor( getSelectedSiteId( context.store.getState() ) ) |
|
|
); |
|
|
} ); |
|
|
} |
|
|
|
|
|
function isPreferredEditorViewAvailable( state ) { |
|
|
const siteId = getSelectedSiteId( state ); |
|
|
if ( ! siteId || getIsRequestingAdminMenu( state ) ) { |
|
|
return false; |
|
|
} |
|
|
return null !== getAdminMenu( state, siteId ); |
|
|
} |
|
|
|
|
|
function waitForPreferredEditorView( context ) { |
|
|
return new Promise( ( resolve ) => { |
|
|
const unsubscribe = context.store.subscribe( () => { |
|
|
const state = context.store.getState(); |
|
|
if ( ! isPreferredEditorViewAvailable( state ) ) { |
|
|
return; |
|
|
} |
|
|
unsubscribe(); |
|
|
resolve(); |
|
|
} ); |
|
|
|
|
|
context.store.dispatch( requestAdminMenu( getSelectedSiteId( context.store.getState() ) ) ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const authenticate = ( context, next ) => { |
|
|
const state = context.store.getState(); |
|
|
|
|
|
const siteId = getSelectedSiteId( state ); |
|
|
const isJetpack = isJetpackSite( state, siteId ); |
|
|
const isDesktop = isEnabled( 'desktop' ); |
|
|
const storageKey = `gutenframe_${ siteId }_is_authenticated`; |
|
|
|
|
|
let isAuthenticated = |
|
|
globalThis.sessionStorage.getItem( storageKey ) || |
|
|
! isJetpack || |
|
|
( isJetpack && isSSOEnabled( state, siteId ) ) || |
|
|
isDesktop || |
|
|
context.query.authWpAdmin; |
|
|
|
|
|
if ( isDesktop && isJetpack && ! isSSOEnabled( state, siteId ) ) { |
|
|
isAuthenticated = false; |
|
|
} |
|
|
|
|
|
if ( isAuthenticated ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.store.dispatch( requestSite( siteId ) ); |
|
|
|
|
|
globalThis.sessionStorage.setItem( storageKey, 'true' ); |
|
|
return next(); |
|
|
} |
|
|
|
|
|
|
|
|
context.primary = <Placeholder />; |
|
|
makeLayout( context, noop ); |
|
|
render( context ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const origin = window.location.origin; |
|
|
let returnUrl = addQueryArgs( |
|
|
{ ...context.query, authWpAdmin: true }, |
|
|
`${ origin }${ context.path }` |
|
|
); |
|
|
|
|
|
const siteAdminUrl = getSiteAdminUrl( state, siteId ); |
|
|
|
|
|
|
|
|
|
|
|
if ( isJetpack && ! isSSOEnabled( state, siteId ) ) { |
|
|
const postType = determinePostType( context ); |
|
|
const postId = getPostID( context ); |
|
|
|
|
|
if ( postType ) { |
|
|
returnUrl = `${ siteAdminUrl }post-new.php?post_type=${ postType }`; |
|
|
|
|
|
if ( postId ) { |
|
|
returnUrl = `${ siteAdminUrl }post.php?post=${ postId }&action=edit`; |
|
|
} |
|
|
} else { |
|
|
returnUrl = `${ siteAdminUrl }site-editor.php`; |
|
|
} |
|
|
|
|
|
|
|
|
returnUrl = addQueryArgs( context.query, returnUrl ); |
|
|
} |
|
|
|
|
|
const wpAdminLoginUrl = addQueryArgs( |
|
|
{ redirect_to: returnUrl }, |
|
|
`${ siteAdminUrl }../wp-login.php` |
|
|
); |
|
|
|
|
|
window.location.replace( wpAdminLoginUrl ); |
|
|
}; |
|
|
|
|
|
export const redirect = async ( context, next ) => { |
|
|
const { |
|
|
store: { getState }, |
|
|
} = context; |
|
|
const tmpState = getState(); |
|
|
const selectedEditor = getSelectedEditor( tmpState, getSelectedSiteId( tmpState ) ); |
|
|
const checkPromises = []; |
|
|
if ( ! selectedEditor ) { |
|
|
checkPromises.push( waitForSiteIdAndSelectedEditor( context ) ); |
|
|
} |
|
|
if ( ! isPreferredEditorViewAvailable( tmpState ) ) { |
|
|
checkPromises.push( waitForPreferredEditorView( context ) ); |
|
|
} |
|
|
await Promise.all( checkPromises ); |
|
|
|
|
|
const state = getState(); |
|
|
const siteId = getSelectedSiteId( state ); |
|
|
const isPostShare = context.query.is_post_share; |
|
|
|
|
|
|
|
|
if ( isPostShare && isPostShare === 'true' && ! isAtomicSite( state, siteId ) ) { |
|
|
return next(); |
|
|
} |
|
|
|
|
|
const postType = determinePostType( context ); |
|
|
if ( ! shouldLoadGutenframe( state, siteId, postType ) ) { |
|
|
const postId = getPostID( context ); |
|
|
|
|
|
const url = postType |
|
|
? getEditorUrl( state, siteId, postId, postType ) |
|
|
: getSiteEditorUrl( state, siteId ); |
|
|
|
|
|
return window.location.replace( addQueryArgs( context.query, url ) ); |
|
|
} |
|
|
return next(); |
|
|
}; |
|
|
|
|
|
export const exitPost = ( context, next ) => { |
|
|
const postId = getPostID( context ); |
|
|
const siteId = getSelectedSiteId( context.store.getState() ); |
|
|
if ( siteId ) { |
|
|
context.store.dispatch( stopEditingPost( siteId, postId ) ); |
|
|
} |
|
|
next(); |
|
|
}; |
|
|
|
|
|
export const redirectPostEditor = async ( context ) => { |
|
|
const state = context.store.getState(); |
|
|
const siteId = getSelectedSiteId( state ); |
|
|
const siteAdminUrl = getSiteAdminUrl( state, siteId ); |
|
|
|
|
|
return window.location.replace( addQueryArgs( context.query, `${ siteAdminUrl }post-new.php` ) ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const redirectSiteEditor = async ( context ) => { |
|
|
const state = context.store.getState(); |
|
|
const siteId = getSelectedSiteId( state ); |
|
|
const siteEditorUrl = getSiteEditorUrl( state, siteId ); |
|
|
|
|
|
return window.location.replace( addQueryArgs( context.query, siteEditorUrl ) ); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function redirectToPermalinkIfLoggedOut( context, next ) { |
|
|
if ( isUserLoggedIn( context.store.getState() ) ) { |
|
|
return next(); |
|
|
} |
|
|
const siteFragment = context.params.site || getSiteFragment( context.path ); |
|
|
if ( ! siteFragment || ! context.path ) { |
|
|
return next(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const postId = parseInt( context.params.post, 10 ); |
|
|
const linksToSingleView = postId > 0; |
|
|
if ( linksToSingleView ) { |
|
|
|
|
|
window.location = `https://public-api.wordpress.com/wpcom/v2/sites/${ siteFragment }/editor/redirect?path=${ context.path }`; |
|
|
return; |
|
|
} |
|
|
|
|
|
return next(); |
|
|
} |
|
|
|