Skip to content

Commit

Permalink
Author: Limit author options by user level
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Aug 24, 2017
1 parent aa342a4 commit 228558a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions editor/sidebar/post-author/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { flowRight } from 'lodash';
import { filter, flowRight } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -31,12 +31,25 @@ class PostAuthor extends Component {
onUpdateAuthor( Number( value ) );
}

getAuthors() {
// While User Levels are officially deprecated, the behavior of the
// existing users dropdown on `who=authors` tests `user_level != 0`
//
// See: https://github.com/WordPress/WordPress/blob/a193916/wp-includes/class-wp-user-query.php#L322-L327
// See: https://codex.wordpress.org/Roles_and_Capabilities#User_Levels
const { users } = this.props;
return filter( users.data, ( user ) => {
return user.capabilities.level_1;
} );
}

render() {
const { users, postAuthor, instanceId } = this.props;
if ( ! users.data || users.data.length < 2 ) {
const authors = this.getAuthors();
if ( authors.length < 2 ) {
return null;
}

const { postAuthor, instanceId } = this.props;
const selectId = 'post-author-selector-' + instanceId;

// Disable reason: A select with an onchange throws a warning
Expand All @@ -51,7 +64,7 @@ class PostAuthor extends Component {
onChange={ this.setAuthorId }
className="editor-post-author__select"
>
{ users.data.map( ( author ) => (
{ authors.map( ( author ) => (
<option key={ author.id } value={ author.id }>{ author.name }</option>
) ) }
</select>
Expand Down

0 comments on commit 228558a

Please sign in to comment.