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 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
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 @@ -38,19 +40,21 @@ class Guest_Contributor_Role {
*/
const SETTINGS_VERSION_OPTION_NAME = 'newspack_coauthors_plus_settings_version';

/**
* The option where we store if the site has CAP's guest authors.
*/
const SITE_HAS_GUEST_AUTHORS_OPTION_NAME = 'newspack_check_site_has_cap_guest_authors';

/**
* 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 Down Expand Up @@ -79,6 +83,64 @@ public static function init() {

// Hide author email on the frontend, if it's a placeholder email.
\add_filter( 'theme_mod_show_author_email', [ __CLASS__, 'should_display_author_email' ] );

// Make sure we check again if the site has guest authors evey hour.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Suggested change
// Make sure we check again if the site has guest authors evey hour.
// Make sure we check again if the site has guest authors every hour.

$re_check_guest_authors = 'newspack_re_check_guest_authors';
if ( ! \wp_next_scheduled( $re_check_guest_authors ) ) {
\wp_schedule_event( time(), 'hourly', $re_check_guest_authors );
}
add_action( $re_check_guest_authors, [ __CLASS__, 'clear_site_has_cap_guest_authors_check' ] );
}

/**
* 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' ] );
}
}

/**
* Clear the option that stores if the site has CAP's guest authors.
* This will enforce a new check in the next request.
* This will make sure we update the option if all guest authors are deleted.
*/
public static function clear_site_has_cap_guest_authors_check() {
if ( self::site_has_cap_guest_authors() ) {
delete_option( self::SITE_HAS_GUEST_AUTHORS_OPTION_NAME );
}
}

/**
* 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() {
$response = get_option( self::SITE_HAS_GUEST_AUTHORS_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( self::SITE_HAS_GUEST_AUTHORS_OPTION_NAME, $response, '', true );
}

return 'yes' === $response;
}

/**
Expand Down Expand Up @@ -488,4 +550,4 @@ public static function should_display_author_email( $value ) {
return $value;
}
}
Guest_Contributor_Role::init();
Guest_Contributor_Role::initialize();