Skip to content

Instantly share code, notes, and snippets.

@james-allan
Last active April 18, 2016 02:19
Show Gist options
  • Save james-allan/9e66e2a87dcbaf0ba005 to your computer and use it in GitHub Desktop.
Save james-allan/9e66e2a87dcbaf0ba005 to your computer and use it in GitHub Desktop.
Built off Matt's version (https://gist.github.com/mattallan/e45730807e019343edbf) as a base, this version just includes a log mode and a db rollback in case something goes wrong.
<?php
/**
* Plugin Name: Prospress Script - Reset next payment dates
* Description: A custom plugin for Phoebe's site that will reset all the next payment dates and push them back 1 minute.
* Author: Matt Allan - Prospress Inc.
* Author URI: http://prospress.com/
* Version: 1.0
*/
function reset_next_payment_dates() {
global $wpdb;
$logger = new WC_Logger();
if ( isset( $_GET['log_next_payment_dates'] ) || isset( $_GET['update_next_payment_dates'] ) ) {
$mode = ( isset( $_GET['log_next_payment_dates'] ) ) ? 'log' : 'update';
if ( isset( $_GET['clear'] ) ) {
delete_option( '_wcs_subscriptions_next_payment_dates_' . $mode );
$logger->clear( 'timezone-next-payment-date-fixer' );
}
if ( ! isset( $_GET['continue'] ) ) {
$logger->add( 'timezone-next-payment-date-fixer', 'Reset next payment dates started in: ' . $mode . ' mode' );
}
// get all active subscriptions
$subscription_ids = get_posts( array(
'post_status' => 'wc-active',
'post_type' => 'shop_subscription',
'posts_per_page' => '-1',
'fields' => 'ids',
)
);
$updated_subscriptions = get_option( '_wcs_subscriptions_next_payment_dates_' . $mode , array() );
$counter = 0;
foreach ( $subscription_ids as $subscription_id ) {
if ( $counter >= 10 ) {
break;
}
if ( in_array( $subscription_id, $updated_subscriptions ) ) {
continue;
}
try {
$subscription = wcs_get_subscription( $subscription_id );
$next_payment = $subscription->get_time( 'next_payment' );
$counter++;
$updated_subscriptions[] = $subscription_id;
update_option( '_wcs_subscriptions_next_payment_dates_' . $mode, $updated_subscriptions );
if ( $subscription->can_date_be_updated( 'next_payment' ) && $next_payment != 0 ) {
$new_next_payment = gmdate( 'Y-m-d H:i:s', $next_payment + 60 ); // add one minute
$logger->add( 'timezone-next-payment-date-fixer', 'updating subscription: ' . $subscription_id . ' from ' . gmdate( 'Y-m-d H:i:s', $next_payment ) . ' to ' . $new_next_payment ); // log any issues with updating
if ( 'update' == $mode ) {
// In case something bad happens start a transaction, commit and rollback structure
$wpdb->query( 'START TRANSACTION' );
$subscription->update_dates( array( 'next_payment' => $new_next_payment ) );
// Everything seems to be in order
if ( isset( $_GET['rollback'] ) ) {
$wpdb->query( 'ROLLBACK' );
} else {
$wpdb->query( 'COMMIT' );
}
}
}
} catch( Exception $e ) {
$logger->add( 'timezone-next-payment-date-fixer', 'FAILED updating ' . $subscription_id . ' error: ' . print_r( $e->getMessage(), true ) ); // log any issues with updating
$wpdb->query( 'ROLLBACK' );
$updated_subscriptions[] = $subscription_id;
update_option( '_wcs_subscriptions_next_payment_dates_' . $mode, $updated_subscriptions );
}
}
if ( count( $subscription_ids ) == count( $updated_subscriptions ) ) {
$logger->add( 'timezone-next-payment-date-fixer', 'UPDATER FINISHED' );
if ( isset( $_GET['rollback'] ) ) {
delete_option( '_wcs_subscriptions_next_payment_dates_' . $mode );
}
wp_safe_redirect( remove_query_arg( array( 'log_next_payment_dates', 'update_next_payment_dates' ) ) );
exit;
} else {
$args = $_GET;
unset($args['clear']);
$args['continue'] = true;
wp_safe_redirect( add_query_arg( $args, admin_url( 'admin.php' ) ) );
exit;
}
}
}
add_action( 'init', 'reset_next_payment_dates' );
function rnp_plugin_actions( $links ) {
$args = array(
'page' => 'wc-status',
'tab' => 'logs',
);
return array_merge( $links, array(
'<a href="' . add_query_arg( array_merge( $args, array( 'clear' => true, 'log_next_payment_dates' => true ) ), admin_url( 'admin.php' ) ) . '">' . __( 'Log the reset (does not update anything)' ) . '</a>',
'<a href="' . add_query_arg( array_merge( $args, array( 'rollback' => true, 'clear' => true, 'update_next_payment_dates' => true ) ), admin_url( 'admin.php' ) ) . '">' . __( 'Reset and rollback' ) . '</a>',
'<a href="' . add_query_arg( array_merge( $args, array( 'update_next_payment_dates' => true ) ), admin_url( 'admin.php' ) ) . '">' . __( 'RESET THE NEXT PAYMENT DATES!' ) . '</a>',
));
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'rnp_plugin_actions' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment