al( $subscription['expires'] ); $time_now_gmt = current_time( 'timestamp', 0 ); $days_until_expiration = intval( ceil( ( $expires - $time_now_gmt ) / DAY_IN_SECONDS ) ); $note = $this->find_note_for_product_id( $product_id ); 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 ) ) { $note->delete(); $note = false; continue; } } } } $note_title = sprintf( /* translators: name of the extension subscription expiring soon */ __( '%s subscription expiring soon', 'woocommerce' ), $product_name ); $note_content = sprintf( /* translators: number of days until the subscription expires */ __( 'Your subscription expires in %d days. Enable autorenew to avoid losing updates and access to support.', 'woocommerce' ), $days_until_expiration ); $note_content_data = (object) array( 'product_id' => $product_id, 'product_name' => $product_name, 'expired' => false, 'days_until_expiration' => $days_until_expiration, ); if ( ! $note ) { $note = new Note(); } // Reset everything in case we are repurposing an expired note as an expiring note. $note->set_title( $note_title ); $note->set_type( Note::E_WC_ADMIN_NOTE_WARNING ); $note->set_name( self::SUBSCRIPTION_NOTE_NAME ); $note->set_source( 'woocommerce-admin' ); $note->clear_actions(); $note->add_action( 'enable-autorenew', __( 'Enable Autorenew', 'woocommerce' ), 'https://woocommerce.com/my-account/my-subscriptions/?utm_medium=product' ); $note->set_content( $note_content ); $note->set_content_data( $note_content_data ); $note->save(); } /** * Adds a note for an expired subscription, or updates an expiring note to expired. * * @param array $subscription The subscription to work with. */ public function add_or_update_subscription_expired( $subscription ) { $product_id = $subscription['product_id']; $product_name = $subscription['product_name']; $product_page = $subscription['product_url']; $expires = intval( $subscription['expires'] ); $expires_date = gmdate( 'F jS', $expires ); $note = $this->find_note_for_product_id( $product_id ); if ( $note ) { $note_content_data = $note->get_content_data(); if ( $note_content_data->expired ) { // We've already got a full fledged expired note for this. Bail. // Expired notes' content don't change with time. return; } } $note_title = sprintf( /* translators: name of the extension subscription that expired */ __( '%s subscription expired', 'woocommerce' ), $product_name ); $note_content = sprintf( /* translators: date the subscription expired, e.g. Jun 7th 2018 */ __( 'Your subscription expired on %s. Get a new subscription to continue receiving updates and access to support.', 'woocommerce' ), $expires_date ); $note_content_data = (object) array( 'product_id' => $product_id, 'product_name' => $product_name, 'expired' => true, 'expires' => $expires, 'expires_date' => $expires_date, ); if ( ! $note ) { $note = new Note(); } $note->set_title( $note_title ); $note->set_content( $note_content ); $note->set_content_data( $note_content_data ); $note->set_type( Note::E_WC_ADMIN_NOTE_WARNING ); $note->set_name( self::SUBSCRIPTION_NOTE_NAME ); $note->set_source( 'woocommerce-admin' ); $note->clear_actions(); $note->add_action( 'renew-subscription', __( 'Renew Subscription', 'woocommerce' ), $product_page ); $note->save(); } /** * For each active subscription on this site, checks the expiration date and creates/updates/deletes notes. */ public function refresh_subscription_notes() { if ( ! $this->is_connected() ) { return; } $this->prune_inactive_subscription_notes(); $subscriptions = \WC_Helper::get_subscriptions(); $active_product_ids = $this->get_subscription_active_product_ids(); foreach ( (array) $subscriptions as $subscription ) { // Only concern ourselves with active products. $product_id = $subscription['product_id']; if ( ! in_array( $product_id, $active_product_ids, true ) ) { continue; } // If the subscription will auto-renew, clean up and exit. if ( $subscription['autorenew'] ) { $this->delete_any_note_for_product_id( $product_id ); continue; } // 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; } // Otherwise, if the subscription can still have auto-renew enabled, let them know that now. if ( $expires > $time_now_gmt ) { $this->add_or_update_subscription_expiring( $subscription ); continue; } // If we got this far, the subscription has completely expired, let them know. $this->add_or_update_subscription_expired( $subscription ); } } }