Created
July 5, 2024 11:11
-
-
Save ipokkel/485945d1de238b34a58c4a833a5a2a17 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
/** | |
* Change the end date of a membership level when a user is approved. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function pmpro_change_level_enddate_on_approval( $user_id, $level_id ) { | |
global $current_user; | |
$level = pmpro_getSpecificMembershipLevelForUser( $user_id, $level_id ); | |
// bail if startdate or enddate in the level object is empty or is recurring level. | |
if ( empty( $level->startdate ) || empty( $level->enddate ) || ! empty( $level->cycle_number ) || ! empty( $level->cycle_period ) ) { | |
return; | |
} | |
// Get the start and end date from the level. | |
$startdate = intval( $level->startdate ); | |
$enddate = intval( $level->enddate ); | |
// how many days passed since start date to today | |
$days_passed = strtotime( 'now' ) - $startdate; | |
$days_passed = floor( $days_passed / ( 60 * 60 * 24 ) ); | |
// extend the end date by the number of days passed | |
$new_end_date = date( 'Y-m-d H:i:s', strtotime( "+$days_passed days", $enddate ) ); | |
$new_start_date = date( 'Y-m-d H:i:s', $startdate ); | |
// Prepare the level array for pmpro_changeMembershipLevel. This is used for a SQL entry so the dates need to be in SQL date format. | |
$level_array = array( | |
'user_id' => $user_id, | |
'membership_id' => $level_id, | |
'code_id' => $level->code_id, | |
'initial_payment' => $level->initial_payment, | |
'billing_amount' => $level->billing_amount, | |
'cycle_number' => $level->cycle_number, | |
'cycle_period' => $level->cycle_period, | |
'billing_limit' => $level->billing_limit, | |
'trial_amount' => $level->trial_amount, | |
'trial_limit' => $level->trial_limit, | |
'startdate' => $new_start_date, | |
'enddate' => $new_end_date, | |
); | |
// Skip if we don't have correct dates for the level update. | |
if ( empty( $level_array['startdate'] ) || empty( $level_array['enddate'] ) || ! strtotime( $level_array['startdate'] ) || ! strtotime( $level_array['enddate'] ) || strtotime( $level_array['enddate'] ) < strtotime( $level->enddate ) ) { | |
return; | |
} | |
$changed = pmpro_changeMembershipLevel( $level_array, $user_id ); | |
// If the change was successful, update the approval meta. | |
if ( $changed ) { | |
// Automatically approve the user. | |
update_user_meta( | |
$user_id, | |
'pmpro_approval_' . $level_id, | |
array( | |
'status' => 'approved', | |
'timestamp' => current_time( 'timestamp' ), | |
'who' => $current_user->ID, // change this to admin ID | |
'approver' => $current_user->login, // change this name. | |
) | |
); | |
} | |
} | |
add_action( 'pmpro_approvals_after_approve_member', 'pmpro_change_level_enddate_on_approval', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment