From 67a5cf0274802525bbb4a9e494ccc4fe3244f7ec Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Tue, 21 Jan 2025 11:04:13 -0500 Subject: [PATCH] fix(memberships): remove managed fields on cancel or expire (#192) This PR removes the network managed meta fields when a network membership is cancelled or expired, and resets network membership meta otherwise. This is so readers can resume a cancelled or expired membership on another network site. --- .../class-woocommerce-membership-updated.php | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/includes/incoming-events/class-woocommerce-membership-updated.php b/includes/incoming-events/class-woocommerce-membership-updated.php index 25f31910..37a680af 100644 --- a/includes/incoming-events/class-woocommerce-membership-updated.php +++ b/includes/incoming-events/class-woocommerce-membership-updated.php @@ -77,16 +77,13 @@ public function update_membership() { $user_membership = wc_memberships_get_user_membership( $user->ID, $local_plan_id ); if ( null === $user_membership ) { + // Create a new membership if it doesn't exist and link it to the remote membership. $user_membership = wc_memberships_create_user_membership( [ 'plan_id' => $local_plan_id, 'user_id' => $user->ID, ] ); - - update_post_meta( $user_membership->get_id(), Memberships_Admin::NETWORK_MANAGED_META_KEY, true ); - update_post_meta( $user_membership->get_id(), Memberships_Admin::REMOTE_ID_META_KEY, $this->get_membership_id() ); - update_post_meta( $user_membership->get_id(), Memberships_Admin::SITE_URL_META_KEY, $this->get_site() ); } if ( is_wp_error( $user_membership ) ) { @@ -99,7 +96,29 @@ public function update_membership() { return; } - $user_membership->update_status( $this->get_new_status() ); + $status = $this->get_new_status(); + $is_managed = get_post_meta( $user_membership->get_id(), Memberships_Admin::NETWORK_MANAGED_META_KEY, true ); + if ( in_array( $status, [ 'cancelled', 'expired' ], true ) && $is_managed ) { + // If the membership is being cancelled or expired, and the membership is managed, we remove the managed meta fields. + // This is to allow the membership to be re-initiated from another site in the network. + delete_post_meta( $user_membership->get_id(), Memberships_Admin::NETWORK_MANAGED_META_KEY ); + delete_post_meta( $user_membership->get_id(), Memberships_Admin::REMOTE_ID_META_KEY ); + delete_post_meta( $user_membership->get_id(), Memberships_Admin::SITE_URL_META_KEY ); + $user_membership->add_note( + sprintf( + // translators: %s is the site URL. + __( 'Membership has been unlinked via Newspack Network. Unlinked from %s', 'newspack-network' ), + $this->get_site() + ) + ); + + } elseif ( ! $is_managed ) { + // Otherwise, if the membership is not managed, reset the relevant meta fields. + update_post_meta( $user_membership->get_id(), Memberships_Admin::NETWORK_MANAGED_META_KEY, true ); + update_post_meta( $user_membership->get_id(), Memberships_Admin::REMOTE_ID_META_KEY, $this->get_membership_id() ); + update_post_meta( $user_membership->get_id(), Memberships_Admin::SITE_URL_META_KEY, $this->get_site() ); + } + $user_membership->update_status( $status ); $user_membership->add_note( sprintf( // translators: %s is the site URL.