Skip to content

Commit

Permalink
feat: command to initialize cron job to slowly backfill CAP term data (
Browse files Browse the repository at this point in the history
…#3425)

* feat: command to initialize cron job to slowly backfill CAP term data

* refactor: run command on init hook

* feat: sending command output to Newspack logger

* Update includes/cli/class-co-authors-plus.php

Adding missing `exit_error` option to be able to receive any errors that may have caused the script to fail.

Co-authored-by: Derrick Koo <dkoo@users.noreply.github.com>

* Update includes/cli/class-co-authors-plus.php

Fixing the payload for Newspack logger so that events can be logged properly.

Co-authored-by: Derrick Koo <dkoo@users.noreply.github.com>

* fix: --batched flag was removed from command, it's now the default mode

---------

Co-authored-by: dkoo <derrick.koo@automattic.com>
Co-authored-by: Derrick Koo <dkoo@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 16, 2024
1 parent ae75548 commit 0b0d79a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
89 changes: 89 additions & 0 deletions includes/cli/class-co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,67 @@
* 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
private static $guest_author_ids = false; // phpcs:ignore Squiz.Commenting.VariableComment.Missing

/**
* Add hooks.
*/
public static function init() {
add_action( self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL, [ __CLASS__, 'run_cap_cli_command' ] );
add_action( 'init', [ __CLASS__, 'register_deactivation_hook' ] );
}

/**
* Execute the co-authors-plus create-terms-for-posts CLI command.
*/
public static function run_cap_cli_command() {
if ( method_exists( 'WP_CLI', 'runcommand' ) ) {
$result = WP_CLI::runcommand(
'co-authors-plus create-author-terms-for-posts --records-per-batch=50',
[
'exit_error' => false, // This allows us to capture any errors that occur during script execution.
'launch' => false, // This keeps any formatting that's been set.
'return' => 'all', // This ensures we get stdout, stderr, and return code.
]
);

WP_CLI::out( $result->stdout );

do_action(
'newspack_log',
self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL,
! empty( $result->stderr ) ? $result->stderr : $result->stdout,
[
'type' => 0 !== $result->return_code || ! empty( $result->stderr ) ? 'error' : 'success',
'data' => [
'stdout' => $result->stdout,
'timestamp' => gmdate( 'c', time() ),
],
'file' => 'newspack_cap_author_terms_backfill',
]
);
}
}

/**
* Unschedule any unexecuted jobs upon plugin deactivation.
*/
public static function register_deactivation_hook() {
register_deactivation_hook( NEWSPACK_PLUGIN_FILE, [ __CLASS__, 'unschedule_author_term_backfill' ] );
}

/**
* Clear the cron job when this plugin is deactivated.
*/
public static function unschedule_author_term_backfill() {
wp_clear_scheduled_hook( self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL );
}

/**
* Migrate Co-Authors Plus guest authors to regular users.
*
Expand Down Expand Up @@ -124,6 +180,38 @@ 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,
[ __CLASS__, 'run_cap_cli_command' ]
);
}

if ( ! wp_next_scheduled( self::NEWSPACK_SCHEDULE_AUTHOR_TERM_BACKFILL ) ) {
$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 {
// Unschedule and reschedule.
self::unschedule_author_term_backfill();
self::schedule_author_term_backfill();
}

WP_CLI::line( '' );
}

/**
* Migrate unlinked guest authors to regular users.
*
Expand Down Expand Up @@ -489,3 +577,4 @@ private static function assign_user_avatar( $guest_author, $user_id ) {
}
}
}
Co_Authors_Plus::init();
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',
]
);
}
}

0 comments on commit 0b0d79a

Please sign in to comment.