Last active
April 16, 2018 19:34
-
-
Save eighty20results/b61a12d7198022b9180f to your computer and use it in GitHub Desktop.
Preserve initial startdate for member regardless of how the membership level is updated.
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 Preserve Startdate | |
Plugin URI: https://eighty20results.com/paid-memberships-pro/do-it-for-me/ | |
Description: Customization to preserve initial membership signup (startdate) value. | |
Version: 1.0 | |
Author: Thomas Sjolshagen @ Stranger Studios <thomas@eighty20results.com> | |
Author URI: https://eighty20results.com/thomas-sjolshagen/ | |
*/ | |
/* | |
Keep members start dates at checkout, even if switching levels. | |
*/ | |
function custom_pmpro_checkout_start_date($date, $user_id, $level) | |
{ | |
$date = custom_get_initial_date($user_id); | |
return $date; | |
} | |
add_action('pmpro_checkout_start_date', 'custom_pmpro_checkout_start_date', 15, 3); | |
function custom_pmpro_updated_level() | |
{ | |
global $wpdb; | |
$user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : null; | |
$level_id = isset($_POST['membership_level']) ? intval($_POST['membership_level']) : null; | |
// grab user info | |
if (! is_null($user_id)) { | |
$user = get_user_by('id', $user_id); | |
} | |
$startdate = custom_get_initial_date($user_id); | |
$sql = $wpdb->prepare( | |
"SELECT id | |
FROM {$wpdb->pmpro_memberships_users} | |
WHERE user_id = %d AND membership_id = %d | |
ORDER BY id DESC | |
LIMIT 1", $user_id, $level_id | |
); | |
$id = $wpdb->get_var($sql); | |
if (WP_DEBUG) | |
error_log("Found most recent DB record for {$user_id} in level {$level_id}: {$id}"); | |
if (! empty($id)) { | |
$wpdb->update( | |
$wpdb->pmpro_memberships_users, | |
array( 'startdate' => $startdate ), | |
array( 'user_id' => $user_id, 'id' => $id ), | |
array( '%s' ), | |
array( '%d', '%d' ) | |
); | |
} | |
} | |
add_action('edit_user_profile_update', 'custom_pmpro_updated_level', 15); | |
add_action('personal_options_update', 'custom_pmpro_updated_level', 15); | |
function custom_get_initial_date($user_id) { | |
global $wpdb; | |
$date = null; | |
$sqlQuery = $wpdb->prepare(" | |
SELECT startdate | |
FROM {$wpdb->pmpro_memberships_users} AS mu | |
WHERE mu.user_id = %d | |
ORDER BY mu.id | |
LIMIT 1 | |
", $user_id | |
); | |
$old_start = $wpdb->get_var($sqlQuery); | |
if (!empty($old_start)) { | |
$old_timestamp = strtotime( $old_start, current_time( 'timestamp' ) ); | |
$date = date("Y-m-d H:i:s", $old_timestamp); | |
} else { | |
$date = date("Y-m-d H:i:s", current_time('timestamp')); | |
} | |
return $date; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment