Created
March 15, 2016 14:53
-
-
Save eighty20results/a92f797440a5dd28e04c to your computer and use it in GitHub Desktop.
For new sign-ups, expire the membership level on Dec 31st, the year after they signed up.
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 | |
/* | |
Plugin Name: PMPro Expire New Memberships Next Year | |
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/ | |
Description: Customization to expire new membership sign-ups on Jan 1 the year after, the year after this one. | |
Version: .1 | |
Author: Thomas Sjolshagen @ Stranger Studios <thomas@eighty20results.com> | |
Author URI: https://eighty20results.com/thomas-sjolshagen/ | |
*/ | |
function pmpro_next_year_expiration($level) | |
{ | |
//ignore renewals (they won't be changed from membership level definition) | |
if (pmpro_isMember(null, true)) | |
return $level; | |
//add to this array (level ID) => (expiration date in yyyy-mm-dd) | |
$nextyear = intval(date("Y")) + 2; | |
$custom_expiration = $nextyear . "-01-01"; | |
//needed below | |
$todays_date = current_time('timestamp'); | |
//check the passed level against your array | |
//how many days until expiration? | |
$time_left = strtotime($custom_expiration) - $todays_date; | |
if($time_left > 0) | |
{ | |
$days_left = ceil($time_left/(60*60*24)); | |
//update number and period | |
$level->expiration_number = $days_left; | |
$level->expiration_period = "Day"; | |
return $level; //stop | |
} | |
else | |
{ | |
//expiration already here, don't let people sign up | |
$level = NULL; | |
return $level; //stop | |
} | |
return $level; //no change | |
} | |
add_filter("pmpro_checkout_level", "pmpro_next_year_expiration"); | |
function pmpro_isMember( $user_id = null, $inactive = false ) | |
{ | |
global $current_user; | |
global $wpdb; | |
if (empty($current_user->ID)) | |
return false; | |
if (empty($user_id)) | |
$user_id = $current_user->ID; | |
if ( false === $inactive ) | |
{ | |
$sql = $wpdb->prepare( | |
"SELECT COUNT(id) AS is_member | |
FROM {$wpdb->pmpro_memberships_users} | |
WHERE user_id = %d", | |
$user_id | |
); | |
} | |
else { | |
$sql = $wpdb->prepare( | |
"SELECT COUNT(id) as is_member | |
FROM {$wpdb->pmpro_memberships_users} | |
WHERE user_id = %d AND status = 'active'", | |
$user_id | |
); | |
} | |
$is_member = $wpdb->get_var($sql); | |
return (!empty($is_member) ? true : false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment