Subscription Cancellation System
Overview
The cancellation system in The Golfers Journal allows subscribers to cancel their subscriptions through a user-friendly interface. The system includes both a Gutenberg block for displaying cancellation links and backend functionality for processing cancellations.
Components
1. Cancel Link Block
A Gutenberg block that displays a cancellation link when appropriate. The block can be added to any page or post.
Features
- Configurable link text
- Option to display as a button
- Text alignment options (left, center, right)
- Automatic styling for both link and button variants
Usage
<!-- Example block usage -->
<!-- wp:journal-upgrades-downgrades/cancel-link {
"linkText": "Cancel Subscription",
"isButton": true,
"align": "center"
} /-->
2. Cancellation Logic
The system implements several checks before allowing a cancellation:
- Subscription Validation
- Verifies the subscription exists
- Checks if the subscription is in a cancellable state
- Validates the subscription has a future payment (unless on hold)
- User Permissions
- Verifies the user is logged in
- Checks if the user has permission to cancel the subscription
- Validates the user owns the subscription
- System Settings
- Checks if cancellations are enabled in the system
- Validates any additional business rules
3. Cancellation Process
- Link Generation
- The system generates a cancellation URL using
jc_get_users_change_status_link()
- The URL includes necessary parameters for processing the cancellation
- The system generates a cancellation URL using
- Status Update
- When clicked, the link triggers a status change to ‘cancelled’
- The system handles any necessary cleanup or notifications
- Error Handling
- Graceful error handling for various scenarios
- User-friendly error messages
- Debug logging when WP_DEBUG is enabled
Configuration
System Settings
The cancellation system can be configured through the WordPress admin:
enable_cancellations
: Toggle to enable/disable the cancellation featurecancellations_interstitial_page
: Optional page to show before cancellation
Block Settings
The cancel link block can be configured through the block editor:
- Link Text: Customize the text displayed on the link/button
- Display as Button: Toggle between link and button styles
- Alignment: Choose between left, center, or right alignment
Styling
The block includes default styling for both link and button variants:
.wp-block-journal-upgrades-downgrades-cancel-link {
margin: 1em 0;
}
.wp-block-journal-upgrades-downgrades-cancel-link.has-text-align-center {
text-align: center;
}
.wp-block-journal-upgrades-downgrades-cancel-link.has-text-align-right {
text-align: right;
}
.wp-block-journal-upgrades-downgrades-cancel-link a:not(.button) {
text-decoration: underline;
font-style: italic;
text-underline-offset: 3px;
}
The styling provides:
- Basic margin spacing
- Text alignment support (left, center, right)
- Special styling for non-button links (underlined, italic)
- Button styling is inherited from the theme’s default button styles
Integration
The cancellation system integrates with:
- WooCommerce Subscriptions
- WordPress User System
- Gutenberg Block Editor
Error Messages
The system provides clear feedback in various scenarios:
- “No Subscription ID Provided” - When no subscription ID is present
- “No Subscription Found” - When the subscription doesn’t exist
- “Cancellations are not enabled” - When the feature is disabled
- “Cannot Cancel Subscription” - When user lacks permission or subscription can’t be cancelled
- “No URL Found” - When the cancellation URL can’t be generated
Development
Building the Block
To build the cancel link block:
cd blocks/cancel-link
npm install
npm run build
Adding Custom Logic
To extend the cancellation system:
- Hook into the
wcs_view_subscription_actions
filter - Use the
maybe_add_cancel_link
function as a reference - Implement additional checks or modifications as needed
Security Considerations
- All URLs are properly escaped
- User permissions are strictly checked
- Subscription ownership is verified
- Error messages are sanitized
- Debug information is only logged when WP_DEBUG is enabled