Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/core-data/src/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,16 @@ export function receiveEditorAssets( assets ) {
assets,
};
}

/**
* Returns an action object used to set whether collaboration is supported.
*
* @param {boolean} supported Whether collaboration is supported.
*
* @return {Object} Action object.
*/
export const setCollaborationSupported =
( supported ) =>
( { dispatch } ) => {
dispatch( { type: 'SET_COLLABORATION_SUPPORTED', supported } );
};
10 changes: 10 additions & 0 deletions packages/core-data/src/private-selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,13 @@ export function getEditorSettings(
export function getEditorAssets( state: State ): Record< string, any > | null {
return state.editorAssets;
}

/**
* Returns whether collaboration is supported.
*
* @param state Data state.
* @return Whether collaboration is supported.
*/
export function isCollaborationSupported( state: State ): boolean {
return state.collaborationSupported;
}
21 changes: 21 additions & 0 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,26 @@ export function syncConnectionStatuses( state = {}, action ) {
return state;
}

/**
* Reducer managing whether collaboration is supported.
*
* Default to true, as collaboration is supported by default
* unless explicitly disabled due to unsupported conditions
* such as metaboxes.
*
* @param {boolean} state Current state.
* @param {Object} action Dispatched action.
*
* @return {boolean} Updated state.
*/
export function collaborationSupported( state = true, action ) {
switch ( action.type ) {
case 'SET_COLLABORATION_SUPPORTED':
return action.supported;
}
return state;
}

export default combineReducers( {
users,
currentTheme,
Expand All @@ -710,4 +730,5 @@ export default combineReducers( {
editorSettings,
editorAssets,
syncConnectionStatuses,
collaborationSupported,
} );
1 change: 1 addition & 0 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface State {
editorSettings: Record< string, any > | null;
editorAssets: Record< string, any > | null;
syncConnectionStatuses?: Record< string, ConnectionStatus >;
collaborationSupported: boolean;
}

type EntityRecordKey = string | number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,48 @@
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../store';
import { unlock } from '../../lock-unlock';

/**
* Initializes WordPress `postboxes` script and the logic for saving meta boxes.
*
* @param { boolean } enabled
*/
export const useMetaBoxInitialization = ( enabled ) => {
const isEnabledAndEditorReady = useSelect(
( select ) =>
enabled && select( editorStore ).__unstableIsEditorReady(),
const { isEnabledAndEditorReady, isCollaborationEnabled } = useSelect(
( select ) => ( {
isEnabledAndEditorReady:
enabled && select( editorStore ).__unstableIsEditorReady(),
isCollaborationEnabled:
select( editorStore ).isCollaborationEnabledForCurrentPost(),
} ),
[ enabled ]
);
const { setCollaborationSupported } = unlock( useDispatch( coreStore ) );

const { initializeMetaBoxes } = useDispatch( editPostStore );
// The effect has to rerun when the editor is ready because initializeMetaBoxes
// will noop until then.
useEffect( () => {
if ( isEnabledAndEditorReady ) {
initializeMetaBoxes();

// Disable real-time collaboration when legacy meta boxes are detected.
if ( isCollaborationEnabled ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this if check necessary? What's the downside of always calling setCollaborationSupported( false )?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I experimented with that on the previous PR. This is a catch-22 situation.

By default, collaborationSupported is set to true. This means that when real-time collaboration is disabled the RTC specific text in the post locked modal won't be disabled. When collaborationSupported is set to false, the RTC specific text in the post locked modal is shown.

So by having the isCollaborationEnabled in place, it ensures that collaborationSupported isn't flipped unless unless real-time collaboration is enabled.

isCollaborationEnabled -> is the setting enabled?
collaborationSupported -> is collaboration supported? Doesn't talk about it being enabled or disabled

I'm open to a better solution on how to improve this. I couldn't really find one that wouldn't trigger a whole circular dependency problem.

setCollaborationSupported( false );
}
}
}, [ isEnabledAndEditorReady, initializeMetaBoxes ] );
}, [
isEnabledAndEditorReady,
initializeMetaBoxes,
isCollaborationEnabled,
setCollaborationSupported,
] );
};
69 changes: 46 additions & 23 deletions packages/editor/src/components/post-locked-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,31 @@ import { useEffect, createInterpolateElement } from '@wordpress/element';
import { addAction, removeAction } from '@wordpress/hooks';
import { useInstanceId } from '@wordpress/compose';
import { store as coreStore } from '@wordpress/core-data';
import { unlock } from '../../lock-unlock';

/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';

function CollaborationContext() {
const isCollaborationSupported = useSelect( ( select ) => {
return unlock( select( coreStore ) ).isCollaborationSupported();
}, [] );

if ( isCollaborationSupported ) {
return null;
}

return (
<p>
{ __(
'Because this post uses plugins that aren’t compatible with real-time collaboration, only one person can edit at a time.'
) }
</p>
);
}

function PostLockedModal() {
const instanceId = useInstanceId( PostLockedModal );
const hookName = 'core/editor/post-locked-modal-' + instanceId;
Expand Down Expand Up @@ -197,29 +216,32 @@ function PostLockedModal() {
) }
<div>
{ !! isTakeover && (
<p>
{ createInterpolateElement(
userDisplayName
? sprintf(
/* translators: %s: user's display name */
__(
'<strong>%s</strong> now has editing control of this post (<PreviewLink />). Don’t worry, your changes up to this moment have been saved.'
),
userDisplayName
)
: __(
'Another user now has editing control of this post (<PreviewLink />). Don’t worry, your changes up to this moment have been saved.'
),
{
strong: <strong />,
PreviewLink: (
<ExternalLink href={ previewLink }>
{ __( 'preview' ) }
</ExternalLink>
),
}
) }
</p>
<>
<p>
{ createInterpolateElement(
userDisplayName
? sprintf(
/* translators: %s: user's display name */
__(
'<strong>%s</strong> now has editing control of this post (<PreviewLink />). Don’t worry, your changes up to this moment have been saved.'
),
userDisplayName
)
: __(
'Another user now has editing control of this post (<PreviewLink />). Don’t worry, your changes up to this moment have been saved.'
),
{
strong: <strong />,
PreviewLink: (
<ExternalLink href={ previewLink }>
{ __( 'preview' ) }
</ExternalLink>
),
}
) }
</p>
<CollaborationContext />
</>
) }
{ ! isTakeover && (
<>
Expand All @@ -246,6 +268,7 @@ function PostLockedModal() {
}
) }
</p>
<CollaborationContext />
<p>
{ __(
'If you take over, the other user will lose editing control to the post, but their changes will be saved.'
Expand Down
5 changes: 5 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,11 @@ export function isPublishSidebarOpened( state ) {
*/
export const isCollaborationEnabledForCurrentPost = createRegistrySelector(
( select ) => ( state ) => {
// Return early, if collaboration is not supported.
if ( ! unlock( select( coreStore ) ).isCollaborationSupported() ) {
return false;
}

const currentPostType = getCurrentPostType( state );
const entityConfig = select( coreStore ).getEntityConfig(
'postType',
Expand Down
106 changes: 106 additions & 0 deletions test/e2e/specs/editor/collaboration/collaboration-metabox-lock.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Internal dependencies
*/
import { test, expect } from './fixtures';
import { SECOND_USER } from './fixtures/collaboration-utils';

const BASE_URL = process.env.WP_BASE_URL || 'http://localhost:8889';

test.describe( 'Collaboration with meta boxes', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activatePlugin( 'gutenberg-test-plugin-meta-box' );
} );

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.deactivatePlugin( 'gutenberg-test-plugin-meta-box' );
} );

test( 'shows post-locked modal when meta boxes disable collaboration', async ( {
collaborationUtils,
requestUtils,
admin,
editor,
page,
} ) => {
// Ensure collaboration is enabled (the fixture enables it, but
// be explicit to match the pattern of other collaboration tests).
await collaborationUtils.setCollaboration( true );

// Create a draft post.
const post = await requestUtils.createPost( {
title: 'Meta Box Lock Test',
status: 'draft',
date_gmt: new Date().toISOString(),
} );

// User 1 (admin) opens the post.
await admin.visitAdminPage(
'post.php',
`post=${ post.id }&action=edit`
);
await editor.setPreferences( 'core/edit-post', {
welcomeGuide: false,
fullscreenMode: false,
} );

// Wait for meta boxes to initialize and disable collaboration.
// collaborationSupported starts as true, then the meta box hook
// sets it to false once meta boxes are detected.
await page.waitForFunction(
() =>
window?.wp?.data
?.select( 'core/editor' )
?.isCollaborationEnabledForCurrentPost?.() === false,
{ timeout: 15000 }
);

// Set up second browser context for User 2.
const secondContext = await admin.browser.newContext( {
baseURL: BASE_URL,
} );
const page2 = await secondContext.newPage();

try {
// Log in the second user.
await page2.goto( '/wp-login.php' );
await page2.locator( '#user_login' ).fill( SECOND_USER.username );
await page2.locator( '#user_pass' ).fill( SECOND_USER.password );
await page2.getByRole( 'button', { name: 'Log In' } ).click();
await page2.waitForURL( '**/wp-admin/**' );

// User 2 navigates to the same post.
await page2.goto(
`/wp-admin/post.php?post=${ post.id }&action=edit`
);

// Wait for wp.data to be available on User 2's page.
await page2.waitForFunction(
() => window?.wp?.data && window?.wp?.blocks,
{ timeout: 15000 }
);

// Assert the post-locked modal appears.
// Because collaboration is disabled (meta boxes), WordPress
// falls back to standard post-locking. User 2 sees the
// "This post is already being edited" modal.
const modal = page2.getByRole( 'dialog', {
name: 'This post is already being edited',
} );
await expect( modal ).toBeVisible( { timeout: 15000 } );

// Assert the explanation about meta box incompatibility.
await expect(
modal.getByText(
'Because this post uses plugins that aren\u2019t compatible with real-time collaboration, only one person can edit at a time.'
)
).toBeVisible();

// Assert the "Take over" option is available.
await expect(
modal.getByRole( 'link', { name: 'Take over' } )
).toBeVisible();
} finally {
await secondContext.close();
}
} );
} );
Loading