Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a UI to add the non editing contrib role #3255

Closed
wants to merge 1 commit into from
Closed
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
58 changes: 58 additions & 0 deletions includes/plugins/class-co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static function init() {
add_filter( 'coauthors_edit_author_cap', [ __CLASS__, 'coauthors_edit_author_cap' ] );
add_action( 'admin_init', [ __CLASS__, 'setup_custom_role_and_capability' ] );
add_action( 'template_redirect', [ __CLASS__, 'prevent_myaccount_update' ] );
add_action( 'edit_user_profile', [ __CLASS__, 'edit_user_profile' ] );
add_action( 'wp_update_user', [ __CLASS__, 'edit_user_profile_update' ] );
}

/**
Expand Down Expand Up @@ -103,5 +105,61 @@ public static function setup_custom_role_and_capability() {

\update_option( self::SETTINGS_VERSION_OPTION_NAME, $current_settings_version );
}

/**
* Save custom fields.
*
* @param int $user_id User ID.
*/
public static function edit_user_profile_update( $user_id ) {
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'update-user_' . $user_id ) ) {
return;
}

if ( ! current_user_can( 'edit_user', $user_id ) ) {
return false;
}

$user = get_userdata( $user_id );
if ( ! empty( $_POST['newspack_cap_custom_cap_option'] ) ) {
$user->add_role( self::CONTRIBUTOR_NO_EDIT_ROLE_NAME );
} else {
$user->remove_role( self::CONTRIBUTOR_NO_EDIT_ROLE_NAME );
}
}

/**
* Add user profile fields.
*
* @param WP_User $user The current WP_User object.
*/
public static function edit_user_profile( $user ) {
$current_status = user_can( $user->ID, self::CONTRIBUTOR_NO_EDIT_ROLE_NAME );
?>
<div class="newspack-plugin-cap-options">

<h2><?php echo esc_html__( 'Co-Authors Plus Options', 'newspack-plugin' ); ?></h2>

<table class="form-table" role="presentation">
<tr class="user-newspack_cap_custom_cap_option-wrap">
<th scope="row">
<?php esc_html_e( 'Enable as coauthor', 'newspack-plugin' ); ?>
</th>
<td>
<label for="newspack_cap_custom_cap_option">
<input type="checkbox" name="newspack_cap_custom_cap_option" id="newspack_cap_custom_cap_option" value="1" <?php checked( $current_status ); ?> />
<?php esc_html_e( 'Allow this user to be assigned as a co-author of a post.', 'newspack-plugin' ); ?>
</label>
<p class="description">
<?php
esc_html_e( 'If this option is checked, this user will be able to be assigned as a co-author of a post even if they are not allowed to edit posts. For users with edit access, this option has no effect.', 'newspack-plugin' );
?>
</p>
</td>
</tr>
</table>
</div>
<?php
}
}
Co_Authors_Plus::init();