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

Updating and reorganizing CAP tweaks #3238

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
75 changes: 75 additions & 0 deletions assets/other-scripts/co-authors-plus/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* globals guestAuthorRole, jQuery */

jQuery( document ).ready( function ( $ ) {
$( 'select#role' ).change( function () {
if ( guestAuthorRole.role !== $( this ).val() ) {
deactivateGuestAuthor();
} else {
activateGuestAuthor();
}
} );

const loginLabel = $( 'label[for="user_login"]' )[ 0 ].innerHTML;

function activateGuestAuthor() {
// Make email not required.
$( 'label[for="email"] > span.description' ).hide();
$( '#createuser input[name=email]' ).closest( 'tr' ).removeClass( 'form-required' );

// User login.
if ( 'new' === guestAuthorRole.screen ) {
$( 'label[for="user_login"]' )[ 0 ].innerHTML = guestAuthorRole.displayNameLabel;
} else {
$( 'label[for="user_login"]' ).parents( 'tr' ).hide();
}

// Password.
$( 'label[for="pass1"]' ).parents( 'tr' ).hide();

// Email notification.
$( 'input#send_user_notification' ).parents( 'tr' ).hide();

// User profile fields.
$( 'label[for="rich_editing"]' ).parents( 'tr' ).hide();
$( 'label[for="comment_shortcuts"]' ).parents( 'tr' ).hide();
$( 'label[for="admin_bar_front"]' ).parents( 'tr' ).hide();
$( 'label[for="locale"]' ).parents( 'tr' ).hide();
$( 'tr.user-admin-color-wrap' ).hide();
}

function deactivateGuestAuthor() {
// Make email required.
$( 'label[for="email"] > span.description' ).show();
$( '#createuser input[name=email]' ).closest( 'tr' ).addClass( 'form-required' );

// User login.
if ( 'new' === guestAuthorRole.screen ) {
$( 'label[for="user_login"]' )[ 0 ].innerHTML = loginLabel;
} else {
$( 'label[for="user_login"]' ).parents( 'tr' ).show();
}

// Password.
$( 'label[for="pass1"]' ).parents( 'tr' ).show();

// Email notification.
$( 'input#send_user_notification' ).parents( 'tr' ).show();

// User profile fields.
$( 'label[for="rich_editing"]' ).parents( 'tr' ).show();
$( 'label[for="comment_shortcuts"]' ).parents( 'tr' ).show();
$( 'label[for="admin_bar_front"]' ).parents( 'tr' ).show();
$( 'label[for="locale"]' ).parents( 'tr' ).show();
$( 'tr.user-admin-color-wrap' ).show();
}

// Check for the role query argument for a quick link to add guest authors.
const urlParams = new URLSearchParams( window.location.search );
const role = urlParams.get( 'role' );
if ( role === guestAuthorRole.role ) {
$( 'select#role' ).val( role );
}

// Trigger change event on page load.
$( 'select#role' ).change();
} );
3 changes: 2 additions & 1 deletion includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-onesignal.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-organic-profile-block.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-perfmatters.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-co-authors-plus.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/co-authors-plus/class-guest-author-role.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/co-authors-plus/class-coauthor-user-cap.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/wc-memberships/class-memberships.php';

include_once NEWSPACK_ABSPATH . 'includes/class-patches.php';
Expand Down
107 changes: 0 additions & 107 deletions includes/plugins/class-co-authors-plus.php

This file was deleted.

92 changes: 92 additions & 0 deletions includes/plugins/co-authors-plus/class-coauthor-user-cap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Co-Authors Plus integration class.
*
* @package Newspack
*/

namespace Newspack;

defined( 'ABSPATH' ) || exit;

/**
* This class creates a new capability and adds it to all roles that have the edit_posts capability
*
* Then it filters the capability used by Co-Authors Plus to determine which users can be assigned as co-authors.
*/
class Coauthor_User_Cap {

/**
* Custom capability name.
*/
const ASSIGNABLE_TO_POSTS_CAPABILITY_NAME = 'edit_cap_posts';

/**
* Option name to mark the version of the settings. If the implementation details
* change, the expected option value should be updated to trigger a reset of the settings.
*/
const SETTINGS_VERSION_OPTION_NAME = 'newsroomnz_coauthors_plus_tweaks_settings_version';
leogermani marked this conversation as resolved.
Show resolved Hide resolved

/**
* Initializes the class.
*
* @return void
*/
public static function init() {
add_filter( 'coauthors_edit_author_cap', [ __CLASS__, 'coauthors_edit_author_cap' ] );
add_action( 'admin_init', [ __CLASS__, 'setup_custom_capability' ] );
add_action( 'newspack_before_delete_account', [ __CLASS__, 'before_delete_account' ] );
}

/**
* Override the capability required to assign a user as a co-author.
*
* @param string $edit_cap Capability required for a user to be assigned as a co-author.
*/
public static function coauthors_edit_author_cap( $edit_cap ) {
return self::ASSIGNABLE_TO_POSTS_CAPABILITY_NAME;
}

/**
* Add custom capability to the roles that should be allowed to author posts.
*/
public static function setup_custom_capability() {
$current_settings_version = '1';
if ( \get_option( self::SETTINGS_VERSION_OPTION_NAME ) === $current_settings_version ) {
return;
}

$wp_roles = wp_roles();
foreach ( $wp_roles->roles as $role_name => $role ) {
$role = $wp_roles->get_role( $role_name );
if ( $role->has_cap( 'edit_posts' ) || $role_name === Guest_Author_Role::CONTRIBUTOR_NO_EDIT_ROLE_NAME ) {
$role->add_cap( self::ASSIGNABLE_TO_POSTS_CAPABILITY_NAME );
}
}

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

/**
* Prevents the Delete Account email to be sent and display an error message to the user
*
* @param int $user_id The user ID trying to delete the account.
* @return void
*/
public static function before_delete_account( $user_id ) {
if ( user_can( $user_id, self::ASSIGNABLE_TO_POSTS_CAPABILITY_NAME ) ) {
\wp_safe_redirect(
\add_query_arg(
[
'message' => __( 'It looks like you are an author on this site. Please contact a site adminstrator to get your account deactivated.', 'newspack-plugin' ),
'is_error' => true,
],
\remove_query_arg( WooCommerce_My_Account::DELETE_ACCOUNT_URL_PARAM )
)
);
exit;
}
}
}

Coauthor_User_Cap::init();
Loading