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

Alpha release Dec 12 #3625

Merged
merged 15 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
90 changes: 0 additions & 90 deletions includes/cli/class-co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,11 @@
* 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 with the [Guest Contributor role](https://help.newspack.com/publishing-and-appearance/guest-contributors/).
*
Expand Down Expand Up @@ -180,39 +124,6 @@ public function backfill_non_editing_contributor( $args, $assoc_args ) {
WP_CLI::line( '' );
}

/**
* Set up a cron job to backfill any missing Co-Author plus author terms for posts.
* Runs incrementally at a rate of up to 250 posts per hour to minimize performance impact.
*
* @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 @@ -578,4 +489,3 @@ private static function assign_user_avatar( $guest_author, $user_id ) {
}
}
}
Co_Authors_Plus::init();
9 changes: 2 additions & 7 deletions includes/cli/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function init() {
include_once NEWSPACK_ABSPATH . 'includes/cli/class-ras.php';
include_once NEWSPACK_ABSPATH . 'includes/cli/class-ras-esp-sync.php';
include_once NEWSPACK_ABSPATH . 'includes/cli/class-co-authors-plus.php';
include_once NEWSPACK_ABSPATH . 'includes/cli/class-woocommerce-subscriptions.php';
}

/**
Expand Down Expand Up @@ -59,12 +60,6 @@ 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',
]
);
WP_CLI::add_command( 'newspack migrate-expired-subscriptions', [ 'Newspack\CLI\WooCommerce_Subscriptions', 'migrate_expired_subscriptions' ] );
}
}
224 changes: 224 additions & 0 deletions includes/cli/class-woocommerce-subscriptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php
/**
* WooCommerce Subscriptions Integration CLI commands.
*
* @package Newspack
*/

namespace Newspack\CLI;

use WP_CLI;
use Newspack\Woocommerce_Subscriptions as WooCommerce_Subscriptions_Integration;
use Newspack\On_Hold_Duration;

defined( 'ABSPATH' ) || exit;

/**
* WooCommerce Subscriptions Integration CLI commands.
*/
class WooCommerce_Subscriptions {
/**
* Flag for live mode.
*
* @var bool
*/
private static $live = false;

/**
* Flag for verbose output.
*
* @var bool
*/
private static $verbose = false;

/**
* Subscription ids to process.
*
* @var bool|array
*/
private static $ids = false;

/**
* Migrate status of on-hold WooCommerce subscriptions that have failed all payment retries to expired.
*
* ## OPTIONS
*
* [--live]
* : Run the command in live mode, updating the subscriptions.
*
* [--verbose]
* : Produce more output.
*
* [--ids]
* : Comma-separated list of subscription IDs. If provided, only ubscriptions with these IDs will be processed.
*
* @param array $args Positional arguments.
* @param array $assoc_args Assoc arguments.
*
* @return void
*/
public function migrate_expired_subscriptions( $args, $assoc_args ) {
WP_CLI::line( '' );
if ( ! WooCommerce_Subscriptions_Integration::is_enabled() ) {
WP_CLI::error( 'WooCommerce Subscriptions Integration is not enabled.' );
WP_CLI::line( '' );
return;
}
self::$ids = isset( $assoc_args['ids'] ) ? explode( ',', $assoc_args['ids'] ) : false;
self::$live = isset( $assoc_args['live'] ) ? true : false;
self::$verbose = isset( $assoc_args['verbose'] ) ? true : false;
$scheduled = 0;
$updated = 0;
$page = 1;
$per_page = 25;
$subscriptions = self::get_subscriptions( $page );
if ( empty( $subscriptions ) ) {
WP_CLI::success( 'No on-hold subscriptions to process.' );
WP_CLI::line( '' );
return;
}
WP_CLI::line( 'Processing subscriptions in ' . ( self::$live ? 'live' : 'dry run' ) . ' mode...' );
WP_CLI::line( '' );
while ( ! empty( $subscriptions ) ) {
foreach ( $subscriptions as $subscription ) {
$id = $subscription->get_id();
if ( self::$verbose ) {
WP_CLI::line( 'Processing subscription ' . $id . '...' );
}
// A pending retry indicates the subscription is awaiting payment retry.
if ( $subscription->get_date( 'payment_retry' ) > 0 ) {
if ( self::$verbose ) {
WP_CLI::line( 'Subscription is awaiting payment retry. Moving to next subscription...' );
WP_CLI::line( '' );
}
continue;
}
$renewal_order = $subscription->get_last_order(
'all',
[ 'renewal' ],
[
'completed',
'processing',
'refunded',
]
);
// No failed or pending renewal orders indicates the subscription was likely manually placed on hold.
if ( empty( $renewal_order ) ) {
if ( self::$verbose ) {
WP_CLI::line( 'Subscription has no pending renewal orders. Moving to next subscription...' );
WP_CLI::line( '' );
}
continue;
}
$last_retry = \WCS_Retry_Manager::store()->get_last_retry_for_order( wcs_get_objects_property( $renewal_order, 'id' ) );
// No retries indicates the subscription was likely manually placed on hold.
if ( empty( $last_retry ) ) {
if ( self::$verbose ) {
WP_CLI::line( 'No retries scheduled. Moving to next subscription...' );
WP_CLI::line( '' );
}
continue;
}
// A non failed status indicates the retry was either manually cancelled
// or was successful at one point but likely placed on hold for some other reason.
if ( 'failed' !== $last_retry->get_status() ) {
if ( self::$verbose ) {
WP_CLI::line( 'Last retry does not have a failed status. Moving to next subscription...' );
WP_CLI::line( '' );
}
continue;
} else {
if ( $subscription->is_manual() || ! $subscription->payment_method_supports( 'subscription_date_changes' ) ) {
if ( self::$verbose ) {
WP_CLI::line( 'Subscription does not support retries. Moving to next subscription...' );
WP_CLI::line( '' );
}
continue;
}
$retry_date = $last_retry->get_date();
$on_hold_duration = On_Hold_Duration::get_on_hold_duration();
// If the retry date is within the on-hold duration, schedule a final retry.
if ( wcs_date_to_time( $retry_date ) + ( $on_hold_duration * DAY_IN_SECONDS ) > time() ) {
if ( self::$verbose ) {
WP_CLI::line( 'Retry date is within the on-hold duration. Scheduling final retry...' );
}
if ( self::$live ) {
// Retry rules can only be applied when payment attempt flag is set.
add_filter( 'wcs_is_scheduled_payment_attempt', '__return_true' );
\WCS_Retry_Manager::maybe_apply_retry_rule( $subscription, $renewal_order );
remove_filter( 'wcs_is_scheduled_payment_attempt', '__return_true' );
if ( 0 === $subscription->get_date( 'payment_retry' ) ) {
if ( self::$verbose ) {
WP_CLI::error( 'Failed to schedule payment retry. Moving to next subscription...' );
WP_CLI::line( '' );
}
continue;
} else {
$subscription->add_order_note(
__( 'Final payment retry scheduled by Newspack CLI command.', 'newspack-plugin' )
);
$subscription->update_meta_data( '_newspack_cli_retry_scheduled', true );
$subscription->save();
}
}
++$scheduled;
} else {
// Otherwise, if the retry date is past the on-hold duration, update the subscription status to expired.
if ( self::$verbose ) {
WP_CLI::line( 'Updating subscription status to expired...' );
}
if ( self::$live ) {
$subscription->update_status( 'expired', __( 'Subscription status updated by Newspack CLI command.', 'newspack-plugin' ) );
$subscription->set_end_date( $retry_date );
$subscription->update_meta_data( '_newspack_cli_status_updated', true );
$subscription->save();
}
++$updated;
}
}
if ( self::$verbose ) {
WP_CLI::line( 'Finished processing subscription ' . $id );
WP_CLI::line( '' );
}
}
$subscriptions = self::get_subscriptions( ++$page );
}
WP_CLI::success( 'Finished processing subscriptions. ' . $updated . ' subscriptions updated. ' . $scheduled . ' retries scheduled.' );
if ( ! self::$live ) {
WP_CLI::warning( 'Dry run. Use --live flag to process live subscriptions.' );
}
WP_CLI::line( '' );
}

/**
* Get subscriptions to process.
*
* @param int $page Page number.
*
* @return array
*/
private static function get_subscriptions( $page = 1 ) {
$subscriptions = [];
if ( false !== self::$ids ) {
while ( ! empty( self::$ids ) ) {
$id = array_shift( self::$ids );
if ( ! is_numeric( $id ) ) {
continue;
}
$subscription = wcs_get_subscription( $id );
if ( $subscription ) {
$subscriptions[] = $subscription;
}
}
} else {
$subscriptions = wcs_get_subscriptions(
[
'paged' => $page,
'subscriptions_per_page' => 25,
'subscription_status' => 'on-hold',
]
);
}
return $subscriptions;
}
}
Loading