feat(cli): add fix-memberships command (NPPM-386)#4733
Draft
adekbadek wants to merge 1 commit into
Draft
Conversation
Reconciles wc_user_membership posts against the user's active subscriptions: relinks orphaned memberships, reactivates expired/cancelled memberships whose subscription is still active, creates missing memberships, and reclaims network-managed memberships when a local subscription exists. Transplanted from newspack-subscription-migrations#36. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
All Submissions:
Changes proposed in this Pull Request:
Transplants
wp newspack-subscription-migrations fix-membershipsfrom newspack-subscription-migrations#36 into newspack-plugin aswp newspack fix-memberships.Addresses NPPM-386.
Why move it here:
wp newspack migrate-expired-subscriptionsandwp newspack teams-for-memberships diagnosticscommands. Same shape (single-site WC Memberships/Subscriptions cleanup CLI), same gating pattern (function_exists( 'wc_memberships' )).newspack-subscription-migrations.What the command does (dry-run by default,
--liveto apply):_managed_by_newspack_network/_remote_id/_remote_site_urland relinks to the local subscription)._product_idmeta._end_dateand unschedules expiration events to avoid re-expiry via cron)._end_dateon memberships whose linked subscription is still active (independent of the primary pass).Handles:
_wc_memberships_for_teams_planmeta on products) and team plans.woocommerce_custom_orders_table_enabled).Differences vs the source #36:
Newspack\CLI\Fix_Memberships) and the existing CLI registration pattern inclass-initializer.php(gated onwc_memberships, like the teams-diagnostics command is gated onWC_Memberships_For_Teams_Loader).fix_membershipsmethod into focused private helpers (process_user,handle_user_without_local_membership,handle_user_with_inactive_membership, etc.).reactivate_if_inactive( $membership )helper used everywhere a reactivation was open-coded in Muriel-styled Text/Number Input component. #36 (5 sites).clear_network_managed_meta( $membership_id )for the three_managed_by_newspack_network/_remote_id/_remote_site_urldeletions.fix_membershipstorunto avoidGeneric.NamingConventions.ConstructorName.OldStyle(the class isFix_Memberships, so afix_membershipsmethod tripped the PHP4 constructor detector).truedefault for--verbose. Opt-in like every other Newspack CLI.Worth considering as a follow-up (not done here):
Membership_Expiry::prevent_membership_expiration()does a structurally similar reactivation when an alternate active subscription is found. It currently omitsunschedule_expiration_events()+set_end_date( '' )because it runs as a pre-expiration filter; this PR's helper does both. Pulling the helper out into somethingMembership_Expirycan also call would let both code paths share one implementation. Left out of this PR to keep behavior of existing code unchanged.How to test the changes in this Pull Request:
expiredwhile the subscription is stillactive, or delete the membership entirely).wp newspack fix-memberships --verboseand confirm the dry-run output describes the right corrective action without changing data.wp newspack fix-memberships --verbose --liveand confirm the membership is reactivated / relinked / created.newspack-networkinstalled, mark a membership as_managed_by_newspack_networkwhile a local subscription exists, and verify the command reclaims it under--live.woocommerce_custom_orders_table_enabled=yes) and re-run – the command should detect HPOS and use the orders-table join.Other information: