-
Notifications
You must be signed in to change notification settings - Fork 382
Description
Describe the bug
When an admin edits a user membership, PMPro saves the membership start date to the database using the server's local timezone. WordPress's current_time function returns the local time unless the $gmt argument is specified.
'startdate' => current_time( 'mysql' )
paid-memberships-pro/adminpages/member-edit/pmpro-class-member-edit-panel-memberships.php
Lines 757 to 771 in 6073625
| // Add the membership level. | |
| $level_to_add = array( | |
| 'user_id' => $user->ID, | |
| 'membership_id' => $level_id, | |
| 'code_id' => '', | |
| 'initial_payment' => 0, | |
| 'billing_amount' => 0, | |
| 'cycle_number' => 0, | |
| 'cycle_period' => 'month', | |
| 'billing_limit' => 0, | |
| 'trial_amount' => 0, | |
| 'trial_limit' => 0, | |
| 'startdate' => current_time( 'mysql' ), | |
| 'enddate' => empty( $expiration ) ? 'NULL' : date( 'Y-m-d H:i:s', strtotime( $expiration ) ) | |
| ); |
However, when retrieving the start date function pmpro_getMemberStartdate assumes that the startdate in the DB is UTC (+00:00), and applies a TZ conversion to it. This causes an inaccurate startdate to be returned.
paid-memberships-pro/includes/functions.php
Lines 2865 to 2869 in 6073625
| if ( ! empty( $level_id ) ) { | |
| $sqlQuery = "SELECT UNIX_TIMESTAMP(CONVERT_TZ(startdate, '+00:00', @@global.time_zone)) FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND membership_id IN(" . (int) $level_id . ") AND user_id = '" . $user_id . "' ORDER BY id LIMIT 1"; | |
| } else { | |
| $sqlQuery = "SELECT UNIX_TIMESTAMP(CONVERT_TZ(startdate, '+00:00', @@global.time_zone)) FROM $wpdb->pmpro_memberships_users WHERE status = 'active' AND user_id = '" . esc_sql( $user_id ) . "' ORDER BY id LIMIT 1"; | |
| } |
To Reproduce
Steps to reproduce the behavior:
- Edit a user's PMPro membership level through the Wordpress dashboard
- While logged in as the user, execute a snippet such as
global $current_user;
$startdate = pmpro_getMemberStartdate($current_user->ID);
echo 'Membership start date: ' . date("Y-m-d H:i:s", $startdate) . '<br/>';- Incorrect start date will be returned
Expected behavior
Dates should be saved in the database as UTC, not local timezone
Isolating the problem (mark completed items with an [x]):
- I have deactivated other plugins and confirmed this bug occurs when only Paid Memberships Pro plugin is active.
- This bug happens with a default WordPress theme active, or Memberlite.
- I can reproduce this bug consistently using the steps above.
WordPress Environment