Skip to content

Commit

Permalink
fix: add more comments and membership note
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Jan 20, 2025
1 parent 1029fab commit 81f6156
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions includes/incoming-events/class-woocommerce-membership-updated.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +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() );
Expand All @@ -101,11 +101,20 @@ public function update_membership() {

$status = $this->get_new_status();
$is_managed = get_post_meta( $user_membership->get_id(), Memberships_Admin::NETWORK_MANAGED_META_KEY, true );
// If the membership is being cancelled or expired, remove the network managed flag.
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 flag.
// 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 );
// Otherwise, if the membership is not managed, reset relevant meta.
$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 ) {
// If the membership is not managed, it was previously unlinked so we 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() );
Expand Down

0 comments on commit 81f6156

Please sign in to comment.