From d71f76150c332e1c849beef14b711dee8f2691ab Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Thu, 15 Aug 2024 18:39:20 -0300 Subject: [PATCH 1/2] feat: automatically disable guest authors --- .../class-guest-contributor-role.php | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/includes/plugins/co-authors-plus/class-guest-contributor-role.php b/includes/plugins/co-authors-plus/class-guest-contributor-role.php index dc3e0f2e0f..5125318622 100644 --- a/includes/plugins/co-authors-plus/class-guest-contributor-role.php +++ b/includes/plugins/co-authors-plus/class-guest-contributor-role.php @@ -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 { /** @@ -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 ); @@ -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. + * + * @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. * @@ -434,4 +474,4 @@ public static function cme_capabilities_add_user_multi_roles( $value ) { return $value; } } -Guest_Contributor_Role::init(); +Guest_Contributor_Role::initialize(); From 73a2a2948179a1544ca7a071935c298ba11997bc Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Fri, 20 Sep 2024 15:10:28 -0300 Subject: [PATCH 2/2] feat: check again the presence of gas on cron --- .../class-guest-contributor-role.php | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/includes/plugins/co-authors-plus/class-guest-contributor-role.php b/includes/plugins/co-authors-plus/class-guest-contributor-role.php index a933616c65..3506dc1376 100644 --- a/includes/plugins/co-authors-plus/class-guest-contributor-role.php +++ b/includes/plugins/co-authors-plus/class-guest-contributor-role.php @@ -40,6 +40,11 @@ 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. */ @@ -78,6 +83,13 @@ public static function initialize() { // 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. + $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' ] ); } /** @@ -95,14 +107,24 @@ public static function early_init() { } } + /** + * 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() { - $option_name = 'newspack_check_site_has_cap_guest_authors'; - $response = get_option( $option_name ); + $response = get_option( self::SITE_HAS_GUEST_AUTHORS_OPTION_NAME ); // Only check in the database once. if ( false === $response ) { @@ -115,7 +137,7 @@ private static function site_has_cap_guest_authors() { ] ); $response = $query->have_posts() ? 'yes' : 'no'; - add_option( $option_name, $response, '', true ); + add_option( self::SITE_HAS_GUEST_AUTHORS_OPTION_NAME, $response, '', true ); } return 'yes' === $response;