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: automatically disable guest authors #3345

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*
* This role can also be assigned to users who have other roles, so they can be assigned as co-authors of a post without having the capability to edit posts.
* This is done via a custom UI in the user profile.
*
* CAP's Guest Authors feature will be disabled by default if there are no Guest Authors in the site. If you want to force enabling it, add the NEWSPACK_ENABLE_CAP_GUEST_AUTHORS constant to your wp-config.php file.
*/
class Guest_Contributor_Role {
/**
Expand All @@ -41,16 +43,13 @@ class Guest_Contributor_Role {
/**
* Initialize hooks and filters.
*/
public static function init() {
public static function initialize() {
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( 'newspack_before_delete_account', [ __CLASS__, 'before_delete_account' ] );

if ( defined( 'NEWSPACK_DISABLE_CAP_GUEST_AUTHORS' ) && NEWSPACK_DISABLE_CAP_GUEST_AUTHORS ) {
add_filter( 'coauthors_guest_authors_enabled', '__return_false' );
add_action( 'admin_menu', [ __CLASS__, 'guest_author_menu_replacement' ] );
}
add_action( 'init', [ __CLASS__, 'early_init' ], 5 );

// Do not allow guest authors to login.
\add_filter( 'wp_authenticate_user', [ __CLASS__, 'wp_authenticate_user' ], 10, 2 );
Expand All @@ -75,6 +74,47 @@ public static function init() {
add_action( 'wp_update_user', [ __CLASS__, 'edit_user_profile_update' ] );
}

/**
* Runs early in the init hook to make sure it runs before Co-Authors Plus initialization.
*
* @return void
*/
public static function early_init() {
if ( defined( 'NEWSPACK_ENABLE_CAP_GUEST_AUTHORS' ) && NEWSPACK_ENABLE_CAP_GUEST_AUTHORS ) {
return;
}
if ( ! self::site_has_cap_guest_authors() ) {
add_filter( 'coauthors_guest_authors_enabled', '__return_false' );
add_action( 'admin_menu', [ __CLASS__, 'guest_author_menu_replacement' ] );
}
}

/**
* Checks if the site has any guest authors. Will check it once in the database and store the result in an option.
adekbadek marked this conversation as resolved.
Show resolved Hide resolved
*
* @return bool
*/
private static function site_has_cap_guest_authors() {
$option_name = 'newspack_check_site_has_cap_guest_authors';
$response = get_option( $option_name );

// Only check in the database once.
if ( false === $response ) {
$query = new \WP_Query(
[
'post_type' => 'guest-author',
'posts_per_page' => 1,
'post_status' => 'any',
'fields' => 'ids',
]
);
$response = $query->have_posts() ? 'yes' : 'no';
add_option( $option_name, $response, '', true );
}

return 'yes' === $response;
}

/**
* Override the capability required to assign a user as a co-author.
*
Expand Down Expand Up @@ -434,4 +474,4 @@ public static function cme_capabilities_add_user_multi_roles( $value ) {
return $value;
}
}
Guest_Contributor_Role::init();
Guest_Contributor_Role::initialize();