From a65ff9e7974a65fef6b683ee6f5c6e1d822303a8 Mon Sep 17 00:00:00 2001 From: Allen Snook Date: Wed, 14 Nov 2018 16:24:27 -0800 Subject: [PATCH] Bump notes for expiring subscriptions at certain days-remaining thresholds --- ...wc-admin-notes-woo-subscriptions-notes.php | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-notes-woo-subscriptions-notes.php b/plugins/woocommerce-admin/includes/class-wc-admin-notes-woo-subscriptions-notes.php index 8d9bb58f919..6f496907854 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-notes-woo-subscriptions-notes.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-notes-woo-subscriptions-notes.php @@ -18,6 +18,16 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { const SUBSCRIPTION_NOTE_NAME = 'wc-admin-wc-helper-subscription'; const NOTIFY_WHEN_DAYS_LEFT = 60; + /** + * We want to bubble up expiration notices when they cross certain age + * thresholds. PHP 5.2 doesn't support constant arrays, so we do this. + * + * @return array + */ + private function get_bump_thresholds() { + return array( 60, 45, 20, 7, 1 ); // days. + } + /** * Hook all the things. */ @@ -70,7 +80,7 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { $refresh_notes = false; // Did the user just do something on the helper page?. - if ( isset( $_GET['wc-helper-status'] ) ) { + if ( isset( $_GET['wc-helper-status'] ) ) { // @codingStandardsIgnoreLine. $refresh_notes = true; } @@ -274,11 +284,26 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { if ( $note ) { $content_data = $note->get_content_data(); if ( property_exists( $content_data, 'days_until_expiration' ) ) { + // Note: There is no reason this property should not exist. This is just defensive programming. $note_days_until_expiration = intval( $content_data->days_until_expiration ); if ( $days_until_expiration === $note_days_until_expiration ) { // Note is already up to date. Bail. return; } + + // If we have a note and we are at or have crossed a threshold, we should delete + // the old note and create a new one, thereby "bumping" the note to the top of the inbox. + $bump_thresholds = $this->get_bump_thresholds(); + $crossing_threshold = false; + + foreach ( (array) $bump_thresholds as $bump_threshold ) { + if ( ( $note_days_until_expiration > $bump_threshold ) && ( $days_until_expiration <= $bump_threshold ) ) { + error_log( "note crossed the $bump_threshold days until expiration threshold" ); + $note->delete(); + unset( $note ); + continue; + } + } } } @@ -410,10 +435,12 @@ class WC_Admin_Notes_Woo_Subscriptions_Notes { continue; } - // If the subscription is not expiring soon, clean up and exit. - $expires = intval( $subscription['expires'] ); - $time_now_gmt = current_time( 'timestamp', 0 ); - if ( $expires > $time_now_gmt + self::NOTIFY_WHEN_DAYS_LEFT * DAY_IN_SECONDS ) { + // If the subscription is not expiring by the first threshold, clean up and exit. + $bump_thresholds = $this->get_bump_thresholds(); + $first_threshold = DAY_IN_SECONDS * $bump_thresholds[0]; + $expires = intval( $subscription['expires'] ); + $time_now_gmt = current_time( 'timestamp', 0 ); + if ( $expires > $time_now_gmt + $first_threshold ) { $this->delete_any_note_for_product_id( $product_id ); continue; }