Skip to content

Commit

Permalink
feat: automatically disable guest authors (#3345)
Browse files Browse the repository at this point in the history
* feat: automatically disable guest authors

* feat: check again the presence of gas on cron

* Update includes/plugins/co-authors-plus/class-guest-contributor-role.php

Co-authored-by: Adam Cassis <adam@adamcassis.com>

---------

Co-authored-by: Adam Cassis <adam@adamcassis.com>
  • Loading branch information
leogermani and adekbadek authored Nov 5, 2024
1 parent 2e05fd4 commit d0db6ba
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions includes/plugins/co-authors-plus/class-guest-contributor-role.php
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 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.
*
* @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();

0 comments on commit d0db6ba

Please sign in to comment.