Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(memberships): remove managed flag on cancel or expire #192

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading