Skip to content

Commit

Permalink
Use wp:action-publish to determine whether to display publish UI (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbachhuber authored May 14, 2018
1 parent 4bcbe3c commit ed5a779
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 120 deletions.
18 changes: 6 additions & 12 deletions editor/components/post-publish-button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { noop, get } from 'lodash';
/**
* WordPress dependencies
*/
import { Button, withAPIData } from '@wordpress/components';
import { Button } from '@wordpress/components';
import { compose } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';

Expand All @@ -25,15 +25,14 @@ export function PostPublishButton( {
visibility,
isPublishable,
isSaveable,
user,
hasPublishAction,
onSubmit = noop,
forceIsSaving,
} ) {
const isButtonEnabled = user.data && ! isSaving && isPublishable && isSaveable;
const isContributor = ! get( user.data, [ 'post_type_capabilities', 'publish_posts' ], false );
const isButtonEnabled = ! isSaving && isPublishable && isSaveable;

let publishStatus;
if ( isContributor ) {
if ( ! hasPublishAction ) {
publishStatus = 'pending';
} else if ( isBeingScheduled ) {
publishStatus = 'future';
Expand Down Expand Up @@ -74,6 +73,7 @@ export default compose( [
getEditedPostVisibility,
isEditedPostSaveable,
isEditedPostPublishable,
getCurrentPost,
getCurrentPostType,
} = select( 'core/editor' );
return {
Expand All @@ -82,6 +82,7 @@ export default compose( [
visibility: getEditedPostVisibility(),
isSaveable: isEditedPostSaveable(),
isPublishable: forceIsDirty || isEditedPostPublishable(),
hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ),
postType: getCurrentPostType(),
};
} ),
Expand All @@ -92,11 +93,4 @@ export default compose( [
onSave: savePost,
};
} ),
withAPIData( ( props ) => {
const { postType } = props;

return {
user: `/wp/v2/users/me?post_type=${ postType }&context=edit`,
};
} ),
] )( PostPublishButton );
17 changes: 4 additions & 13 deletions editor/components/post-publish-button/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { get } from 'lodash';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { withAPIData } from '@wordpress/components';
import { compose } from '@wordpress/element';
import { withSelect } from '@wordpress/data';

Expand All @@ -21,11 +20,8 @@ export function PublishButtonLabel( {
isBeingScheduled,
isSaving,
isPublishing,
user,
hasPublishAction,
} ) {
const userCanPublishPosts = get( user.data, [ 'post_type_capabilities', 'publish_posts' ], false );
const isContributor = user.data && ! userCanPublishPosts;

if ( isPublishing ) {
return __( 'Publishing…' );
} else if ( isPublished && isSaving ) {
Expand All @@ -34,7 +30,7 @@ export function PublishButtonLabel( {
return __( 'Scheduling…' );
}

if ( isContributor ) {
if ( ! hasPublishAction ) {
return __( 'Submit for Review' );
} else if ( isPublished ) {
return __( 'Update' );
Expand All @@ -52,21 +48,16 @@ export default compose( [
isEditedPostBeingScheduled,
isSavingPost,
isPublishingPost,
getCurrentPost,
getCurrentPostType,
} = select( 'core/editor' );
return {
isPublished: isCurrentPostPublished(),
isBeingScheduled: isEditedPostBeingScheduled(),
isSaving: forceIsSaving || isSavingPost(),
isPublishing: isPublishingPost(),
hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ),
postType: getCurrentPostType(),
};
} ),
withAPIData( ( props ) => {
const { postType } = props;

return {
user: `/wp/v2/users/me?post_type=${ postType }&context=edit`,
};
} ),
] )( PublishButtonLabel );
46 changes: 10 additions & 36 deletions editor/components/post-publish-button/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,41 @@
* External dependencies
*/
import { shallow } from 'enzyme';
import { merge } from 'lodash';

/**
* Internal dependencies
*/
import { PostPublishButton } from '../';

describe( 'PostPublishButton', () => {
const user = {
data: {
id: 1,
post_type_capabilities: {
publish_posts: true,
},
},
};

const contributor = merge( {}, user, {
data: {
post_type_capabilities: {
publish_posts: false,
},
},
} );

describe( 'disabled', () => {
it( 'should be disabled if current user is unknown', () => {
const wrapper = shallow(
<PostPublishButton user={ {} } />
);

expect( wrapper.prop( 'disabled' ) ).toBe( true );
} );

it( 'should be disabled if post is currently saving', () => {
const wrapper = shallow(
<PostPublishButton user={ user } isSaving />
<PostPublishButton hasPublishAction={ true } isSaving />
);

expect( wrapper.prop( 'disabled' ) ).toBe( true );
} );

it( 'should be disabled if post is not publishable', () => {
const wrapper = shallow(
<PostPublishButton user={ user } isPublishable={ false } />
<PostPublishButton hasPublishAction={ true } isPublishable={ false } />
);

expect( wrapper.prop( 'disabled' ) ).toBe( true );
} );

it( 'should be disabled if post is not saveable', () => {
const wrapper = shallow(
<PostPublishButton user={ user } isSaveable={ false } />
<PostPublishButton hasPublishAction={ true } isSaveable={ false } />
);

expect( wrapper.prop( 'disabled' ) ).toBe( true );
} );

it( 'should be enabled otherwise', () => {
const wrapper = shallow(
<PostPublishButton user={ user } isPublishable isSaveable />
<PostPublishButton hasPublishAction={ true } isPublishable isSaveable />
);

expect( wrapper.prop( 'disabled' ) ).toBe( false );
Expand All @@ -75,7 +49,7 @@ describe( 'PostPublishButton', () => {
const onSave = jest.fn();
const wrapper = shallow(
<PostPublishButton
user={ contributor }
hasPublishAction={ false }
onStatusChange={ onStatusChange }
onSave={ onSave } />
);
Expand All @@ -90,7 +64,7 @@ describe( 'PostPublishButton', () => {
const onSave = jest.fn();
const wrapper = shallow(
<PostPublishButton
user={ user }
hasPublishAction={ true }
onStatusChange={ onStatusChange }
onSave={ onSave }
isBeingScheduled />
Expand All @@ -106,7 +80,7 @@ describe( 'PostPublishButton', () => {
const onSave = jest.fn();
const wrapper = shallow(
<PostPublishButton
user={ user }
hasPublishAction={ true }
onStatusChange={ onStatusChange }
onSave={ onSave }
visibility="private" />
Expand All @@ -122,7 +96,7 @@ describe( 'PostPublishButton', () => {
const onSave = jest.fn();
const wrapper = shallow(
<PostPublishButton
user={ user }
hasPublishAction={ true }
onStatusChange={ onStatusChange }
onSave={ onSave } />
);
Expand All @@ -139,7 +113,7 @@ describe( 'PostPublishButton', () => {
const onSave = jest.fn();
const wrapper = shallow(
<PostPublishButton
user={ user }
hasPublishAction={ true }
onStatusChange={ onStatusChange }
onSave={ onSave } />
);
Expand All @@ -153,7 +127,7 @@ describe( 'PostPublishButton', () => {

it( 'should have save modifier class', () => {
const wrapper = shallow(
<PostPublishButton user={ user } isSaving />
<PostPublishButton hasPublishAction={ true } isSaving />
);

expect( wrapper.hasClass( 'is-saving' ) ).toBe( true );
Expand Down
43 changes: 8 additions & 35 deletions editor/components/post-publish-button/test/label.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,46 @@
/**
* External dependencies
*/
import { merge } from 'lodash';

/**
* Internal dependencies
*/
import { PublishButtonLabel } from '../label';

describe( 'PublishButtonLabel', () => {
const user = {
data: {
id: 1,
post_type_capabilities: {
publish_posts: true,
},
},
};

const contributor = merge( {}, user, {
data: {
post_type_capabilities: {
publish_posts: false,
},
},
} );

it( 'should show publishing if publishing in progress', () => {
const label = PublishButtonLabel( { user, isPublishing: true } );
const label = PublishButtonLabel( { hasPublishAction: true, isPublishing: true } );
expect( label ).toBe( 'Publishing…' );
} );

it( 'should show updating if published and saving in progress', () => {
const label = PublishButtonLabel( { user, isPublished: true, isSaving: true } );
const label = PublishButtonLabel( { hasPublishAction: true, isPublished: true, isSaving: true } );
expect( label ).toBe( 'Updating…' );
} );

it( 'should show scheduling if scheduled and saving in progress', () => {
const label = PublishButtonLabel( { user, isBeingScheduled: true, isSaving: true } );
const label = PublishButtonLabel( { hasPublishAction: true, isBeingScheduled: true, isSaving: true } );
expect( label ).toBe( 'Scheduling…' );
} );

it( 'should show publish if not published and saving in progress', () => {
const label = PublishButtonLabel( { user, isPublished: false, isSaving: true } );
expect( label ).toBe( 'Publish' );
} );

it( 'should show publish if user unknown', () => {
const label = PublishButtonLabel( { user: {} } );
const label = PublishButtonLabel( { hasPublishAction: true, isPublished: false, isSaving: true } );
expect( label ).toBe( 'Publish' );
} );

it( 'should show submit for review for contributor', () => {
const label = PublishButtonLabel( { user: contributor } );
const label = PublishButtonLabel( { hasPublishAction: false } );
expect( label ).toBe( 'Submit for Review' );
} );

it( 'should show update for already published', () => {
const label = PublishButtonLabel( { user, isPublished: true } );
const label = PublishButtonLabel( { hasPublishAction: true, isPublished: true } );
expect( label ).toBe( 'Update' );
} );

it( 'should show schedule for scheduled', () => {
const label = PublishButtonLabel( { user, isBeingScheduled: true } );
const label = PublishButtonLabel( { hasPublishAction: true, isBeingScheduled: true } );
expect( label ).toBe( 'Schedule' );
} );

it( 'should show publish otherwise', () => {
const label = PublishButtonLabel( { user } );
const label = PublishButtonLabel( { hasPublishAction: true } );
expect( label ).toBe( 'Publish' );
} );
} );
17 changes: 5 additions & 12 deletions editor/components/post-publish-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { get } from 'lodash';
*/
import { __ } from '@wordpress/i18n';
import { compose, Component } from '@wordpress/element';
import { withAPIData, IconButton, Spinner } from '@wordpress/components';
import { IconButton, Spinner } from '@wordpress/components';
import { withSelect } from '@wordpress/data';

/**
Expand Down Expand Up @@ -51,10 +51,8 @@ class PostPublishPanel extends Component {
}

onSubmit() {
const { user, onClose } = this.props;
const userCanPublishPosts = get( user.data, [ 'post_type_capabilities', 'publish_posts' ], false );
const isContributor = user.data && ! userCanPublishPosts;
if ( isContributor ) {
const { onClose, hasPublishAction } = this.props;
if ( ! hasPublishAction ) {
onClose();
return;
}
Expand Down Expand Up @@ -96,6 +94,7 @@ class PostPublishPanel extends Component {
export default compose( [
withSelect( ( select ) => {
const {
getCurrentPost,
getCurrentPostType,
isCurrentPostPublished,
isCurrentPostScheduled,
Expand All @@ -104,17 +103,11 @@ export default compose( [
} = select( 'core/editor' );
return {
postType: getCurrentPostType(),
hasPublishAction: get( getCurrentPost(), [ '_links', 'wp:action-publish' ], false ),
isPublished: isCurrentPostPublished(),
isScheduled: isCurrentPostScheduled(),
isSaving: isSavingPost(),
isDirty: isEditedPostDirty(),
};
} ),
withAPIData( ( props ) => {
const { postType } = props;

return {
user: `/wp/v2/users/me?post_type=${ postType }&context=edit`,
};
} ),
] )( PostPublishPanel );
Loading

0 comments on commit ed5a779

Please sign in to comment.