Skip to content

Commit

Permalink
fix(memberships): remove managed fields on cancel or expire (#192)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chickenn00dle authored Jan 21, 2025
1 parent 3f30cdb commit 67a5cf0
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions includes/incoming-events/class-woocommerce-membership-updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand All @@ -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.
Expand Down

0 comments on commit 67a5cf0

Please sign in to comment.