Last active
August 29, 2015 14:17
-
-
Save vodanh1213/e533fe64dcf334ba4b28 to your computer and use it in GitHub Desktop.
membership days remaining
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
add_shortcode('m_count_down', 'm_count_down'); | |
function m_count_down($attr) | |
{ | |
if (class_exists('Membership_Plugin')) { | |
$subscription_id = isset($attr['s_id']) ? $attr['s_id'] : 0; | |
if (!is_user_logged_in()) { | |
return ''; | |
} | |
if ($subscription_id == 0) { | |
return ''; | |
} | |
if (!current_user_on_subscription($subscription_id)) { | |
return ''; | |
} | |
//get the current day left | |
$subscription = new Membership_Model_Subscription($subscription_id); | |
ob_start(); | |
$now = strtotime(current_time('mysql')); | |
global $wpdb; | |
$sql = "SELECT * FROM " . MEMBERSHIP_TABLE_RELATIONS . " WHERE user_id=%d AND sub_id=%d"; | |
$relation = $wpdb->get_row($wpdb->prepare($sql, get_current_user_id(), $subscription_id)); | |
$expires = strtotime($relation->expirydate); | |
//get ordered subscription levels | |
$levels = $subscription->get_levels(); | |
$tolevel_order = 1; | |
$fromlevel_order = 1; | |
foreach ($levels as $level) { | |
if (!empty($tolevel_id) && $level->level_id == $tolevel_id) { | |
$tolevel_order = $level->level_order; | |
continue; | |
} | |
if (!empty($fromsub_id) && !empty($fromlevel_id) && $fromsub_id == $subscription->id && $level->level_id == $fromlevel_id) { | |
$fromlevel_order = $level->level_order; | |
continue; | |
} | |
} | |
//sum every level period to get the subscription end date. | |
foreach ($levels as $level) { | |
if ($level->level_order < $tolevel_order) { | |
//if entering in the middle of a subscription, jumping beginning levels, those not count | |
continue; | |
} | |
if (($fromlevel_order < $tolevel_order) && $level->level_order == $fromlevel_order) { | |
//reset to current date if already a subscription member and moving to another level | |
$expires = $now; | |
} | |
switch ($level->level_period_unit) { | |
case 'd': | |
$period = 'days'; | |
break; | |
case 'w': | |
$period = 'weeks'; | |
break; | |
case 'm': | |
$period = 'months'; | |
break; | |
case 'y': | |
$period = 'years'; | |
break; | |
} | |
$expires = strtotime('+' . $level->level_period . ' ' . $period, $expires); | |
if ($level->sub_type == 'indefinite') { | |
//never expires | |
//$expires = strtotime( '+10 years', $expires ); | |
break; | |
} elseif ($level->sub_type == 'serial') { | |
//sum only once and break - will not go to next level if exists | |
break; | |
} | |
} | |
$now = new DateTime(date('Y-m-d', time())); | |
$expiry_date = new DateTime(date('Y-m-d', $expires)); | |
$day_left = $now->diff($expiry_date); | |
echo 'Days left: ' . $day_left->days . ' days'; | |
$content = ob_get_contents(); | |
ob_end_clean(); | |
return $content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment