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: command to initialize cron job to slowly backfill CAP term data #3425

Merged
merged 8 commits into from
Oct 16, 2024
42 changes: 42 additions & 0 deletions includes/cli/class-co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* Co-Authors Plus CLI commands.
*/
class Co_Authors_Plus {
const NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL = 'newspack_cap_author_term_backfill';

private static $live = false; // phpcs:ignore Squiz.Commenting.VariableComment.Missing
private static $verbose = true; // phpcs:ignore Squiz.Commenting.VariableComment.Missing
private static $user_logins = false; // phpcs:ignore Squiz.Commenting.VariableComment.Missing
Expand Down Expand Up @@ -124,6 +126,46 @@ public function backfill_non_editing_contributor( $args, $assoc_args ) {
WP_CLI::line( '' );
}

/**
* This function handles setting up a cron job to backfill author terms for posts.
*
* @return void
* @throws WP_CLI\ExitException When the command fails.
*/
public function schedule_author_term_backfill() {
WP_CLI::line( '' );

if ( has_action( self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL ) ) {
remove_action(
self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL,
function () {
eddiesshop marked this conversation as resolved.
Show resolved Hide resolved
// Do Nothing.
}
);
}

add_action(
self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL,
function () {
WP_CLI::runcommand( 'co-authors-plus create-author-terms-for-posts --batched --records-per-batch=50' );
eddiesshop marked this conversation as resolved.
Show resolved Hide resolved
}
);

if ( ! wp_next_scheduled( self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL ) ) {
eddiesshop marked this conversation as resolved.
Show resolved Hide resolved
$result = wp_schedule_event( time(), 'hourly', self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL );

if ( $result ) {
WP_CLI::success( 'Scheduled author term backfill.' );
} else {
WP_CLI::error( 'Could not schedule author term backfill.' );
}
} else {
WP_CLI::warning( 'Author term backfill already scheduled. Remove it by running `wp cron event delete ' . self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL . '`.' );
}

WP_CLI::line( '' );
}

/**
* Migrate unlinked guest authors to regular users.
*
Expand Down
7 changes: 7 additions & 0 deletions includes/cli/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,12 @@ public static function register_comands() {

WP_CLI::add_command( 'newspack migrate-co-authors-guest-authors', [ 'Newspack\CLI\Co_Authors_Plus', 'migrate_guest_authors' ] );
WP_CLI::add_command( 'newspack backfill-non-editing-contributors', [ 'Newspack\CLI\Co_Authors_Plus', 'backfill_non_editing_contributor' ] );
WP_CLI::add_command(
'newspack schedule-co-authors-author-term-backfill',
[
'Newspack\CLI\Co_Authors_Plus',
'schedule_author_term_backfill',
]
);
}
}