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

Merged
merged 4 commits into from
Nov 5, 2024
Merged
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 @@ -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.
*/
Expand Down Expand Up @@ -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.
leogermani marked this conversation as resolved.
Show resolved Hide resolved
$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' ] );
}

/**
Expand All @@ -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.
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 );
$response = get_option( self::SITE_HAS_GUEST_AUTHORS_OPTION_NAME );

// Only check in the database once.
if ( false === $response ) {
Expand All @@ -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;
Expand Down